Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

Prevent MiMo TTS from replaying paid requests after a lost response

Rudy Celekli6b0ff3596c9f
diff --git a/app/services/voice.py b/app/services/voice.py
index 0358cc31..30e171ce 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -1878,14 +1878,17 @@ def mimo_tts(
_configure_pydub_ffmpeg(AudioSegment)
- for i in range(3):
- try:
- logger.info(
- f"start mimo tts, model: {model_name}, voice: {voice_name}, try: {i + 1}"
- )
- ensure_file_path_exists(voice_file)
+ try:
+ logger.info(
+ f"start mimo tts, model: {model_name}, voice: {voice_name}"
+ )
+ ensure_file_path_exists(voice_file)
- client = OpenAI(api_key=api_key, base_url=base_url)
+ # A lost response may still have generated and billed the narration.
+ # Disable SDK retries as well as the outer retry loop.
+ with OpenAI(
+ api_key=api_key, base_url=base_url, max_retries=0, timeout=120.0
+ ) as client:
completion = client.chat.completions.create(
model=model_name,
messages=[
@@ -1898,44 +1901,44 @@ def mimo_tts(
},
)
- if not completion or not getattr(completion, "choices", None):
- raise ValueError("MiMo TTS returned empty response")
+ if not completion or not getattr(completion, "choices", None):
+ raise ValueError("MiMo TTS returned empty response")
- message = completion.choices[0].message
- audio = getattr(message, "audio", None)
- audio_data = None
- if isinstance(audio, dict):
- audio_data = audio.get("data")
- elif audio is not None:
- audio_data = getattr(audio, "data", None)
+ message = completion.choices[0].message
+ audio = getattr(message, "audio", None)
+ audio_data = None
+ if isinstance(audio, dict):
+ audio_data = audio.get("data")
+ elif audio is not None:
+ audio_data = getattr(audio, "data", None)
- if not audio_data:
- raise ValueError("MiMo TTS returned empty audio data")
+ if not audio_data:
+ raise ValueError("MiMo TTS returned empty audio data")
- audio_bytes = base64.b64decode(audio_data)
- audio_segment = AudioSegment.from_file(io.BytesIO(audio_bytes), format="wav")
+ audio_bytes = base64.b64decode(audio_data)
+ audio_segment = AudioSegment.from_file(io.BytesIO(audio_bytes), format="wav")
- output_format = utils.parse_extension(voice_file) or "mp3"
- if output_format == "wav":
- with open(voice_file, "wb") as f:
- f.write(audio_bytes)
- else:
- audio_segment.export(voice_file, format=output_format)
+ output_format = utils.parse_extension(voice_file) or "mp3"
+ if output_format == "wav":
+ with open(voice_file, "wb") as f:
+ f.write(audio_bytes)
+ else:
+ audio_segment.export(voice_file, format=output_format)
- audio_duration = len(audio_segment) / 1000.0
- sub_maker = ensure_legacy_submaker_fields(SubMaker())
- logger.success(f"mimo tts succeeded: {voice_file}")
- logger.debug(
- "mimo subtitle timeline generated, "
- f"duration: {audio_duration:.3f}s, output_format: {output_format}"
- )
- return populate_legacy_submaker_with_full_text(
- sub_maker=sub_maker,
- text=text,
- audio_duration_seconds=audio_duration,
- )
- except Exception as e:
- logger.error(f"mimo tts failed: {str(e)}")
+ audio_duration = len(audio_segment) / 1000.0
+ sub_maker = ensure_legacy_submaker_fields(SubMaker())
+ logger.success(f"mimo tts succeeded: {voice_file}")
+ logger.debug(
+ "mimo subtitle timeline generated, "
+ f"duration: {audio_duration:.3f}s, output_format: {output_format}"
+ )
+ return populate_legacy_submaker_with_full_text(
+ sub_maker=sub_maker,
+ text=text,
+ audio_duration_seconds=audio_duration,
+ )
+ except Exception as e:
+ logger.error(f"mimo tts failed: {str(e)}")
return None
diff --git a/test/services/test_mimo_paid_retries.py b/test/services/test_mimo_paid_retries.py
new file mode 100644
index 00000000..42a84c93
--- /dev/null
+++ b/test/services/test_mimo_paid_retries.py
@@ -0,0 +1,45 @@
+import tempfile
+from pathlib import Path
+from unittest.mock import patch
+
+import httpx
+from openai import OpenAI
+
+from app.services import voice
+
+
+def test_mimo_lost_paid_response_is_not_replayed_and_client_closes():
+ submitted = []
+ clients = []
+ options = []
+
+ def response_lost(request):
+ submitted.append(request)
+ raise httpx.ReadTimeout("response lost after submission", request=request)
+
+ def client_factory(**kwargs):
+ options.append(kwargs)
+ client = OpenAI(
+ **kwargs,
+ http_client=httpx.Client(
+ transport=httpx.MockTransport(response_lost), trust_env=False
+ ),
+ )
+ clients.append(client)
+ return client
+
+ with (
+ tempfile.TemporaryDirectory() as temp_dir,
+ patch.object(voice.config, "app", {"mimo_api_key": "fixture-key"}),
+ patch.object(voice, "OpenAI", side_effect=client_factory),
+ patch("openai._base_client.time.sleep"),
+ ):
+ result = voice.mimo_tts("Hello", "fixture-voice", 1.0, str(Path(temp_dir) / "voice.mp3"))
+ assert result is None
+ assert list(Path(temp_dir).iterdir()) == []
+
+ assert len(submitted) == 1
+ assert len(clients) == 1
+ assert clients[0].is_closed()
+ assert options[0]["max_retries"] == 0
+ assert options[0]["timeout"] > 0
diff --git a/test/services/test_voice.py b/test/services/test_voice.py
index 5508472c..b4ad1a8c 100644
--- a/test/services/test_voice.py
+++ b/test/services/test_voice.py
@@ -637,9 +637,16 @@ class TestVoiceService(unittest.TestCase):
Path(output_file).write_bytes(b"fake-mp3")
fake_completions = _FakeCompletions()
- fake_client = SimpleNamespace(
- chat=SimpleNamespace(completions=fake_completions)
- )
+ class _FakeClient:
+ chat = SimpleNamespace(completions=fake_completions)
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *_args):
+ self.closed = True
+
+ fake_client = _FakeClient()
with tempfile.TemporaryDirectory() as tmp_dir, patch.object(
vs,
@@ -672,7 +679,10 @@ class TestVoiceService(unittest.TestCase):
openai_client.assert_called_once_with(
api_key="mimo-key",
base_url="https://api.xiaomimimo.com/v1",
+ max_retries=0,
+ timeout=120.0,
)
+ self.assertTrue(fake_client.closed)
self.assertEqual(fake_completions.kwargs["model"], "mimo-v2.5-tts")
self.assertEqual(
fake_completions.kwargs["messages"],