nagasurendra commited on
Commit
cf58c92
·
verified ·
1 Parent(s): 7ec356c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -36
app.py CHANGED
@@ -5,6 +5,7 @@ import os
5
  import ffmpeg
6
  import logging
7
  from werkzeug.exceptions import BadRequest
 
8
 
9
  app = Flask(__name__)
10
  logging.basicConfig(level=logging.INFO)
@@ -24,19 +25,16 @@ prices = {
24
  "chicken manchurian": 14,
25
  "veg manchurian": 12,
26
  "chilli chicken": 14,
27
- "channa masala": 12,
28
  "panner biryani": 13,
29
- "prawn fry": 15,
30
- "sukka gosht": 18,
31
  "chicken curry": 14
32
  }
33
  menus = {
34
  "all": list(prices.keys()),
35
  "vegetarian": [
36
- "samosa", "onion pakoda", "chilli gobi", "veg biryani", "panner butter", "veg manchurian", "channa masala", "panner biryani"
37
  ],
38
  "non-vegetarian": [
39
- "chicken biryani", "mutton biryani", "fish curry", "chicken manchurian", "chilli chicken", "prawn fry", "sukka gosht", "chicken curry"
40
  ],
41
  "guilt-free": ["samosa", "onion pakoda"]
42
  }
