import json
import os
import time
from datetime import datetime
import openai
import requests
import playsound
openai_key = os.environ.get("OPENAI_API_KEY")
def create_synthesis(text,count):
parameters = {"text": text,
"id": 0}
endpoint = "http://localhost:23456/voice/bert-vits2"
headers = {
"Content-Type": "application/json"
}
response_synth = requests.get(endpoint, params=parameters, headers=headers)
audio_data = response_synth.content
output_path = "./voices/output" + str(count) + ".wav"
with open(output_path, mode="wb") as f:
f.write(audio_data)
time.sleep(0.4)
return output_path
system_content = f"""あなたは優秀なAIアシスタントです。楽しい会話をしましょう。
現在時刻は{datetime.now()}です。"""
messages = [{"role": "system", "content": system_content}]
client = openai.OpenAI()
print("チャッピーとチャットしよう。with Bert-Vits2 TTS")
print("Speakerを取得する")
speaker_url = "http://127.0.0.1:23456/voice/speakers"
speaker = requests.get(speaker_url,params=json.dumps({"BERT-VITS2":[{"id": 0,"lang":"ja"}]}))
print(speaker.text)
count = 0
while True:
try:
u = input("ユーザー: ")
if not u:
break
messages.append({"role": "user", "content": u})
resp = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
)
a = resp.choices[0].message.content
print(" チャッピー: " + str(a))
messages.append({"role": "assistant", "content": str(a)})
output_path = create_synthesis(str(a),count)
if count == 0:
time.sleep(5)
playsound.playsound(output_path)
count += 1
except KeyboardInterrupt:
break