nagasurendra commited on
Commit
b766996
·
verified ·
1 Parent(s): 80a96d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -53
app.py CHANGED
@@ -2,11 +2,12 @@ import gradio as gr
2
  from gtts import gTTS
3
  import os
4
  import speech_recognition as sr
 
5
 
6
- # Initialize recognizer
7
  recognizer = sr.Recognizer()
8
 
9
- # Menu items
10
  menu_items = {
11
  "biryani": ["Chicken Biryani", "Mutton Biryani", "Vegetable Biryani", "Egg Biryani"],
12
  "starters": ["Chicken Tikka", "Paneer Tikka", "Fish Fry", "Veg Manchurian"],
@@ -14,84 +15,73 @@ menu_items = {
14
  }
15
  cart = []
16
 
17
- # Text-to-Speech Function
18
  def text_to_speech(text):
19
- """Convert text to speech and provide audio file."""
20
  tts = gTTS(text=text, lang='en')
21
- file_path = "response.mp3"
22
- tts.save(file_path)
23
- return file_path
24
 
25
- # Read Menu Function
26
  def read_menu():
27
- """Generate the menu text and read it aloud."""
28
- menu_text = "Here is the menu. Starting with Biryani options: "
29
- for item in menu_items["biryani"]:
30
- menu_text += item + ". "
31
- menu_text += "Now the Starters: "
32
- for item in menu_items["starters"]:
33
- menu_text += item + ". "
34
- menu_text += "Finally, Drinks: "
35
- for item in menu_items["drinks"]:
36
- menu_text += item + ". "
37
- return menu_text, text_to_speech(menu_text)
38
 
39
- # Process Voice Command
40
- def process_command(audio_path):
41
- """Process the user's voice command."""
42
  try:
43
  with sr.AudioFile(audio_path) as source:
44
- audio_data = recognizer.record(source)
45
- command = recognizer.recognize_google(audio_data).lower()
46
  except Exception as e:
47
- error_text = "Sorry, I could not process the audio."
48
- return "Error", text_to_speech(error_text)
49
 
50
  if "menu" in command:
51
- menu_text, menu_audio = read_menu()
52
- return menu_text, menu_audio
53
 
54
  for category, items in menu_items.items():
55
  for item in items:
56
  if item.lower() in command:
57
  cart.append(item)
58
- response_text = f"{item} has been added to your cart."
59
- return response_text, text_to_speech(response_text)
60
 
61
  if "cart" in command:
62
  if not cart:
63
- response_text = "Your cart is empty."
64
  else:
65
- response_text = "Your cart contains: " + ", ".join(cart)
66
- return response_text, text_to_speech(response_text)
67
 
68
- if "submit" in command or "done" in command:
69
  if not cart:
70
- response_text = "Your cart is empty. Add some items before submitting."
71
  else:
72
- response_text = "Your final order is: " + ", ".join(cart) + ". Thank you for your order!"
73
  cart.clear()
74
- return response_text, text_to_speech(response_text)
75
 
76
- error_text = "Sorry, I couldn't understand your request."
77
- return error_text, text_to_speech(error_text)
78
 
79
- # Gradio App
80
- def app():
81
- """Create the Gradio interface."""
82
- with gr.Blocks() as demo:
83
- gr.Markdown("# Voice-Activated Restaurant Menu System")
84
- gr.Markdown("Speak your command to interact with the menu system dynamically.")
85
 
86
- with gr.Row():
87
- voice_input = gr.Audio(type="filepath", label="Speak Your Command")
88
- transcribed_text = gr.Textbox(label="Transcribed Command")
89
- response_text = gr.Textbox(label="Response Text")
90
- audio_output = gr.Audio(label="Audio Response")
91
 
92
- voice_input.change(fn=process_command, inputs=voice_input, outputs=[response_text, audio_output])
 
 
 
 
93
 
94
- return demo
95
 
96
  if __name__ == "__main__":
97
- app().launch()
 
2
  from gtts import gTTS
3
  import os
4
  import speech_recognition as sr
5
+ from tempfile import NamedTemporaryFile
6
 
7
+ # Initialize the recognizer
8
  recognizer = sr.Recognizer()
9
 
10
+ # Menu items and cart initialization
11
  menu_items = {
12
  "biryani": ["Chicken Biryani", "Mutton Biryani", "Vegetable Biryani", "Egg Biryani"],
13
  "starters": ["Chicken Tikka", "Paneer Tikka", "Fish Fry", "Veg Manchurian"],
 
15
  }
16
  cart = []
17
 
 
18
  def text_to_speech(text):
19
+ """Convert text to speech and provide an audio file."""
20
  tts = gTTS(text=text, lang='en')
21
+ audio_file = NamedTemporaryFile(delete=False, suffix=".mp3")
22
+ tts.save(audio_file.name)
23
+ return audio_file.name
24
 
 
25
  def read_menu():
26
+ """Generate and return the menu text."""
27
+ menu_text = "Here is the menu: \n"
28
+ for category, items in menu_items.items():
29
+ menu_text += f"{category.capitalize()}: \n" + ", ".join(items) + "\n"
30
+ menu_text += "Please tell me the items you want to add to your cart."
31
+ return menu_text
 
 
 
 
 
32
 
33
+ def process_audio_command(audio_path):
34
+ """Process the user's audio command."""
35
+ global cart
36
  try:
37
  with sr.AudioFile(audio_path) as source:
38
+ audio = recognizer.record(source)
39
+ command = recognizer.recognize_google(audio).lower()
40
  except Exception as e:
41
+ return "Sorry, I could not understand. Could you repeat?"
 
42
 
43
  if "menu" in command:
44
+ return read_menu()
 
45
 
46
  for category, items in menu_items.items():
47
  for item in items:
48
  if item.lower() in command:
49
  cart.append(item)
50
+ return f"{item} has been added to your cart."
 
51
 
52
  if "cart" in command:
53
  if not cart:
54
+ return "Your cart is empty."
55
  else:
56
+ return "Your cart contains: " + ", ".join(cart)
 
57
 
58
+ if "submit" in command or "finalize" in command:
59
  if not cart:
60
+ return "Your cart is empty. Add some items before submitting."
61
  else:
62
+ response = "Your final order is: " + ", ".join(cart) + ". Thank you for your order!"
63
  cart.clear()
64
+ return response
65
 
66
+ return "Sorry, I didn't understand that. Please try again."
 
67
 
68
+ # Define the continuous conversation function
69
+ def conversation_loop(audio_path):
70
+ """Continuous conversation handling for dynamic interactions."""
71
+ user_response = process_audio_command(audio_path)
72
+ return user_response, text_to_speech(user_response)
 
73
 
74
+ with gr.Blocks() as app:
75
+ gr.Markdown("# Voice-Activated Restaurant Menu System")
76
+ gr.Markdown("Speak your command to interact with the menu dynamically.")
 
 
77
 
78
+ with gr.Row():
79
+ audio_input = gr.Audio(label="Speak Your Command", type="filepath")
80
+ text_output = gr.Textbox(label="Transcribed Command")
81
+ response_text = gr.Textbox(label="Response Text")
82
+ audio_output = gr.Audio(label="Assistant Response")
83
 
84
+ audio_input.change(conversation_loop, inputs=audio_input, outputs=[response_text, audio_output])
85
 
86
  if __name__ == "__main__":
87
+ app.launch()