nagasurendra commited on
Commit
9d03ef3
·
verified ·
1 Parent(s): 2ca89fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -65
app.py CHANGED
@@ -11,7 +11,7 @@ app = Flask(__name__)
11
  logging.basicConfig(level=logging.INFO)
12
 
13
  # Global variables
14
- cart = {} # Stores items and their quantities in the cart
15
  menu_preferences = "all" # Tracks the current menu preference
16
  default_menu_preferences = "all" # To reset menu preferences
17
  prices = {
@@ -47,7 +47,7 @@ def index():
47
  @app.route("/reset-cart", methods=["GET"])
48
  def reset_cart():
49
  global cart, menu_preferences
50
- cart = {}
51
  menu_preferences = default_menu_preferences # Reset menu preferences
52
  return "Cart reset successfully."
53
 
@@ -144,76 +144,64 @@ def process_command(command):
144
  menu_preferences = "all"
145
  return "You have chosen the complete menu. To view the menu, say 'menu'."
146
 
147
- # Split command into potential action and item
148
- words = command.split()
149
- if len(words) > 1:
150
- action = words[0] # First word as action
151
- item_name = " ".join(words[1:]) # Remaining words as item name
152
- else:
153
- action = command
154
- item_name = ""
155
-
156
- # Handle specific actions like "remove" or "price"
157
- if action == "remove":
158
- return handle_remove(item_name)
159
- if action == "price":
160
- return handle_price(item_name)
161
-
162
- # Check if command matches an item name in the menu
163
  menu = menus.get(menu_preferences, menus["all"])
164
- closest_match = process.extractOne(command, menu, scorer=fuzz.token_set_ratio)
165
- if closest_match and closest_match[1] > 60:
166
- matched_item = closest_match[0]
167
- cart[matched_item] = cart.get(matched_item, 0) + 1
168
- return f"{matched_item.capitalize()} added to your cart. Current cart: {dict(cart)}. To finalize, say 'final order'."
169
 
170
- # Prompt user if action is unclear
171
- if action not in ["menu", "final order", "goodbye"]:
172
- return "Sorry, I didn't understand your action. Did you mean to add, remove, or check the price of an item?"
 
 
 
 
173
 
174
- # Generic menu or cart commands
175
- if action == "menu":
176
- menu = menus.get(menu_preferences, menus["all"])
177
  return f"Here is your menu: {', '.join(menu)}. To select an item, say the item name."
178
- if action == "final order":
179
- return finalize_order()
180
- if action == "goodbye":
181
- cart.clear()
182
- menu_preferences = default_menu_preferences
183
- return "Goodbye! Thank you for using AI Dining Assistant."
184
 
185
- return "Sorry, I couldn't understand that. Please try again."
 
 
 
 
 
 
 
186
 
187
- def handle_remove(item_name):
188
- if not item_name:
189
- return "Please specify the item to remove."
190
- closest_match = process.extractOne(item_name, list(cart.keys()), scorer=fuzz.token_set_ratio)
191
- if closest_match and closest_match[1] > 60:
192
- matched_item = closest_match[0]
193
- if cart[matched_item] > 1:
194
- cart[matched_item] -= 1
195
- return f"One {matched_item} has been removed. Current cart: {dict(cart)}."
196
- else:
197
- del cart[matched_item]
198
- return f"{matched_item.capitalize()} has been completely removed from your cart."
199
- return "Sorry, that item is not in your cart."
200
 
201
- def handle_price(item_name):
202
- if not item_name:
203
- return "Please specify the item to check the price."
204
- closest_match = process.extractOne(item_name, prices.keys(), scorer=fuzz.token_set_ratio)
205
- if closest_match and closest_match[1] > 60:
206
- matched_item = closest_match[0]
207
- return f"The price of {matched_item} is ${prices[matched_item]}."
208
- return "Sorry, I couldn't find that item in the menu."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
 
210
- def finalize_order():
211
- if cart:
212
- total = sum(prices[item] * count for item, count in cart.items())
213
- response = f"Your final order is: {', '.join(f'{item} x{count}' for item, count in cart.items())}. Your total bill is ${total}. Thank you for ordering! To exit this conversation, say 'goodbye'."
214
  cart.clear()
215
- return response
216
- return "Your cart is empty. Please add items to your cart first."
 
 
217
 
218
  html_code = """
219
  <!DOCTYPE html>
@@ -313,12 +301,23 @@ html_code = """
313
  if (data.response.includes("Goodbye")) {
314
  status.textContent = 'Conversation ended. Press the mic button to start again.';
315
  isConversationActive = false;
316
- fetch('/reset-cart');
 
 
 
 
317
  } else {
318
  status.textContent = 'Listening...';
319
- setTimeout(() => startListening(), 100);
 
 
320
  }
321
  };
 
 
 
 
 
