Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import whisper
|
| 2 |
+
import tempfile
|
| 3 |
+
import os
|
| 4 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 5 |
+
from fastapi.responses import JSONResponse
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
|
| 8 |
+
app = FastAPI(title="Whisper Transcription API")
|
| 9 |
+
model = whisper.load_model("base") # Ganti 'large-v3' kalau GPU
|
| 10 |
+
|
| 11 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 12 |
+
|
| 13 |
+
@app.post("/transcribe")
|
| 14 |
+
async def transcribe(file: UploadFile = File(...), language: str = Form(default="auto")):
|
| 15 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 16 |
+
content = await file.read()
|
| 17 |
+
tmp.write(content)
|
| 18 |
+
tmp_path = tmp.name
|
| 19 |
+
try:
|
| 20 |
+
result = model.transcribe(tmp_path, language=language if language != "auto" else None)
|
| 21 |
+
return JSONResponse({"text": result["text"], "language": result.get("language", "unknown")})
|
| 22 |
+
finally:
|
| 23 |
+
os.unlink(tmp_path)
|
| 24 |
+
|
| 25 |
+
@app.get("/health")
|
| 26 |
+
async def health():
|
| 27 |
+
return {"status": "ok", "model": "whisper-base"}
|