From 6f84597da33557692dbf7d20a62e91142e6717aa Mon Sep 17 00:00:00 2001
From: Subliminal Guy <subliminal_kid@posteo.de>
Date: Mon, 21 Apr 2025 16:35:07 +0200
Subject: [PATCH] Make Transcription async so that Status Route stays accesible

---
 app/webservice.py | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/app/webservice.py b/app/webservice.py
index 38431fa..d0c39b9 100644
--- a/app/webservice.py
+++ b/app/webservice.py
@@ -11,6 +11,7 @@ from fastapi.openapi.docs import get_swagger_ui_html
 from fastapi.responses import RedirectResponse, StreamingResponse
 from fastapi.staticfiles import StaticFiles
 from whisper import tokenizer
+import asyncio
 
 from app.config import CONFIG
 from app.factory.asr_model_factory import ASRModelFactory
@@ -88,16 +89,22 @@ async def asr(
     ),
     output: Union[str, None] = Query(default="txt", enum=["txt", "vtt", "srt", "tsv", "json"]),
 ):
-    result = asr_model.transcribe(
-        load_audio(audio_file.file, encode),
-        task,
-        language,
-        initial_prompt,
-        vad_filter,
-        word_timestamps,
-        {"diarize": diarize, "min_speakers": min_speakers, "max_speakers": max_speakers},
-        output,
-    )
+    # Run transcription in a background thread to keep the event loop responsive
+    def _run_transcription():
+        audio = load_audio(audio_file.file, encode)
+        return asr_model.transcribe(
+            audio,
+            task,
+            language,
+            initial_prompt,
+            vad_filter,
+            word_timestamps,
+            {"diarize": diarize, "min_speakers": min_speakers, "max_speakers": max_speakers},
+            output,
+        )
+    # offload blocking transcription to a thread
+    result = await asyncio.to_thread(_run_transcription)
+    # stream the transcription result back to the client
     return StreamingResponse(
         result,
         media_type="text/plain",
-- 
GitLab