zanemotiwala commited on
Commit
b3e5e27
Β·
verified Β·
1 Parent(s): 053d920

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -1,37 +1,36 @@
1
  import gradio as gr
2
  import logging
3
  from transformers import pipeline
4
- import torch
5
 
 
6
  logging.basicConfig(level=logging.INFO)
7
 
8
- # Load ASR pipeline
9
  asr = pipeline(task="automatic-speech-recognition", model="openai/whisper-tiny.en")
10
 
11
- def transcribe_speech(audio):
12
- if audio is None:
 
13
  logging.error("No audio provided.")
14
  return "No audio found, please retry."
15
 
16
  try:
17
- logging.info("Received audio input")
18
- audio_array, sample_rate = audio
19
- # Hugging Face Whisper expects a 1D array or file path
20
- output = asr(audio_array, sampling_rate=sample_rate)
21
  return output["text"]
22
  except Exception as e:
23
  logging.error(f"Error during transcription: {str(e)}")
24
  return f"Error processing the audio file: {str(e)}"
25
 
26
- # Gradio UI
27
  with gr.Blocks() as demo:
28
- gr.Markdown("# Simple Speech Recognition App")
29
- gr.Markdown("### Record or upload audio, then click 'Transcribe Audio'")
30
 
31
- mic = gr.Audio(label="Microphone or Upload", type="numpy")
32
- transcribe_button = gr.Button("Transcribe Audio")
33
- transcription = gr.Textbox(label="Transcription", lines=3, placeholder="Transcription will appear here...")
34
 
35
  transcribe_button.click(fn=transcribe_speech, inputs=mic, outputs=transcription)
36
 
37
- demo.launch(share=True)
 
1
  import gradio as gr
2
  import logging
3
  from transformers import pipeline
 
4
 
5
+ # Set up logging
6
  logging.basicConfig(level=logging.INFO)
7
 
8
+ # Load Whisper model (tiny version for speed)
9
  asr = pipeline(task="automatic-speech-recognition", model="openai/whisper-tiny.en")
10
 
11
+ # Function to transcribe audio from a file path
12
+ def transcribe_speech(audio_path):
13
+ if audio_path is None:
14
  logging.error("No audio provided.")
15
  return "No audio found, please retry."
16
 
17
  try:
18
+ logging.info(f"Received audio file path: {audio_path}")
19
+ output = asr(audio_path)
 
 
20
  return output["text"]
21
  except Exception as e:
22
  logging.error(f"Error during transcription: {str(e)}")
23
  return f"Error processing the audio file: {str(e)}"
24
 
25
+ # Gradio Interface
26
  with gr.Blocks() as demo:
27
+ gr.Markdown("# 🎀 Simple Speech Recognition App")
28
+ gr.Markdown("Record or upload audio, then click **Transcribe Audio**")
29
 
30
+ mic = gr.Audio(label="πŸŽ™οΈ Microphone or Upload", type="filepath") # This is the key change
31
+ transcribe_button = gr.Button("πŸ“ Transcribe Audio")
32
+ transcription = gr.Textbox(label="πŸ—’οΈ Transcription", lines=3, placeholder="Transcription will appear here...")
33
 
34
  transcribe_button.click(fn=transcribe_speech, inputs=mic, outputs=transcription)
35
 
36
+ demo.launch(share=True)