@@ -148,7 +146,7 @@ html_code = """
148
  status.textContent = 'Listening...';
149
  setTimeout(() => {
150
  startListening();
151
- }, 2000);
152
  }
153
  };
154
  utterance.onerror = (e) => {
@@ -205,14 +203,24 @@ def process_audio():
205
  ).run(overwrite_output=True)
206
 
207
  recognizer = sr.Recognizer()
208
- recognizer.energy_threshold = 300 # Adjust for low-volume recognition
 
 
209
  with sr.AudioFile(converted_file.name) as source:
210
  audio_data = recognizer.record(source)
211
  try:
212
- command = recognizer.recognize_google(audio_data)
213
- response = process_command(command)
 
 
 
 
 
 
 
 
214
  except sr.UnknownValueError:
215
- response = "Sorry, I could not understand. Please try again."
216
 
217
  return jsonify({"response": response})
218
 
@@ -227,56 +235,61 @@ def process_audio():
227
  def process_command(command):
228
  global cart, menu_preferences
229
  command = command.lower()
230
-
231
- # Recognize menu preferences first
232
  if menu_preferences == "all":
233
- if "vegetarian" in command:
234
  menu_preferences = "vegetarian"
235
- return "You have chosen the Vegetarian menu."
236
  elif "non-vegetarian" in command:
237
  menu_preferences = "non-vegetarian"
238
- return "You have chosen the Non-Vegetarian menu."
239
  elif "guilt-free" in command:
240
  menu_preferences = "guilt-free"
241
- return "You have chosen the Guilt-Free menu."
242
  elif "all" in command:
243
  menu_preferences = "all"
244
- return "You have chosen the complete menu."
245
 
246
- # Use filtered menu based on preference
247
  menu = menus.get(menu_preferences, menus["all"])
248
 
249
  if "menu" in command:
250
- return f"Here is your menu: {', '.join(menu)}."
251
  elif "price of" in command:
252
  item = command.replace("price of", "").strip()
253
- if item in prices:
254
- return f"The price of {item} is ${prices[item]}."
 
 
255
  return "Sorry, I couldn't find that item in the menu."
256
  elif "remove" in command:
257
  item = command.replace("remove", "").strip()
258
- if item in cart:
259
- cart.remove(item)
260
- return f"{item.capitalize()} has been removed from your cart."
261
- return f"{item.capitalize()} is not in your cart."
 
 
262
  elif any(item in command for item in menu):
263
- item = next((item for item in menu if item in command), None)
264
- if item:
265
- cart.append(item)
266
- return f"{item.capitalize()} added to your cart. Your cart now has: {', '.join(cart)}. To submit your order say final order. To know the price of an item say price of item name. To remove an item say remove item name."
267
- elif "final order" in command or "submit" in command:
 
 
268
  if cart:
269
  total = sum(prices[item] for item in cart if item in prices)
270
- response = f"Your final order is: {', '.join(cart)}. Your total bill is ${total}. Thank you for ordering! To stop this conversation say nothing or good bye! "
271
- cart.clear() # Reset the cart after final order
272
  return response
273
- else:
274
- return "Your cart is empty. Please add items to your cart first."
275
  elif "no" in command or "nothing" in command or "goodbye" in command:
276
- cart.clear() # Clear cart on exit
277
  menu_preferences = "all"
278
  return "Goodbye! Thank you for using AI Dining Assistant."
279
- return "Sorry, I didn't understand that. Please try again."
280
 
281
  if __name__ == "__main__":
282
  app.run(host="0.0.0.0", port=7860)
 
5
  import ffmpeg
6
  import logging
7
  from werkzeug.exceptions import BadRequest
8
+ from fuzzywuzzy import process
9
 
10
  app = Flask(__name__)
11
  logging.basicConfig(level=logging.INFO)
 
25
  "chicken manchurian": 14,
26
  "veg manchurian": 12,
27
  "chilli chicken": 14,
 
28
  "panner biryani": 13,
 
 
29
  "chicken curry": 14
30
  }
31
  menus = {
32
  "all": list(prices.keys()),
33
  "vegetarian": [
34
+ "samosa", "onion pakoda", "chilli gobi", "veg biryani", "panner butter", "veg manchurian", "panner biryani"
35
  ],
36
  "non-vegetarian": [
37
+ "chicken biryani", "mutton biryani", "fish curry", "chicken manchurian", "chilli chicken", "chicken curry"
38
  ],
39
  "guilt-free": ["samosa", "onion pakoda"]
40
  }
 
146
  status.textContent = 'Listening...';
147
  setTimeout(() => {
148
  startListening();
149
+ }, 500);
150
  }
151
  };
152
  utterance.onerror = (e) => {
 
203
  ).run(overwrite_output=True)
204
 
205
  recognizer = sr.Recognizer()
206
+ recognizer.dynamic_energy_threshold = True # Enable dynamic threshold
207
+ recognizer.energy_threshold = 100 # Lower threshold for better sensitivity
208
+
209
  with sr.AudioFile(converted_file.name) as source:
210
  audio_data = recognizer.record(source)
211
  try:
212
+ command = recognizer.recognize_google(audio_data, show_all=True)
213
+ if "alternative" in command:
214
+ best_match = command["alternative"][0]
215
+ if best_match.get("confidence", 0) > 0.6: # Confidence threshold
216
+ command_text = best_match["transcript"].lower()
217
+ response = process_command(command_text)
218
+ else:
219
+ response = "I couldn't confidently understand that. Please repeat."
220
+ else:
221
+ response = "Sorry, I couldn't understand. Please try again."
222
  except sr.UnknownValueError:
223
+ response = "Sorry, I couldn't understand. Please try again."
224
 
225
  return jsonify({"response": response})
226
 
 
235
  def process_command(command):
236
  global cart, menu_preferences
237
  command = command.lower()
238
+
239
+ # Recognize menu preferences
240
  if menu_preferences == "all":
241
+ if "vegetarian" in command and "non-vegetarian" not in command:
242
  menu_preferences = "vegetarian"
243
+ return "You have chosen the Vegetarian menu. To view menu say menu"
244
  elif "non-vegetarian" in command:
245
  menu_preferences = "non-vegetarian"
246
+ return "You have chosen the Non-Vegetarian menu. To view menu say menu"
247
  elif "guilt-free" in command:
248
  menu_preferences = "guilt-free"
249
+ return "You have chosen the Guilt-Free menu. To view menu say menu"
250
  elif "all" in command:
251
  menu_preferences = "all"
252
+ return "You have chosen the complete menu. To view menu say menu"
253
 
254
+ # Filtered menu based on preference
255
  menu = menus.get(menu_preferences, menus["all"])
256
 
257
  if "menu" in command:
258
+ return f"Here is your menu: {', '.join(menu)}. To select an item say item name."
259
  elif "price of" in command:
260
  item = command.replace("price of", "").strip()
261
+ closest_match = process.extractOne(item, prices.keys())
262
+ if closest_match and closest_match[1] > 80: # Match threshold
263
+ matched_item = closest_match[0]
264
+ return f"The price of {matched_item} is ${prices[matched_item]}."
265
  return "Sorry, I couldn't find that item in the menu."
266
  elif "remove" in command:
267
  item = command.replace("remove", "").strip()
268
+ closest_match = process.extractOne(item, cart)
269
+ if closest_match and closest_match[1] > 80:
270
+ matched_item = closest_match[0]
271
+ cart.remove(matched_item)
272
+ return f"{matched_item.capitalize()} has been removed from your cart."
273
+ return "Sorry, that item is not in your cart."
274
  elif any(item in command for item in menu):
275
+ closest_match = process.extractOne(command, menu)
276
+ if closest_match and closest_match[1] > 80: # Fuzzy match threshold
277
+ matched_item = closest_match[0]
278
+ cart.append(matched_item)
279
+ return f"{matched_item.capitalize()} added to your cart. Your cart now has: {', '.join(cart)}. To add another item say item name. To submit your order say final order."
280
+ return "Sorry, I couldn't recognize the item. Could you try again?"
281
+ elif "final order" in command:
282
  if cart:
283
  total = sum(prices[item] for item in cart if item in prices)
284
+ response = f"Your final order is: {', '.join(cart)}. Your total bill is ${total}. Thank you for ordering!"
285
+ cart.clear()
286
  return response
287
+ return "Your cart is empty. Please add items to your cart first."
 
288
  elif "no" in command or "nothing" in command or "goodbye" in command:
289
+ cart.clear()
290
  menu_preferences = "all"
291
  return "Goodbye! Thank you for using AI Dining Assistant."
292
+ return "Sorry, I couldn't understand that. Please try again."
293
 
294
  if __name__ == "__main__":
295
  app.run(host="0.0.0.0", port=7860)