Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,14 @@ 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(
|
| 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 |
menu_data = load_menu()
|
| 15 |
-
|
| 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":
|
|
@@ -34,91 +32,72 @@ def filter_menu(preference):
|
|
| 34 |
</div>
|
| 35 |
<div style="flex-shrink: 0; text-align: center;">
|
| 36 |
<img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
|
| 37 |
-
<button value="{item['Dish Name']}" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">
|
| 38 |
</div>
|
| 39 |
</div>
|
| 40 |
"""
|
| 41 |
return html_content
|
| 42 |
|
| 43 |
-
# Function to render
|
| 44 |
def render_dish_details(dish_name):
|
| 45 |
-
print(f"Dish name received: {dish_name}") # Debugging
|
| 46 |
menu_data = load_menu()
|
| 47 |
try:
|
| 48 |
dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
|
| 49 |
-
print(f"Dish details: {dish}") # Debugging
|
| 50 |
return (
|
| 51 |
-
dish["Image URL"],
|
| 52 |
-
|
| 53 |
-
dish["Description"]
|
| 54 |
-
f"${dish['Price ($)']}"
|
| 55 |
)
|
| 56 |
except IndexError:
|
| 57 |
raise ValueError(f"Dish '{dish_name}' not found!")
|
| 58 |
|
| 59 |
-
# Gradio app
|
| 60 |
def app():
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
-
gr.Markdown("##
|
| 63 |
-
|
| 64 |
-
# Track selected dish
|
| 65 |
-
selected_dish = gr.State("")
|
| 66 |
|
| 67 |
# Menu page
|
| 68 |
preference_selector = gr.Radio(
|
| 69 |
choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
|
| 70 |
value="All",
|
| 71 |
-
label="
|
| 72 |
-
)
|
| 73 |
-
menu_html = gr.HTML(value=filter_menu("All"))
|
| 74 |
-
|
| 75 |
-
# Suggestion page (hidden by default)
|
| 76 |
-
suggestion_page = gr.Column(visible=False)
|
| 77 |
-
dish_image = gr.Image(label="Dish Image")
|
| 78 |
-
dish_name = gr.Markdown()
|
| 79 |
-
dish_description = gr.Markdown()
|
| 80 |
-
dish_price = gr.Markdown()
|
| 81 |
-
spice_level = gr.Radio(
|
| 82 |
-
choices=["Mild", "Medium", "Spicy"],
|
| 83 |
-
label="Choose Spice Level"
|
| 84 |
-
)
|
| 85 |
-
extras = gr.CheckboxGroup(
|
| 86 |
-
choices=["Extra Raita", "Extra Salan", "Extra Fried Onion"],
|
| 87 |
-
label="Choose Extras"
|
| 88 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
quantity = gr.Number(value=1, label="Quantity", interactive=True)
|
| 90 |
special_instructions = gr.Textbox(label="Special Instructions", placeholder="Add any requests...")
|
| 91 |
add_to_cart_button = gr.Button("Add to Bag")
|
| 92 |
-
|
| 93 |
|
| 94 |
-
# Cart
|
| 95 |
cart_state = gr.State([])
|
| 96 |
cart_output = gr.HTML(value="Your cart is empty.")
|
| 97 |
|
| 98 |
-
#
|
| 99 |
def update_menu(preference):
|
| 100 |
return filter_menu(preference)
|
| 101 |
|
| 102 |
-
def
|
| 103 |
try:
|
| 104 |
img, name, desc, price = render_dish_details(dish_name)
|
| 105 |
return (
|
| 106 |
-
gr.update(visible=False),
|
| 107 |
gr.update(visible=True),
|
| 108 |
-
img,
|
| 109 |
-
name,
|
| 110 |
-
desc,
|
| 111 |
-
price
|
| 112 |
)
|
| 113 |
except ValueError as e:
|
| 114 |
-
return (
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
f"Error: {str(e)}",
|
| 119 |
-
"",
|
| 120 |
-
""
|
| 121 |
-
)
|
| 122 |
|
| 123 |
def add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart):
|
| 124 |
cart.append({
|
|
@@ -134,46 +113,26 @@ def app():
|
|
| 134 |
)
|
| 135 |
return cart, cart_html
|
| 136 |
|
| 137 |
-
#
|
| 138 |
-
preference_selector.change(
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
menu_html.change(
|
| 145 |
-
show_suggestion,
|
| 146 |
-
inputs=[selected_dish],
|
| 147 |
-
outputs=[menu_html, suggestion_page, dish_image, dish_name, dish_description, dish_price]
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
add_to_cart_button.click(
|
| 151 |
-
add_to_cart,
|
| 152 |
-
inputs=[dish_name, spice_level, extras, quantity, special_instructions, cart_state],
|
| 153 |
-
outputs=[cart_state, cart_output]
|
| 154 |
-
)
|
| 155 |
-
|
| 156 |
-
back_to_menu_button.click(
|
| 157 |
-
lambda: (gr.update(visible=True), gr.update(visible=False)),
|
| 158 |
-
outputs=[menu_html, suggestion_page]
|
| 159 |
-
)
|
| 160 |
|
| 161 |
# Layout
|
| 162 |
with gr.Row():
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
dish_price
|
| 170 |
spice_level
|
| 171 |
extras
|
| 172 |
quantity
|
| 173 |
special_instructions
|
| 174 |
add_to_cart_button
|
| 175 |
-
|
| 176 |
-
|
| 177 |
with gr.Row():
|
| 178 |
cart_output
|
| 179 |
|
|
@@ -181,5 +140,4 @@ def app():
|
|
| 181 |
|
| 182 |
|
| 183 |
if __name__ == "__main__":
|
| 184 |
-
|
| 185 |
-
demo.launch()
|
|
|
|
| 3 |
|
| 4 |
# Function to load the menu data from Excel
|
| 5 |
def load_menu():
|
|
|
|
| 6 |
try:
|
| 7 |
+
return pd.read_excel("menu.xlsx") # Ensure menu.xlsx is in the same directory
|
| 8 |
except Exception as e:
|
| 9 |
raise ValueError(f"Error loading menu file: {e}")
|
| 10 |
|
| 11 |
# Function to filter menu items based on preference
|
| 12 |
def filter_menu(preference):
|
| 13 |
menu_data = load_menu()
|
|
|
|
| 14 |
if preference == "Halal/Non-Veg":
|
| 15 |
filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
|
| 16 |
elif preference == "Vegetarian":
|
|
|
|
| 32 |
</div>
|
| 33 |
<div style="flex-shrink: 0; text-align: center;">
|
| 34 |
<img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
|
| 35 |
+
<button value="{item['Dish Name']}" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">View Details</button>
|
| 36 |
</div>
|
| 37 |
</div>
|
| 38 |
"""
|
| 39 |
return html_content
|
| 40 |
|
| 41 |
+
# Function to render detailed view of a dish
|
| 42 |
def render_dish_details(dish_name):
|
|
|
|
| 43 |
menu_data = load_menu()
|
| 44 |
try:
|
| 45 |
dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
|
|
|
|
| 46 |
return (
|
| 47 |
+
dish["Image URL"], # Image
|
| 48 |
+
dish_name, # Dish name
|
| 49 |
+
dish["Description"],# Description
|
| 50 |
+
f"${dish['Price ($)']}" # Price
|
| 51 |
)
|
| 52 |
except IndexError:
|
| 53 |
raise ValueError(f"Dish '{dish_name}' not found!")
|
| 54 |
|
| 55 |
+
# Main Gradio app
|
| 56 |
def app():
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
+
gr.Markdown("## Menu with Suggestions")
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# Menu page
|
| 61 |
preference_selector = gr.Radio(
|
| 62 |
choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
|
| 63 |
value="All",
|
| 64 |
+
label="Filter Menu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
)
|
| 66 |
+
menu_output = gr.HTML(value=filter_menu("All"))
|
| 67 |
+
|
| 68 |
+
# Modal window (hidden by default)
|
| 69 |
+
modal_window = gr.Column(visible=False)
|
| 70 |
+
modal_image = gr.Image(label="Dish Image")
|
| 71 |
+
modal_name = gr.Markdown()
|
| 72 |
+
modal_description = gr.Markdown()
|
| 73 |
+
modal_price = gr.Markdown()
|
| 74 |
+
spice_level = gr.Radio(choices=["Mild", "Medium", "Spicy"], label="Choose Spice Level")
|
| 75 |
+
extras = gr.CheckboxGroup(choices=["Extra Raita", "Extra Salan", "Extra Fried Onion"], label="Choose Extras")
|
| 76 |
quantity = gr.Number(value=1, label="Quantity", interactive=True)
|
| 77 |
special_instructions = gr.Textbox(label="Special Instructions", placeholder="Add any requests...")
|
| 78 |
add_to_cart_button = gr.Button("Add to Bag")
|
| 79 |
+
close_modal_button = gr.Button("Close")
|
| 80 |
|
| 81 |
+
# Cart
|
| 82 |
cart_state = gr.State([])
|
| 83 |
cart_output = gr.HTML(value="Your cart is empty.")
|
| 84 |
|
| 85 |
+
# Handlers
|
| 86 |
def update_menu(preference):
|
| 87 |
return filter_menu(preference)
|
| 88 |
|
| 89 |
+
def show_modal(dish_name):
|
| 90 |
try:
|
| 91 |
img, name, desc, price = render_dish_details(dish_name)
|
| 92 |
return (
|
|
|
|
| 93 |
gr.update(visible=True),
|
| 94 |
+
img, name, desc, price
|
|
|
|
|
|
|
|
|
|
| 95 |
)
|
| 96 |
except ValueError as e:
|
| 97 |
+
return gr.update(value=f"Error: {str(e)}")
|
| 98 |
+
|
| 99 |
+
def close_modal():
|
| 100 |
+
return gr.update(visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
def add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart):
|
| 103 |
cart.append({
|
|
|
|
| 113 |
)
|
| 114 |
return cart, cart_html
|
| 115 |
|
| 116 |
+
# Events
|
| 117 |
+
preference_selector.change(update_menu, inputs=[preference_selector], outputs=[menu_output])
|
| 118 |
+
menu_output.change(show_modal, inputs=[menu_output], outputs=[modal_window, modal_image, modal_name, modal_description, modal_price])
|
| 119 |
+
close_modal_button.click(close_modal, outputs=[modal_window])
|
| 120 |
+
add_to_cart_button.click(add_to_cart, inputs=[modal_name, spice_level, extras, quantity, special_instructions, cart_state], outputs=[cart_state, cart_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
# Layout
|
| 123 |
with gr.Row():
|
| 124 |
+
menu_output
|
| 125 |
+
with modal_window:
|
| 126 |
+
modal_image
|
| 127 |
+
modal_name
|
| 128 |
+
modal_description
|
| 129 |
+
modal_price
|
|
|
|
| 130 |
spice_level
|
| 131 |
extras
|
| 132 |
quantity
|
| 133 |
special_instructions
|
| 134 |
add_to_cart_button
|
| 135 |
+
close_modal_button
|
|
|
|
| 136 |
with gr.Row():
|
| 137 |
cart_output
|
| 138 |
|
|
|
|
| 140 |
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
| 143 |
+
app().launch()
|
|
|