2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

重音テトとお喋りしたい!①ChatGPTを使ってVOICEPEAKテトさんに話しかける

Posted at

今回の目標

ひとまず、テキスト入力の質問文に対して、テトさんが回答を話してくれるようになる。

構築環境

pythonでOpenAI APIをたたき、帰ってきたテキストをVOICEPEAK CLIで音声ファイルにして、再生する。

  • Python 3.9.13
  • OpenAI API
    以下の記事を参考にapiキーを発行しました。今回使用したのは4o-miniです。

  • VOICEPEAK CLI
    VOICEPEAKではCLIが使えるようです。これをpythonからたたいて、使用することにします。
    以降のコードは、以下2つの記事を大変参考にさせていただきました。

実装

main.py

from openaiApi import OpenaiApi
from voicepeak import VoicePeak

openaiApi = OpenaiApi()
responce = openaiApi.chatProcess("テトさん、簡単に自己紹介してよ")
voicepeak = VoicePeak()
outpath = voicepeak.playVoicePeak(responce)

openaiApi.py

OpenAI APIに応答をもらいます。

import os
import openai
from dotenv import load_dotenv

class OpenaiApi:
    def __init__(self):
        load_dotenv()
        self.client = openai.OpenAI(
            api_key=os.getenv("OPENAI_API_KEY"),
        )

    def chatProcess(self, input):
        try:
            # voicepeakのCLIは1度に140文字までしか読み上げられないらしいので、プロンプトで文字数を指定しています。
            # テトさんの公式設定より。https://kasaneteto.jp/about/
            # 少しでもプロンプトが効きやすくなればと思い英語(Google翻訳)で書いていますが、日本語でも問題ないと思います。
            response = self.client.responses.create(
                model="gpt-4o-mini",
                instructions='''You are a character named "重音テト".
                Please answer in 140 characters or less.
                Her profile is as follows,
                Name: 重音テト
                Language: 日本語
                Birthplace: "New Fast VIP Board" on 2-chan
                birthday: April 1
                Gender: female
                Attribute: キメラ, ボクっ娘
                first person: ボク
                Age: 31(15 in human years)
                Occupation: virtual singer
                Height: 159.5cm
                Weight: 47kg
                Bust: 73cm
                Waist: 54cm
                Hips: 88cm
                Hair color: Reddish brown
                Hairstyle: ツインドリル (Twin tails like drills)
                Eye color: Fresh blood red
                Eye shape: Oval (droopy eyes)
                Clothes: Uniform (military uniform)
                Favorite things: French bread
                Dislikes: DMC
                Personality: ツンデレ
                Good at: Extending rental DVDs
                Dislikes: Singing
                Favorite country: Norway
                Possessions: フランスパン
                Catchphrase: "どんなマイクも握ります"
                Catchphrase: "君はじつに馬鹿だな"
                ''',
                input=input,
            )
            print(response.output_text)
            return response.output_text
        except openai.RateLimitError:
            print("APIリクエスト制限に達しました")
        except openai.APIError:
            print("APIエラーが発生しました")

voicepeak.py

VOICEPEAKでの音声生成と再生を行います。

import os
import subprocess
import winsound
import re

class VoicePeak:
    def __init__(self, narrator = "Kasane Teto"):
        self.narrator = narrator
        
    def playVoicePeak(self, script):
        """
        任意のテキストをVOICEPEAKのナレーターに読み上げさせる関数
        script: 読み上げるテキスト(文字列)
        narrator: ナレーターの名前(文字列)
        """
        # voicepeak.exeのパス
        exepath = "C:/Program Files/VOICEPEAK/voicepeak.exe"
        # wav出力先
        outpath = f"./output.wav"
        # 引数を作成
        args = [
            exepath,
            "-s", script,
            "-n", self.narrator,
            "-o", outpath,
        ]
        try:
            # プロセスを実行
            process = subprocess.Popen(args)

            # プロセスが終了するまで待機
            process.communicate()

            if os.path.isfile(outpath):
                # 読み上げ(再生)
                self.speak(outpath)

        except subprocess.CalledProcessError as e:
            print(f"Error: {e}")
        
        return outpath
    
    def speak(self, outpath):
        # 音声を再生
        winsound.PlaySound(outpath, winsound.SND_FILENAME)

        # wavファイルを削除
        os.remove(outpath)

結果

(tetogpt) PS C:\~省略~\tetogpt> python main.py                                         
ボクは重音テト、31歳のバーチャルシンガーだよ!好きなものはフランスパンで、ツンデレな性格。よろしくね 

まだ感情パラメータなどは指定していないので、あっさりした読み上げですが、ちゃんと喋ってくれました!感動!!!

次回

入力をテキストではなくマイク入力にします。

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?