Skip to content

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

fix(tts): avoid duplicate paid requests (#1436)

Rudy Mizrahi Celekli713491bf6d60
diff --git a/app/services/voice.py b/app/services/voice.py
index 69e73a6c..82c15ed3 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -1497,6 +1497,16 @@ def siliconflow_tts(
logger.error(
f"siliconflow tts failed with status code {response.status_code}: {response.text}"
)
+ except requests.exceptions.ConnectTimeout as e:
+ logger.warning(f"siliconflow tts could not connect, retrying: {e}")
+ except requests.exceptions.RequestException as e:
+ # The server may have synthesized and charged for the POST even
+ # though its response was lost. A fresh POST could bill again.
+ logger.error(
+ "siliconflow tts result is unconfirmed after a transport error; "
+ f"stop paid retries: {type(e).__name__}"
+ )
+ return None
except Exception as e:
logger.error(f"siliconflow tts failed: {str(e)}")
@@ -2085,7 +2095,15 @@ def minimax_tts(text: str, voice_id: str, voice_rate: float, voice_file: str, vo
return populate_legacy_submaker_with_full_text(
ensure_legacy_submaker_fields(SubMaker()), text, audio_duration
)
- except (OSError, ValueError, requests.RequestException) as exc:
+ except requests.exceptions.ConnectTimeout as exc:
+ logger.warning(f"MiniMax TTS could not connect, retrying: {exc}")
+ except requests.exceptions.RequestException as exc:
+ logger.error(
+ "MiniMax TTS result is unconfirmed after a transport error; "
+ f"stop paid retries: {type(exc).__name__}"
+ )
+ return None
+ except (OSError, ValueError) as exc:
logger.error(f"MiniMax TTS failed: {str(exc)}")
return None
@@ -2175,6 +2193,14 @@ def elevenlabs_tts(
text=text,
audio_duration_seconds=audio_duration,
)
+ except requests.exceptions.ConnectTimeout as e:
+ logger.warning(f"elevenlabs tts could not connect, retrying: {e}")
+ except requests.exceptions.RequestException as e:
+ logger.error(
+ "elevenlabs tts result is unconfirmed after a transport error; "
+ f"stop paid retries: {type(e).__name__}"
+ )
+ return None
except Exception as e:
logger.error(f"elevenlabs tts failed: {str(e)}")
@@ -2497,6 +2523,14 @@ def fish_audio_tts(
text=text,
audio_duration_seconds=audio_duration,
)
+ except requests.exceptions.ConnectTimeout as e:
+ logger.warning(f"fish audio tts could not connect, retrying: {e}")
+ except requests.exceptions.RequestException as e:
+ logger.error(
+ "fish audio tts result is unconfirmed after a transport error; "
+ f"stop paid retries: {type(e).__name__}"
+ )
+ return None
except Exception as e:
logger.error(f"fish audio tts failed: {str(e)}")
@@ -2780,10 +2814,19 @@ def voxcpm_tts(
text=text,
audio_duration_seconds=audio_duration,
)
- except requests.RequestException as exc:
- logger.error(f"VoxCPM TTS request failed: {exc}")
+ except requests.exceptions.ConnectTimeout as exc:
+ logger.error(f"VoxCPM TTS connection timed out: {exc}")
+ # A timeout before POST returns is safe to retry only when no
+ # response has been received. Never resubmit after SSE started.
+ if response is not None:
+ return None
if attempt < 2:
time.sleep(_VOXCPM_RETRY_DELAY_SECONDS[attempt])
+ except requests.RequestException as exc:
+ # The server may have generated speech before a read timeout or
+ # stream disconnect. Resubmitting can create duplicate work.
+ logger.error(f"VoxCPM TTS request outcome is unconfirmed: {exc}")
+ return None
except Exception as exc:
# Invalid SSE/WAV data and local conversion failures are deterministic;
# retrying the same response cannot repair them.
diff --git a/test/services/test_voice.py b/test/services/test_voice.py
index aa258e5c..d50cb9cc 100644
--- a/test/services/test_voice.py
+++ b/test/services/test_voice.py
@@ -1584,12 +1584,12 @@ class TestElevenLabsVoice(unittest.TestCase):
)
def test_siliconflow_tts_bounds_each_network_attempt(self):
- """A stalled speech endpoint must not block the task indefinitely."""
+ """A pre-connect timeout can be retried without submitting paid work."""
timeouts = []
def stalled_post(_url, **kwargs):
timeouts.append(kwargs.get("timeout"))
- raise vs.requests.exceptions.ReadTimeout("server stalled")
+ raise vs.requests.exceptions.ConnectTimeout("could not connect")
with (
tempfile.TemporaryDirectory() as temp_dir,
@@ -1609,6 +1609,45 @@ class TestElevenLabsVoice(unittest.TestCase):
self.assertIsNone(result)
self.assertEqual(timeouts, [(10, 300)] * 3)
+ def test_paid_tts_does_not_repeat_ambiguous_read_timeout(self):
+ """A lost POST response may still have created billable speech remotely."""
+ settings = {
+ "api_key": "test-key",
+ "model_id": vs.MINIMAX_TTS_DEFAULT_MODEL,
+ "audio_format": "mp3",
+ }
+ cases = (
+ ("elevenlabs", lambda path: vs.elevenlabs_tts("Hello", "voice", path)),
+ ("fish", lambda path: vs.fish_audio_tts("Hello", path)),
+ (
+ "minimax",
+ lambda path: vs.minimax_tts("Hello", "voice", 1.0, path),
+ ),
+ (
+ "siliconflow",
+ lambda path: vs.siliconflow_tts(
+ "Hello", "model", "voice", 1.0, path
+ ),
+ ),
+ )
+ with (
+ tempfile.TemporaryDirectory() as tmp_dir,
+ patch.object(vs.requests, "post", side_effect=vs.requests.exceptions.ReadTimeout("response lost")) as post,
+ patch.object(vs, "get_elevenlabs_api_key", return_value="test-key"),
+ patch.object(vs, "get_fish_audio_api_key", return_value="test-key"),
+ patch.object(vs, "get_minimax_tts_api_key", return_value="test-key"),
+ patch.object(vs, "get_minimax_tts_endpoint", return_value="https://example.test/v1/t2a_v2"),
+ patch.object(vs.config, "fish_audio", {"model": vs.FISH_AUDIO_DEFAULT_MODEL}),
+ patch.object(vs.config, "minimax_tts", settings),
+ patch.object(vs.config, "siliconflow", {"api_key": "test-key"}),
+ ):
+ for provider, synthesize in cases:
+ with self.subTest(provider=provider):
+ post.reset_mock()
+ result = synthesize(str(Path(tmp_dir) / f"{provider}.mp3"))
+ self.assertIsNone(result)
+ post.assert_called_once()
+
def test_siliconflow_tts_rejects_invalid_success_audio(self):
"""HTTP 200 with corrupt audio must not become a fake 10-second success."""
fake_response = SimpleNamespace(status_code=200, content=b"invalid mp3")
diff --git a/test/services/test_voxcpm.py b/test/services/test_voxcpm.py
index 0c1eec2f..dc3c63d7 100644
--- a/test/services/test_voxcpm.py
+++ b/test/services/test_voxcpm.py
@@ -10,6 +10,7 @@ from types import SimpleNamespace
from unittest.mock import Mock
import pytest
+import requests
from app.config import config
from app.services import voice
@@ -307,6 +308,58 @@ def test_voxcpm_http_error_is_retried_and_preserves_output(
assert sleep.call_args_list == [((1.0,),), ((2.0,),)]
[email protected]("failure_stage", ["post", "stream"])
+def test_voxcpm_does_not_repeat_speech_after_ambiguous_transport_failure(
+ monkeypatch, tmp_path, voxcpm_config, failure_stage
+):
+ """A dropped POST response or SSE stream can follow a completed generation."""
+
+ def interrupted_stream(**_kwargs):
+ yield _sse_event(
+ "speech.audio.delta", audio=base64.b64encode(b"partial").decode()
+ )
+ yield ""
+ raise requests.ReadTimeout("stream dropped")
+
+ response = SimpleNamespace(
+ status_code=200,
+ text="",
+ iter_lines=interrupted_stream,
+ close=Mock(),
+ )
+ post = Mock(
+ side_effect=requests.ReadTimeout("response lost")
+ if failure_stage == "post"
+ else None,
+ return_value=response,
+ )
+ monkeypatch.setattr(voice.requests, "post", post)
+ sleep = Mock()
+ monkeypatch.setattr(voice.time, "sleep", sleep)
+ output = tmp_path / "existing.mp3"
+ output.write_bytes(b"previous-audio")
+
+ assert voice.voxcpm_tts("Hello", "default", str(output)) is None
+ assert output.read_bytes() == b"previous-audio"
+ post.assert_called_once()
+ sleep.assert_not_called()
+ if failure_stage == "stream":
+ response.close.assert_called_once()
+
+
+def test_voxcpm_retries_before_connection_is_established(
+ monkeypatch, tmp_path, voxcpm_config
+):
+ post = Mock(side_effect=requests.ConnectTimeout("could not connect"))
+ monkeypatch.setattr(voice.requests, "post", post)
+ sleep = Mock()
+ monkeypatch.setattr(voice.time, "sleep", sleep)
+
+ assert voice.voxcpm_tts("Hello", "default", str(tmp_path / "voice.mp3")) is None
+ assert post.call_count == 3
+ assert sleep.call_args_list == [((1.0,),), ((2.0,),)]
+
+
@pytest.mark.parametrize("status_code", [400, 401, 403, 404, 422])
def test_voxcpm_auth_and_invalid_parameter_errors_fail_without_retry(
monkeypatch, tmp_path, voxcpm_config, status_code