Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| # Load menu data from Excel | |
| def load_menu(): | |
| menu_file = "menu.xlsx" # Ensure this file exists in the same directory | |
| try: | |
| return pd.read_excel(menu_file) | |
| except Exception as e: | |
| raise ValueError(f"Error loading menu file: {e}") | |
| # Main menu view | |
| def render_menu(preference): | |
| menu_data = load_menu() | |
| if preference == "Halal/Non-Veg": | |
| filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)] | |
| elif preference == "Vegetarian": | |
| filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)] | |
| elif preference == "Guilt-Free": | |
| filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)] | |
| else: | |
| filtered_data = menu_data | |
| # Generate menu as a list of tuples (dish_name, button_label) | |
| return [{"label": f"{row['Dish Name']} - ${row['Price ($)']}", "value": row['Dish Name']} for _, row in filtered_data.iterrows()] | |
| # Dish details view | |
| def render_dish_details(dish_name): | |
| menu_data = load_menu() | |
| dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0] | |
| return ( | |
| dish["Image URL"], | |
| dish_name, | |
| dish["Description"], | |
| dish["Price ($)"] | |
| ) | |
| # Add to cart function | |
| def add_to_cart(dish_name, spice_level, extras, quantity, special_instructions, cart): | |
| cart.append({ | |
| "name": dish_name, | |
| "spice_level": spice_level, | |
| "extras": extras, | |
| "quantity": quantity, | |
| "instructions": special_instructions | |
| }) | |
| return cart, f"Added {dish_name} to cart!" | |
| # Gradio app | |
| def app(): | |
| with gr.Blocks() as demo: | |
| cart_state = gr.State([]) | |
| # Menu page | |
| menu_dropdown = gr.Dropdown( | |
| label="Select a dish to view details", | |
| choices=[], | |
| interactive=True, | |
| ) | |
| menu_output = gr.Dropdown(interactive=True) | |
| # Dish details view | |
| detailed_view = gr.Column(visible=False) | |
| dish_image = gr.Image() | |
| dish_name = gr.Markdown() | |
| dish_description = gr.Markdown() | |
| dish_price = gr.Markdown() | |
| spice_level = gr.Radio( | |
| choices=["Mild", "Medium", "Spicy"], | |
| label="Choose Spice Level", | |
| ) | |
| extras = gr.CheckboxGroup( | |
| choices=["Extra Raita", "Extra Salan", "Extra Fried Onion"], | |
| label="Choose Extras", | |
| ) | |
| quantity = gr.Number(label="Quantity", value=1, interactive=True) | |
| special_instructions = gr.Textbox( | |
| label="Special Instructions", | |
| placeholder="Add any special instructions here", | |
| ) | |
| add_button = gr.Button("Add to Cart") | |
| back_button = gr.Button("Back to Menu") | |
| cart_view = gr.HTML(value="Your cart is empty.") | |
| # Show the menu | |
| def update_menu(preference): | |
| return render_menu(preference) | |
| # Show dish details | |
| def show_dish_details(dish_name): | |
| img, name, desc, price = render_dish_details(dish_name) | |
| return ( | |
| gr.update(visible=False), | |
| gr.update(visible=True), | |
| img, | |
| f"## {name}", | |
| desc, | |
| f"**Price:** ${price}", | |
| ) | |
| # Add to cart | |
| def handle_add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart): | |
| updated_cart, message = add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart) | |
| cart_html = "<br>".join( | |
| [f"{item['quantity']}x {item['name']} - {item['spice_level']}" for item in updated_cart] | |
| ) | |
| return updated_cart, cart_html | |
| # Update menu based on preference | |
| menu_dropdown.change( | |
| update_menu, | |
| inputs=["All"], | |
| outputs=[menu_view()], | |
| ) | |
| # Show dish details when an item is selected | |
| menu_dropdown.change( | |
| show_dish_details, | |
| inputs=[menu_dropdown], | |
| outputs=[ | |
| menu_dropdown, | |
| detailed_view, | |
| dish_image, | |
| dish_name, | |
| dish_description, | |
| dish_price, | |
| ], | |
| ) | |
| # Handle adding items to the cart | |
| add_button.click( | |
| handle_add_to_cart, | |
| inputs=[ | |
| dish_name, | |
| spice_level, | |
| extras, | |
| quantity, | |
| special_instructions, | |
| cart_state, | |
| ], | |
| outputs=[cart_state, cart_view], | |
| ) | |
| # Back button to return to menu | |
| back_button.click( | |
| lambda: (gr.update(visible=True), gr.update(visible=False)), | |
| outputs=[menu_dropdown, detailed_view], | |
| ) | |
| # Layout for Gradio app | |
| with gr.Row(): | |
| gr.Markdown("## Menu") | |
| menu_dropdown | |
| cart_view | |
| with detailed_view: | |
| dish_image | |
| dish_name | |
| dish_description | |
| dish_price | |
| spice_level | |
| extras | |
| quantity | |
| special_instructions | |
| add_button | |
| back_button | |
| return demo | |
| if __name__ == "__main__": | |
| demo = app() | |
| demo.launch() | |