Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
# Menu data (simulate database)
|
| 5 |
+
menu_data = {
|
| 6 |
+
"Veg Samosa": {"price": 9, "suggestions": ["Green Chutney", "Masala Tea"]},
|
| 7 |
+
"Onion Pakoda": {"price": 10, "suggestions": ["Tamarind Sauce", "Hot Tea"]},
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
# Cart to store selected items
|
| 11 |
+
cart = []
|
| 12 |
+
|
| 13 |
+
def display_menu():
|
| 14 |
+
"""
|
| 15 |
+
Display menu items in a user-friendly format.
|
| 16 |
+
"""
|
| 17 |
+
menu_list = []
|
| 18 |
+
for item, details in menu_data.items():
|
| 19 |
+
menu_list.append(f"{item} - ${details['price']}")
|
| 20 |
+
return "\n".join(menu_list)
|
| 21 |
+
|
| 22 |
+
def add_to_cart(food_item, quantity):
|
| 23 |
+
"""
|
| 24 |
+
Add an item to the cart with quantity and suggestions.
|
| 25 |
+
"""
|
| 26 |
+
if food_item not in menu_data:
|
| 27 |
+
return "Error: Item not found in menu."
|
| 28 |
+
|
| 29 |
+
quantity = int(quantity)
|
| 30 |
+
cart.append({
|
| 31 |
+
"foodItem": food_item,
|
| 32 |
+
"quantity": quantity,
|
| 33 |
+
"price": menu_data[food_item]['price'] * quantity,
|
| 34 |
+
"suggestions": menu_data[food_item]['suggestions']
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
cart_summary = "\n".join([f"{item['foodItem']} (x{item['quantity']}) - ${item['price']}" for item in cart])
|
| 38 |
+
return f"{food_item} added to cart!\n\nCart Summary:\n{cart_summary}"
|
| 39 |
+
|
| 40 |
+
def view_cart():
|
| 41 |
+
"""
|
| 42 |
+
Display the current cart contents.
|
| 43 |
+
"""
|
| 44 |
+
if not cart:
|
| 45 |
+
return "Your cart is empty."
|
| 46 |
+
cart_summary = "\n".join([f"{item['foodItem']} (x{item['quantity']}) - ${item['price']}" for item in cart])
|
| 47 |
+
return f"Cart Summary:\n{cart_summary}"
|
| 48 |
+
|
| 49 |
+
# Gradio Interface
|
| 50 |
+
with gr.Blocks() as demo:
|
| 51 |
+
gr.Markdown("# Dynamic Food Menu")
|
| 52 |
+
|
| 53 |
+
# Display menu
|
| 54 |
+
menu_output = gr.Textbox(label="Menu", value=display_menu(), interactive=False)
|
| 55 |
+
|
| 56 |
+
# Input fields for adding to cart
|
| 57 |
+
with gr.Row():
|
| 58 |
+
food_item_input = gr.Textbox(label="Food Item", placeholder="Enter item name")
|
| 59 |
+
quantity_input = gr.Number(label="Quantity", value=1, precision=0)
|
| 60 |
+
add_button = gr.Button("Add to Cart")
|
| 61 |
+
|
| 62 |
+
# Output field for cart actions
|
| 63 |
+
cart_output = gr.Textbox(label="Cart", interactive=False)
|
| 64 |
+
|
| 65 |
+
# View cart button
|
| 66 |
+
view_cart_button = gr.Button("View Cart")
|
| 67 |
+
|
| 68 |
+
# Actions
|
| 69 |
+
add_button.click(add_to_cart, inputs=[food_item_input, quantity_input], outputs=cart_output)
|
| 70 |
+
view_cart_button.click(view_cart, outputs=cart_output)
|
| 71 |
+
|
| 72 |
+
# Launch Gradio App
|
| 73 |
+
demo.launch()
|