-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.py
50 lines (37 loc) · 1.4 KB
/
routes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
import uuid
import json
from starlette.responses import JSONResponse
from routes_definition.transcribe.transcribe import transcribe_helper
from base import model_small, model_base_en, model_tiny
from utils.utils import adownload_audio_file
from base import ROOT_DIR
async def ping(request):
return JSONResponse({"status": 200, "message": "success", "data": {}})
async def health_check(request):
return JSONResponse({"status": 200, "message": "success", "data": {}})
async def transcribe(request):
form = await request.form()
audio = form.get("audio")
model_name = form.get("model", "tiny")
language = form.get("language", "en")
if audio is None:
return JSONResponse(
status_code=400,
content={"status": 400, "message": "Bad Request", "data": {}},
)
audio_directory = f"{str(ROOT_DIR)}/audios"
if not os.path.exists(audio_directory):
os.makedirs(audio_directory)
file_path = f"{audio_directory}/{str(uuid.uuid4())}.mp3"
with open(file_path, "wb") as f:
f.write(audio.file.read())
model = model_tiny
if model_name == "base.en":
model = model_base_en
elif model_name == "small":
model = model_small
res = transcribe_helper(model, audio_path=file_path, language=language)
return JSONResponse(
{"status": 200, "message": "success", "data": {"transcription": res}}
)