geethareddy commited on
Commit
8e20c69
·
verified ·
1 Parent(s): 2475fd0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -60
app.py CHANGED
@@ -14,7 +14,6 @@ logging.basicConfig(level=logging.INFO)
14
  cart = [] # Stores items as [item_name, price, quantity] in the cart
15
  menu_preferences = None # Tracks the current menu preference
16
  section_preferences = None # Tracks the current section preference
17
- default_menu_preferences = "all" # To reset menu preferences
18
  default_sections = {
19
  "biryanis": ["veg biryani", "paneer biryani", "chicken biryani", "mutton biryani"],
20
  "starters": ["samosa", "onion pakoda", "chilli gobi", "chicken manchurian", "veg manchurian"],
@@ -41,15 +40,6 @@ prices = {
41
  "cola": 5,
42
  "lemon soda": 6
43
  }
44
- menus = {
45
- "all": list(prices.keys()),
46
- "vegetarian": [
47
- "samosa", "onion pakoda", "chilli gobi", "veg biryani", "paneer butter", "veg manchurian", "paneer biryani", "gulab jamun", "ice cream", "cola", "lemon soda"
48
- ],
49
- "non-vegetarian": [
50
- "chicken biryani", "mutton biryani", "fish curry", "chicken manchurian", "chilli chicken", "chicken curry", "gulab jamun", "ice cream", "cola", "lemon soda"
51
- ]
52
- }
53
 
54
  @app.route("/")
55
  def index():
@@ -89,7 +79,7 @@ def process_audio():
89
  recognizer.adjust_for_ambient_noise(source, duration=1)
90
  audio_data = recognizer.record(source)
91
 
92
- # Use multiple recognition services with fallbacks
93
  try:
94
  raw_command = recognizer.recognize_google(audio_data).lower()
95
  except sr.UnknownValueError:
@@ -134,47 +124,15 @@ def process_command(command):
134
 
135
  return f"Your order has been placed successfully:\n{order_summary}\nTotal: {total_price} INR.\nThank you for ordering!"
136
 
137
- # Greet the user and ask for preferences when first started
138
- if menu_preferences is None:
139
- if "hello" in command or "hi" in command:
140
- return "Hello, welcome to Biryani Hub! Please choose your preference: All, Vegetarian, or Non-Vegetarian."
141
-
142
- preferences = ["non-vegetarian", "vegetarian", "all"]
143
- if command in preferences:
144
- menu_preferences = command
145
- return f"You've selected the {command} menu! Which section would you like to browse next? (e.g., biryanis, starters, curries, desserts, soft drinks)"
146
-
147
- # Use fuzzy matching to help recognize similar inputs
148
- closest_match = process.extractOne(command, preferences, scorer=fuzz.partial_ratio)
149
- if closest_match and closest_match[1] > 75:
150
- menu_preferences = closest_match[0]
151
- return f"Great choice! You've chosen the {menu_preferences} menu. Which section would you like to browse next?"
152
-
153
- return "I couldn't recognize your choice. Please say either 'Non-Vegetarian', 'Vegetarian', or 'All'."
154
-
155
  if section_preferences is None:
156
  sections = list(default_sections.keys())
157
- for section in sections:
158
- if section in command:
159
- section_preferences = section
160
- return f"Here are the items in the {section_preferences} section: {', '.join(default_sections[section_preferences])}. Please choose an item."
161
-
162
- closest_match = process.extractOne(command, sections, scorer=fuzz.partial_ratio)
163
- if closest_match and closest_match[1] > 75:
164
- section_preferences = closest_match[0]
165
- return f"Here are the items in the {section_preferences} section: {', '.join(default_sections[section_preferences])}. What would you like to add?"
166
-
167
- return "I didn't catch that. Please say a section like 'biryanis', 'starters', 'curries', 'desserts', or 'soft drinks'."
168
-
169
- # Filter items based on the menu preference (vegetarian/non-vegetarian)
170
- available_items = []
171
- if menu_preferences == "vegetarian":
172
- available_items = [item for item in default_sections[section_preferences] if item in menus["vegetarian"]]
173
- elif menu_preferences == "non-vegetarian":
174
- available_items = [item for item in default_sections[section_preferences] if item in menus["non-vegetarian"]]
175
- elif menu_preferences == "all":
176
- available_items = [item for item in default_sections[section_preferences]]
177
 
 
 
178
  for item in available_items:
179
  if item in command:
180
  quantity = extract_quantity(command)
@@ -182,7 +140,7 @@ def process_command(command):
182
  cart.append([item, prices[item], quantity])
183
  return f"Added {quantity} x {item} to your cart. Your current cart: {', '.join([f'{i[0]} x{i[2]}' for i in cart])}. Would you like to add more items?"
184
 
185
- return "I didn't recognize the item you mentioned. Please say the item name clearly, or choose from the available items."
186
 
187
  def extract_quantity(command):
188
  """
@@ -268,7 +226,7 @@ html_code = """
268
  }
269
  });
270
  function startConversation() {
271
- const utterance = new SpeechSynthesisUtterance('Hello, welcome to Biryani Hub! Please choose your preference: All, Vegetarian, or Non-Vegetarian.');
272
  speechSynthesis.speak(utterance);
273
  utterance.onend = () => {
274
  status.textContent = 'Listening...';
@@ -295,15 +253,10 @@ html_code = """
295
  speechSynthesis.speak(utterance);
296
  utterance.onend = () => {
297
  console.log("Speech synthesis completed.");
298
- if (data.response.includes("final order") || data.response.includes("Thank you for ordering")) {
299
- status.textContent = 'Order completed. Press the mic button to start again.';
300
- isConversationActive = false;
301
- } else {
302
- status.textContent = 'Listening...';
303
- setTimeout(() => {
304
- startListening();
305
- }, 100);
306
- }
307
  };
308
  utterance.onerror = (e) => {
309
  console.error("Speech synthesis error:", e.error);
 
14
  cart = [] # Stores items as [item_name, price, quantity] in the cart
15
  menu_preferences = None # Tracks the current menu preference
16
  section_preferences = None # Tracks the current section preference
 
17
  default_sections = {
18
  "biryanis": ["veg biryani", "paneer biryani", "chicken biryani", "mutton biryani"],
19
  "starters": ["samosa", "onion pakoda", "chilli gobi", "chicken manchurian", "veg manchurian"],
 
40
  "cola": 5,
41
  "lemon soda": 6
42
  }
 
 
 
 
 
 
 
 
 
43
 
44
  @app.route("/")
45
  def index():
 
79
  recognizer.adjust_for_ambient_noise(source, duration=1)
80
  audio_data = recognizer.record(source)
81
 
82
+ # Try using the Google API for speech recognition
83
  try:
84
  raw_command = recognizer.recognize_google(audio_data).lower()
85
  except sr.UnknownValueError:
 
124
 
125
  return f"Your order has been placed successfully:\n{order_summary}\nTotal: {total_price} INR.\nThank you for ordering!"
126
 
127
+ # Ask for section preference when first started
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  if section_preferences is None:
129
  sections = list(default_sections.keys())
130
+ sections_text = ', '.join(sections) # List sections in a text format
131
+ section_preferences = sections[0] # Default section, e.g. "biryanis"
132
+ return f"Please choose a section: {sections_text}. If you'd like to choose one, please say its name."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
+ # Item addition logic after section is selected
135
+ available_items = default_sections[section_preferences]
136
  for item in available_items:
137
  if item in command:
138
  quantity = extract_quantity(command)
 
140
  cart.append([item, prices[item], quantity])
141
  return f"Added {quantity} x {item} to your cart. Your current cart: {', '.join([f'{i[0]} x{i[2]}' for i in cart])}. Would you like to add more items?"
142
 
143
+ return f"I didn't recognize the item you mentioned. Please say the item name clearly, or choose from the available items in the {section_preferences} section."
144
 
145
  def extract_quantity(command):
146
  """
 
226
  }
227
  });
228
  function startConversation() {
229
+ const utterance = new SpeechSynthesisUtterance('Hello, welcome to Biryani Hub! Please choose a section: biryanis, starters, curries, desserts, or soft drinks.');
230
  speechSynthesis.speak(utterance);
231
  utterance.onend = () => {
232
  status.textContent = 'Listening...';
 
253
  speechSynthesis.speak(utterance);
254
  utterance.onend = () => {
255
  console.log("Speech synthesis completed.");
256
+ status.textContent = 'Listening...';
257
+ setTimeout(() => {
258
+ startListening();
259
+ }, 100);
 
 
 
 
 
260
  };
261
  utterance.onerror = (e) => {
262
  console.error("Speech synthesis error:", e.error);