Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, UploadFile, File
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from multiprocessing import Process, Queue
|
4 |
+
import whisper
|
5 |
+
import io
|
6 |
+
import uvicorn
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
model = whisper.load_model("large")
|
11 |
+
|
12 |
+
class TranscriptionRequest(BaseModel):
|
13 |
+
file: UploadFile
|
14 |
+
|
15 |
+
def transcribe_audio(file, queue):
|
16 |
+
try:
|
17 |
+
audio = io.BytesIO(file.file.read())
|
18 |
+
result = model.transcribe(audio)
|
19 |
+
queue.put(result["text"])
|
20 |
+
except Exception as e:
|
21 |
+
queue.put(f"Error: {str(e)}")
|
22 |
+
|
23 |
+
@app.post("/transcribe_audio")
|
24 |
+
async def transcribe_audio(file: UploadFile = File(...)):
|
25 |
+
queue = Queue()
|
26 |
+
p = Process(target=transcribe_audio, args=(file, queue))
|
27 |
+
p.start()
|
28 |
+
p.join()
|
29 |
+
response = queue.get()
|
30 |
+
if "Error" in response:
|
31 |
+
raise HTTPException(status_code=500, detail=response)
|
32 |
+
return {"transcription": response}
|
33 |
+
|
34 |
+
if __name__ == "__main__":
|
35 |
+
uvicorn.run(app, host="0.0.0.0", port=8006)
|