diff --git a/saasshorts.py b/saasshorts.py index d947424..ff69863 100644 --- a/saasshorts.py +++ b/saasshorts.py @@ -1073,10 +1073,17 @@ def transcribe_audio_for_subs(audio_path: str) -> list: Transcribe audio with word-level timestamps via the configured ASR backend. Returns list of {"word": str, "start": float, "end": float}. """ - from transcribe_backends import transcribe_media + from transcribe_backends import transcribe_media, release_models print(f"[SaaSShorts] 🎙️ Transcribing audio for subtitles...") - transcript = transcribe_media(audio_path) + try: + transcript = transcribe_media(audio_path) + finally: + # This runs inside the API process, like the thumbnail studio and + # the dubbed-subtitle path: the models must not stay resident + # (transcribe_backends.release_models). Measured 17-sep-2026, 30 min + # after a deploy: one SaaSShorts job left uvicorn holding 5.1 GB. + release_models() words = [ {"word": w["word"].strip(), "start": w["start"], "end": w["end"]} diff --git a/tests/test_transcribe_backends.py b/tests/test_transcribe_backends.py index e882390..1ddea7a 100644 --- a/tests/test_transcribe_backends.py +++ b/tests/test_transcribe_backends.py @@ -282,3 +282,16 @@ def test_whisper_model_is_fetched_inside_the_gate(monkeypatch): tb._run_whisper_once("x.mp4") assert order == ["gate", "model", "transcribe"] + + +def test_release_models_drops_the_transnetv2_singleton_too(monkeypatch): + import types + fake = types.ModuleType("scene_detection") + fake._tn2_model = object() + monkeypatch.setitem(sys.modules, "scene_detection", fake) + monkeypatch.setattr(tb, "_whisper_model", None) + monkeypatch.setattr(tb, "_parakeet_model", None) + + tb.release_models() + + assert fake._tn2_model is None diff --git a/transcribe_backends.py b/transcribe_backends.py index 6dd0f85..87d940d 100644 --- a/transcribe_backends.py +++ b/transcribe_backends.py @@ -244,6 +244,11 @@ def release_models(): except Exception as e: print(f"⚠️ [ASR] whisper unload failed ({type(e).__name__}: {e})") del whisper, parakeet + # TransNetV2 is the other torch tenant an in-process pipeline leaves + # behind (scene_detection keeps it as a module singleton too). + tn2 = sys.modules.get("scene_detection") + if tn2 is not None: + tn2._tn2_model = None import gc gc.collect() try: