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,"model_id": 1,"style": "Neutral",
"style_weight": 0}
endpoint = "http://localhost:5000/voice/"
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()}です。"""
client = openai.OpenAI()
messages = [
{"role": "system", "content": system_content}
]
count = 0
print("チャッピーとチャットしよう。with Style-bert-vits2")
while True:
try:
u = input("ユーザー: ")
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