fix: prevent STT from blocking the uvicorn event loop (#24338)
The transcription endpoint was async but called the synchronous transcribe() function directly, blocking the single-threaded uvicorn event loop for the entire duration of inference. This caused all HTTP and WebSocket connections to stall for every user on the instance during STT processing. - Add asyncio import - Use async UploadFile.read() instead of synchronous file.file.read() - Offload the blocking transcribe() call via asyncio.to_thread() Closes #24169
This commit is contained in:
1 parent
1c1c8b18e5
commit
7e275c1daa
1 file changed
+3
-2
@@ -1,3 +1,4 @@
|
|||||||
|
import asyncio
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
@@ -1258,7 +1259,7 @@ async def transcription(
|
|||||||
id = uuid.uuid4()
|
id = uuid.uuid4()
|
||||||
|
|
||||||
filename = f'{id}.{ext}'
|
filename = f'{id}.{ext}'
|
||||||
contents = file.file.read()
|
contents = await file.read()
|
||||||
|
|
||||||
file_dir = os.path.join(CACHE_DIR, 'audio', 'transcriptions')
|
file_dir = os.path.join(CACHE_DIR, 'audio', 'transcriptions')
|
||||||
os.makedirs(file_dir, exist_ok=True)
|
os.makedirs(file_dir, exist_ok=True)
|
||||||
@@ -1277,7 +1278,7 @@ async def transcription(
|
|||||||
if language:
|
if language:
|
||||||
metadata = {'language': language}
|
metadata = {'language': language}
|
||||||
|
|
||||||
result = transcribe(request, file_path, metadata, user)
|
result = await asyncio.to_thread(transcribe, request, file_path, metadata, user)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
**result,
|
**result,
|
||||||
|
|||||||
Reference in new issue
Block a user