利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
fix(voice): stop redirected speech requests without replaying synthesis
Rudy Celeklif530755cc3bc
diff --git a/app/services/voice.py b/app/services/voice.pyindex ef008d7c..d8433f82 100644--- a/app/services/voice.py+++ b/app/services/voice.py@@ -1608,9 +1608,15 @@ def siliconflow_tts(url,json=payload,headers=headers,+ allow_redirects=False,timeout=_SILICONFLOW_TTS_TIMEOUT_SECONDS,)+ if 300 <= response.status_code < 400:+ # Redirecting a speech POST may replay a billed synthesis or+ # forward its input/credentials. Do not follow or resubmit it.+ logger.error("TTS endpoint returned a redirect; stop speech retries")+ return Noneif response.status_code == 200:response_accepted = Trueif not response.content:@@ -2282,7 +2288,12 @@ def minimax_tts(text: str, voice_id: str, voice_rate: float, voice_file: str, voreceived_success = Falsetry:logger.info(f"start MiniMax TTS, model: {model}, voice: {voice_id}, try: {attempt + 1}")- response = requests.post(url, json=payload, headers=headers, timeout=120)+ response = requests.post(url, json=payload, headers=headers, allow_redirects=False, timeout=120)+ if 300 <= response.status_code < 400:+ # Redirecting a speech POST may replay a billed synthesis or+ # forward its input/credentials. Do not follow or resubmit it.+ logger.error("TTS endpoint returned a redirect; stop speech retries")+ return Noneif response.status_code != 200:logger.error(f"MiniMax TTS failed with status {response.status_code}: {response.text[:200]}")continue@@ -2524,7 +2535,12 @@ def _openai_compatible_tts(logger.info(f"start {provider} tts, voice: {voice}, try: {i + 1}")ensure_file_path_exists(voice_file)- response = requests.post(url, json=payload, headers=headers, timeout=120)+ response = requests.post(url, json=payload, headers=headers, allow_redirects=False, timeout=120)+ if 300 <= response.status_code < 400:+ # Redirecting a speech POST may replay a billed synthesis or+ # forward its input/credentials. Do not follow or resubmit it.+ logger.error("TTS endpoint returned a redirect; stop speech retries")+ return Noneif response.status_code != 200:logger.error(f"{provider} tts failed with status {response.status_code}: {response.text[:200]}"@@ -2759,7 +2775,12 @@ def fish_audio_tts()ensure_file_path_exists(voice_file)- response = requests.post(url, json=payload, headers=headers, timeout=60)+ response = requests.post(url, json=payload, headers=headers, allow_redirects=False, timeout=60)+ if 300 <= response.status_code < 400:+ # Redirecting a speech POST may replay a billed synthesis or+ # forward its input/credentials. Do not follow or resubmit it.+ logger.error("TTS endpoint returned a redirect; stop speech retries")+ return Noneif response.status_code == 401:logger.error("Fish Audio TTS failed: Invalid API key (401). "@@ -3059,9 +3080,15 @@ def voxcpm_tts(url,json=payload,headers=headers,+ allow_redirects=False,stream=True,timeout=(10, 120),)+ if 300 <= response.status_code < 400:+ # Redirecting a speech POST may replay a billed synthesis or+ # forward its input/credentials. Do not follow or resubmit it.+ logger.error("TTS endpoint returned a redirect; stop speech retries")+ return Noneif response.status_code != 200:logger.error(f"VoxCPM TTS failed with status {response.status_code}: "diff --git a/test/services/test_fish_audio.py b/test/services/test_fish_audio.pyindex 414489fc..aa16a018 100644--- a/test/services/test_fish_audio.py+++ b/test/services/test_fish_audio.py@@ -146,7 +146,8 @@ class TestFishAudioTTSRequest(unittest.TestCase):"""Helper: call fish_audio_tts and capture the outgoing request."""captured = {}- def _fake_post(url, json=None, headers=None, timeout=None):+ def _fake_post(url, json=None, headers=None, timeout=None, allow_redirects=True):+ self.assertFalse(allow_redirects)captured["url"] = urlcaptured["json"] = jsoncaptured["headers"] = headersdiff --git a/test/services/test_speech_post_redirects.py b/test/services/test_speech_post_redirects.pynew file mode 100644index 00000000..cd34df1b--- /dev/null+++ b/test/services/test_speech_post_redirects.py@@ -0,0 +1,41 @@+from unittest.mock import Mock++import pytest+from app.config import config+from app.services import voice++[email protected]("status", [301, 302, 307, 308])[email protected]("provider", ["siliconflow", "minimax", "compatible", "fish", "voxcpm"])+def test_speech_redirect_is_terminal_and_preserves_existing_audio(monkeypatch, tmp_path, provider, status):+ response = Mock(status_code=status, text="redirect", content=b"not audio")+ response.headers = {"Location": "https://other-host.test/speech"}+ post = Mock(return_value=response)+ decode = Mock(side_effect=AssertionError("redirect response must not be decoded"))+ monkeypatch.setattr(voice.requests, "post", post)+ monkeypatch.setattr(voice, "AudioFileClip", decode)+ monkeypatch.setattr(config, "siliconflow", {"api_key": "test-key"})+ monkeypatch.setattr(config, "minimax_tts", {"api_key": "test-key"})+ monkeypatch.setattr(voice, "get_minimax_tts_api_key", lambda: "test-key")+ monkeypatch.setattr(voice, "get_minimax_tts_endpoint", lambda: "https://api.test/t2a")+ monkeypatch.setattr(voice, "get_fish_audio_api_key", lambda: "test-key")+ monkeypatch.setattr(config, "voxcpm", {"api_key": "test-key", "model_id": "test-model"})+ monkeypatch.setattr(voice.time, "sleep", lambda _: None)+ output = tmp_path / "previous.mp3"+ output.write_bytes(b"last successful narration")+ calls = {+ "siliconflow": lambda: voice.siliconflow_tts("hello", "model", "voice", 1, str(output)),+ "minimax": lambda: voice.minimax_tts("hello", "voice", 1, str(output)),+ "compatible": lambda: voice._openai_compatible_tts("test", "https://api.test", "test-key", "model", "voice", "hello", 1, str(output)),+ "fish": lambda: voice.fish_audio_tts("hello", str(output)),+ "voxcpm": lambda: voice.voxcpm_tts("hello", "voice", str(output)),+ }+ assert calls[provider]() is None+ post.assert_called_once()+ assert post.call_args.kwargs["allow_redirects"] is False+ response.json.assert_not_called()+ decode.assert_not_called()+ assert output.read_bytes() == b"last successful narration"+ assert list(tmp_path.iterdir()) == [output]+ if provider == "voxcpm":+ response.close.assert_called_once()diff --git a/test/services/test_voice.py b/test/services/test_voice.pyindex b4ad1a8c..a84663a6 100644--- a/test/services/test_voice.py+++ b/test/services/test_voice.py@@ -716,7 +716,8 @@ class TestVoiceService(unittest.TestCase):captured = {}- def _post(url, json=None, headers=None, timeout=None):+ def _post(url, json=None, headers=None, timeout=None, allow_redirects=True):+ self.assertFalse(allow_redirects)captured.update(url=url, json=json, headers=headers, timeout=timeout)return _Response()@@ -759,7 +760,8 @@ class TestVoiceService(unittest.TestCase):captured = {}- def _post(url, json=None, headers=None, timeout=None):+ def _post(url, json=None, headers=None, timeout=None, allow_redirects=True):+ self.assertFalse(allow_redirects)captured.update(url=url, headers=headers)return _Response()@@ -942,7 +944,8 @@ class TestVoiceService(unittest.TestCase):captured = {}- def _fake_post(url, json=None, headers=None, timeout=None):+ def _fake_post(url, json=None, headers=None, timeout=None, allow_redirects=True):+ self.assertFalse(allow_redirects)captured["url"] = urlcaptured["json"] = jsoncaptured["headers"] = headers