refactor: use enum to specify type

This commit is contained in:
lanvent
2023-03-13 19:44:24 +08:00
parent 1dc3f85a66
commit ad6ae0b32a
13 changed files with 185 additions and 118 deletions

View File

@@ -4,6 +4,7 @@ google voice service
"""
import json
import openai
from bridge.reply import Reply, ReplyType
from config import conf
from common.log import logger
from voice.voice import Voice
@@ -16,16 +17,15 @@ class OpenaiVoice(Voice):
def voiceToText(self, voice_file):
logger.debug(
'[Openai] voice file name={}'.format(voice_file))
reply={}
try:
file = open(voice_file, "rb")
result = openai.Audio.transcribe("whisper-1", file)
text = result["text"]
reply = {"type": "TEXT", "content": text}
reply = Reply(ReplyType.TEXT, text)
logger.info(
'[Openai] voiceToText text={} voice file name={}'.format(text, voice_file))
except Exception as e:
reply = {"type": "ERROR", "content": str(e)}
reply = Reply(ReplyType.ERROR, str(e))
finally:
return reply