|
|
@@ -9,7 +9,9 @@ from bridge.reply import Reply, ReplyType
|
|
|
from common.log import logger
|
|
|
from config import conf
|
|
|
from voice.voice import Voice
|
|
|
-
|
|
|
+import requests
|
|
|
+from common import const
|
|
|
+import datetime, random
|
|
|
|
|
|
class OpenaiVoice(Voice):
|
|
|
def __init__(self):
|
|
|
@@ -27,3 +29,28 @@ class OpenaiVoice(Voice):
|
|
|
reply = Reply(ReplyType.ERROR, "我暂时还无法听清您的语音,请稍后再试吧~")
|
|
|
finally:
|
|
|
return reply
|
|
|
+
|
|
|
+
|
|
|
+ def textToVoice(self, text):
|
|
|
+ try:
|
|
|
+ url = 'https://api.openai.com/v1/audio/speech'
|
|
|
+ headers = {
|
|
|
+ 'Authorization': 'Bearer ' + conf().get("open_ai_api_key"),
|
|
|
+ 'Content-Type': 'application/json'
|
|
|
+ }
|
|
|
+ data = {
|
|
|
+ 'model': conf().get("text_to_voice_model") or const.TTS_1,
|
|
|
+ 'input': text,
|
|
|
+ 'voice': conf().get("tts_voice_id") or "alloy"
|
|
|
+ }
|
|
|
+ response = requests.post(url, headers=headers, json=data)
|
|
|
+ file_name = "tmp/" + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + str(random.randint(0, 1000)) + ".mp3"
|
|
|
+ logger.debug(f"[OPENAI] text_to_Voice file_name={file_name}, input={text}")
|
|
|
+ with open(file_name, 'wb') as f:
|
|
|
+ f.write(response.content)
|
|
|
+ logger.info(f"[OPENAI] text_to_Voice success")
|
|
|
+ reply = Reply(ReplyType.VOICE, file_name)
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(e)
|
|
|
+ reply = Reply(ReplyType.ERROR, "遇到了一点小问题,请稍后再问我吧")
|
|
|
+ return reply
|