mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-20 13:47:15 +08:00
formatting: run precommit on all files
This commit is contained in:
@@ -40,57 +40,33 @@ class AzureVoice(Voice):
|
||||
config = json.load(fr)
|
||||
self.api_key = conf().get("azure_voice_api_key")
|
||||
self.api_region = conf().get("azure_voice_region")
|
||||
self.speech_config = speechsdk.SpeechConfig(
|
||||
subscription=self.api_key, region=self.api_region
|
||||
)
|
||||
self.speech_config.speech_synthesis_voice_name = config[
|
||||
"speech_synthesis_voice_name"
|
||||
]
|
||||
self.speech_config.speech_recognition_language = config[
|
||||
"speech_recognition_language"
|
||||
]
|
||||
self.speech_config = speechsdk.SpeechConfig(subscription=self.api_key, region=self.api_region)
|
||||
self.speech_config.speech_synthesis_voice_name = config["speech_synthesis_voice_name"]
|
||||
self.speech_config.speech_recognition_language = config["speech_recognition_language"]
|
||||
except Exception as e:
|
||||
logger.warn("AzureVoice init failed: %s, ignore " % e)
|
||||
|
||||
def voiceToText(self, voice_file):
|
||||
audio_config = speechsdk.AudioConfig(filename=voice_file)
|
||||
speech_recognizer = speechsdk.SpeechRecognizer(
|
||||
speech_config=self.speech_config, audio_config=audio_config
|
||||
)
|
||||
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=self.speech_config, audio_config=audio_config)
|
||||
result = speech_recognizer.recognize_once()
|
||||
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
|
||||
logger.info(
|
||||
"[Azure] voiceToText voice file name={} text={}".format(
|
||||
voice_file, result.text
|
||||
)
|
||||
)
|
||||
logger.info("[Azure] voiceToText voice file name={} text={}".format(voice_file, result.text))
|
||||
reply = Reply(ReplyType.TEXT, result.text)
|
||||
else:
|
||||
logger.error(
|
||||
"[Azure] voiceToText error, result={}, canceldetails={}".format(
|
||||
result, result.cancellation_details
|
||||
)
|
||||
)
|
||||
logger.error("[Azure] voiceToText error, result={}, canceldetails={}".format(result, result.cancellation_details))
|
||||
reply = Reply(ReplyType.ERROR, "抱歉,语音识别失败")
|
||||
return reply
|
||||
|
||||
def textToVoice(self, text):
|
||||
fileName = TmpDir().path() + "reply-" + str(int(time.time())) + ".wav"
|
||||
audio_config = speechsdk.AudioConfig(filename=fileName)
|
||||
speech_synthesizer = speechsdk.SpeechSynthesizer(
|
||||
speech_config=self.speech_config, audio_config=audio_config
|
||||
)
|
||||
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=self.speech_config, audio_config=audio_config)
|
||||
result = speech_synthesizer.speak_text(text)
|
||||
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
|
||||
logger.info(
|
||||
"[Azure] textToVoice text={} voice file name={}".format(text, fileName)
|
||||
)
|
||||
logger.info("[Azure] textToVoice text={} voice file name={}".format(text, fileName))
|
||||
reply = Reply(ReplyType.VOICE, fileName)
|
||||
else:
|
||||
logger.error(
|
||||
"[Azure] textToVoice error, result={}, canceldetails={}".format(
|
||||
result, result.cancellation_details
|
||||
)
|
||||
)
|
||||
logger.error("[Azure] textToVoice error, result={}, canceldetails={}".format(result, result.cancellation_details))
|
||||
reply = Reply(ReplyType.ERROR, "抱歉,语音合成失败")
|
||||
return reply
|
||||
|
||||
Reference in New Issue
Block a user