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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -26
app.py CHANGED
@@ -25,7 +25,7 @@ def generate_voice_response(text):
25
  return temp_file.name
26
 
27
  def calculate_total(cart):
28
- return sum(menu_items[item] for item in cart)
29
 
30
  def restaurant_voice_assistant(audio, state_json):
31
  global cart
@@ -46,46 +46,53 @@ def restaurant_voice_assistant(audio, state_json):
46
  if not state.get("menu_shown", False):
47
  # Show menu dynamically
48
  response = "Welcome to our restaurant! Here is our menu:\n"
49
- for item, price in menu_items.items():
50
- response += f"{item}: ${price:.2f}\n"
51
- response += "\nPlease tell me the item you would like to add to your cart."
52
  state["menu_shown"] = True
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)
 
25
  return temp_file.name
26
 
27
  def calculate_total(cart):
28
+ return len(cart) # Only count items, not prices
29
 
30
  def restaurant_voice_assistant(audio, state_json):
31
  global cart
 
46
  if not state.get("menu_shown", False):
47
  # Show menu dynamically
48
  response = "Welcome to our restaurant! Here is our menu:\n"
49
+ for item in menu_items.keys():
50
+ response += f"{item}\n"
51
+ response += "\nPlease tell me the item you would like to add to your cart or ask for the price of an item."
52
  state["menu_shown"] = True
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 "price of" in input_text:
57
+ # Handle price queries
58
+ matched_items = [item for item in menu_items if item.lower() in input_text]
59
+ if len(matched_items) == 1:
60
+ item = matched_items[0]
61
+ response = f"The price of {item} is ${menu_items[item]:.2f}."
62
+ elif len(matched_items) > 1:
63
+ response = f"I detected multiple items in your input: {', '.join(matched_items)}. Please ask for the price of one item at a time."
64
+ else:
65
+ response = "I couldn't find that item on the menu. Please ask for an item available in the menu."
66
+ elif any(item.lower() in input_text for item in menu_items):
67
  # Match the input text with menu items
68
  matched_items = [item for item in menu_items if item.lower() in input_text and item not in state.get("current_items", [])]
 
69
  if len(matched_items) == 1:
70
  item = matched_items[0]
71
  cart.append(item)
72
  state.setdefault("current_items", []).append(item) # Track items added in the current cycle
 
73
  response = f"{item} has been added to your cart. Your current cart includes:\n"
74
  for cart_item in cart:
75
+ response += f"- {cart_item}\n"
76
+ response += "\nWould you like to add anything else?"
77
  elif len(matched_items) > 1:
78
  response = f"I detected multiple items in your input: {', '.join(matched_items)}. Please mention one item at a time."
79
+ elif "menu" in input_text:
80
+ response = "Here is our menu again:\n"
81
+ for item in menu_items.keys():
82
+ response += f"{item}\n"
83
+ response += "\nWhat would you like to add to your cart or ask about?"
84
+ elif "final order" in input_text or "submit order" in input_text:
85
+ if cart:
86
+ response = "Your final order includes:\n"
87
+ for item in cart:
88
+ response += f"- {item}\n"
89
+ response += f"\nThank you for ordering!"
90
+ cart = [] # Clear cart after finalizing order
91
+ state["current_items"] = [] # Clear current cycle tracking
 
 
 
92
  else:
93
+ response = "Your cart is empty. Would you like to order something?"
94
+ else:
95
+ response = "I didn’t quite catch that. Please tell me what you’d like to order or ask about."
96
 
97
  voice_path = generate_voice_response(response)
98
  return response, voice_path, json.dumps(state)