Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import gradio as grfrom transformers import pipeline# 1. Load the ASR pipeline (this will download the model the first time)asr_pipeline = pipeline("automatic-speech-recognition",model="facebook/wav2vec2-base-960h")# 2. Define the transcription functiondef transcribe(audio):if audio is None:return "No audio received."sr, data = audioresult = asr_pipeline({"array": data, "sampling_rate": sr})return result["text"]# 3. Build the Gradio interfacewith gr.Blocks() as demo:gr.Markdown("# 🎤 Browser ASR Demo\nRecord your voice and get the transcription.")with gr.Row():audio_input = gr.Audio(sources=["microphone"], # enables browser mic recordingtype="numpy", # gives (sample_rate, data) to the functionlabel="Record or upload audio")transcribe_btn = gr.Button("Transcribe")output_text = gr.Textbox(label="Transcription",placeholder="Your transcript will appear here...")# Link button to functiontranscribe_btn.click(fn=transcribe,inputs=audio_input,outputs=output_text)# 4. Launch app locallyif __name__ == "__main__":demo.launch(True)12345678910111213141516171819202122232425262728293031323334353637383940414243import gradio as grfrom 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 pathdef 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 directlyresult = asr_pipeline(audio_path)return result["text"]# 3. Gradio UIwith 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 numpyformat="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()
app.py+22-26