Skip to content

Commit 2a5d574

Browse files
authored
fix: failed to initialize FishAudio TTS instance (#4200)
fixes: #4172 * fix: 修复 FishAudio 源的配置加载问题并增强请求鲁棒性 - Fix `KeyError: 'model'``: 适配新版配置结构。 - Add `timeout` support: 防止长文本生成时超时。 - Improve response handling: 使用更标准的 Header 检查方式。 * feat: 使用更安全的类型转换并优化错误信息打印
1 parent f2924fb commit 2a5d574

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

astrbot/core/provider/sources/fishaudio_tts_api_source.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ def __init__(
5656
"api_base",
5757
"https://api.fish-audio.cn/v1",
5858
)
59+
try:
60+
self.timeout: int = int(provider_config.get("timeout", 20))
61+
except ValueError:
62+
self.timeout = 20
5963
self.headers = {
6064
"Authorization": f"Bearer {self.chosen_api_key}",
6165
}
62-
self.set_model(provider_config["model"])
66+
self.set_model(provider_config.get("model", None))
6367

6468
async def _get_reference_id_by_character(self, character: str) -> str | None:
6569
"""获取角色的reference_id
@@ -135,17 +139,21 @@ async def get_audio(self, text: str) -> str:
135139
path = os.path.join(temp_dir, f"fishaudio_tts_api_{uuid.uuid4()}.wav")
136140
self.headers["content-type"] = "application/msgpack"
137141
request = await self._generate_request(text)
138-
async with AsyncClient(base_url=self.api_base).stream(
142+
async with AsyncClient(base_url=self.api_base, timeout=self.timeout).stream(
139143
"POST",
140144
"/tts",
141145
headers=self.headers,
142146
content=ormsgpack.packb(request, option=ormsgpack.OPT_SERIALIZE_PYDANTIC),
143147
) as response:
144-
if response.headers["content-type"] == "audio/wav":
148+
if response.status_code == 200 and response.headers.get(
149+
"content-type", ""
150+
).startswith("audio/"):
145151
with open(path, "wb") as f:
146152
async for chunk in response.aiter_bytes():
147153
f.write(chunk)
148154
return path
149-
body = await response.aread()
150-
text = body.decode("utf-8", errors="replace")
151-
raise Exception(f"Fish Audio API请求失败: {text}")
155+
error_bytes = await response.aread()
156+
error_text = error_bytes.decode("utf-8", errors="replace")[:1024]
157+
raise Exception(
158+
f"Fish Audio API请求失败: 状态码 {response.status_code}, 响应内容: {error_text}"
159+
)

0 commit comments

Comments
 (0)