TTS: 修正音色为stepaudio合法voice_id, 修回落格式与错误透传

This commit is contained in:
zhiqui
2026-11-15 16:48:04 +08:00
parent 6aa3559033
commit 9f90de0541
3 changed files with 25 additions and 17 deletions
+21 -13
View File
@@ -44,20 +44,28 @@ class LLMClient:
d = r.json()["data"][0]
return d.get("url") or ("data:image/png;base64," + d.get("b64_json", ""))
# 阶跃 stepaudio 系合法音色(system_voices 实测 + 官方文档)
TTS_VOICES = {"elegantgentle-female", "livelybreezy-female", "lively-girl",
"cixingnansheng", "wenrounansheng", "shenchennanyin",
"zhengpaiqingnian", "shuangkuainansheng", "ganliannvsheng",
"qinhenvsheng", "huolinvsheng"}
@staticmethod
def fix_voice(voice: str) -> str:
return voice if voice in LLMClient.TTS_VOICES else "elegantgentle-female"
async def tts(self, model: str, text: str, voice: str) -> bytes:
"""优先 OpenAI 兼容 /audio/speech;失败回落阶跃 stepaudio TTS 格式。"""
headers = {"Authorization": f"Bearer {self.api_key}"}
try:
r = await get_http().post(f"{self.base_url}/audio/speech", headers=headers,
json={"model": model, "input": text, "voice": voice})
r.raise_for_status()
return r.content
except httpx.HTTPError:
r = await get_http().post(f"{self.base_url}/audio/speech", headers=headers,
json={"model": model, "text": text, "voice": voice,
"response_format": "wav"})
r.raise_for_status()
return r.content
"""OpenAI 兼容 /audio/speech;input 上限 1000 字符(stepaudio-2.5-tts)。"""
voice = self.fix_voice(voice)
r = await get_http().post(
f"{self.base_url}/audio/speech",
headers={"Authorization": f"Bearer {self.api_key}"},
json={"model": model, "input": text[:1000], "voice": voice,
"response_format": "wav"})
if r.status_code >= 400:
detail = r.text[:150]
raise RuntimeError(f"{r.status_code}: {detail}")
return r.content
async def ping(self, model: str) -> tuple[bool, str]:
try: