geethareddy commited on
Commit
91ca779
·
verified ·
1 Parent(s): 95721b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -49
app.py CHANGED
@@ -1,59 +1,72 @@
1
  import gradio as gr
2
- import json
3
 
 
 
 
 
 
 
 
4
 
5
- # Load menu data
6
- def load_menu_data():
7
- with open("data/menu.json", "r") as file:
8
- return json.load(file)
9
 
 
 
 
 
 
 
 
 
 
10
 
11
- # Function to render the popup card
12
- def render_popup(item):
13
- return f"""
14
- <div class='popup'>
15
- <div class='popup-header'>
16
- <h2>{item['name']}</h2>
17
- <button class='close-btn' onclick="closePopup()">×</button>
 
 
 
 
 
 
 
18
  </div>
19
- <img src='{item['image']}' alt='{item['name']}' class='popup-image'>
20
- <p>{item['description']}</p>
21
- <div class='nutrition-info'>
22
- <p><strong>Nutrition:</strong> {item['nutrition']}</p>
23
- </div>
24
- <button class='add-btn'>Add to Order (${item['price']})</button>
25
- </div>
26
- """
27
-
28
-
29
- # Gradio UI
30
- def menu_ui():
31
- menu_data = load_menu_data()
32
-
33
- with gr.Blocks(css="assets/style.css") as app:
34
- gr.Markdown("<h1>Dynamic Menu with Preferences</h1>")
35
-
36
- # Preferences Section
37
- with gr.Row():
38
- gr.Radio(["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"], label="Choose a Preference", value="All")
39
-
40
- # Menu Items
41
- menu_items = gr.Markdown()
42
-
43
- for item in menu_data:
44
- with gr.Row():
45
- with gr.Column(scale=3):
46
- gr.Markdown(f"**{item['name']}** \n${item['price']} \n{item['description']}")
47
- with gr.Column(scale=1):
48
- gr.Image(item['image'], elem_id="food-image")
49
- gr.Button("Add").click(lambda x=item: render_popup(x), outputs=menu_items)
50
-
51
- # Popup card
52
- menu_items.render()
53
-
54
- return app
55
 
 
56
 
57
  # Run the app
58
  if __name__ == "__main__":
59
- menu_ui().launch()
 
 
1
  import gradio as gr
2
+ import pandas as pd
3
 
4
+ # Function to load the menu data from Excel
5
+ def load_menu():
6
+ menu_file = "menu.xlsx" # Ensure this file exists in the same directory
7
+ try:
8
+ return pd.read_excel(menu_file)
9
+ except Exception as e:
10
+ raise ValueError(f"Error loading menu file: {e}")
11
 
12
+ # Function to filter menu items based on preference
13
+ def filter_menu(preference):
14
+ # Load menu data
15
+ menu_data = load_menu()
16
 
17
+ # Define filter conditions
18
+ if preference == "Halal/Non-Veg":
19
+ filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
20
+ elif preference == "Vegetarian":
21
+ filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
22
+ elif preference == "Guilt-Free":
23
+ filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
24
+ else: # Default to "All"
25
+ filtered_data = menu_data
26
 
27
+ # Generate HTML for the menu
28
+ html_content = ""
29
+ for _, item in filtered_data.iterrows():
30
+ html_content += f"""
31
+ <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);">
32
+ <div style="flex: 1; margin-right: 15px;">
33
+ <h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
34
+ <p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
35
+ <p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
36
+ </div>
37
+ <div style="flex-shrink: 0; text-align: center;">
38
+ <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
39
+ <button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button>
40
+ </div>
41
  </div>
42
+ """
43
+ return html_content
44
+
45
+ # Gradio app definition
46
+ def app():
47
+ with gr.Blocks(title="Dynamic Menu with Filters") as demo:
48
+ gr.Markdown("## Dynamic Menu with Preferences")
49
+
50
+ # Radio button for selecting preference
51
+ selected_preference = gr.Radio(
52
+ choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
53
+ value="All",
54
+ label="Choose a Preference",
55
+ )
56
+
57
+ # Output area for menu items
58
+ menu_output = gr.HTML(value=filter_menu("All"))
59
+
60
+ # Define interactivity
61
+ selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
62
+
63
+ # Layout
64
+ gr.Row([selected_preference])
65
+ gr.Row(menu_output)
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ return demo
68
 
69
  # Run the app
70
  if __name__ == "__main__":
71
+ demo = app()
72
+ demo.launch()