File size: 1,855 Bytes
a7a067e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import gradio as gr
import whisper
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import JSONResponse
import tempfile
import os

# Load the Whisper model once
model = whisper.load_model("base")  # You can change this to "tiny", "small", "medium", "large"

# FastAPI app
app = FastAPI()

# API endpoint: POST /api/transcribe
@app.post("/api/transcribe")
async def transcribe_audio(file: UploadFile = File(...)):
    try:
        with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
            tmp.write(await file.read())
            temp_path = tmp.name

        result = model.transcribe(temp_path)
        text = result.get("text", "")
        os.remove(temp_path)
        return {"transcript": text}

    except Exception as e:
        return JSONResponse(content={"error": str(e)}, status_code=500)

# Gradio function
def transcribe_from_ui(audio_file):
    if audio_file is None:
        return "Please upload an audio file."

    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
        tmp.write(audio_file.read())
        temp_path = tmp.name

    try:
        result = model.transcribe(temp_path)
        text = result.get("text", "")
    except Exception as e:
        text = f"Error: {str(e)}"
    finally:
        os.remove(temp_path)

    return text

# Gradio UI
interface = gr.Interface(
    fn=transcribe_from_ui,
    inputs=gr.File(label="Upload Audio File (MP3/WAV/OGG/etc)"),
    outputs=gr.Textbox(label="Transcript"),
    title="🎙️ Audio to Text Transcriber",
    description="Upload an audio file and get transcription using Whisper. No API key required."
)

# Mount Gradio app at /
app = gr.mount_gradio_app(app, interface, path="/")

# Run with: uvicorn app:app --reload
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)