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.
This commit is contained in:
juancarlos.cavero 2026-09-17 20:20:56 +02:00
parent 27d4916ca7
commit 86bbc8b1cb
3 changed files with 27 additions and 2 deletions

View file

@ -1073,10 +1073,17 @@ def transcribe_audio_for_subs(audio_path: str) -> list:
Transcribe audio with word-level timestamps via the configured ASR backend. Transcribe audio with word-level timestamps via the configured ASR backend.
Returns list of {"word": str, "start": float, "end": float}. 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...") 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 = [ words = [
{"word": w["word"].strip(), "start": w["start"], "end": w["end"]} {"word": w["word"].strip(), "start": w["start"], "end": w["end"]}

View file

@ -282,3 +282,16 @@ def test_whisper_model_is_fetched_inside_the_gate(monkeypatch):
tb._run_whisper_once("x.mp4") tb._run_whisper_once("x.mp4")
assert order == ["gate", "model", "transcribe"] 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

View file

@ -244,6 +244,11 @@ def release_models():
except Exception as e: except Exception as e:
print(f"⚠️ [ASR] whisper unload failed ({type(e).__name__}: {e})") print(f"⚠️ [ASR] whisper unload failed ({type(e).__name__}: {e})")
del whisper, parakeet 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 import gc
gc.collect() gc.collect()
try: try: