diff --git a/voice/ali/ali_api.py b/voice/ali/ali_api.py index d17db14e..bd524024 100644 --- a/voice/ali/ali_api.py +++ b/voice/ali/ali_api.py @@ -10,7 +10,6 @@ Description: import http.client import json -import time import requests import datetime import hashlib @@ -51,7 +50,7 @@ def text_to_speech_aliyun(url, text, appkey, token): response = requests.post(url, headers=headers, data=json.dumps(data), timeout=(5, 60), stream=True) if response.status_code == 200 and response.headers['Content-Type'] == 'audio/mpeg': - output_file = TmpDir().path() + "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".wav" + output_file = TmpDir().path() + "reply-" + uuid.uuid4().hex + ".wav" try: save_response(response, output_file, MAX_FILE_BYTES) diff --git a/voice/azure/azure_voice.py b/voice/azure/azure_voice.py index b0ec8b81..133efb00 100644 --- a/voice/azure/azure_voice.py +++ b/voice/azure/azure_voice.py @@ -3,7 +3,7 @@ azure voice service """ import json import os -import time +import uuid import azure.cognitiveservices.speech as speechsdk from langid import classify @@ -81,7 +81,7 @@ class AzureVoice(Voice): else: self.speech_config.speech_synthesis_voice_name = self.config["speech_synthesis_voice_name"] # Avoid the same filename under multithreading - fileName = TmpDir().path() + "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".wav" + fileName = TmpDir().path() + "reply-" + uuid.uuid4().hex + ".wav" audio_config = speechsdk.AudioConfig(filename=fileName) speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=self.speech_config, audio_config=audio_config) result = speech_synthesizer.speak_text(text) diff --git a/voice/baidu/baidu_voice.py b/voice/baidu/baidu_voice.py index e717df41..8c11131e 100644 --- a/voice/baidu/baidu_voice.py +++ b/voice/baidu/baidu_voice.py @@ -6,6 +6,7 @@ import os import time import threading import requests +import uuid from aip import AipSpeech @@ -157,7 +158,7 @@ class BaiduVoice(Voice): # used to be buffered whole by `.content` and written out before # anything could object. `download_to_file` also writes through a temp # file, so an oversized or truncated download leaves nothing behind. - fn = TmpDir().path() + f"reply-long-{int(time.time())}-{hash(text)&0x7FFFFFFF}.mp3" + fn = TmpDir().path() + f"reply-long-{uuid.uuid4().hex}.mp3" try: download_to_file(audio_url, fn, MAX_FILE_BYTES, timeout=REQUEST_TIMEOUT) except Exception as e: @@ -177,7 +178,7 @@ class BaiduVoice(Voice): {"spd":self.spd, "pit":self.pit, "vol":self.vol, "per":self.per} ) if not isinstance(result, dict): - fn = TmpDir().path() + f"reply-{int(time.time())}-{hash(text)&0x7FFFFFFF}.mp3" + fn = TmpDir().path() + f"reply-{uuid.uuid4().hex}.mp3" with open(fn, "wb") as f: f.write(result) logger.info("[Baidu] 短文本合成 success: %s", fn) diff --git a/voice/elevent/elevent_voice.py b/voice/elevent/elevent_voice.py index e6e12028..74c4a80d 100644 --- a/voice/elevent/elevent_voice.py +++ b/voice/elevent/elevent_voice.py @@ -1,5 +1,5 @@ # Requires: elevenlabs==1.0.3 (pip install elevenlabs==1.0.3) -import time +import uuid from elevenlabs.client import ElevenLabs from elevenlabs import save @@ -22,7 +22,7 @@ class ElevenLabsVoice(Voice): pass def textToVoice(self, text): - fileName = TmpDir().path() + "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".mp3" + fileName = TmpDir().path() + "reply-" + uuid.uuid4().hex + ".mp3" try: audio = client.generate( diff --git a/voice/tencent/tencent_voice.py b/voice/tencent/tencent_voice.py index a02a9e13..de86a654 100644 --- a/voice/tencent/tencent_voice.py +++ b/voice/tencent/tencent_voice.py @@ -2,6 +2,7 @@ import json import base64 import os import time +import uuid from voice.voice import Voice from common.log import logger from tencentcloud.common import credential @@ -105,7 +106,7 @@ class TencentVoice(Voice): response = client.TextToVoice(req) if response.Audio: - fileName = TmpDir().path() + "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".mp3" + fileName = TmpDir().path() + "reply-" + uuid.uuid4().hex + ".mp3" with open(fileName, "wb") as f: f.write(base64.b64decode(response.Audio)) logger.info("[Tencent] textToVoice text={} voice file name={}".format(text, fileName)) diff --git a/voice/xunfei/xunfei_voice.py b/voice/xunfei/xunfei_voice.py index d40e0895..f289262f 100644 --- a/voice/xunfei/xunfei_voice.py +++ b/voice/xunfei/xunfei_voice.py @@ -29,6 +29,7 @@ from voice.voice import Voice from .xunfei_asr import xunfei_asr from .xunfei_tts import xunfei_tts import shutil +import uuid try: from voice.audio_convert import any_to_mp3 @@ -81,7 +82,7 @@ class XunfeiVoice(Voice): def textToVoice(self, text): try: # Avoid the same filename under multithreading - fileName = TmpDir().path() + "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".mp3" + fileName = TmpDir().path() + "reply-" + uuid.uuid4().hex + ".mp3" xunfei_tts(self.APPID,self.APIKey,self.APISecret,self.BusinessArgsTTS,text,fileName) logger.info("[Xunfei] textToVoice text={} voice file name={}".format(text, fileName)) reply = Reply(ReplyType.VOICE, fileName)