|
|
from fastapi import FastAPI, UploadFile, File |
|
|
from faster_whisper import WhisperModel |
|
|
import uvicorn |
|
|
import tempfile |
|
|
import shutil |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
model = WhisperModel( |
|
|
"base", |
|
|
device="cpu", |
|
|
compute_type="int8" |
|
|
) |
|
|
|
|
|
@app.post("/transcribe") |
|
|
async def transcribe(file: UploadFile = File(...)): |
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp: |
|
|
shutil.copyfileobj(file.file, tmp) |
|
|
audio_path = tmp.name |
|
|
|
|
|
|
|
|
segments, info = model.transcribe(audio_path) |
|
|
text = "".join([seg.text for seg in segments]) |
|
|
|
|
|
return { |
|
|
"text": text, |
|
|
"language": info.language, |
|
|
"duration": info.duration |
|
|
} |
|
|
|
|
|
@app.get("/") |
|
|
def root(): |
|
|
return {"status": "API Whisper BASE OK"} |
|
|
|
|
|
if __name__ == "__main__": |
|
|
uvicorn.run(app, host="0.0.0.0", port=7860) |
|
|
|