LoginSignup
0
2

【2023年12月最新】Whisper APIを使って英文をリスニング問題にする

Last updated at Posted at 2023-12-08

はじめに

この記事は、下記のOpenAI APIについての記事の発展的な内容を取り扱います。

本日のお題は、「Whisper APIを使って英文をリスニング問題にする」です。

前提として、OpenAI APIの使い方講座【公式Quickstart】のStep2まで完了している必要があります。

本日のお題:Whisper APIを使って英文をリスニング問題にする

Whisper APIとは

Whisperは、OpenAIが開発した音声をテキストに変換する非常に高性能な音声認識モデルです。Whisper APIは、WhisperをAPIの形で呼び出すことができるようにしたものになります。

題材の英文は下記の記事の冒頭を英語にしたものを使います。

出来上がったソースコード

text_to_speech/text_to_speech.py
from openai import OpenAI


def text_to_speech(input_text, output_file="output.mp3", model="tts-1", voice="alloy"):
    client = OpenAI()

    response = client.audio.speech.create(
        model=model,
        voice=voice,
        input=input_text
    )

    response.stream_to_file(output_file)

if __name__ == "__main__":
    input_text = "Hello world! This is a streaming test."
    text_to_speech(input_text)
english_question_generation.py
from text_generation import text_generation
from text_to_speech import text_to_speech


def main(model_name, system_message, user_message):
    # 英文から問題のテキストを生成する
    generated_text = text_generation.generate_text(model_name, system_message, user_message)
    print(generated_text)

    # 問題文を読み上げる音声合成
    text_to_speech.text_to_speech(generated_text)


if __name__ == "__main__":
    # テキスト生成のパラメータ
    text_model_name = "gpt-4"
    system_message = "Create a TOEIC-style multiple choice question (with options A, B, and C) based on the following English text. Also, provide the correct answer."
    user_message = """
                ---

                **Introduction:**
                This year, I've taken up the challenge of a solo AdCalendar initiative, choosing generative AI as my central theme. In this article, I aim to outline my preparations for this endeavor in a Q&A format. Hopefully, this will be beneficial to anyone contemplating their own AdCalendar challenge, whether going solo or not. Let’s dive in together!

                **Summary of Preparations for a Solo AdCalendar in Q&A Format**

                - **Q1: What motivated you to undertake a solo AdCalendar?**
                **A:** My primary objective is to win a Qiitan stuffed animal.

                - **Q2: Why did you choose to focus on generative AI?**
                **A:** This year has seen significant advancements in generative AI. The vast amount of new information, which I believe has overwhelmed most people, spurred me to increase my creative output. I decided to zero in on this single topic, convinced that working within certain boundaries would enhance my creativity.

                ---
                """

    main(text_model_name, system_message, user_message)

Chat Completions APIで京都旅行プランを考える」で実装したtext_generation.pyに加えて、テキストから音声合成を行うtext_to_speech.pyを今回実装し、メインのソースコードとなるenglish_question_generation.pyからimportしています。
前半のChat Completions APIを用いたテキスト生成関数で英文から問題文を生成し、後半のWhisper APIを用いた音声合成関数に引数として渡し、リスニング問題の音声を生成しています。

実行結果(生成されたテキスト)

Generated text: Question: According to the text, what is motivating the author to undertake a solo AdCalendar?
A. To win a Qiitan stuffed animal
B. To understand more about generative AI
C. To enhance their creativity
Correct Answer: A. To win a Qiitan stuffed animal

問題文と解答が自動で生成され、自然な英文になっています!

実行結果(生成された音声)

下記のGitHubレポジトリをcloneしてください。output.mp3に音声データが保存されています。

もしくは、下記のツイートに音声を添付しているのでそちらをお聞きください。

聞き心地はとても自然、、!
TOEICのリスニング問題と言われても信じるレベルです。

参考にしたもの

ソースコード

本記事で使用したソースコードは、下記のGitHubレポジトリに格納しています。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2