geethareddy commited on
Commit
6b13e93
·
verified ·
1 Parent(s): 24b4bee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +470 -59
app.py CHANGED
@@ -1,91 +1,490 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  import edge_tts
4
  import asyncio
5
  import tempfile
6
 
7
- # Load menu data
8
  def load_menu():
9
- menu_file = "menu.xlsx"
10
  try:
11
  return pd.read_excel(menu_file)
12
  except Exception as e:
13
  raise ValueError(f"Error loading menu file: {e}")
14
 
15
- # Generate menu details
16
- def generate_menu_details():
17
  menu_data = load_menu()
18
- menu_details = ""
19
- for _, item in menu_data.iterrows():
20
- menu_details += f"{item['Dish Name']} - ${item['Price ($)']}\n"
21
- menu_details += f"{item['Description']}\n"
22
- menu_details += f"[Energy: {item['Energy (kcal)']} kcal, Protein: {item['Protein (g)']}g, Carbohydrates: {item['Carbohydrates (g)']}g, Fiber: {item['Fiber (g)']}g, Fat: {item['Fat (g)']}g, Sugar: {item['Sugar (g)']}g]\n\n"
23
- return menu_details
24
-
25
- # Text-to-speech response
26
- def generate_tts_response(text):
 
 
 
 
 
 
 
27
  communicate = edge_tts.Communicate(text)
28
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
29
  tmp_path = tmp_file.name
30
- asyncio.run(communicate.save(tmp_path))
31
  return tmp_path
32
 
33
- # Process user input and respond
34
- def process_user_input(preference, cart):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  menu_data = load_menu()
36
- response_text = ""
 
37
 
38
- if "menu" in preference.lower():
39
- response_text = "Here are the menu details:\n" + generate_menu_details()
40
- else:
41
- matched_item = menu_data[menu_data['Dish Name'].str.contains(preference, case=False, na=False)]
42
- if not matched_item.empty:
43
- dish_name = matched_item.iloc[0]['Dish Name']
44
- price = matched_item.iloc[0]['Price ($)']
45
- cart.append((dish_name, price))
46
- response_text = f"Order confirmed for {dish_name}. It has been added to your cart."
47
- elif "remove" in preference.lower():
48
- item_to_remove = preference.lower().replace("remove", "").strip()
49
- cart = [item for item in cart if item[0].lower() != item_to_remove]
50
- response_text = f"Removed {item_to_remove} from your cart."
 
 
 
 
 
51
  else:
52
- response_text = "I'm sorry, I didn't understand that. Please try again."
 
 
53
 
54
- tts_path = generate_tts_response(response_text)
55
- return tts_path, response_text, cart
 
 
56
 
57
- # Calculate total bill
58
- def calculate_total(cart):
59
- total = sum(item[1] for item in cart)
60
- return f"Total: ${total:.2f}"
61
 
62
- # Build Gradio app
63
- def app():
64
- cart = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- def handle_interaction(preference):
67
- tts_path, response_text, updated_cart = process_user_input(preference, cart)
68
- cart_display = "\n".join([f"{item[0]} - ${item[1]}" for item in updated_cart])
69
- total_bill = calculate_total(updated_cart)
70
- return tts_path, response_text, cart_display, total_bill
71
 
 
 
72
  with gr.Blocks() as demo:
73
- gr.Markdown("# Welcome to the Menu")
 
74
 
75
- # Input and outputs
76
  with gr.Row():
77
- preference_input = gr.Textbox(label="Your Request", placeholder="Ask for menu or order items")
78
- audio_output = gr.Audio(label="Assistant Response", autoplay=True)
79
-
80
- transcription_output = gr.Textbox(label="Response Text")
81
- cart_output = gr.Textbox(label="Cart")
82
- total_output = gr.Textbox(label="Total Bill")
83
-
84
- # Button for interaction
85
- preference_input.submit(
86
- handle_interaction,
87
- inputs=preference_input,
88
- outputs=[audio_output, transcription_output, cart_output, total_output],
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  )
90
 
91
  return demo
@@ -93,3 +492,15 @@ def app():
93
  if __name__ == "__main__":
94
  demo = app()
95
  demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from utils.database_handler import check_credentials, save_user
4
+
5
+ # Function to load the menu data
6
+ def load_menu():
7
+ menu_file = "menu.xlsx" # Ensure this file exists in the same directory
8
+ try:
9
+ return pd.read_excel(menu_file)
10
+ except Exception as e:
11
+ raise ValueError(f"Error loading menu file: {e}")
12
+
13
+ # Function to filter menu items based on preference
14
+ def filter_menu(preference):
15
+ menu_data = load_menu()
16
+ if preference == "Halal/Non-Veg":
17
+ filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
18
+ elif preference == "Vegetarian":
19
+ filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
20
+ elif preference == "Guilt-Free":
21
+ filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
22
+ else:
23
+ filtered_data = menu_data
24
+
25
+ html_content = ""
26
+ for _, item in filtered_data.iterrows():
27
+ html_content += f"""
28
+ <div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
29
+ <div style="flex: 1; margin-right: 15px;">
30
+ <h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
31
+ <p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
32
+ <p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
33
+ </div>
34
+ <div style="flex-shrink: 0; text-align: center;">
35
+ <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
36
+ <button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="openModal('{item['Dish Name']}', '{item['Image URL']}', '{item['Description']}', '{item['Price ($)']}')">Add</button>
37
+ </div>
38
+ </div>
39
+ """
40
+ return html_content
41
+
42
+ # Login and Signup Functions
43
+ def authenticate_user(email, password):
44
+ if check_credentials(email, password):
45
+ return gr.update(visible=False), gr.update(visible=True), ""
46
+ else:
47
+ return gr.update(visible=True), gr.update(visible=False), "Invalid email or password. Try again."
48
+
49
+ def navigate_to_signup():
50
+ return gr.update(visible=False), gr.update(visible=True)
51
+
52
+ def create_account(name, phone, email, password):
53
+ if save_user(name, phone, email, password):
54
+ return "Account created successfully! You can now log in.", gr.update(visible=True), gr.update(visible=False)
55
+ else:
56
+ return "Email already exists. Try logging in.", gr.update(visible=False), gr.update(visible=True)
57
+
58
+ def navigate_to_login():
59
+ return gr.update(visible=True), gr.update(visible=False)
60
+
61
+ # Gradio app with popup modal and cart system
62
+ def app():
63
+ with gr.Blocks() as demo:
64
+ # Login Page
65
+ with gr.Column(visible=True) as login_section:
66
+ gr.Markdown("# Login Page")
67
+ email = gr.Textbox(label="Email", placeholder="Enter your email")
68
+ password = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
69
+ error_box = gr.Markdown("", visible=False)
70
+ login_btn = gr.Button("Login")
71
+ create_account_btn = gr.Button("Create an Account")
72
+
73
+ # Sign-Up Page
74
+ with gr.Column(visible=False) as signup_section:
75
+ gr.Markdown("# Sign Up Page")
76
+ name = gr.Textbox(label="Name", placeholder="Enter your full name")
77
+ phone = gr.Textbox(label="Phone", placeholder="Enter your phone number")
78
+ signup_email = gr.Textbox(label="Email", placeholder="Enter your email")
79
+ signup_password = gr.Textbox(label="Password", placeholder="Enter a password", type="password")
80
+ success_message = gr.Markdown("", visible=False)
81
+ error_message = gr.Markdown("", visible=False)
82
+ submit_btn = gr.Button("Sign Up")
83
+ back_to_login_btn = gr.Button("Back to Login")
84
+
85
+ # Preferences and Menu Section
86
+ with gr.Column(visible=False) as preference_section:
87
+ gr.Markdown("## Preferences")
88
+ selected_preference = gr.Radio(
89
+ choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
90
+ value="All",
91
+ label="Choose a Preference",
92
+ )
93
+ menu_output = gr.HTML(value=filter_menu("All"))
94
+ cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
95
+ modal_window = gr.HTML("""
96
+ <div id="modal" style="display: none; position: fixed; top: 10%; left: 50%; transform: translate(-50%, 0); background: white; padding: 20px; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000;">
97
+ <div style="text-align: right;">
98
+ <button onclick="closeModal()" style="background: none; border: none; font-size: 20px; cursor: pointer;">&times;</button>
99
+ </div>
100
+ <img id="modal-image" style="width: 100%; height: auto; border-radius: 8px; margin-bottom: 15px;" />
101
+ <h3 id="modal-name"></h3>
102
+ <p id="modal-description"></p>
103
+ <p id="modal-price" style="font-size: 18px; color: #28a745; font-weight: bold;"></p>
104
+ <label>Extras:</label>
105
+ <div style="display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 10px;">
106
+ <label><input type="checkbox" name="extra" value="Thums up" /> Thums up + $2.00</label>
107
+ <label><input type="checkbox" name="extra" value="Sprite" /> Sprite + $2.00</label>
108
+ <label><input type="checkbox" name="extra" value="Extra Raitha" /> Extra Raitha + $1.00</label>
109
+ <label><input type="checkbox" name="extra" value="Extra Salan" /> Extra Salan + $2.00</label>
110
+ <label><input type="checkbox" name="extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
111
+ <label><input type="checkbox" name="extra" value="Chilli Chicken" /> Chilli Chicken + $14.00</label>
112
+ <label><input type="checkbox" name="extra" value="Veg Manchurian" /> Veg Manchurian + $12.00</label>
113
+ </div>
114
+ <label for="quantity">Quantity:</label>
115
+ <input type="number" id="quantity" value="1" min="1" style="width: 100%; padding: 8px; margin-bottom: 10px; border: 1px solid #ddd; border-radius: 5px;" />
116
+ <label for="special-instructions">Special Instructions:</label>
117
+ <textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px; padding: 10px; border: 1px solid #ddd; border-radius: 5px; margin-bottom: 10px;"></textarea>
118
+ <button onclick="addToCart()" style="width: 100%; padding: 10px; background-color: #28a745; color: white; border: none; border-radius: 5px; font-size: 16px;">Add to Cart</button>
119
+ </div>
120
+ """)
121
+ selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
122
+
123
+ # Button Actions
124
+ login_btn.click(authenticate_user, inputs=[email, password], outputs=[login_section, preference_section, error_box])
125
+ create_account_btn.click(navigate_to_signup, inputs=[], outputs=[login_section, signup_section])
126
+ submit_btn.click(create_account, inputs=[name, phone, signup_email, signup_password], outputs=[success_message, signup_section, login_section])
127
+ back_to_login_btn.click(navigate_to_login, inputs=[], outputs=[login_section, signup_section])
128
+
129
+ # Cart HTML Placeholder
130
+ gr.HTML('<div id="floating-cart" style="margin-top: 20px;"></div>')
131
+
132
+ # JavaScript for Popup and Cart Management
133
+ gr.HTML("""
134
+ <script>
135
+ let cart = [];
136
+ const extrasPrices = {
137
+ "Thums up": 2,
138
+ "Sprite": 2,
139
+ "Extra Raitha": 1,
140
+ "Extra Salan": 2,
141
+ "Extra Onion & Lemon": 2,
142
+ "Chilli Chicken": 14,
143
+ "Veg Manchurian": 12
144
+ };
145
+
146
+ function openModal(name, image, description, price) {
147
+ const modal = document.getElementById('modal');
148
+ modal.style.display = 'block';
149
+ modal.style.position = 'fixed';
150
+ modal.style.top = '10%';
151
+ modal.style.left = '50%';
152
+ modal.style.transform = 'translate(-50%, 0)';
153
+ document.getElementById('modal-image').src = image;
154
+ document.getElementById('modal-name').innerText = name;
155
+ document.getElementById('modal-description').innerText = description;
156
+ document.getElementById('modal-price').innerText = `$${price}`;
157
+ }
158
+
159
+ function closeModal() {
160
+ document.getElementById('modal').style.display = 'none';
161
+ }
162
+
163
+ function addToCart() {
164
+ const name = document.getElementById('modal-name').innerText;
165
+ const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
166
+ const quantity = parseInt(document.getElementById('quantity').value) || 1;
167
+ const specialInstructions = document.getElementById('special-instructions').value || "None";
168
+
169
+ const selectedExtras = Array.from(document.querySelectorAll('input[name="extra"]:checked')).map(extra => extra.value);
170
+ const extrasCost = selectedExtras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
171
+
172
+ const itemTotal = (price + extrasCost) * quantity;
173
+
174
+ cart.push({
175
+ name,
176
+ price,
177
+ quantity,
178
+ selectedExtras,
179
+ extrasCost,
180
+ itemTotal,
181
+ specialInstructions
182
+ });
183
+
184
+ alert(`${name} added to cart!`);
185
+ closeModal();
186
+ updateCart();
187
+ }
188
+
189
+ function updateCart() {
190
+ let cartHTML = '<div style="padding: 10px; border: 1px solid #ddd; border-radius: 8px;">';
191
+ let totalAmount = 0;
192
+
193
+ cart.forEach((item, index) => {
194
+ totalAmount += item.itemTotal;
195
+ const extrasList = item.selectedExtras.length ? item.selectedExtras.join(', ') : "No extras";
196
+
197
+ cartHTML += `
198
+ <div style="margin-bottom: 10px; padding: 10px; border: 1px solid #ddd; border-radius: 5px;">
199
+ <strong>${item.name}</strong> <br>
200
+ Price: $${item.price.toFixed(2)} <br>
201
+ Quantity: ${item.quantity} <br>
202
+ Extras: ${extrasList} (Total Extras Cost: $${item.extrasCost.toFixed(2)}) <br>
203
+ Special Instructions: ${item.specialInstructions} <br>
204
+ Item Total: $${item.itemTotal.toFixed(2)}
205
+ <button style="margin-top: 5px; background-color: red; color: white; border: none; padding: 5px 10px; border-radius: 5px; cursor: pointer;" onclick="removeFromCart(${index})">Remove</button>
206
+ </div>
207
+ `;
208
+ });
209
+
210
+ cartHTML += `<div style="text-align: right; font-size: 16px; font-weight: bold;">Total Amount: $${totalAmount.toFixed(2)}</div>`;
211
+ cartHTML += '</div>';
212
+ document.getElementById('floating-cart').innerHTML = cartHTML;
213
+ }
214
+
215
+ function removeFromCart(index) {
216
+ cart.splice(index, 1);
217
+ updateCart();
218
+ }
219
+ </script>
220
+ """)
221
+
222
+ return demo
223
+
224
+ if __name__ == "__main__":
225
+ demo = app()
226
+ demo.launch()
227
+
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+ voice reading:
246
+
247
+
248
  import gradio as gr
