"vscode:/vscode.git/clone" did not exist on "9ca5a6fcd675c60c9edf651e9e48b9b2799dde9c"
main.py 1.99 KB
Newer Older
1
import os
Timothy J. Baek's avatar
Timothy J. Baek committed
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
51
52
53
54
55
56
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

from config import CACHE_DIR, UPLOAD_DIR, WHISPER_MODEL_NAME

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),
):
    print(file.content_type)

    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()

57
58
59
        model_name = os.getenv('WHISPER_MODEL', WHISPER_MODEL_NAME)
        download_root = os.getenv('WHISPER_DIR', f"{CACHE_DIR}/whisper/models")

Timothy J. Baek's avatar
Timothy J. Baek committed
60
61
62
63
        model = WhisperModel(
            model_name,
            device="cpu",
            compute_type="int8",
64
            download_root=download_root,
Timothy J. Baek's avatar
Timothy J. Baek committed
65
66
67
68
69
70
71
72
73
74
        )

        segments, info = model.transcribe(file_path, beam_size=5)
        print(
            "Detected language '%s' with probability %f"
            % (info.language, info.language_probability)
        )

        transcript = "".join([segment.text for segment in list(segments)])

75
        return {"text": transcript.strip()}
Timothy J. Baek's avatar
Timothy J. Baek committed
76
77
78
79
80
81
82
83

    except Exception as e:
        print(e)

        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=ERROR_MESSAGES.DEFAULT(e),
        )