322
  } catch (error) {
323
  response.textContent = 'Sorry, I could not understand. Please try again.';
324
  response.style.display = 'block';
 
11
  logging.basicConfig(level=logging.INFO)
12
 
13
  # Global variables
14
+ cart = [] # Stores items as [item_name, price, quantity] in the cart
15
  menu_preferences = "all" # Tracks the current menu preference
16
  default_menu_preferences = "all" # To reset menu preferences
17
  prices = {
 
47
  @app.route("/reset-cart", methods=["GET"])
48
  def reset_cart():
49
  global cart, menu_preferences
50
+ cart = []
51
  menu_preferences = default_menu_preferences # Reset menu preferences
52
  return "Cart reset successfully."
53
 
 
144
  menu_preferences = "all"
145
  return "You have chosen the complete menu. To view the menu, say 'menu'."
146
 
147
+ # Handle commands related to the current menu
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  menu = menus.get(menu_preferences, menus["all"])
 
 
 
 
 
149
 
150
+ if "price of" in command:
151
+ item = command.replace("price of", "").strip()
152
+ closest_match = process.extractOne(item, prices.keys(), scorer=fuzz.token_set_ratio)
153
+ if closest_match and closest_match[1] > 60:
154
+ matched_item = closest_match[0]
155
+ return f"The price of {matched_item} is ${prices[matched_item]}."
156
+ return "Sorry, I couldn't find that item in the menu."
157
 
158
+ if "menu" in command:
 
 
159
  return f"Here is your menu: {', '.join(menu)}. To select an item, say the item name."
 
 
 
 
 
 
160
 
161
+ if "remove" in command:
162
+ for item in cart:
163
+ if item[0].lower() in command:
164
+ cart.remove(item)
165
+ total = sum(i[1] * i[2] for i in cart)
166
+ cart_summary = ", ".join([f"{i[0]} x{i[2]} (₹{i[1] * i[2]})" for i in cart])
167
+ return f"Removed {item[0]} from your cart. Updated cart: {cart_summary}. Total: ₹{total}."
168
+ return "The item you are trying to remove is not in your cart."
169
 
170
+ if "clear cart" in command or "empty cart" in command:
171
+ if cart:
172
+ cart.clear()
173
+ return "Your cart has been cleared."
174
+ return "Your cart is already empty."
 
 
 
 
 
 
 
 
175
 
176
+ if any(item in command for item in menu):
177
+ closest_match = process.extractOne(command, menu, scorer=fuzz.token_set_ratio)
178
+ if closest_match and closest_match[1] > 60:
179
+ matched_item = closest_match[0]
180
+ for cart_item in cart:
181
+ if cart_item[0] == matched_item:
182
+ cart_item[2] += 1
183
+ break
184
+ else:
185
+ cart.append([matched_item, prices[matched_item], 1])
186
+ cart_summary = ", ".join([f"{i[0]} x{i[2]} (₹{i[1] * i[2]})" for i in cart])
187
+ total = sum(i[1] * i[2] for i in cart)
188
+ return f"{matched_item.capitalize()} added to your cart. Current cart: {cart_summary}. Total: ₹{total}."
189
+ return "Sorry, I couldn't recognize the item. Could you try again."
190
+
191
+ if "final order" in command:
192
+ if cart:
193
+ total = sum(i[1] * i[2] for i in cart)
194
+ response = f"Your final order is: {', '.join(f'{i[0]} x{i[2]} (₹{i[1] * i[2]})' for i in cart)}. Total bill: ₹{total}. Thank you for ordering! To exit this conversation, say 'goodbye'."
195
+ cart.clear()
196
+ return response
197
+ return "Your cart is empty. Please add items to your cart first."
198
 
199
+ if "goodbye" in command:
 
 
 
200
  cart.clear()
201
+ menu_preferences = default_menu_preferences
202
+ return "Goodbye! Thank you for using AI Dining Assistant."
203
+
204
+ return "Sorry, I couldn't understand that. Please try again."
205
 
206
  html_code = """
207
  <!DOCTYPE html>
 
301
  if (data.response.includes("Goodbye")) {
302
  status.textContent = 'Conversation ended. Press the mic button to start again.';
303
  isConversationActive = false;
304
+ fetch('/reset-cart'); // Reset the cart dynamically on end
305
+ } else if (data.response.includes("Your order is complete")) {
306
+ status.textContent = 'Order complete. Thank you for using AI Dining Assistant.';
307
+ isConversationActive = false;
308
+ fetch('/reset-cart'); // Reset the cart after final order
309
  } else {
310
  status.textContent = 'Listening...';
311
+ setTimeout(() => {
312
+ startListening();
313
+ }, 100);
314
  }
315
  };
316
+ utterance.onerror = (e) => {
317
+ console.error("Speech synthesis error:", e.error);
318
+ status.textContent = 'Error with speech output.';
319
+ isConversationActive = false;
320
+ };
321
  } catch (error) {
322
  response.textContent = 'Sorry, I could not understand. Please try again.';
323
  response.style.display = 'block';