formatting code

This commit is contained in:
lanvent
2023-04-17 01:00:08 +08:00
parent 3b8972ce1f
commit 8f72e8c3e6
92 changed files with 1850 additions and 1188 deletions

View File

@@ -1,11 +1,12 @@
"""
google voice service
"""
import time
import speech_recognition
from gtts import gTTS
from bridge.reply import Reply, ReplyType
from common.log import logger
from common.tmp_dir import TmpDir
@@ -22,9 +23,12 @@ class GoogleVoice(Voice):
with speech_recognition.AudioFile(voice_file) as source:
audio = self.recognizer.record(source)
try:
text = self.recognizer.recognize_google(audio, language='zh-CN')
text = self.recognizer.recognize_google(audio, language="zh-CN")
logger.info(
'[Google] voiceToText text={} voice file name={}'.format(text, voice_file))
"[Google] voiceToText text={} voice file name={}".format(
text, voice_file
)
)
reply = Reply(ReplyType.TEXT, text)
except speech_recognition.UnknownValueError:
reply = Reply(ReplyType.ERROR, "抱歉,我听不懂")
@@ -32,13 +36,15 @@ class GoogleVoice(Voice):
reply = Reply(ReplyType.ERROR, "抱歉,无法连接到 Google 语音识别服务;{0}".format(e))
finally:
return reply
def textToVoice(self, text):
try:
mp3File = TmpDir().path() + 'reply-' + str(int(time.time())) + '.mp3'
tts = gTTS(text=text, lang='zh')
tts.save(mp3File)
mp3File = TmpDir().path() + "reply-" + str(int(time.time())) + ".mp3"
tts = gTTS(text=text, lang="zh")
tts.save(mp3File)
logger.info(
'[Google] textToVoice text={} voice file name={}'.format(text, mp3File))
"[Google] textToVoice text={} voice file name={}".format(text, mp3File)
)
reply = Reply(ReplyType.VOICE, mp3File)
except Exception as e:
reply = Reply(ReplyType.ERROR, str(e))