Skip to content
Snippets Groups Projects
Commit 6f84597d authored by Subliminal Guy's avatar Subliminal Guy
Browse files

Make Transcription async so that Status Route stays accesible

parent e38082e9
No related branches found
No related tags found
No related merge requests found
......@@ -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,8 +89,11 @@ 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),
# 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,
......@@ -98,6 +102,9 @@ async def asr(
{"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",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment