geethareddy commited on
Commit
5d91f83
·
verified ·
1 Parent(s): 5922597

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -27
app.py CHANGED
@@ -1,52 +1,70 @@
1
  import gradio as gr
2
  import pyttsx3
 
 
3
  import os
4
 
5
- # Initialize Text-to-Speech engine (you can use gTTS as well if you prefer)
6
  engine = pyttsx3.init()
7
 
8
- # Function to convert text to speech
 
 
 
 
 
 
 
 
 
 
 
9
  def speak_response(text):
10
- engine.say(text)
11
- engine.runAndWait()
 
12
 
13
- # Function to process the speech input
14
- def process_audio(audio):
15
- text = audio['text']
16
- speak_response(text)
17
- return text
 
 
 
 
 
 
 
 
18
 
19
- # Function to handle menu items and return response
20
  def handle_order(item):
21
- if item.lower() == "pizza":
22
- response = "You have selected Pizza. It contains cheese, tomato sauce, and pepperoni."
23
- elif item.lower() == "burger":
24
- response = "You have selected Burger. It contains beef patty, lettuce, and cheese."
25
- else:
26
- response = "Sorry, I don't have information about that item."
27
  speak_response(response)
28
  return response
29
 
30
- # Gradio Interface setup
31
  def chatbot_interface():
32
  with gr.Blocks() as demo:
33
- # Display the menu logo image (use your image path)
34
  gr.Image("assets/menu_logo.png", label="Click to speak", elem_id="voice_logo", interactive=True)
35
 
36
- # Microphone input to get user's speech
37
- audio_input = gr.Audio(source="microphone", type="numpy", label="Speak your order")
38
-
39
- # Text output for displaying response
40
  output_text = gr.Textbox(label="Assistant Response", interactive=False)
41
 
42
- # Function to process and return speech input
 
 
 
43
  def on_audio_input(audio):
44
- user_input = process_audio(audio)
45
- response = handle_order(user_input)
46
- output_text.update(value=response)
47
  return response
48
 
49
- # Attach microphone action
50
  audio_input.change(on_audio_input, inputs=[audio_input], outputs=[output_text])
51
 
52
  demo.launch()
 
1
  import gradio as gr
2
  import pyttsx3
3
+ import speech_recognition as sr
4
+ from gtts import gTTS
5
  import os
6
 
7
+ # Initialize Text-to-Speech engine (pyttsx3 for offline or gTTS for better quality)
8
  engine = pyttsx3.init()
9
 
10
+ # Initialize Speech Recognition engine
11
+ recognizer = sr.Recognizer()
12
+
13
+ # Define Menu items for the restaurant
14
+ menu = {
15
+ "pizza": "Delicious pepperoni pizza with cheese and sauce.",
16
+ "burger": "Beef burger with lettuce, tomato, and cheese.",
17
+ "pasta": "Creamy pasta with garlic and parmesan.",
18
+ "salad": "A healthy vegetable salad with olive oil dressing."
19
+ }
20
+
21
+ # Function to convert text to speech using gTTS
22
  def speak_response(text):
23
+ tts = gTTS(text=text, lang='en')
24
+ tts.save("response.mp3")
25
+ os.system("start response.mp3") # Windows; on Linux, use 'mpg321 response.mp3'
26
 
27
+ # Function to listen to user speech and convert it to text
28
+ def listen_input():
29
+ with sr.Microphone() as source:
30
+ print("Listening for order...")
31
+ audio = recognizer.listen(source)
32
+ try:
33
+ command = recognizer.recognize_google(audio)
34
+ print(f"You said: {command}")
35
+ return command
36
+ except sr.UnknownValueError:
37
+ return "Sorry, I didn't understand that. Could you please repeat?"
38
+ except sr.RequestError:
39
+ return "Sorry, there was an issue with the speech service."
40
 
41
+ # Function to handle menu interaction and confirm orders
42
  def handle_order(item):
43
+ item = item.lower()
44
+ response = menu.get(item, "Sorry, we don't have that item on the menu.")
 
 
 
 
45
  speak_response(response)
46
  return response
47
 
48
+ # Gradio interface setup
49
  def chatbot_interface():
50
  with gr.Blocks() as demo:
51
+ # Display menu logo image (optional)
52
  gr.Image("assets/menu_logo.png", label="Click to speak", elem_id="voice_logo", interactive=True)
53
 
54
+ # Textbox for displaying responses
 
 
 
55
  output_text = gr.Textbox(label="Assistant Response", interactive=False)
56
 
57
+ # Microphone button to listen to user input
58
+ audio_input = gr.Audio(source="microphone", type="numpy", label="Speak your order")
59
+
60
+ # Handle audio input and provide the appropriate response
61
  def on_audio_input(audio):
62
+ user_input = listen_input() # Convert speech to text
63
+ response = handle_order(user_input) # Handle order
64
+ output_text.update(value=response) # Display assistant's response
65
  return response
66
 
67
+ # Attach microphone action to trigger audio input
68
  audio_input.change(on_audio_input, inputs=[audio_input], outputs=[output_text])
69
 
70
  demo.launch()