249
  import pandas as pd
250
  import edge_tts
251
  import asyncio
252
  import tempfile
253
 
254
+ # Function to load the menu data
255
  def load_menu():
256
+ menu_file = "menu.xlsx" # Ensure this file exists in the same directory
257
  try:
258
  return pd.read_excel(menu_file)
259
  except Exception as e:
260
  raise ValueError(f"Error loading menu file: {e}")
261
 
262
+ # Function to filter menu items based on preference
263
+ def filter_menu(preference):
264
  menu_data = load_menu()
265
+ if preference == "Halal/Non-Veg":
266
+ filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
267
+ elif preference == "Vegetarian":
268
+ filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
269
+ elif preference == "Guilt-Free":
270
+ filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
271
+ else:
272
+ filtered_data = menu_data
273
+
274
+ return filtered_data
275
+
276
+ # Text-to-Speech Function
277
+ async def generate_audio_response(items):
278
+ text = ""
279
+ for _, item in items.iterrows():
280
+ text += f"{item['Dish Name']} costs ${item['Price ($)']}. "
281
  communicate = edge_tts.Communicate(text)
282
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
283
  tmp_path = tmp_file.name
284
+ await communicate.save(tmp_path)
285
  return tmp_path
286
 
287
+ # Function to handle user preference and generate audio
288
+ async def handle_preference(preference):
289
+ filtered_menu = filter_menu(preference)
290
+ menu_html = ""
291
+ for _, item in filtered_menu.iterrows():
292
+ menu_html += f"""
293
+ <div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
294
+ <div style="flex: 1; margin-right: 15px;">
295
+ <h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
296
+ <p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
297
+ <p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
298
+ </div>
299
+ <div style="flex-shrink: 0; text-align: center;">
300
+ <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
301
+ </div>
302
+ </div>
303
+ """
304
+ audio_path = await generate_audio_response(filtered_menu)
305
+ return menu_html, audio_path
306
+
307
+ # Gradio app with dynamic audio response
308
+ def app():
309
+ with gr.Blocks() as demo:
310
+ with gr.Row():
311
+ gr.Markdown("### Select a Preference")
312
+ preference_selector = gr.Radio(
313
+ choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
314
+ value="All",
315
+ label="Preferences",
316
+ )
317
+
318
+ menu_output = gr.HTML(label="Menu")
319
+ audio_output = gr.Audio(label="Audio Response", autoplay=True)
320
+
321
+ preference_selector.change(
322
+ handle_preference,
323
+ inputs=[preference_selector],
324
+ outputs=[menu_output, audio_output],
325
+ )
326
+
327
+ return demo
328
+
329
+ if __name__ == "__main__":
330
+ demo = app()
331
+ demo.launch()
332
+
333
+
334
+
335
+
336
+
337
+
338
+
339
+
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+
349
+
350
+ cart respond
351
+
352
+ import gradio as gr
353
+ import pandas as pd
354
+ import asyncio
355
+ import tempfile
356
+ from edge_tts import Communicate
357
+
358
+ # Global cart to store ordered items
359
+ cart = []
360
+
361
+ # Load Menu Data
362
+ def load_menu():
363
+ menu_file = "menu.xlsx" # Ensure this file exists in the same directory
364
+ try:
365
+ return pd.read_excel(menu_file)
366
+ except Exception as e:
367
+ raise ValueError(f"Error loading menu file: {e}")
368
+
369
+ # Generate Text-to-Speech Response
370
+ async def generate_tts_response(text):
371
+ communicate = Communicate(text, "en-US-JennyNeural")
372
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
373
+ await communicate.save(tmp_file.name)
374
+ return tmp_file.name
375
+
376
+ # Search for Dish
377
+ def search_dish(dish_name):
378
  menu_data = load_menu()
