利用 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(tts): validate SiliconFlow responses (#1418)
Rudy Mizrahi Celeklia43b16ba79c5
diff --git a/app/services/voice.py b/app/services/voice.pyindex 7e14895a..f9db0e0c 100644--- a/app/services/voice.py+++ b/app/services/voice.py@@ -32,6 +32,7 @@ from app.config import configfrom app.utils import utils_DEFAULT_EDGE_TTS_TIMEOUT_SECONDS = 30.0+_SILICONFLOW_TTS_TIMEOUT_SECONDS = (10, 300) # connect, read_MIMO_DEFAULT_BASE_URL = "https://api.xiaomimimo.com/v1"_MIMO_DEFAULT_TTS_MODEL = "mimo-v2.5-tts"MINIMAX_TTS_GLOBAL_URL = "https://api.minimax.io/v1/t2a_v2"@@ -1428,9 +1429,17 @@ def siliconflow_tts(f"start siliconflow tts, model: {model}, voice: {voice}, try: {i + 1}")- response = requests.post(url, json=payload, headers=headers)+ response = requests.post(+ url,+ json=payload,+ headers=headers,+ timeout=_SILICONFLOW_TTS_TIMEOUT_SECONDS,+ )if response.status_code == 200:+ if not response.content:+ logger.error("siliconflow tts returned empty audio")+ return None# 保存音频文件with open(voice_file, "wb") as f:f.write(response.content)@@ -1443,9 +1452,24 @@ def siliconflow_tts(audio_duration = audio_clip.durationfinally:audio_clip.close()+ if (+ not isinstance(audio_duration, (int, float))+ or not math.isfinite(audio_duration)+ or audio_duration <= 0+ ):+ raise ValueError("audio duration must be positive and finite")except Exception as e:- logger.warning(f"Failed to read audio duration: {str(e)}")- audio_duration = 10.0+ # A 200 response is not proof that the bytes contain usable+ # narration. Returning a fabricated duration lets an invalid+ # file advance into the costly video pipeline.+ logger.error(f"siliconflow tts returned invalid audio: {e}")+ try:+ os.remove(voice_file)+ except OSError as cleanup_error:+ logger.warning(+ f"failed to remove invalid siliconflow audio: {cleanup_error}"+ )+ return Nonelogger.success(f"siliconflow tts succeeded: {voice_file}")return populate_legacy_submaker_with_full_text(diff --git a/test/services/test_voice.py b/test/services/test_voice.pyindex 631dc18c..2b457096 100644--- a/test/services/test_voice.py+++ b/test/services/test_voice.py@@ -1544,6 +1544,54 @@ class TestElevenLabsVoice(unittest.TestCase):f"({expected_end_100ns} units = {audio_duration_seconds}s)",)+ def test_siliconflow_tts_bounds_each_network_attempt(self):+ """A stalled speech endpoint must not block the task indefinitely."""+ timeouts = []++ def stalled_post(_url, **kwargs):+ timeouts.append(kwargs.get("timeout"))+ raise vs.requests.exceptions.ReadTimeout("server stalled")++ with (+ tempfile.TemporaryDirectory() as temp_dir,+ patch.object(vs.requests, "post", side_effect=stalled_post),+ patch.object(vs.config, "siliconflow", {"api_key": "test-key"}),+ ):+ voice_file = str(Path(temp_dir) / "narration.mp3")+ result = vs.siliconflow_tts(+ text="An example narration",+ model="FunAudioLLM/CosyVoice2-0.5B",+ voice="FunAudioLLM/CosyVoice2-0.5B:alex",+ voice_rate=1.0,+ voice_file=voice_file,+ )+ self.assertFalse(Path(voice_file).exists())++ self.assertIsNone(result)+ self.assertEqual(timeouts, [(10, 300)] * 3)++ 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")+ with (+ tempfile.TemporaryDirectory() as temp_dir,+ patch.object(vs.requests, "post", return_value=fake_response) as post,+ patch.object(vs, "AudioFileClip", side_effect=OSError("invalid audio")),+ patch.object(vs.config, "siliconflow", {"api_key": "test-key"}),+ ):+ voice_file = str(Path(temp_dir) / "narration.mp3")+ result = vs.siliconflow_tts(+ text="An example narration",+ model="FunAudioLLM/CosyVoice2-0.5B",+ voice="FunAudioLLM/CosyVoice2-0.5B:alex",+ voice_rate=1.0,+ voice_file=voice_file,+ )+ self.assertFalse(Path(voice_file).exists())++ self.assertIsNone(result)+ post.assert_called_once()+def test_pause_tag_detection_and_parsing(self):"""测试多语言停顿标签的检测、解析与清洗。"""sample_script = (