fix: 修复 minimax 相关问题

This commit is contained in:
Soulter
2025-05-16 18:32:08 +08:00
parent c6eaf3d010
commit c15f966669

View File

@@ -91,31 +91,42 @@ class ProviderMiniMaxTTSAPI(TTSProvider):
) as response: ) as response:
response.raise_for_status() response.raise_for_status()
async for chunk in response.content.iter_any(): buffer = b""
if not chunk or not chunk.startswith(b"data:"): while True:
logger.warning(f"Minimax TTS resp: {chunk}") chunk = await response.content.read(8192)
if "invalid api key" in chunk.decode("utf-8"): if not chunk:
raise Exception("MiniMax TTS: 无效的 API 密钥") break
continue
try: buffer += chunk
data = json.loads(chunk[5:])
if "extra_info" in data: while b"\n\n" in buffer:
continue try:
audio = data.get("data", {}).get("audio") message, buffer = buffer.split(b"\n\n", 1)
if audio is not None: if message.startswith(b"data: "):
yield audio try:
except json.JSONDecodeError: data = json.loads(message[6:])
continue if "extra_info" in data:
continue
audio = data.get("data", {}).get("audio")
if audio is not None:
yield audio
except json.JSONDecodeError:
logger.warning(
"Failed to parse JSON data from SSE message"
)
continue
except ValueError:
buffer = buffer[-1024:]
except aiohttp.ClientError as e: except aiohttp.ClientError as e:
raise Exception(f"MiniMax TTS API请求失败: {str(e)}") raise Exception(f"MiniMax TTS API请求失败: {str(e)}")
async def _audio_play(self, audio_stream: AsyncIterator[bytes]) -> bytes: async def _audio_play(self, audio_stream: AsyncIterator[str]) -> bytes:
"""解码数据流到 audio 比特流""" """解码数据流到 audio 比特流"""
chunks = [] chunks = []
async for chunk in audio_stream: async for chunk in audio_stream:
if chunk and chunk != b"\n": if chunk.strip():
chunks.append(bytes.fromhex(chunk.decode("utf-8"))) chunks.append(bytes.fromhex(chunk.strip()))
return b"".join(chunks) return b"".join(chunks)
async def get_audio(self, text: str) -> str: async def get_audio(self, text: str) -> str: