Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,43 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
-
# 1.
|
| 7 |
asr_pipeline = pipeline(
|
| 8 |
"automatic-speech-recognition",
|
| 9 |
-
model="facebook/wav2vec2-base-960h"
|
| 10 |
)
|
| 11 |
|
| 12 |
-
# 2.
|
| 13 |
-
def transcribe(
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
return "No audio received."
|
| 16 |
|
| 17 |
-
|
| 18 |
-
result = asr_pipeline(
|
| 19 |
return result["text"]
|
| 20 |
|
| 21 |
-
# 3.
|
| 22 |
with gr.Blocks() as demo:
|
| 23 |
-
gr.Markdown("# 🎤
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
transcribe_btn = gr.Button("Transcribe")
|
| 33 |
-
output_text = gr.Textbox(
|
| 34 |
-
label="Transcription",
|
| 35 |
-
placeholder="Your transcript will appear here..."
|
| 36 |
)
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
transcribe_btn.click(
|
| 40 |
fn=transcribe,
|
| 41 |
inputs=audio_input,
|
| 42 |
outputs=output_text
|
| 43 |
)
|
| 44 |
|
| 45 |
-
# 4. Launch app locally
|
| 46 |
if __name__ == "__main__":
|
| 47 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# 1. Build the ASR pipeline (English-only model)
|
| 5 |
asr_pipeline = pipeline(
|
| 6 |
"automatic-speech-recognition",
|
| 7 |
+
model="facebook/wav2vec2-base-960h" # good English model
|
| 8 |
)
|
| 9 |
|
| 10 |
+
# 2. Transcription function using a file path
|
| 11 |
+
def transcribe(audio_path):
|
| 12 |
+
"""
|
| 13 |
+
audio_path: path to a .wav file recorded by Gradio
|
| 14 |
+
"""
|
| 15 |
+
if audio_path is None:
|
| 16 |
return "No audio received."
|
| 17 |
|
| 18 |
+
# pipeline can take a file path directly
|
| 19 |
+
result = asr_pipeline(audio_path)
|
| 20 |
return result["text"]
|
| 21 |
|
| 22 |
+
# 3. Gradio UI
|
| 23 |
with gr.Blocks() as demo:
|
| 24 |
+
gr.Markdown("# 🎤 ASR Demo (Hugging Face Space)\nSpeak into your mic and get a transcript.")
|
| 25 |
+
|
| 26 |
+
audio_input = gr.Audio(
|
| 27 |
+
sources=["microphone"],
|
| 28 |
+
type="filepath", # <-- IMPORTANT: send a file path, not numpy
|
| 29 |
+
format="wav", # ensure WAV format (easier to decode)
|
| 30 |
+
label="Record your voice"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
+
transcribe_btn = gr.Button("Transcribe")
|
| 34 |
+
output_text = gr.Textbox(label="Transcription")
|
| 35 |
+
|
| 36 |
transcribe_btn.click(
|
| 37 |
fn=transcribe,
|
| 38 |
inputs=audio_input,
|
| 39 |
outputs=output_text
|
| 40 |
)
|
| 41 |
|
|
|
|
| 42 |
if __name__ == "__main__":
|
| 43 |
+
demo.launch()
|