-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
49 lines (38 loc) · 1.72 KB
/
Copy pathapp.py
File metadata and controls
49 lines (38 loc) · 1.72 KB
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
import io
import os
import time
from fastapi import FastAPI, File, UploadFile
from faster_whisper import WhisperModel
# The model is configurable via an environment variable so you can trade
# speed vs. accuracy without touching code:
# heroku config:set WHISPER_MODEL=tiny.en (fastest)
# heroku config:set WHISPER_MODEL=base.en (default, balanced)
# heroku config:set WHISPER_MODEL=small.en (most accurate, slowest)
MODEL_NAME = os.environ.get("WHISPER_MODEL", "base.en")
# int8 keeps memory + latency low on a CPU dyno (Heroku has no GPU).
# The model downloads once when the dyno starts, then stays cached for the
# life of that dyno. The very first request after a deploy/restart is slow
# because of that one-time download + load.
model = WhisperModel(MODEL_NAME, device="cpu", compute_type="int8")
app = FastAPI(title="Voice STT server")
@app.get("/")
def health():
"""Simple health check so you can confirm the dyno is up."""
return {"status": "ok", "model": MODEL_NAME}
@app.post("/transcribe")
async def transcribe(file: UploadFile = File(...)):
"""Accepts an audio file, returns the transcribed text.
Test from a computer:
curl -X POST -F "file=@sample.wav" https://YOUR-APP.herokuapp.com/transcribe
"""
audio_bytes = await file.read()
started = time.time()
segments, _info = model.transcribe(
io.BytesIO(audio_bytes),
language="en",
beam_size=1, # greedy decoding = fastest
vad_filter=True, # trims silence so short clips come back quicker
)
text = " ".join(segment.text.strip() for segment in segments).strip()
elapsed = round(time.time() - started, 2)
return {"text": text, "seconds": elapsed, "model": MODEL_NAME}