Spaces:
Running
Running
Rivalcoder
commited on
Commit
·
a7a067e
1
Parent(s):
c744068
Add application file
Browse files- main.py +64 -0
- requirements.txt +6 -0
main.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import whisper
|
3 |
+
from fastapi import FastAPI, UploadFile, File
|
4 |
+
from fastapi.responses import JSONResponse
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Load the Whisper model once
|
9 |
+
model = whisper.load_model("base") # You can change this to "tiny", "small", "medium", "large"
|
10 |
+
|
11 |
+
# FastAPI app
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
# API endpoint: POST /api/transcribe
|
15 |
+
@app.post("/api/transcribe")
|
16 |
+
async def transcribe_audio(file: UploadFile = File(...)):
|
17 |
+
try:
|
18 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
19 |
+
tmp.write(await file.read())
|
20 |
+
temp_path = tmp.name
|
21 |
+
|
22 |
+
result = model.transcribe(temp_path)
|
23 |
+
text = result.get("text", "")
|
24 |
+
os.remove(temp_path)
|
25 |
+
return {"transcript": text}
|
26 |
+
|
27 |
+
except Exception as e:
|
28 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
29 |
+
|
30 |
+
# Gradio function
|
31 |
+
def transcribe_from_ui(audio_file):
|
32 |
+
if audio_file is None:
|
33 |
+
return "Please upload an audio file."
|
34 |
+
|
35 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
36 |
+
tmp.write(audio_file.read())
|
37 |
+
temp_path = tmp.name
|
38 |
+
|
39 |
+
try:
|
40 |
+
result = model.transcribe(temp_path)
|
41 |
+
text = result.get("text", "")
|
42 |
+
except Exception as e:
|
43 |
+
text = f"Error: {str(e)}"
|
44 |
+
finally:
|
45 |
+
os.remove(temp_path)
|
46 |
+
|
47 |
+
return text
|
48 |
+
|
49 |
+
# Gradio UI
|
50 |
+
interface = gr.Interface(
|
51 |
+
fn=transcribe_from_ui,
|
52 |
+
inputs=gr.File(label="Upload Audio File (MP3/WAV/OGG/etc)"),
|
53 |
+
outputs=gr.Textbox(label="Transcript"),
|
54 |
+
title="🎙️ Audio to Text Transcriber",
|
55 |
+
description="Upload an audio file and get transcription using Whisper. No API key required."
|
56 |
+
)
|
57 |
+
|
58 |
+
# Mount Gradio app at /
|
59 |
+
app = gr.mount_gradio_app(app, interface, path="/")
|
60 |
+
|
61 |
+
# Run with: uvicorn app:app --reload
|
62 |
+
if __name__ == "__main__":
|
63 |
+
import uvicorn
|
64 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
fastapi
|
3 |
+
uvicorn
|
4 |
+
openai-whisper
|
5 |
+
ffmpeg-python
|
6 |
+
torch
|