From 86bbc8b1cb5f01c713fd2f79ba064eb9f49d749a Mon Sep 17 00:00:00 2001 From: "juancarlos.cavero" Date: Thu, 17 Sep 2026 20:20:56 +0200 Subject: [PATCH] fix(gpu): release the ASR models after a SaaSShorts transcription too 30 minutes after 60fa37a reached prod the uvicorn process held 5.1 GB of VRAM again, with the release counter at zero: saasshorts.generate_tiktok_subs transcribes in-process through its own call to transcribe_media, a third path next to the thumbnail studio and the dubbed-subtitle one. Same finally-release there, and release_models() now also drops the TransNetV2 singleton that the same in-process pipeline leaves in torch. --- saasshorts.py | 11 +++++++++-- tests/test_transcribe_backends.py | 13 +++++++++++++ transcribe_backends.py | 5 +++++ 3 files changed, 27 insertions(+), 2 deletions(-) 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: