Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| # 1. Build the ASR pipeline (English-only model) | |
| asr_pipeline = pipeline( | |
| "automatic-speech-recognition", | |
| model="facebook/wav2vec2-base-960h" # good English model | |
| ) | |
| # 2. Transcription function using a file path | |
| def transcribe(audio_path): | |
| """ | |
| audio_path: path to a .wav file recorded by Gradio | |
| """ | |
| if audio_path is None: | |
| return "No audio received." | |
| # pipeline can take a file path directly | |
| result = asr_pipeline(audio_path) | |
| return result["text"] | |
| # 3. Gradio UI | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🎤 ASR Demo (Hugging Face Space)\nSpeak into your mic and get a transcript.") | |
| audio_input = gr.Audio( | |
| sources=["microphone"], | |
| type="filepath", # <-- IMPORTANT: send a file path, not numpy | |
| format="wav", # ensure WAV format (easier to decode) | |
| label="Record your voice" | |
| ) | |
| transcribe_btn = gr.Button("Transcribe") | |
| output_text = gr.Textbox(label="Transcription") | |
| transcribe_btn.click( | |
| fn=transcribe, | |
| inputs=audio_input, | |
| outputs=output_text | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |