import gradio as gr # Persistent cart to store items and customizations cart = {"items": [], "customizations": []} # Menu Items menu = { "Bread": ["Roti", "Butter Naan", "Plain Naan", "Garlic Naan", "Chilly Naan"], "Veg Curries": ["Paneer Butter Masala", "Kadai Paneer", "Dum Ka Paneer", "Mixed Veg Curry", "Dal Makhani"], "Non-Veg Curries": ["Chicken Curry", "Mutton Curry", "Fish Curry", "Prawns Curry"], "Biryani": ["Veg Biryani", "Paneer Biryani", "Chicken Biryani", "Mutton Biryani", "Fish Biryani", "Prawns Biryani"] } # Function to generate suggestions based on selected items def generate_suggestions(): selected_categories = set() for item in cart['items']: for category, items in menu.items(): if item in items: selected_categories.add(category) # Identify missing categories missing_categories = set(menu.keys()) - selected_categories # Suggest items from missing categories suggestions = [] for category in missing_categories: suggestions.extend(menu[category]) return suggestions # Home Page Logic def home_page(items): if items: for item in items: # Prevent duplicates if item not in cart['items']: cart['items'].append(item) suggestions = generate_suggestions() print(f"Selected Items: {cart['items']}") # Debugging output print(f"Generated Suggestions: {suggestions}") # Debugging output return ( f"Selected: {', '.join(items)}. Redirecting to Suggestions...", gr.update(choices=suggestions), # Dynamically update suggestion list gr.update(visible=False), # Hide Home Page gr.update(visible=True) # Show Suggestion Page ) return "Please select at least one item to proceed.", gr.update(), gr.update(), gr.update() # Suggestion Page Logic # Suggestion Page Logic def suggestion_page(selected_suggestions, custom_ingredients, custom_text, action): if selected_suggestions: for item in selected_suggestions: # Prevent duplicates if item not in cart['items']: cart['items'].append(item) if custom_ingredients: cart["customizations"].append(", ".join(custom_ingredients)) if custom_text: cart["customizations"].append(custom_text) if action == "continue": return ( "Returning to Home Page...", [], # Clear suggestions gr.update(visible=False), # Hide Suggestion Page gr.update(visible=True) # Show Home Page ) elif action == "place_order": return place_order() # Final Order Summary def place_order(): order_summary = f"Your Order:\n- Items: {', '.join(cart['items'])}\n- Customizations: {', '.join(cart['customizations'])}" return order_summary # Gradio UI Setup with gr.Blocks(css="styles.css") as demo: # Home Page with gr.Row(visible=True) as home_page_block: gr.Markdown("### Welcome to the Restaurant App") selected_items = gr.CheckboxGroup( menu['Bread'] + menu['Veg Curries'] + menu['Non-Veg Curries'] + menu['Biryani'], label="Select Items to Proceed" ) home_message = gr.Textbox(label="Message") home_btn = gr.Button("Select Items") place_order_home = gr.Button("Place Your Order") # Suggestion Page with gr.Row(visible=False) as suggestion_page_block: gr.Markdown("### Suggestion Page") suggestion_items = gr.CheckboxGroup([], label="Select Suggestions") custom_ingredients = gr.CheckboxGroup( ["Extra Cheese", "Extra Spicy", "Less Oil", "No Onions", "No Garlic"], label="Customize Your Dish" ) custom_text = gr.Textbox(label="Write Your Custom Dish Preference") suggestion_message = gr.Textbox(label="Message") continue_btn = gr.Button("Continue") place_order_btn = gr.Button("Place Your Order") # Output Block for Final Order final_output = gr.Textbox(label="Order Summary") # Event Handlers home_btn.click( home_page, inputs=selected_items, outputs=[home_message, suggestion_items, home_page_block, suggestion_page_block] ) continue_btn.click( suggestion_page, inputs=[suggestion_items, custom_ingredients, custom_text, gr.Textbox(value="continue")], outputs=[suggestion_message, suggestion_items, suggestion_page_block, home_page_block] ) place_order_btn.click( suggestion_page, inputs=[suggestion_items, custom_ingredients, custom_text, gr.Textbox(value="place_order")], outputs=final_output ) place_order_home.click( place_order, outputs=final_output ) demo.launch()