mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-06-02 00:57:41 +08:00
refactor: use enum to specify type
This commit is contained in:
@@ -4,6 +4,7 @@ baidu voice service
|
||||
"""
|
||||
import time
|
||||
from aip import AipSpeech
|
||||
from bridge.reply import Reply, ReplyType
|
||||
from common.log import logger
|
||||
from common.tmp_dir import TmpDir
|
||||
from voice.voice import Voice
|
||||
@@ -30,8 +31,8 @@ class BaiduVoice(Voice):
|
||||
with open(fileName, 'wb') as f:
|
||||
f.write(result)
|
||||
logger.info('[Baidu] textToVoice text={} voice file name={}'.format(text, fileName))
|
||||
reply = {"type": "VOICE", "content": fileName}
|
||||
reply = Reply(ReplyType.VOICE, fileName)
|
||||
else:
|
||||
logger.error('[Baidu] textToVoice error={}'.format(result))
|
||||
reply = {"type": "ERROR", "content": "抱歉,语音合成失败"}
|
||||
reply = Reply(ReplyType.ERROR, "抱歉,语音合成失败")
|
||||
return reply
|
||||
|
||||
@@ -6,6 +6,7 @@ google voice service
|
||||
import pathlib
|
||||
import subprocess
|
||||
import time
|
||||
from bridge.reply import Reply, ReplyType
|
||||
import speech_recognition
|
||||
import pyttsx3
|
||||
from common.log import logger
|
||||
@@ -32,16 +33,15 @@ class GoogleVoice(Voice):
|
||||
' -acodec pcm_s16le -ac 1 -ar 16000 ' + new_file, shell=True)
|
||||
with speech_recognition.AudioFile(new_file) as source:
|
||||
audio = self.recognizer.record(source)
|
||||
reply = {}
|
||||
try:
|
||||
text = self.recognizer.recognize_google(audio, language='zh-CN')
|
||||
logger.info(
|
||||
'[Google] voiceToText text={} voice file name={}'.format(text, voice_file))
|
||||
reply = {"type": "TEXT", "content": text}
|
||||
reply = Reply(ReplyType.TEXT, text)
|
||||
except speech_recognition.UnknownValueError:
|
||||
reply = {"type": "ERROR", "content": "抱歉,我听不懂"}
|
||||
reply = Reply(ReplyType.ERROR, "抱歉,我听不懂")
|
||||
except speech_recognition.RequestError as e:
|
||||
reply = {"type": "ERROR", "content": "抱歉,无法连接到 Google 语音识别服务;{0}".format(e)}
|
||||
reply = Reply(ReplyType.ERROR, "抱歉,无法连接到 Google 语音识别服务;{0}".format(e))
|
||||
finally:
|
||||
return reply
|
||||
def textToVoice(self, text):
|
||||
@@ -51,8 +51,8 @@ class GoogleVoice(Voice):
|
||||
self.engine.runAndWait()
|
||||
logger.info(
|
||||
'[Google] textToVoice text={} voice file name={}'.format(text, textFile))
|
||||
reply = {"type": "VOICE", "content": textFile}
|
||||
reply = Reply(ReplyType.VOICE, textFile)
|
||||
except Exception as e:
|
||||
reply = {"type": "ERROR", "content": str(e)}
|
||||
reply = Reply(ReplyType.ERROR, str(e))
|
||||
finally:
|
||||
return reply
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user