nagasurendra commited on
Commit
bea0744
·
verified ·
1 Parent(s): c9de19e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -27
app.py CHANGED
@@ -39,7 +39,7 @@ def restaurant_voice_assistant(audio, state_json):
39
  recognizer = sr.Recognizer()
40
  with sr.AudioFile(audio) as source:
41
  try:
42
- input_text = recognizer.recognize_google(recognizer.record(source))
43
  except sr.UnknownValueError:
44
  input_text = ""
45
 
@@ -53,34 +53,39 @@ def restaurant_voice_assistant(audio, state_json):
53
  elif not input_text.strip():
54
  # Wait for valid input without responding if no input is provided
55
  return "", None, json.dumps(state)
56
- elif any(item.lower() in input_text.lower() for item in menu_items):
57
- # Check if input matches a menu item
58
- for item in menu_items:
59
- if item.lower() in input_text.lower():
60
- cart.append(item)
61
- total = calculate_total(cart)
62
- response = f"{item} has been added to your cart. Your current cart includes:\n"
63
- for cart_item in cart:
64
- response += f"- {cart_item}: ${menu_items[cart_item]:.2f}\n"
65
- response += f"\nTotal: ${total:.2f}. Would you like to add anything else?"
66
- break
67
- elif "menu" in input_text.lower():
68
- response = "Here is our menu again:\n"
69
- for item, price in menu_items.items():
70
- response += f"{item}: ${price:.2f}\n"
71
- response += "\nWhat would you like to add to your cart?"
72
- elif "final order" in input_text.lower() or "submit order" in input_text.lower():
73
- if cart:
74
  total = calculate_total(cart)
75
- response = "Your final order includes:\n"
76
- for item in cart:
77
- response += f"- {item}: ${menu_items[item]:.2f}\n"
78
- response += f"\nTotal: ${total:.2f}.\nThank you for ordering!"
79
- cart = [] # Clear cart after finalizing order
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  else:
81
- response = "Your cart is empty. Would you like to order something?"
82
- else:
83
- response = "I didn’t quite catch that. Please tell me what you’d like to order."
84
 
85
  voice_path = generate_voice_response(response)
86
  return response, voice_path, json.dumps(state)
 
39
  recognizer = sr.Recognizer()
40
  with sr.AudioFile(audio) as source:
41
  try:
42
+ input_text = recognizer.recognize_google(recognizer.record(source)).lower()
43
  except sr.UnknownValueError:
44
  input_text = ""
45
 
 
53
  elif not input_text.strip():
54
  # Wait for valid input without responding if no input is provided
55
  return "", None, json.dumps(state)
56
+ else:
57
+ # Match the input text with menu items
58
+ matched_items = [item for item in menu_items if item.lower() in input_text and item not in state.get("current_items", [])]
59
+
60
+ if len(matched_items) == 1:
61
+ item = matched_items[0]
62
+ cart.append(item)
63
+ state.setdefault("current_items", []).append(item) # Track items added in the current cycle
 
 
 
 
 
 
 
 
 
 
64
  total = calculate_total(cart)
65
+ response = f"{item} has been added to your cart. Your current cart includes:\n"
66
+ for cart_item in cart:
67
+ response += f"- {cart_item}: ${menu_items[cart_item]:.2f}\n"
68
+ response += f"\nTotal: ${total:.2f}. Would you like to add anything else?"
69
+ elif len(matched_items) > 1:
70
+ response = f"I detected multiple items in your input: {', '.join(matched_items)}. Please mention one item at a time."
71
+ elif "menu" in input_text:
72
+ response = "Here is our menu again:\n"
73
+ for item, price in menu_items.items():
74
+ response += f"{item}: ${price:.2f}\n"
75
+ response += "\nWhat would you like to add to your cart?"
76
+ elif "final order" in input_text or "submit order" in input_text:
77
+ if cart:
78
+ total = calculate_total(cart)
79
+ response = "Your final order includes:\n"
80
+ for item in cart:
81
+ response += f"- {item}: ${menu_items[item]:.2f}\n"
82
+ response += f"\nTotal: ${total:.2f}.\nThank you for ordering!"
83
+ cart = [] # Clear cart after finalizing order
84
+ state["current_items"] = [] # Clear current cycle tracking
85
+ else:
86
+ response = "Your cart is empty. Would you like to order something?"
87
  else:
88
+ response = "I didn’t quite catch that. Please tell me what you’d like to order."
 
 
89
 
90
  voice_path = generate_voice_response(response)
91
  return response, voice_path, json.dumps(state)