File size: 5,085 Bytes
5aea4af
740950d
 
5aea4af
 
 
 
 
 
 
 
740950d
5aea4af
740950d
 
 
 
 
 
 
 
5aea4af
 
740950d
5aea4af
 
740950d
 
 
 
 
5aea4af
 
740950d
 
5aea4af
740950d
 
 
 
 
 
a8277c5
740950d
 
 
 
 
 
 
 
 
 
 
5aea4af
 
d7ee472
 
 
 
 
5aea4af
740950d
5aea4af
 
 
740950d
 
 
 
 
 
 
 
5aea4af
740950d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aea4af
740950d
5aea4af
740950d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aea4af
 
740950d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import shutil
import tempfile
from typing import Any, Dict, List, Optional

from fastapi import (  # type: ignore[import]
    FastAPI,
    File,
    UploadFile,
    HTTPException,
)
from fastapi.responses import JSONResponse  # type: ignore[import]

try:
    # Import lazily; model will be instantiated on first use
    from faster_whisper import WhisperModel  # type: ignore
except Exception as import_error:  # pragma: no cover
    WhisperModel = None  # type: ignore
    _import_error: Optional[Exception] = import_error
else:
    _import_error = None


app = FastAPI(title="faster-whisper REST API")


def _get_bool_env(name: str, default: bool = False) -> bool:
    val = os.getenv(name)
    if val is None:
        return default
    return val.strip().lower() in {"1", "true", "yes", "y", "on"}


class _ModelHolder:
    _model: Optional[WhisperModel] = None  # type: ignore

    @classmethod
    def get_model(cls) -> "WhisperModel":  # type: ignore
        if _import_error is not None:
            raise RuntimeError(
                f"Failed to import faster-whisper: {_import_error}"
            )

        if cls._model is None:
            model_size = os.getenv("WHISPER_MODEL", "small")
            device = os.getenv("WHISPER_DEVICE", "cpu")
            compute_type = os.getenv("WHISPER_COMPUTE_TYPE", "int8")
            # Construct model once and reuse across requests
            cls._model = WhisperModel(
                model_size,
                device=device,
                compute_type=compute_type,
            )  # type: ignore
        return cls._model


@app.get("/")
def root() -> Dict[str, Any]:
    return {"status": "ok"}


@app.get("/health")
def health() -> Dict[str, Any]:
    return {"status": "ok"}


@app.post("/inference")
def transcribe(
    file: UploadFile = File(...),
    language: Optional[str] = None,
    beam_size: Optional[int] = None,
    vad: Optional[bool] = None,
    word_timestamps: Optional[bool] = None,
) -> JSONResponse:
    try:
        model = _ModelHolder.get_model()
    except Exception as e:  # pragma: no cover
        raise HTTPException(status_code=500, detail=str(e))

    # Fallback to env-configurable defaults
    effective_beam_size = (
        int(os.getenv("WHISPER_BEAM_SIZE", "5"))
        if beam_size is None
        else beam_size
    )
    effective_vad = (
        _get_bool_env("WHISPER_VAD", False)
        if vad is None
        else vad
    )
    effective_word_ts = (
        _get_bool_env("WHISPER_WORD_TIMESTAMPS", False)
        if word_timestamps is None
        else word_timestamps
    )

    # Persist upload to a temporary file so ffmpeg/backends can access it
    try:
        with tempfile.NamedTemporaryFile(
            delete=False,
            suffix=os.path.splitext(file.filename or "audio")[1],
        ) as tmp:
            shutil.copyfileobj(file.file, tmp)
            tmp_path = tmp.name
    except Exception as e:
        raise HTTPException(
            status_code=400,
            detail=f"Failed to read uploaded file: {e}",
        )
    finally:
        try:
            file.file.close()
        except Exception:
            pass
    try:
        segments_iter, info = model.transcribe(
            tmp_path,
            beam_size=effective_beam_size,
            language=language,
            vad_filter=effective_vad,
            word_timestamps=effective_word_ts,
        )

        segments = list(segments_iter)

        # Build response
        all_text = "".join(seg.text for seg in segments)

        segments_json: List[Dict[str, Any]] = []
        for seg in segments:
            seg_obj: Dict[str, Any] = {
                "id": seg.id,
                "start": seg.start,
                "end": seg.end,
                "text": seg.text,
                "avg_log_prob": seg.avg_logprob,
                "compression_ratio": seg.compression_ratio,
                "no_speech_prob": seg.no_speech_prob,
            }
            if effective_word_ts and getattr(seg, "words", None):
                seg_obj["words"] = [
                    {
                        "start": w.start,
                        "end": w.end,
                        "word": w.word,
                        "probability": getattr(w, "probability", None),
                    }
                    for w in seg.words
                ]
            segments_json.append(seg_obj)

        result: Dict[str, Any] = {
            "text": all_text.strip(),
            "segments": segments_json,
            "info": {
                "language": getattr(info, "language", None),
                "language_probability": getattr(
                    info, "language_probability", None
                ),
                "duration": getattr(info, "duration", None),
            },
        }
        return JSONResponse(result)
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"Transcription error: {e}",
        )
    finally:
        try:
            os.unlink(tmp_path)
        except Exception:
            pass