nagasurendra commited on
Commit
5796daf
·
verified ·
1 Parent(s): 9d03ef3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -106
app.py CHANGED
@@ -12,8 +12,16 @@ 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 = {
18
  "samosa": 9,
19
  "onion pakoda": 10,
@@ -27,17 +35,20 @@ prices = {
27
  "veg manchurian": 12,
28
  "chilli chicken": 14,
29
  "paneer biryani": 13,
30
- "chicken curry": 14
 
 
 
 
31
  }
32
  menus = {
33
  "all": list(prices.keys()),
34
  "vegetarian": [
35
- "samosa", "onion pakoda", "chilli gobi", "veg biryani", "paneer butter", "veg manchurian", "paneer biryani"
36
  ],
37
  "non-vegetarian": [
38
- "chicken biryani", "mutton biryani", "fish curry", "chicken manchurian", "chilli chicken", "chicken curry"
39
- ],
40
- "guilt-free": ["samosa", "onion pakoda"]
41
  }
42
 
43
  @app.route("/")
@@ -46,9 +57,10 @@ def index():
46
 
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
 
54
  @app.route("/process-audio", methods=["POST"])
@@ -80,8 +92,7 @@ def process_audio():
80
  logging.info(f"Raw recognized command: {raw_command}")
81
 
82
  # Preprocess the command
83
- all_menu_items = menus["all"]
84
- command = preprocess_command(raw_command, all_menu_items)
85
 
86
  # Process the command and get a response
87
  response = process_command(command)
@@ -96,113 +107,90 @@ def process_audio():
96
 
97
  return jsonify({"response": response})
98
 
99
- def preprocess_command(command, menu_items):
100
  """
101
  Preprocess the user command to improve matching:
102
- - Use fuzzy matching for menu items.
103
- - Use phonetic matching as a fallback.
104
  """
105
- def phonetic_match(word, options):
106
- word_phonetic = doublemetaphone(word)[0]
107
- for option in options:
108
- if doublemetaphone(option)[0] == word_phonetic:
109
- return option
110
- return None
111
-
112
- # First, try fuzzy matching
113
- closest_match = process.extractOne(command, menu_items, scorer=fuzz.token_set_ratio)
114
- if closest_match and closest_match[1] > 60:
115
- return closest_match[0]
116
-
117
- # Fallback to phonetic matching
118
- words = command.split()
119
- for word in words:
120
- match = phonetic_match(word, menu_items)
121
- if match:
122
- return match
123
-
124
- return command
125
 
126
  def process_command(command):
127
  """
128
  Process the user command based on the current menu and cart.
129
  """
130
- global cart, menu_preferences
131
- command = command.lower()
132
 
133
  # Handle menu preferences
134
- if "non-vegetarian" in command:
135
- menu_preferences = "non-vegetarian"
136
- return "You have chosen the Non-Vegetarian menu. To view the menu, say 'menu'."
137
- elif "vegetarian" in command and "non-vegetarian" not in command:
138
- menu_preferences = "vegetarian"
139
- return "You have chosen the Vegetarian menu. To view the menu, say 'menu'."
140
- elif "guilt-free" in command:
141
- menu_preferences = "guilt-free"
142
- return "You have chosen the Guilt-Free menu. To view the menu, say 'menu'."
143
- elif "all" in command:
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>
208
  <html lang="en">
@@ -271,7 +259,7 @@ html_code = """
271
  }
272
  });
273
  function startConversation() {
274
- const utterance = new SpeechSynthesisUtterance('Please choose your preference: All, Vegetarian, Non-Vegetarian, or Guilt-Free.');
275
  speechSynthesis.speak(utterance);
276
  utterance.onend = () => {
277
  status.textContent = 'Listening...';
@@ -298,20 +286,10 @@ html_code = """
298
  speechSynthesis.speak(utterance);
299
  utterance.onend = () => {
300
  console.log("Speech synthesis completed.");
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);
 
12
 
13
  # Global variables
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
+ "biryani": ["veg biryani", "paneer biryani", "chicken biryani", "mutton biryani"],
20
+ "starters": ["samosa", "onion pakoda", "chilli gobi", "chicken manchurian", "veg manchurian"],
21
+ "curries": ["paneer butter", "chicken curry", "fish curry", "chilli chicken"],
22
+ "desserts": ["gulab jamun", "ice cream"],
23
+ "soft drinks": ["cola", "lemon soda"]
24
+ }
25
  prices = {
26
  "samosa": 9,
27
  "onion pakoda": 10,
 
35
  "veg manchurian": 12,
36
  "chilli chicken": 14,
37
  "paneer biryani": 13,
38
+ "chicken curry": 14,
39
+ "gulab jamun": 8,
40
+ "ice cream": 6,
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", "cola", "lemon soda"
51
+ ]
 
52
  }
53
 
54
  @app.route("/")
 
57
 
58
  @app.route("/reset-cart", methods=["GET"])
59
  def reset_cart():
60
+ global cart, menu_preferences, section_preferences
61
  cart = []
