Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,36 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import whisper
|
| 3 |
import torch
|
| 4 |
import tempfile
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
model = whisper.load_model("large-v3", device=device)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
try:
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
except Exception as e:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
app = gr.Interface(
|
| 22 |
-
fn=transcribe_audio,
|
| 23 |
-
inputs=gr.Audio(type="filepath"),
|
| 24 |
-
outputs=gr.JSON()
|
| 25 |
-
)
|
| 26 |
|
| 27 |
if __name__ == "__main__":
|
| 28 |
-
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
import whisper
|
| 4 |
import torch
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
+
import uvicorn
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
model = whisper.load_model("large-v3", device=device)
|
| 13 |
|
| 14 |
+
@app.post("/transcribe")
|
| 15 |
+
async def transcribe_audio(file: UploadFile = File(...)):
|
| 16 |
+
if not file:
|
| 17 |
+
raise HTTPException(status_code=400, detail="No file provided")
|
| 18 |
|
| 19 |
try:
|
| 20 |
+
contents = await file.read()
|
| 21 |
+
|
| 22 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 23 |
+
tmp_file.write(contents)
|
| 24 |
+
tmp_file_path = tmp_file.name
|
| 25 |
+
|
| 26 |
+
result = model.transcribe(tmp_file_path, fp16=False if device == "cpu" else True)
|
| 27 |
+
|
| 28 |
+
os.unlink(tmp_file_path)
|
| 29 |
+
|
| 30 |
+
return JSONResponse({"text": result["text"]})
|
| 31 |
|
| 32 |
except Exception as e:
|
| 33 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|