From 9f90de05415304d62d8eb2ff0caa3f6a530e2711 Mon Sep 17 00:00:00 2001 From: zhiqui Date: Sun, 15 Nov 2026 16:48:04 +0800 Subject: [PATCH] =?UTF-8?q?TTS:=20=E4=BF=AE=E6=AD=A3=E9=9F=B3=E8=89=B2?= =?UTF-8?q?=E4=B8=BAstepaudio=E5=90=88=E6=B3=95voice=5Fid,=20=E4=BF=AE?= =?UTF-8?q?=E5=9B=9E=E8=90=BD=E6=A0=BC=E5=BC=8F=E4=B8=8E=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E9=80=8F=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/routers/characters.py | 2 +- app/api/routers/chat.py | 6 +++--- app/services/llm.py | 34 +++++++++++++++++++++------------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/app/api/routers/characters.py b/app/api/routers/characters.py index f364c0ff..d4bf021b 100644 --- a/app/api/routers/characters.py +++ b/app/api/routers/characters.py @@ -15,7 +15,7 @@ class CharacterIn(BaseModel): name: str persona: str = "" greeting: str = "" - voice: str = "tongtong" + voice: str = "elegantgentle-female" avatar_url: str = "" diff --git a/app/api/routers/chat.py b/app/api/routers/chat.py index f5cf7055..f889ca51 100644 --- a/app/api/routers/chat.py +++ b/app/api/routers/chat.py @@ -271,11 +271,11 @@ async def tts(sid: int, mid: int, user: User = Depends(current_user), raise HTTPException(404, "消息不存在") client, models = await _channel_for(db, user, s) model = models.get("tts") or "stepaudio-2.5-tts" - voice = "tongtong" + voice = "elegantgentle-female" if s.character_id: ch = await db.get(Character, s.character_id) - if ch: - voice = ch.voice + if ch and ch.voice: + voice = ch.voice # 非法音色由 client.fix_voice 兜底 try: audio = await client.tts(model, _dec(user, m.content_enc), voice) except Exception as e: diff --git a/app/services/llm.py b/app/services/llm.py index 7c42921c..575c750e 100644 --- a/app/services/llm.py +++ b/app/services/llm.py @@ -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: