Skip to content

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

fix(subtitle): initialize Whisper once across concurrent jobs

Rudy Celeklid7fcecf34a6f
diff --git a/app/services/subtitle.py b/app/services/subtitle.py
index ef62694e..3bde812d 100644
--- a/app/services/subtitle.py
+++ b/app/services/subtitle.py
@@ -2,6 +2,7 @@ import json
import os.path
import re
import tempfile
+import threading
from timeit import default_timer as timer
try:
@@ -18,19 +19,18 @@ device = config.whisper.get("device", "cpu")
compute_type = config.whisper.get("compute_type", "int8")
initial_prompt = config.whisper.get("initial_prompt", "") or None
model = None
+_model_init_lock = threading.Lock()
-def create(
- audio_file,
- subtitle_file: str = "",
- word_level: bool = False,
- log_details: bool = True,
-):
+def _ensure_model_loaded() -> bool:
+ """Load the large Whisper model once, even when jobs start concurrently."""
global model
- if WhisperModel is None:
- logger.warning("faster_whisper not available, skipping whisper subtitle generation")
- return ""
- if not model:
+ if model is not None:
+ return True
+
+ with _model_init_lock:
+ if model is not None:
+ return True
model_path = f"{utils.root_dir()}/models/whisper-{model_size}"
model_bin_file = f"{model_path}/model.bin"
if not os.path.isdir(model_path) or not os.path.isfile(model_bin_file):
@@ -52,7 +52,21 @@ def create(
f"see [README.md FAQ](https://github.com/harry0703/MoneyPrinterTurbo) for more details.\n"
f"********************************************\n\n"
)
- return None
+ return False
+ return True
+
+
+def create(
+ audio_file,
+ subtitle_file: str = "",
+ word_level: bool = False,
+ log_details: bool = True,
+):
+ if WhisperModel is None:
+ logger.warning("faster_whisper not available, skipping whisper subtitle generation")
+ return ""
+ if not _ensure_model_loaded():
+ return None
if log_details:
logger.info(f"start, output file: {subtitle_file}")
diff --git a/test/services/test_subtitle.py b/test/services/test_subtitle.py
index 1fdd129a..3aeda5a6 100644
--- a/test/services/test_subtitle.py
+++ b/test/services/test_subtitle.py
@@ -1,6 +1,8 @@
import sys
import tempfile
+import threading
import unittest
+from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
@@ -12,6 +14,48 @@ from app.services import subtitle
class TestSubtitleService(unittest.TestCase):
+ def test_concurrent_subtitles_initialize_whisper_once(self):
+ """Concurrent jobs must not load the large Whisper model twice."""
+ first_constructor_started = threading.Event()
+ release_constructor = threading.Event()
+ second_constructor_started = threading.Event()
+ constructor_calls = []
+
+ class FakeWhisperModel:
+ def __init__(self, **_kwargs):
+ constructor_calls.append(1)
+ if len(constructor_calls) == 1:
+ first_constructor_started.set()
+ else:
+ second_constructor_started.set()
+ release_constructor.wait(timeout=2)
+
+ def transcribe(self, _audio_file, **_kwargs):
+ return [], SimpleNamespace(language="en", language_probability=1.0)
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ with (
+ patch.object(subtitle, "model", None),
+ patch.object(subtitle, "WhisperModel", FakeWhisperModel),
+ ):
+ with ThreadPoolExecutor(max_workers=2) as executor:
+ first = executor.submit(
+ subtitle.create, "audio-1.mp3", str(Path(temp_dir) / "1.srt")
+ )
+ self.assertTrue(first_constructor_started.wait(timeout=1))
+ second = executor.submit(
+ subtitle.create, "audio-2.mp3", str(Path(temp_dir) / "2.srt")
+ )
+ try:
+ duplicate_load = second_constructor_started.wait(timeout=0.2)
+ finally:
+ release_constructor.set()
+ first.result(timeout=2)
+ second.result(timeout=2)
+
+ self.assertFalse(duplicate_load, "Whisper was loaded twice")
+ self.assertEqual(len(constructor_calls), 1)
+
def test_file_to_subtitles_returns_empty_for_missing_input(self):
"""空路径和不存在的文件都应安全返回空列表。"""
self.assertEqual(subtitle.file_to_subtitles(""), [])