Spaces:
Build error
Build error
Update app.py
Browse files2 unmodified lines345678910111213141516171815 unmodified lines3435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231249 unmodified lines1341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791 unmodified line1811821831841852 unmodified lines# Function to load the menu data from Exceldef load_menu():menu_file = "menu.xlsx" # Ensure this file exists in the same directorytry:return pd.read_excel(menu_file)except Exception as e:raise ValueError(f"Error loading menu file: {e}")# Function to filter menu items based on preferencedef filter_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":15 unmodified lines</div><div style="flex-shrink: 0; text-align: center;"><img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;"><button value="{item['Dish Name']}" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button></div></div>"""return html_content# Function to render dish details for the suggestion pagedef render_dish_details(dish_name):print(f"Dish name received: {dish_name}") # Debuggingmenu_data = load_menu()try:dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]print(f"Dish details: {dish}") # Debuggingreturn (dish["Image URL"],f"## {dish_name}",dish["Description"],f"${dish['Price ($)']}")except IndexError:raise ValueError(f"Dish '{dish_name}' not found!")# Gradio app definitiondef app():with gr.Blocks() as demo:gr.Markdown("## Dynamic Menu with Preferences")# Track selected dishselected_dish = gr.State("")# Menu pagepreference_selector = gr.Radio(choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],value="All",label="Choose a Preference")menu_html = gr.HTML(value=filter_menu("All"))# Suggestion page (hidden by default)suggestion_page = gr.Column(visible=False)dish_image = gr.Image(label="Dish 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(value=1, label="Quantity", interactive=True)special_instructions = gr.Textbox(label="Special Instructions", placeholder="Add any requests...")add_to_cart_button = gr.Button("Add to Bag")back_to_menu_button = gr.Button("Back to Menu")# Cart state and displaycart_state = gr.State([])cart_output = gr.HTML(value="Your cart is empty.")# Functionsdef update_menu(preference):return filter_menu(preference)def show_suggestion(dish_name):try:img, name, desc, price = render_dish_details(dish_name)return (gr.update(visible=False),gr.update(visible=True),img,name,desc,price)except ValueError as e:return (gr.update(visible=True),gr.update(visible=False),"",f"Error: {str(e)}","","")def add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart):cart.append({9 unmodified lines)return cart, cart_html# Event Handlerspreference_selector.change(update_menu,inputs=[preference_selector],outputs=[menu_html])menu_html.change(show_suggestion,inputs=[selected_dish],outputs=[menu_html, suggestion_page, dish_image, dish_name, dish_description, dish_price])add_to_cart_button.click(add_to_cart,inputs=[dish_name, spice_level, extras, quantity, special_instructions, cart_state],outputs=[cart_state, cart_output])back_to_menu_button.click(lambda: (gr.update(visible=True), gr.update(visible=False)),outputs=[menu_html, suggestion_page])# Layoutwith gr.Row():menu_htmlwith suggestion_page:dish_imagedish_namedish_descriptiondish_pricespice_levelextrasquantityspecial_instructionsadd_to_cart_buttonback_to_menu_buttonwith gr.Row():cart_output1 unmodified lineif __name__ == "__main__":demo = app()demo.launch()2 unmodified lines34567891011121314151615 unmodified lines32333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021039 unmodified lines1131141151161171181191201211221231241251261271281291301311321331341351361371381 unmodified line1401411421432 unmodified lines# Function to load the menu data from Exceldef load_menu():try:return pd.read_excel("menu.xlsx") # Ensure menu.xlsx is in the same directoryexcept Exception as e:raise ValueError(f"Error loading menu file: {e}")# Function to filter menu items based on preferencedef filter_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":15 unmodified lines</div><div style="flex-shrink: 0; text-align: center;"><img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;"><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></div></div>"""return html_content# Function to render detailed view of a dishdef render_dish_details(dish_name):menu_data = load_menu()try:dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]return (dish["Image URL"], # Imagedish_name, # Dish namedish["Description"],# Descriptionf"${dish['Price ($)']}" # Price)except IndexError:raise ValueError(f"Dish '{dish_name}' not found!")# Main Gradio appdef app():with gr.Blocks() as demo:gr.Markdown("## Menu with Suggestions")# Menu pagepreference_selector = gr.Radio(choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],value="All",label="Filter Menu")menu_output = gr.HTML(value=filter_menu("All"))# Modal window (hidden by default)modal_window = gr.Column(visible=False)modal_image = gr.Image(label="Dish Image")modal_name = gr.Markdown()modal_description = gr.Markdown()modal_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(value=1, label="Quantity", interactive=True)special_instructions = gr.Textbox(label="Special Instructions", placeholder="Add any requests...")add_to_cart_button = gr.Button("Add to Bag")close_modal_button = gr.Button("Close")# Cartcart_state = gr.State([])cart_output = gr.HTML(value="Your cart is empty.")# Handlersdef update_menu(preference):return filter_menu(preference)def show_modal(dish_name):try:img, name, desc, price = render_dish_details(dish_name)return (gr.update(visible=True),img, name, desc, price)except ValueError as e:return gr.update(value=f"Error: {str(e)}")def close_modal():return gr.update(visible=False)def add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart):cart.append({9 unmodified lines)return cart, cart_html# Eventspreference_selector.change(update_menu, inputs=[preference_selector], outputs=[menu_output])menu_output.change(show_modal, inputs=[menu_output], outputs=[modal_window, modal_image, modal_name, modal_description, modal_price])close_modal_button.click(close_modal, outputs=[modal_window])add_to_cart_button.click(add_to_cart, inputs=[modal_name, spice_level, extras, quantity, special_instructions, cart_state], outputs=[cart_state, cart_output])# Layoutwith gr.Row():menu_outputwith modal_window:modal_imagemodal_namemodal_descriptionmodal_pricespice_levelextrasquantityspecial_instructionsadd_to_cart_buttonclose_modal_buttonwith gr.Row():cart_output1 unmodified lineif __name__ == "__main__":app().launch()
app.py+42-84