danicor commited on
Commit
d6701a4
·
verified ·
1 Parent(s): 5556b32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -1,28 +1,36 @@
1
- import gradio as gr
 
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
- def transcribe_audio(audio_file):
11
- if audio_file is None:
12
- return {"error": "No audio file provided"}
 
13
 
14
  try:
15
- result = model.transcribe(audio_file, fp16=False if device == "cpu" else True)
16
- return {"text": result["text"]}
 
 
 
 
 
 
 
 
 
17
 
18
  except Exception as e:
19
- return {"error": str(e)}
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
- app.launch(share=False, server_name="0.0.0.0", server_port=7860)
 
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)