62
+ menu_preferences = None
63
+ section_preferences = None
64
  return "Cart reset successfully."
65
 
66
  @app.route("/process-audio", methods=["POST"])
 
92
  logging.info(f"Raw recognized command: {raw_command}")
93
 
94
  # Preprocess the command
95
+ command = preprocess_command(raw_command)
 
96
 
97
  # Process the command and get a response
98
  response = process_command(command)
 
107
 
108
  return jsonify({"response": response})
109
 
110
+ def preprocess_command(command):
111
  """
112
  Preprocess the user command to improve matching:
113
+ - Normalize user input.
 
114
  """
115
+ return command.strip().lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  def process_command(command):
118
  """
119
  Process the user command based on the current menu and cart.
120
  """
121
+ global cart, menu_preferences, section_preferences
 
122
 
123
  # Handle menu preferences
124
+ if menu_preferences is None:
125
+ if "vegetarian" in command:
126
+ menu_preferences = "vegetarian"
127
+ return "You have chosen the Vegetarian menu. Which section would you like? (biryani, starters, curries, desserts, soft drinks)"
128
+ elif "non-vegetarian" in command:
129
+ menu_preferences = "non-vegetarian"
130
+ return "You have chosen the Non-Vegetarian menu. Which section would you like? (biryani, starters, curries, desserts, soft drinks)"
131
+ elif "all" in command:
132
+ menu_preferences = "all"
133
+ return "You have chosen the complete menu. Which section would you like? (biryani, starters, curries, desserts, soft drinks)"
134
+ else:
135
+ return "Please specify your preference: Vegetarian, Non-Vegetarian, or All."
136
 
137
+ # Handle section preferences
138
+ if section_preferences is None:
139
+ for section in default_sections.keys():
140
+ if section in command:
141
+ section_preferences = section
142
+ available_items = [item for item in default_sections[section] if item in menus[menu_preferences]]
143
+ return f"Here are the items in {section}: {', '.join(available_items)}. Say the item name to add to the cart."
144
+ return "Please specify a section: biryani, starters, curries, desserts, or soft drinks."
145
 
146
+ # Handle item addition
147
+ available_items = [item for item in default_sections[section_preferences] if item in menus[menu_preferences]]
148
+ for item in available_items:
149
+ if item in command:
150
+ # Ask for quantity
151
+ quantity = extract_quantity(command)
152
+ if quantity:
153
+ for cart_item in cart:
154
+ if cart_item[0] == item:
155
+ cart_item[2] += quantity
156
+ break
157
+ else:
158
+ cart.append([item, prices[item], quantity])
159
+ cart_summary = ", ".join([f"{i[0]} x{i[2]} (₹{i[1] * i[2]})" for i in cart])
160
+ return f"Added {quantity} x {item} to your cart. Current cart: {cart_summary}. Would you like to choose another section or finalize your order?"
161
+ else:
162
+ return "Please specify a quantity between 1 and 10."
163
 
164
+ # Handle remove command
165
  if "remove" in command:
166
  for item in cart:
167
+ if item[0] in command:
168
  cart.remove(item)
 
169
  cart_summary = ", ".join([f"{i[0]} x{i[2]} (₹{i[1] * i[2]})" for i in cart])
170
+ return f"Removed {item[0]} from your cart. Current cart: {cart_summary}."
171
  return "The item you are trying to remove is not in your cart."
172
 
173
+ # Handle final order
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  if "final order" in command:
175
  if cart:
176
  total = sum(i[1] * i[2] for i in cart)
177
+ order_summary = ", ".join([f"{i[0]} x{i[2]} (₹{i[1] * i[2]})" for i in cart])
178
+ return f"Your final order is: {order_summary}. Total bill: ₹{total}. Thank you for ordering!"
 
179
  return "Your cart is empty. Please add items to your cart first."
180
 
 
 
 
 
 
181
  return "Sorry, I couldn't understand that. Please try again."
182
 
183
+ def extract_quantity(command):
184
+ """
185
+ Extract quantity from the user command.
186
+ """
187
+ for word in command.split():
188
+ if word.isdigit():
189
+ quantity = int(word)
190
+ if 1 <= quantity <= 10:
191
+ return quantity
192
+ return None
193
+
194
  html_code = """
195
  <!DOCTYPE html>
196
  <html lang="en">
 
259
  }
260
  });
261
  function startConversation() {
262
+ const utterance = new SpeechSynthesisUtterance('Please choose your preference: All, Vegetarian, or Non-Vegetarian.');
263
  speechSynthesis.speak(utterance);
264
  utterance.onend = () => {
265
  status.textContent = 'Listening...';
 
286
  speechSynthesis.speak(utterance);
287
  utterance.onend = () => {
288
  console.log("Speech synthesis completed.");
289
+ status.textContent = 'Listening...';
290
+ setTimeout(() => {
291
+ startListening();
292
+ }, 100);
 
 
 
 
 
 
 
 
 
 
293
  };
294
  utterance.onerror = (e) => {
295
  console.error("Speech synthesis error:", e.error);