Spaces:
Running
Running
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 | |
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) | |