Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,90 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@app.post("/transcribe")
|
| 14 |
-
async def transcribe(
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
content = await file.read()
|
| 17 |
tmp.write(content)
|
| 18 |
tmp_path = tmp.name
|
|
|
|
| 19 |
try:
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
finally:
|
| 23 |
os.unlink(tmp_path)
|
| 24 |
|
| 25 |
@app.get("/health")
|
| 26 |
async def health():
|
| 27 |
return {"status": "ok", "model": "whisper-base"}
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import tempfile
|
| 3 |
import os
|
| 4 |
from fastapi import FastAPI, UploadFile, File, Form
|
| 5 |
+
from fastapi.responses import JSONResponse, HTMLResponse
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
import uvicorn
|
| 8 |
|
| 9 |
app = FastAPI(title="Whisper Transcription API")
|
|
|
|
| 10 |
|
| 11 |
+
print("Loading Whisper model...")
|
| 12 |
+
model = whisper.load_model("base")
|
| 13 |
+
print("Model loaded.")
|
| 14 |
+
|
| 15 |
+
app.add_middleware(
|
| 16 |
+
CORSMiddleware,
|
| 17 |
+
allow_origins=["*"],
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
@app.get("/", response_class=HTMLResponse)
|
| 23 |
+
async def root():
|
| 24 |
+
return """
|
| 25 |
+
<!DOCTYPE html>
|
| 26 |
+
<html>
|
| 27 |
+
<head>
|
| 28 |
+
<title>Whisper Transcription API</title>
|
| 29 |
+
<style>
|
| 30 |
+
body { font-family: monospace; background: #0d0d0d; color: #e0e0e0; padding: 40px; }
|
| 31 |
+
h1 { color: #7fff7f; }
|
| 32 |
+
a { color: #7fbfff; }
|
| 33 |
+
code { background: #1a1a1a; padding: 2px 6px; border-radius: 4px; }
|
| 34 |
+
</style>
|
| 35 |
+
</head>
|
| 36 |
+
<body>
|
| 37 |
+
<h1>🎙️ Whisper Transcription API</h1>
|
| 38 |
+
<p>Status: <strong style="color:#7fff7f">Running</strong></p>
|
| 39 |
+
<p>Endpoints:</p>
|
| 40 |
+
<ul>
|
| 41 |
+
<li><code>POST /transcribe</code> — Upload audio file, returns text</li>
|
| 42 |
+
<li><code>GET /health</code> — Health check</li>
|
| 43 |
+
<li><a href="/docs">📖 Swagger Docs</a></li>
|
| 44 |
+
</ul>
|
| 45 |
+
<p>Example curl:</p>
|
| 46 |
+
<pre style="background:#1a1a1a;padding:16px;border-radius:8px;">
|
| 47 |
+
curl -X POST /transcribe \\
|
| 48 |
+
-F "file=@audio.wav" \\
|
| 49 |
+
-F "language=auto"
|
| 50 |
+
</pre>
|
| 51 |
+
</body>
|
| 52 |
+
</html>
|
| 53 |
+
"""
|
| 54 |
|
| 55 |
@app.post("/transcribe")
|
| 56 |
+
async def transcribe(
|
| 57 |
+
file: UploadFile = File(...),
|
| 58 |
+
language: str = Form(default="auto")
|
| 59 |
+
):
|
| 60 |
+
suffix = os.path.splitext(file.filename)[-1] if file.filename else ".wav"
|
| 61 |
+
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
|
| 62 |
content = await file.read()
|
| 63 |
tmp.write(content)
|
| 64 |
tmp_path = tmp.name
|
| 65 |
+
|
| 66 |
try:
|
| 67 |
+
lang = language if language != "auto" else None
|
| 68 |
+
result = model.transcribe(tmp_path, language=lang)
|
| 69 |
+
return JSONResponse({
|
| 70 |
+
"text": result["text"].strip(),
|
| 71 |
+
"language": result.get("language", "unknown"),
|
| 72 |
+
"segments": [
|
| 73 |
+
{
|
| 74 |
+
"start": round(s["start"], 2),
|
| 75 |
+
"end": round(s["end"], 2),
|
| 76 |
+
"text": s["text"].strip()
|
| 77 |
+
}
|
| 78 |
+
for s in result.get("segments", [])
|
| 79 |
+
]
|
| 80 |
+
})
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return JSONResponse({"error": str(e)}, status_code=500)
|
| 83 |
finally:
|
| 84 |
os.unlink(tmp_path)
|
| 85 |
|
| 86 |
@app.get("/health")
|
| 87 |
async def health():
|
| 88 |
return {"status": "ok", "model": "whisper-base"}
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|