mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-18 12:40:06 +08:00
formatting: run precommit on all files
This commit is contained in:
@@ -33,6 +33,7 @@ def get_pcm_from_wav(wav_path):
|
||||
wav = wave.open(wav_path, "rb")
|
||||
return wav.readframes(wav.getnframes())
|
||||
|
||||
|
||||
def any_to_mp3(any_path, mp3_path):
|
||||
"""
|
||||
把任意格式转成mp3文件
|
||||
@@ -40,16 +41,13 @@ def any_to_mp3(any_path, mp3_path):
|
||||
if any_path.endswith(".mp3"):
|
||||
shutil.copy2(any_path, mp3_path)
|
||||
return
|
||||
if (
|
||||
any_path.endswith(".sil")
|
||||
or any_path.endswith(".silk")
|
||||
or any_path.endswith(".slk")
|
||||
):
|
||||
if any_path.endswith(".sil") or any_path.endswith(".silk") or any_path.endswith(".slk"):
|
||||
sil_to_wav(any_path, any_path)
|
||||
any_path = mp3_path
|
||||
audio = AudioSegment.from_file(any_path)
|
||||
audio.export(mp3_path, format="mp3")
|
||||
|
||||
|
||||
def any_to_wav(any_path, wav_path):
|
||||
"""
|
||||
把任意格式转成wav文件
|
||||
@@ -57,11 +55,7 @@ def any_to_wav(any_path, wav_path):
|
||||
if any_path.endswith(".wav"):
|
||||
shutil.copy2(any_path, wav_path)
|
||||
return
|
||||
if (
|
||||
any_path.endswith(".sil")
|
||||
or any_path.endswith(".silk")
|
||||
or any_path.endswith(".slk")
|
||||
):
|
||||
if any_path.endswith(".sil") or any_path.endswith(".silk") or any_path.endswith(".slk"):
|
||||
return sil_to_wav(any_path, wav_path)
|
||||
audio = AudioSegment.from_file(any_path)
|
||||
audio.export(wav_path, format="wav")
|
||||
@@ -71,11 +65,7 @@ def any_to_sil(any_path, sil_path):
|
||||
"""
|
||||
把任意格式转成sil文件
|
||||
"""
|
||||
if (
|
||||
any_path.endswith(".sil")
|
||||
or any_path.endswith(".silk")
|
||||
or any_path.endswith(".slk")
|
||||
):
|
||||
if any_path.endswith(".sil") or any_path.endswith(".silk") or any_path.endswith(".slk"):
|
||||
shutil.copy2(any_path, sil_path)
|
||||
return 10000
|
||||
audio = AudioSegment.from_file(any_path)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -85,9 +85,7 @@ class BaiduVoice(Voice):
|
||||
fileName = TmpDir().path() + "reply-" + str(int(time.time())) + ".mp3"
|
||||
with open(fileName, "wb") as f:
|
||||
f.write(result)
|
||||
logger.info(
|
||||
"[Baidu] textToVoice text={} voice file name={}".format(text, fileName)
|
||||
)
|
||||
logger.info("[Baidu] textToVoice text={} voice file name={}".format(text, fileName))
|
||||
reply = Reply(ReplyType.VOICE, fileName)
|
||||
else:
|
||||
logger.error("[Baidu] textToVoice error={}".format(result))
|
||||
|
||||
@@ -24,11 +24,7 @@ class GoogleVoice(Voice):
|
||||
audio = self.recognizer.record(source)
|
||||
try:
|
||||
text = self.recognizer.recognize_google(audio, language="zh-CN")
|
||||
logger.info(
|
||||
"[Google] voiceToText text={} voice file name={}".format(
|
||||
text, voice_file
|
||||
)
|
||||
)
|
||||
logger.info("[Google] voiceToText text={} voice file name={}".format(text, voice_file))
|
||||
reply = Reply(ReplyType.TEXT, text)
|
||||
except speech_recognition.UnknownValueError:
|
||||
reply = Reply(ReplyType.ERROR, "抱歉,我听不懂")
|
||||
@@ -42,9 +38,7 @@ class GoogleVoice(Voice):
|
||||
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)
|
||||
)
|
||||
logger.info("[Google] textToVoice text={} voice file name={}".format(text, mp3File))
|
||||
reply = Reply(ReplyType.VOICE, mp3File)
|
||||
except Exception as e:
|
||||
reply = Reply(ReplyType.ERROR, str(e))
|
||||
|
||||
@@ -22,11 +22,7 @@ class OpenaiVoice(Voice):
|
||||
result = openai.Audio.transcribe("whisper-1", file)
|
||||
text = result["text"]
|
||||
reply = Reply(ReplyType.TEXT, text)
|
||||
logger.info(
|
||||
"[Openai] voiceToText text={} voice file name={}".format(
|
||||
text, voice_file
|
||||
)
|
||||
)
|
||||
logger.info("[Openai] voiceToText text={} voice file name={}".format(text, voice_file))
|
||||
except Exception as e:
|
||||
reply = Reply(ReplyType.ERROR, str(e))
|
||||
finally:
|
||||
|
||||
@@ -5,6 +5,7 @@ pytts voice service (offline)
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
import pyttsx3
|
||||
|
||||
from bridge.reply import Reply, ReplyType
|
||||
@@ -12,6 +13,7 @@ from common.log import logger
|
||||
from common.tmp_dir import TmpDir
|
||||
from voice.voice import Voice
|
||||
|
||||
|
||||
class PyttsVoice(Voice):
|
||||
engine = pyttsx3.init()
|
||||
|
||||
@@ -20,7 +22,7 @@ class PyttsVoice(Voice):
|
||||
self.engine.setProperty("rate", 125)
|
||||
# 音量
|
||||
self.engine.setProperty("volume", 1.0)
|
||||
if sys.platform == 'win32':
|
||||
if sys.platform == "win32":
|
||||
for voice in self.engine.getProperty("voices"):
|
||||
if "Chinese" in voice.name:
|
||||
self.engine.setProperty("voice", voice.id)
|
||||
@@ -33,23 +35,23 @@ class PyttsVoice(Voice):
|
||||
def textToVoice(self, text):
|
||||
try:
|
||||
# avoid the same filename
|
||||
wavFileName = "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7fffffff) + ".wav"
|
||||
wavFileName = "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".wav"
|
||||
wavFile = TmpDir().path() + wavFileName
|
||||
logger.info("[Pytts] textToVoice text={} voice file name={}".format(text, wavFile))
|
||||
|
||||
self.engine.save_to_file(text, wavFile)
|
||||
|
||||
if sys.platform == 'win32':
|
||||
if sys.platform == "win32":
|
||||
self.engine.runAndWait()
|
||||
else:
|
||||
# In ubuntu, runAndWait do not really wait until the file created.
|
||||
# In ubuntu, runAndWait do not really wait until the file created.
|
||||
# It will return once the task queue is empty, but the task is still running in coroutine.
|
||||
# And if you call runAndWait() and time.sleep() twice, it will stuck, so do not use this.
|
||||
# If you want to fix this, add self._proxy.setBusy(True) in line 127 in espeak.py, at the beginning of the function save_to_file.
|
||||
# self.engine.runAndWait()
|
||||
|
||||
# Before espeak fix this problem, we iterate the generator and control the waiting by ourself.
|
||||
# But this is not the canonical way to use it, for example if the file already exists it also cannot wait.
|
||||
# But this is not the canonical way to use it, for example if the file already exists it also cannot wait.
|
||||
self.engine.iterate()
|
||||
while self.engine.isBusy() or wavFileName not in os.listdir(TmpDir().path()):
|
||||
time.sleep(0.1)
|
||||
|
||||
Reference in New Issue
Block a user