DSatishchandra commited on
Commit
49e2d43
1 Parent(s): c218b1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -6
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import speech_recognition as sr
3
  from gtts import gTTS
4
  import os
5
- from playsound import playsound # Import playsound library
6
  from transformers import pipeline
7
 
8
  # Initialize recognizer for speech recognition
@@ -26,11 +26,19 @@ def process_order(order):
26
  else:
27
  return "Sorry, we didn't catch that. Please try again."
28
 
29
- # Function to handle speech recognition
30
  def recognize_speech(audio):
31
  try:
32
- # Recognize speech using SpeechRecognition
33
- text = recognizer.recognize_google(audio)
 
 
 
 
 
 
 
 
34
  response = process_order(text)
35
 
36
  # Using gTTS to respond back with speech
@@ -42,6 +50,7 @@ def recognize_speech(audio):
42
 
43
  return response
44
  except Exception as e:
 
45
  return "Sorry, I could not understand."
46
 
47
  # Gradio Interface for the app
@@ -49,8 +58,8 @@ def create_gradio_interface():
49
  with gr.Blocks() as demo:
50
  gr.Markdown("## AI Voice Bot for Food Ordering")
51
 
52
- # Audio Input: User speaks into microphone
53
- audio_input = gr.Audio(type="numpy", label="Speak to the bot")
54
 
55
  # Display the bot's response after recognition
56
  output_text = gr.Textbox(label="Bot Response")
 
2
  import speech_recognition as sr
3
  from gtts import gTTS
4
  import os
5
+ from playsound import playsound
6
  from transformers import pipeline
7
 
8
  # Initialize recognizer for speech recognition
 
26
  else:
27
  return "Sorry, we didn't catch that. Please try again."
28
 
29
+ # Function to handle speech recognition from audio files or microphone
30
  def recognize_speech(audio):
31
  try:
32
+ # If audio is from file, use SpeechRecognition to convert speech to text
33
+ if isinstance(audio, str): # Audio file input
34
+ with sr.AudioFile(audio) as source:
35
+ audio_data = recognizer.record(source)
36
+ text = recognizer.recognize_google(audio_data)
37
+ else: # Audio from microphone input
38
+ text = recognizer.recognize_google(audio)
39
+
40
+ print(f"Recognized text: {text}") # Print the recognized text for debugging
41
+
42
  response = process_order(text)
43
 
44
  # Using gTTS to respond back with speech
 
50
 
51
  return response
52
  except Exception as e:
53
+ print(f"Error: {e}") # Print the error for debugging
54
  return "Sorry, I could not understand."
55
 
56
  # Gradio Interface for the app
 
58
  with gr.Blocks() as demo:
59
  gr.Markdown("## AI Voice Bot for Food Ordering")
60
 
61
+ # Audio Input: User speaks into microphone or uploads a file
62
+ audio_input = gr.Audio(type="file", label="Speak to the bot (Upload or Record Audio)")
63
 
64
  # Display the bot's response after recognition
65
  output_text = gr.Textbox(label="Bot Response")