2024-02-13 14:11:53 +00:00
|
|
|
import os
|
2024-03-20 23:11:36 +00:00
|
|
|
import logging
|
2024-02-11 08:17:50 +00:00
|
|
|
from fastapi import (
|
|
|
|
FastAPI,
|
|
|
|
Request,
|
|
|
|
Depends,
|
|
|
|
HTTPException,
|
|
|
|
status,
|
|
|
|
UploadFile,
|
|
|
|
File,
|
|
|
|
Form,
|
|
|
|
)
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from faster_whisper import WhisperModel
|
|
|
|
|
|
|
|
from constants import ERROR_MESSAGES
|
|
|
|
from utils.utils import (
|
|
|
|
decode_token,
|
|
|
|
get_current_user,
|
|
|
|
get_verified_user,
|
|
|
|
get_admin_user,
|
|
|
|
)
|
|
|
|
from utils.misc import calculate_sha256
|
|
|
|
|
2024-03-20 23:11:36 +00:00
|
|
|
from config import SRC_LOG_LEVELS, CACHE_DIR, UPLOAD_DIR, WHISPER_MODEL, WHISPER_MODEL_DIR
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log.setLevel(SRC_LOG_LEVELS["AUDIO"])
|
2024-02-11 08:17:50 +00:00
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origins=["*"],
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/transcribe")
|
|
|
|
def transcribe(
|
|
|
|
file: UploadFile = File(...),
|
|
|
|
user=Depends(get_current_user),
|
|
|
|
):
|
2024-03-20 23:11:36 +00:00
|
|
|
log.info(f"file.content_type: {file.content_type}")
|
2024-02-11 08:17:50 +00:00
|
|
|
|
|
|
|
if file.content_type not in ["audio/mpeg", "audio/wav"]:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
|
|
detail=ERROR_MESSAGES.FILE_NOT_SUPPORTED,
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
filename = file.filename
|
|
|
|
file_path = f"{UPLOAD_DIR}/{filename}"
|
|
|
|
contents = file.file.read()
|
|
|
|
with open(file_path, "wb") as f:
|
|
|
|
f.write(contents)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
model = WhisperModel(
|
2024-02-15 07:32:54 +00:00
|
|
|
WHISPER_MODEL,
|
2024-02-19 06:51:17 +00:00
|
|
|
device="auto",
|
2024-02-11 08:17:50 +00:00
|
|
|
compute_type="int8",
|
2024-02-15 07:32:54 +00:00
|
|
|
download_root=WHISPER_MODEL_DIR,
|
2024-02-11 08:17:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
segments, info = model.transcribe(file_path, beam_size=5)
|
2024-03-20 23:11:36 +00:00
|
|
|
log.info(
|
2024-02-11 08:17:50 +00:00
|
|
|
"Detected language '%s' with probability %f"
|
|
|
|
% (info.language, info.language_probability)
|
|
|
|
)
|
|
|
|
|
|
|
|
transcript = "".join([segment.text for segment in list(segments)])
|
|
|
|
|
2024-02-11 10:17:24 +00:00
|
|
|
return {"text": transcript.strip()}
|
2024-02-11 08:17:50 +00:00
|
|
|
|
|
|
|
except Exception as e:
|
2024-03-20 23:11:36 +00:00
|
|
|
log.exception(e)
|
2024-02-11 08:17:50 +00:00
|
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
|
|
detail=ERROR_MESSAGES.DEFAULT(e),
|
|
|
|
)
|