diff --git a/main/manager-api/src/main/resources/db/changelog/202608131030.sql b/main/manager-api/src/main/resources/db/changelog/202608131030.sql new file mode 100644 index 000000000..626607906 --- /dev/null +++ b/main/manager-api/src/main/resources/db/changelog/202608131030.sql @@ -0,0 +1,4 @@ +UPDATE `sys_params` +SET `param_value` = '15' +WHERE `param_code` = 'tts_timeout' + AND `param_value` = '10'; diff --git a/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml b/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml index 90a5148dc..f96280d13 100644 --- a/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml @@ -725,3 +725,10 @@ databaseChangeLog: - sqlFile: encoding: utf8 path: classpath:db/changelog/202608061030.sql + - changeSet: + id: 202608131030 + author: tykechen + changes: + - sqlFile: + encoding: utf8 + path: classpath:db/changelog/202608131030.sql diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index c35cc523e..25f87225e 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -60,7 +60,7 @@ delete_audio: true # 没有语音输入多久后断开连接(秒),默认2分钟,即120秒 close_connection_no_voice_time: 120 # TTS请求超时时间(秒) -tts_timeout: 10 +tts_timeout: 15 # 工具调用超时时间(秒) tool_call_timeout: 30 # 开启唤醒词加速 diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 8d46d802c..718f39095 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -808,6 +808,7 @@ class ConnectionHandler: self.headers.get("client-id", self.headers.get("device-id")), ) private_config["delete_audio"] = bool(self.config.get("delete_audio", True)) + private_config["tts_timeout"] = self.config.get("tts_timeout", 15) self.logger.bind(tag=TAG).info( f"{time.time() - begin_time} 秒,异步获取差异化配置成功: {json.dumps(filter_sensitive_info(private_config), ensure_ascii=False)}" ) diff --git a/main/xiaozhi-server/core/providers/tts/aliyun.py b/main/xiaozhi-server/core/providers/tts/aliyun.py index 2dc39a9d6..bfe30ff3a 100644 --- a/main/xiaozhi-server/core/providers/tts/aliyun.py +++ b/main/xiaozhi-server/core/providers/tts/aliyun.py @@ -31,7 +31,7 @@ class AccessToken: return encoded_text.replace("+", "%20").replace("*", "%2A").replace("%7E", "~") @staticmethod - def create_token(access_key_id, access_key_secret): + def create_token(access_key_id, access_key_secret, timeout): parameters = { "AccessKeyId": access_key_id, "Action": "CreateToken", @@ -73,7 +73,7 @@ class AccessToken: ) # print('url: %s' % full_url) # 提交HTTP GET请求 - response = requests.get(full_url) + response = requests.get(full_url, timeout=timeout) if response.ok: root_obj = response.json() key = "Token" @@ -135,7 +135,7 @@ class TTSProvider(TTSProviderBase): """刷新Token并记录过期时间""" if self.access_key_id and self.access_key_secret: self.token, expire_time_str = AccessToken.create_token( - self.access_key_id, self.access_key_secret + self.access_key_id, self.access_key_secret, self.tts_timeout ) if not expire_time_str: raise ValueError("无法获取有效的Token过期时间") @@ -189,12 +189,18 @@ class TTSProvider(TTSProviderBase): # print(self.api_url, json.dumps(request_json, ensure_ascii=False)) try: resp = requests.post( - self.api_url, json.dumps(request_json), headers=self.header + self.api_url, + json.dumps(request_json), + headers=self.header, + timeout=self.tts_timeout, ) if resp.status_code == 401: # Token过期特殊处理 self._refresh_token() resp = requests.post( - self.api_url, json.dumps(request_json), headers=self.header + self.api_url, + json.dumps(request_json), + headers=self.header, + timeout=self.tts_timeout, ) # 检查返回请求数据的mime类型是否是audio/***,是则保存到指定路径下;返回的是binary格式的 if resp.headers["Content-Type"].startswith("audio/"): diff --git a/main/xiaozhi-server/core/providers/tts/aliyun_stream.py b/main/xiaozhi-server/core/providers/tts/aliyun_stream.py index 88f573b53..37c6077b7 100644 --- a/main/xiaozhi-server/core/providers/tts/aliyun_stream.py +++ b/main/xiaozhi-server/core/providers/tts/aliyun_stream.py @@ -38,7 +38,7 @@ class AccessToken: return encoded_text.replace("+", "%20").replace("*", "%2A").replace("%7E", "~") @staticmethod - def create_token(access_key_id, access_key_secret): + def create_token(access_key_id, access_key_secret, timeout): parameters = { "AccessKeyId": access_key_id, "Action": "CreateToken", @@ -75,7 +75,7 @@ class AccessToken: import requests - response = requests.get(full_url) + response = requests.get(full_url, timeout=timeout) if response.ok: root_obj = response.json() key = "Token" @@ -152,7 +152,7 @@ class TTSProvider(TTSProviderBase): """刷新Token并记录过期时间""" if self.access_key_id and self.access_key_secret: self.token, expire_time_str = AccessToken.create_token( - self.access_key_id, self.access_key_secret + self.access_key_id, self.access_key_secret, self.tts_timeout ) if not expire_time_str: raise ValueError("无法获取有效的Token过期时间") @@ -646,4 +646,4 @@ class TTSProvider(TTSProviderBase): return audio_data except Exception as e: logger.bind(tag=TAG).error(f"生成音频数据失败: {str(e)}") - return [] \ No newline at end of file + return [] diff --git a/main/xiaozhi-server/core/providers/tts/cozecn.py b/main/xiaozhi-server/core/providers/tts/cozecn.py index 1d727a9df..dbc6748a6 100644 --- a/main/xiaozhi-server/core/providers/tts/cozecn.py +++ b/main/xiaozhi-server/core/providers/tts/cozecn.py @@ -47,7 +47,11 @@ class TTSProvider(TTSProviderBase): try: response = requests.request( - "POST", self.api_url, json=request_json, headers=headers + "POST", + self.api_url, + json=request_json, + headers=headers, + timeout=self.tts_timeout, ) data = response.content if output_file: diff --git a/main/xiaozhi-server/core/providers/tts/custom.py b/main/xiaozhi-server/core/providers/tts/custom.py index 5e6a43acf..45b1b91f9 100644 --- a/main/xiaozhi-server/core/providers/tts/custom.py +++ b/main/xiaozhi-server/core/providers/tts/custom.py @@ -38,9 +38,19 @@ class TTSProvider(TTSProviderBase): request_params[k] = v if self.method.upper() == "POST": - resp = requests.post(self.url, json=request_params, headers=self.headers) + resp = requests.post( + self.url, + json=request_params, + headers=self.headers, + timeout=self.tts_timeout, + ) else: - resp = requests.get(self.url, params=request_params, headers=self.headers) + resp = requests.get( + self.url, + params=request_params, + headers=self.headers, + timeout=self.tts_timeout, + ) if resp.status_code == 200: if output_file: with open(output_file, "wb") as file: diff --git a/main/xiaozhi-server/core/providers/tts/doubao.py b/main/xiaozhi-server/core/providers/tts/doubao.py index 42434c4fd..3e3f180c2 100644 --- a/main/xiaozhi-server/core/providers/tts/doubao.py +++ b/main/xiaozhi-server/core/providers/tts/doubao.py @@ -80,7 +80,10 @@ class TTSProvider(TTSProviderBase): try: resp = requests.post( - self.api_url, json.dumps(request_json), headers=self.header + self.api_url, + json.dumps(request_json), + headers=self.header, + timeout=self.tts_timeout, ) if "data" in resp.json(): data = resp.json()["data"] diff --git a/main/xiaozhi-server/core/providers/tts/fishspeech.py b/main/xiaozhi-server/core/providers/tts/fishspeech.py index 163ba2c1b..2844f91c2 100644 --- a/main/xiaozhi-server/core/providers/tts/fishspeech.py +++ b/main/xiaozhi-server/core/providers/tts/fishspeech.py @@ -169,6 +169,7 @@ class TTSProvider(TTSProviderBase): "Authorization": f"Bearer {self.api_key}", "Content-Type": "application/msgpack", }, + timeout=self.tts_timeout, ) if response.status_code == 200: diff --git a/main/xiaozhi-server/core/providers/tts/gpt_sovits_v2.py b/main/xiaozhi-server/core/providers/tts/gpt_sovits_v2.py index 33586168f..b0eec74fe 100644 --- a/main/xiaozhi-server/core/providers/tts/gpt_sovits_v2.py +++ b/main/xiaozhi-server/core/providers/tts/gpt_sovits_v2.py @@ -90,7 +90,9 @@ class TTSProvider(TTSProviderBase): "repetition_penalty": self.repetition_penalty, } - resp = requests.post(self.url, json=request_json) + resp = requests.post( + self.url, json=request_json, timeout=self.tts_timeout + ) if resp.status_code == 200: if output_file: with open(output_file, "wb") as file: diff --git a/main/xiaozhi-server/core/providers/tts/gpt_sovits_v3.py b/main/xiaozhi-server/core/providers/tts/gpt_sovits_v3.py index a71e11e44..d219a1c59 100644 --- a/main/xiaozhi-server/core/providers/tts/gpt_sovits_v3.py +++ b/main/xiaozhi-server/core/providers/tts/gpt_sovits_v3.py @@ -51,7 +51,9 @@ class TTSProvider(TTSProviderBase): "if_sr": self.if_sr, } - resp = requests.get(self.url, params=request_params) + resp = requests.get( + self.url, params=request_params, timeout=self.tts_timeout + ) if resp.status_code == 200: if output_file: with open(output_file, "wb") as file: diff --git a/main/xiaozhi-server/core/providers/tts/index_stream.py b/main/xiaozhi-server/core/providers/tts/index_stream.py index 7314ac9c2..49af09c39 100644 --- a/main/xiaozhi-server/core/providers/tts/index_stream.py +++ b/main/xiaozhi-server/core/providers/tts/index_stream.py @@ -130,7 +130,9 @@ class TTSProvider(TTSProviderBase): ) # 16-bit = 2 bytes try: async with aiohttp.ClientSession() as session: - async with session.post(self.api_url, json=payload, timeout=10) as resp: + async with session.post( + self.api_url, json=payload, timeout=self.tts_timeout + ) as resp: if resp.status != 200: logger.bind(tag=TAG).error( @@ -212,7 +214,9 @@ class TTSProvider(TTSProviderBase): payload = {"text": text, "character": self.voice} try: - with requests.post(self.api_url, json=payload, timeout=5) as response: + with requests.post( + self.api_url, json=payload, timeout=self.tts_timeout + ) as response: if response.status_code != 200: logger.bind(tag=TAG).error( f"TTS请求失败: {response.status_code}, {response.text}" @@ -251,4 +255,4 @@ class TTSProvider(TTSProviderBase): except Exception as e: logger.bind(tag=TAG).error(f"TTS请求异常: {e}") - return [] \ No newline at end of file + return [] diff --git a/main/xiaozhi-server/core/providers/tts/minimax_httpstream.py b/main/xiaozhi-server/core/providers/tts/minimax_httpstream.py index f620adfb1..1ff59d681 100644 --- a/main/xiaozhi-server/core/providers/tts/minimax_httpstream.py +++ b/main/xiaozhi-server/core/providers/tts/minimax_httpstream.py @@ -201,7 +201,7 @@ class TTSProvider(TTSProviderBase): self.api_url, headers=self.header, data=json.dumps(payload), - timeout=10, + timeout=self.tts_timeout, ) as resp: if resp.status != 200: @@ -324,7 +324,10 @@ class TTSProvider(TTSProviderBase): try: with requests.post( - self.api_url, data=json.dumps(payload), headers=headers, timeout=5 + self.api_url, + data=json.dumps(payload), + headers=headers, + timeout=self.tts_timeout, ) as response: if response.status_code != 200: logger.bind(tag=TAG).error( diff --git a/main/xiaozhi-server/core/providers/tts/openai.py b/main/xiaozhi-server/core/providers/tts/openai.py index 40a0134e2..28a6501bf 100644 --- a/main/xiaozhi-server/core/providers/tts/openai.py +++ b/main/xiaozhi-server/core/providers/tts/openai.py @@ -47,7 +47,9 @@ class TTSProvider(TTSProviderBase): "response_format": self.audio_file_type, "speed": self.speed, } - response = requests.post(self.api_url, json=data, headers=headers) + response = requests.post( + self.api_url, json=data, headers=headers, timeout=self.tts_timeout + ) if response.status_code == 200: if output_file: with open(output_file, "wb") as audio_file: diff --git a/main/xiaozhi-server/core/providers/tts/paddle_speech.py b/main/xiaozhi-server/core/providers/tts/paddle_speech.py index 1427499ec..e9562d60e 100644 --- a/main/xiaozhi-server/core/providers/tts/paddle_speech.py +++ b/main/xiaozhi-server/core/providers/tts/paddle_speech.py @@ -111,7 +111,7 @@ class TTSProvider(TTSProviderBase): await ws.send(json.dumps(data_request)) audio_chunks = b"" - timeout_seconds = 60 # 设置超时 + timeout_seconds = self.tts_timeout try: while True: response = await asyncio.wait_for(ws.recv(), timeout=timeout_seconds) @@ -154,4 +154,4 @@ class TTSProvider(TTSProviderBase): return wav_data except Exception as e: - raise Exception(f"Error during TTS WebSocket request: {e} while processing text: {text}") \ No newline at end of file + raise Exception(f"Error during TTS WebSocket request: {e} while processing text: {text}") diff --git a/main/xiaozhi-server/core/providers/tts/tencent.py b/main/xiaozhi-server/core/providers/tts/tencent.py index 50964d336..6f1415509 100644 --- a/main/xiaozhi-server/core/providers/tts/tencent.py +++ b/main/xiaozhi-server/core/providers/tts/tencent.py @@ -154,7 +154,10 @@ class TTSProvider(TTSProviderBase): # 发送请求 resp = requests.post( - self.api_url, json.dumps(request_json), headers=headers + self.api_url, + json.dumps(request_json), + headers=headers, + timeout=self.tts_timeout, ) # 检查响应 diff --git a/main/xiaozhi-server/core/utils/modules_initialize.py b/main/xiaozhi-server/core/utils/modules_initialize.py index afe041098..7c43dfe37 100644 --- a/main/xiaozhi-server/core/utils/modules_initialize.py +++ b/main/xiaozhi-server/core/utils/modules_initialize.py @@ -100,14 +100,16 @@ def initialize_modules( def initialize_tts(config): select_tts_module = config["selected_module"]["TTS"] + tts_config = config["TTS"][select_tts_module].copy() + tts_config.setdefault("tts_timeout", config.get("tts_timeout", 15)) tts_type = ( select_tts_module - if "type" not in config["TTS"][select_tts_module] - else config["TTS"][select_tts_module]["type"] + if "type" not in tts_config + else tts_config["type"] ) new_tts = tts.create_instance( tts_type, - config["TTS"][select_tts_module], + tts_config, str(config.get("delete_audio", True)).lower() in ("true", "1", "yes"), ) return new_tts @@ -148,4 +150,3 @@ def initialize_voiceprint(asr_instance, config): except Exception as e: logger.bind(tag=TAG).error(f"动态初始化声纹识别功能失败: {str(e)}") return False -