Spaces:
Build error
Build error
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778import pandas as pdimport gradio as gr# Load menu data from the Excel filedef load_menu(file_path="menu.xlsx"):menu_data = pd.read_excel(file_path)grouped_menu = menu_data.groupby("Category")menu = {category: items.to_dict(orient="records") for category, items in grouped_menu}return menu# Generate HTML content for the menudef generate_menu_html(menu):html_content = ""for category, items in menu.items():html_content += f"<h2>{category}</h2><div style='margin-bottom: 20px;'>"for item in items:html_content += f"""<div style="border: 1px solid #ddd; margin: 10px; padding: 10px; border-radius: 8px; display: inline-block; width: 200px; vertical-align: top; text-align: center;"><img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 150px; height: 100px; object-fit: cover; margin-bottom: 10px;"><h4>{item['Dish Name']}</h4><p>Price: ${item['Price']}</p><button onclick="showDetails('{item['Dish Name']}')">Add</button></div>"""html_content += "</div>"return html_content# Fetch dish details for the pop-up carddef fetch_dish_details(dish_name, menu_data_path="menu.xlsx"):menu_data = pd.read_excel(menu_data_path)item = menu_data[menu_data["Dish Name"] == dish_name].to_dict(orient="records")if item:details = item[0]html = f"""<h2>{details['Dish Name']}</h2><img src="{details['Image URL']}" alt="{details['Dish Name']}" style="width: 200px; margin-bottom: 20px;"><p><strong>Description:</strong> {details['Description']}</p><p><strong>Ingredients:</strong> {details['Ingredients']}</p><p><strong>Allergen Info:</strong> {details['Allergen Info']}</p><p><strong>Recommended Items:</strong> {details['Recommended Items']}</p><p><strong>Spice Levels:</strong> {details['Spice Levels']}</p>"""return htmlelse:return "<p style='color: red;'>Dish not found.</p>"# Gradio interface for the main menu and dish detailsdef main_interface():menu = load_menu()menu_html = generate_menu_html(menu)return menu_htmldef detail_interface(dish_name):details_html = fetch_dish_details(dish_name)return details_html# Gradio Appwith gr.Blocks() as app:with gr.Row():gr.Markdown("# Dynamic Menu Display")with gr.Row():menu_output = gr.HTML()detail_output = gr.HTML()with gr.Row():dish_name_input = gr.Textbox(label="Dish Name", placeholder="Enter dish name to view details.")fetch_button = gr.Button("Fetch Details")fetch_button.click(fn=detail_interface, inputs=[dish_name_input], outputs=[detail_output])menu_output.value = main_interface()# Launch the appif __name__ == "__main__":app.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import pandas as pdimport gradio as gr# Load the menu data from the Excel filedef load_menu(file_path="menu.xlsx"):menu_data = pd.read_excel(file_path)return menu_data# Filter the menu based on user preferencedef filter_menu(preference):menu_data = load_menu()if preference != "All":filtered_data = menu_data[menu_data["Category"] == preference]else:filtered_data = menu_data# Generate HTML for filtered menuhtml_content = ""for _, item in filtered_data.iterrows():html_content += f"""<div style="border: 1px solid #ddd; margin: 10px; padding: 10px; border-radius: 8px; display: flex; align-items: center;"><img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; object-fit: cover; margin-right: 10px;"><div><h4>{item['Dish Name']}</h4><p>{item['Description']}</p><p><strong>Price:</strong> ${item['Price']}</p><button style="background-color: #28a745; color: white; border: none; padding: 5px 10px; border-radius: 5px; cursor: pointer;">Add</button></div></div>"""return html_content# Gradio app definitiondef app():preferences = ["All", "Vegan", "Halal", "Guilt-Free"]with gr.Blocks() as demo:gr.Markdown("# Dynamic Menu with Preferences")# Radio buttons for preferencesselected_preference = gr.Radio(preferences, label="Choose a Preference")# Display the filtered menumenu_output = gr.HTML()selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])# Initial menu load (all items)menu_output.value = filter_menu("All")return demo# Run the appif __name__ == "__main__":demo = app()demo.launch()
app.py+38-60