379
+ dish = menu_data[menu_data["Dish Name"].str.contains(dish_name, case=False, na=False)]
380
+ return dish
381
 
382
+ # Handle Voice Commands
383
+ async def handle_voice_command(audio_path, preference):
384
+ transcription = transcribe_audio(audio_path)
385
+ transcription = transcription.lower()
386
+
387
+ if transcription.startswith("order"):
388
+ dish_name = transcription.replace("order", "").strip()
389
+ dish = search_dish(dish_name)
390
+
391
+ if not dish.empty:
392
+ # Add to cart
393
+ item_name = dish.iloc[0]["Dish Name"]
394
+ item_price = dish.iloc[0]["Price ($)"]
395
+ cart.append({"name": item_name, "price": item_price})
396
+
397
+ confirmation_text = f"{item_name} is available for ${item_price}. Order confirmed and added to cart."
398
+ audio_response = await generate_tts_response(confirmation_text)
399
+ return audio_response, preference, f"Order: {confirmation_text}"
400
  else:
401
+ unavailable_text = f"Sorry, {dish_name} is not available in the menu."
402
+ audio_response = await generate_tts_response(unavailable_text)
403
+ return audio_response, preference, unavailable_text
404
 
405
+ if "menu items" in transcription:
406
+ menu_text = list_menu_items(preference)
407
+ audio_response = await generate_tts_response(menu_text)
408
+ return audio_response, preference, f"Menu: {menu_text}"
409
 
