TTS: 修正音色为stepaudio合法voice_id, 修回落格式与错误透传
This commit is contained in:
@@ -15,7 +15,7 @@ class CharacterIn(BaseModel):
|
|||||||
name: str
|
name: str
|
||||||
persona: str = ""
|
persona: str = ""
|
||||||
greeting: str = ""
|
greeting: str = ""
|
||||||
voice: str = "tongtong"
|
voice: str = "elegantgentle-female"
|
||||||
avatar_url: str = ""
|
avatar_url: str = ""
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -271,11 +271,11 @@ async def tts(sid: int, mid: int, user: User = Depends(current_user),
|
|||||||
raise HTTPException(404, "消息不存在")
|
raise HTTPException(404, "消息不存在")
|
||||||
client, models = await _channel_for(db, user, s)
|
client, models = await _channel_for(db, user, s)
|
||||||
model = models.get("tts") or "stepaudio-2.5-tts"
|
model = models.get("tts") or "stepaudio-2.5-tts"
|
||||||
voice = "tongtong"
|
voice = "elegantgentle-female"
|
||||||
if s.character_id:
|
if s.character_id:
|
||||||
ch = await db.get(Character, s.character_id)
|
ch = await db.get(Character, s.character_id)
|
||||||
if ch:
|
if ch and ch.voice:
|
||||||
voice = ch.voice
|
voice = ch.voice # 非法音色由 client.fix_voice 兜底
|
||||||
try:
|
try:
|
||||||
audio = await client.tts(model, _dec(user, m.content_enc), voice)
|
audio = await client.tts(model, _dec(user, m.content_enc), voice)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
+21
-13
@@ -44,20 +44,28 @@ class LLMClient:
|
|||||||
d = r.json()["data"][0]
|
d = r.json()["data"][0]
|
||||||
return d.get("url") or ("data:image/png;base64," + d.get("b64_json", ""))
|
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:
|
async def tts(self, model: str, text: str, voice: str) -> bytes:
|
||||||
"""优先 OpenAI 兼容 /audio/speech;失败回落阶跃 stepaudio TTS 格式。"""
|
"""OpenAI 兼容 /audio/speech;input 上限 1000 字符(stepaudio-2.5-tts)。"""
|
||||||
headers = {"Authorization": f"Bearer {self.api_key}"}
|
voice = self.fix_voice(voice)
|
||||||
try:
|
r = await get_http().post(
|
||||||
r = await get_http().post(f"{self.base_url}/audio/speech", headers=headers,
|
f"{self.base_url}/audio/speech",
|
||||||
json={"model": model, "input": text, "voice": voice})
|
headers={"Authorization": f"Bearer {self.api_key}"},
|
||||||
r.raise_for_status()
|
json={"model": model, "input": text[:1000], "voice": voice,
|
||||||
return r.content
|
"response_format": "wav"})
|
||||||
except httpx.HTTPError:
|
if r.status_code >= 400:
|
||||||
r = await get_http().post(f"{self.base_url}/audio/speech", headers=headers,
|
detail = r.text[:150]
|
||||||
json={"model": model, "text": text, "voice": voice,
|
raise RuntimeError(f"{r.status_code}: {detail}")
|
||||||
"response_format": "wav"})
|
return r.content
|
||||||
r.raise_for_status()
|
|
||||||
return r.content
|
|
||||||
|
|
||||||
async def ping(self, model: str) -> tuple[bool, str]:
|
async def ping(self, model: str) -> tuple[bool, str]:
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user