idofgaurav commited on
Commit
b8a7969
·
verified ·
1 Parent(s): d04a88d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -26
app.py CHANGED
@@ -1,47 +1,43 @@
1
-
2
-
3
  import gradio as gr
4
  from transformers import pipeline
5
 
6
- # 1. Load the ASR pipeline (this will download the model the first time)
7
  asr_pipeline = pipeline(
8
  "automatic-speech-recognition",
9
- model="facebook/wav2vec2-base-960h"
10
  )
11
 
12
- # 2. Define the transcription function
13
- def transcribe(audio):
14
- if audio is None:
 
 
 
15
  return "No audio received."
16
 
17
- sr, data = audio
18
- result = asr_pipeline({"array": data, "sampling_rate": sr})
19
  return result["text"]
20
 
21
- # 3. Build the Gradio interface
22
  with gr.Blocks() as demo:
23
- gr.Markdown("# 🎤 Browser ASR Demo\nRecord your voice and get the transcription.")
24
-
25
- with gr.Row():
26
- audio_input = gr.Audio(
27
- sources=["microphone"], # enables browser mic recording
28
- type="numpy", # gives (sample_rate, data) to the function
29
- label="Record or upload audio"
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
- # Link button to function
 
 
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(True)
 
 
 
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()