410
+ if "stop" in transcription:
411
+ return None, preference, "Stop command recognized."
412
+ elif "start" in transcription:
413
+ return None, preference, "Start command recognized."
414
 
415
+ # Update preference
416
+ if "vegetarian" in transcription:
417
+ preference = "Vegetarian"
418
+ elif "non-veg" in transcription or "halal" in transcription:
419
+ preference = "Halal/Non-Veg"
420
+ elif "guilt-free" in transcription:
421
+ preference = "Guilt-Free"
422
+ else:
423
+ preference = "All"
424
+
425
+ preference_text = f"Updated preference to {preference}."
426
+ audio_response = await generate_tts_response(preference_text)
427
+ return audio_response, preference, preference_text
428
+
429
+ # List Menu Items
430
+ def list_menu_items(preference):
431
+ menu_data = load_menu()
432
+ if preference == "Halal/Non-Veg":
433
+ filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
434
+ elif preference == "Vegetarian":
435
+ filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
436
+ elif preference == "Guilt-Free":
437
+ filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
438
+ else:
439
+ filtered_data = menu_data
440
+
441
+ # Create a textual summary for speech
442
+ text_summary = ""
443
+ for _, item in filtered_data.iterrows():
444
+ text_summary += f"{item['Dish Name']} for ${item['Price ($)']}. "
445
+
446
+ if not text_summary:
447
+ text_summary = "No items available in this category."
448
+
449
+ return text_summary
450
 
