File size: 1,355 Bytes
270736b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
from fastapi import APIRouter, Depends, HTTPException, status
from faster_whisper import WhisperModel

from libs.header_api_auth import get_api_key

router = APIRouter(prefix="/get-transcript", tags=["transcript"])

# model_size: distil-large-v2
# model_size: distil-large-v3

@router.get("/")
def get_transcript(audio_path: str, model_size: str = "distil-large-v3", api_key: str = Depends(get_api_key)):
     # Run on GPU with FP16
    # model = WhisperModel(model_size, device="cuda", compute_type="float16")

    # or run on GPU with INT8
    # model = WhisperModel(model_size, device="cuda", cosmpute_type="int8_float16")
    # or run on CPU with INT8
    # model_run = WhisperModel(model_size, device="cpu", compute_type="int8")


    print(f"model>>>: {model_size}")

    st = time.time()


    try:
        model_run = WhisperModel(model_size, device="cpu", compute_type="int8")
        segments, info = model_run.transcribe(
            audio_path,
            beam_size=16,
            language="en",
            condition_on_previous_text=False,
        )
    except Exception as error:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=f"error>>>: {error}")

    text = ""

    for segment in segments:
        text += segment.text

    et = time.time()
    elapsed_time = et - st
    return {"text": text}