File size: 816 Bytes
2d25adc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import pipeline

MODEL = "openai/whisper-tiny"  # good balance of speed + accuracy

# Load model once at startup
device = 0 if torch.cuda.is_available() else -1

transcriber = pipeline(
    "automatic-speech-recognition",
    model=MODEL,
    device=device
)

def transcribe(audio):
    if audio is None:
        return "Please upload or record audio."

    result = transcriber(audio)
    return result["text"]


demo = gr.Interface(
    fn=transcribe,
    inputs=gr.Audio(
        sources=["microphone", "upload"],
        type="filepath"
    ),
    outputs=gr.Textbox(label="Transcription"),
    title="🎙 Voice to Text",
    description="Upload or record audio. Powered by Whisper running locally in HF Spaces."
)

if __name__ == "__main__":
    demo.launch()