451
+ # Transcribe Audio Placeholder
452
+ def transcribe_audio(audio_path):
453
+ # Replace with actual transcription logic (e.g., Whisper API or Google Speech-to-Text)
454
+ return "order veg biryani"
 
455
 
456
+ # Gradio App
457
+ def app():
458
  with gr.Blocks() as demo:
459
+ with gr.Row():
460
+ gr.Markdown("## Welcome to the Menu")
461
 
 
462
  with gr.Row():
463
+ audio_input = gr.Audio(label="Speak your preference or order", type="filepath")
464
+ tts_output = gr.Audio(label="Assistant Response", autoplay=True)
465
+ preference = gr.Textbox(label="Preference", value="All", interactive=False)
466
+ menu_output = gr.Textbox(label="Transcript", value="")
467
+ cart_output = gr.Textbox(label="Cart", value="Your cart is empty.", interactive=False)
468
+
469
+ # Update outputs dynamically
470
+ audio_input.change(
471
+ handle_voice_command,
472
+ inputs=[audio_input, preference],
473
+ outputs=[tts_output, preference, menu_output],
474
+ )
475
+
476
+ # Update cart content dynamically
477
+ def update_cart():
478
+ if cart:
479
+ cart_content = "\n".join([f"{item['name']} - ${item['price']}" for item in cart])
480
+ else:
481
+ cart_content = "Your cart is empty."
482
+ return cart_content
483
+
484
+ gr.Button("Update Cart").click(
485
+ update_cart,
486
+ inputs=[],
487
+ outputs=cart_output
488
  )
489
 
490
  return demo
 
492
  if __name__ == "__main__":
493
  demo = app()
494
  demo.launch()
495
+
496
+
497
+
498
+
499
+
500
+
501
+
502
+
503
+
504
+
505
+
506
+