Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,50 +26,45 @@ cart = []
|
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
-
# Function to add item to cart
|
| 30 |
-
def add_to_cart(category, item, quantity):
|
| 31 |
-
# Check if category and item are selected
|
| 32 |
-
if not category:
|
| 33 |
-
return "Please select a category.", [(i["category"], i["item"], i["quantity"], i["price"]) for i in cart], sum([i['price'] for i in cart])
|
| 34 |
-
if not item:
|
| 35 |
-
return "Item not selected.", [(i["category"], i["item"], i["quantity"], i["price"]) for i in cart], sum([i['price'] for i in cart])
|
| 36 |
-
|
| 37 |
-
# Handle Alfaham special case with list prices
|
| 38 |
-
try:
|
| 39 |
-
price = menu_items[category][item] if category != "Alfaham" else menu_items[category][item][min(quantity, len(menu_items[category][item])) - 1]
|
| 40 |
-
except (IndexError, KeyError) as e:
|
| 41 |
-
return "Item or quantity selection is invalid.", [(i["category"], i["item"], i["quantity"], i["price"]) for i in cart], sum([i['price'] for i in cart])
|
| 42 |
-
|
| 43 |
-
item_total = price * quantity
|
| 44 |
-
cart.append({"category": category, "item": item, "quantity": quantity, "price": item_total})
|
| 45 |
-
|
| 46 |
-
# Calculate total price
|
| 47 |
-
total_price = sum([i['price'] for i in cart])
|
| 48 |
-
cart_summary = [(i["category"], i["item"], i["quantity"], i["price"]) for i in cart]
|
| 49 |
-
|
| 50 |
-
return "Item added to cart!", cart_summary, total_price
|
| 51 |
-
|
| 52 |
|
| 53 |
|
| 54 |
# Function to remove item from cart
|
| 55 |
def remove_from_cart(index):
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
#
|
|
|
|
|
|
|
|
|
|
| 60 |
total_price = sum([i['price'] for i in cart])
|
| 61 |
-
cart_summary = [(i["category"], i["item"], i["quantity"], i["price"]) for i in cart]
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
return cart_summary, total_price
|
| 64 |
|
| 65 |
# Function to place final order
|
| 66 |
def place_order(name, phone, hostel_name, confirmation):
|
| 67 |
-
if
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
if confirmation != "Yes":
|
| 71 |
return "Order was not placed."
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
order_data = {
|
| 74 |
"name": name,
|
| 75 |
"phone": phone,
|
|
@@ -79,26 +74,51 @@ def place_order(name, phone, hostel_name, confirmation):
|
|
| 79 |
"date": datetime.now().strftime("%Y-%m-%d") # Store date for easier querying
|
| 80 |
}
|
| 81 |
|
|
|
|
| 82 |
db.collection("orders").add(order_data)
|
| 83 |
cart.clear() # Clear cart after placing order
|
| 84 |
return "Order placed successfully!"
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
# Gradio Interface
|
| 87 |
def gradio_interface():
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
# Display the menu image at the top
|
| 90 |
gr.Image("menu.png", label="Menu", interactive=False) # Replace "menu.jpg" with your actual menu image path
|
| 91 |
|
| 92 |
-
|
| 93 |
|
| 94 |
name = gr.Textbox(label="Name")
|
| 95 |
phone = gr.Textbox(label="Phone Number")
|
| 96 |
-
hostel_name = gr.Textbox(label="Hostel Name")
|
| 97 |
|
| 98 |
# Set "Shawarma" as the default selected category
|
| 99 |
category = gr.Dropdown(list(menu_items.keys()), label="Select Category", value="Shawarma")
|
| 100 |
item = gr.Dropdown(list(menu_items["Shawarma"].keys()), label="Select Item")
|
| 101 |
-
quantity = gr.Slider(1, 10, step=1, label="Quantity", value=1)
|
| 102 |
|
| 103 |
# Update items dropdown based on selected category
|
| 104 |
def update_items(selected_category):
|
|
@@ -111,7 +131,9 @@ def gradio_interface():
|
|
| 111 |
# Add to Cart button
|
| 112 |
add_button = gr.Button("Add to Cart")
|
| 113 |
cart_status = gr.Textbox(label="Status")
|
| 114 |
-
|
|
|
|
|
|
|
| 115 |
total_price_display = gr.Number(label="Total Price", interactive=False)
|
| 116 |
|
| 117 |
# On Add to Cart button click
|
|
@@ -122,13 +144,14 @@ def gradio_interface():
|
|
| 122 |
)
|
| 123 |
|
| 124 |
# Remove from Cart button
|
| 125 |
-
remove_index = gr.Number(label="Index to Remove", interactive=True)
|
| 126 |
remove_button = gr.Button("Remove from Cart")
|
| 127 |
|
|
|
|
| 128 |
remove_button.click(
|
| 129 |
fn=remove_from_cart,
|
| 130 |
inputs=remove_index,
|
| 131 |
-
outputs=[cart_display, total_price_display]
|
| 132 |
)
|
| 133 |
|
| 134 |
# Place Order button
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
# Function to remove item from cart
|
| 32 |
def remove_from_cart(index):
|
| 33 |
+
# Convert the index to 0-based for list access and check if it's within bounds
|
| 34 |
+
item_index = int(index) - 1
|
| 35 |
+
if item_index < 0 or item_index >= len(cart):
|
| 36 |
+
# Return a message if the index is out of range
|
| 37 |
+
return "Invalid index. Please enter a valid index to remove an item.", [(idx + 1, i["category"], i["item"], i["quantity"], i["price"]) for idx, i in enumerate(cart)], sum([i['price'] for i in cart])
|
| 38 |
|
| 39 |
+
# Remove the item if the index is valid
|
| 40 |
+
cart.pop(item_index)
|
| 41 |
+
|
| 42 |
+
# Calculate the new total price and update the cart summary with the index starting from 1
|
| 43 |
total_price = sum([i['price'] for i in cart])
|
| 44 |
+
cart_summary = [(idx + 1, i["category"], i["item"], i["quantity"], i["price"]) for idx, i in enumerate(cart)]
|
| 45 |
+
|
| 46 |
+
return "Item removed successfully.", cart_summary, total_price
|
| 47 |
+
|
| 48 |
|
|
|
|
| 49 |
|
| 50 |
# Function to place final order
|
| 51 |
def place_order(name, phone, hostel_name, confirmation):
|
| 52 |
+
# Check if all required fields are filled
|
| 53 |
+
if not name:
|
| 54 |
+
return "Please enter your name."
|
| 55 |
+
if not phone:
|
| 56 |
+
return "Please enter your phone number."
|
| 57 |
+
if not hostel_name:
|
| 58 |
+
return "Please enter your hostel name."
|
| 59 |
+
|
| 60 |
+
# Check if the user confirmed the order
|
| 61 |
if confirmation != "Yes":
|
| 62 |
return "Order was not placed."
|
| 63 |
|
| 64 |
+
if not cart:
|
| 65 |
+
return "Your cart is empty. Please add items before placing the order."
|
| 66 |
+
|
| 67 |
+
# Prepare order data
|
| 68 |
order_data = {
|
| 69 |
"name": name,
|
| 70 |
"phone": phone,
|
|
|
|
| 74 |
"date": datetime.now().strftime("%Y-%m-%d") # Store date for easier querying
|
| 75 |
}
|
| 76 |
|
| 77 |
+
# Add order to Firestore
|
| 78 |
db.collection("orders").add(order_data)
|
| 79 |
cart.clear() # Clear cart after placing order
|
| 80 |
return "Order placed successfully!"
|
| 81 |
|
| 82 |
+
|
| 83 |
+
# Function to add item to cart
|
| 84 |
+
def add_to_cart(category, item, quantity):
|
| 85 |
+
# Check if category and item are selected
|
| 86 |
+
if not category:
|
| 87 |
+
return "Please select a category.", [(i["category"], i["item"], i["quantity"], i["price"]) for i in cart], sum([i['price'] for i in cart])
|
| 88 |
+
if not item:
|
| 89 |
+
return "Item not selected.", [(i["category"], i["item"], i["quantity"], i["price"]) for i in cart], sum([i['price'] for i in cart])
|
| 90 |
+
|
| 91 |
+
# Handle Alfaham special case with list prices
|
| 92 |
+
try:
|
| 93 |
+
price = menu_items[category][item] if category != "Alfaham" else menu_items[category][item][min(quantity, len(menu_items[category][item])) - 1]
|
| 94 |
+
except (IndexError, KeyError) as e:
|
| 95 |
+
return "Item or quantity selection is invalid.", [(i["category"], i["item"], i["quantity"], i["price"]) for i in cart], sum([i['price'] for i in cart])
|
| 96 |
+
|
| 97 |
+
item_total = price * quantity
|
| 98 |
+
cart.append({"category": category, "item": item, "quantity": quantity, "price": item_total})
|
| 99 |
+
|
| 100 |
+
# Calculate total price and add index to each row
|
| 101 |
+
total_price = sum([i['price'] for i in cart])
|
| 102 |
+
cart_summary = [(idx + 1, i["category"], i["item"], i["quantity"], i["price"]) for idx, i in enumerate(cart)]
|
| 103 |
+
|
| 104 |
+
return "Item added to cart!", cart_summary, total_price
|
| 105 |
+
|
| 106 |
# Gradio Interface
|
| 107 |
def gradio_interface():
|
| 108 |
with gr.Blocks() as demo:
|
| 109 |
# Display the menu image at the top
|
| 110 |
gr.Image("menu.png", label="Menu", interactive=False) # Replace "menu.jpg" with your actual menu image path
|
| 111 |
|
| 112 |
+
|
| 113 |
|
| 114 |
name = gr.Textbox(label="Name")
|
| 115 |
phone = gr.Textbox(label="Phone Number")
|
| 116 |
+
hostel_name = gr.Textbox(label="Hostel Name")
|
| 117 |
|
| 118 |
# Set "Shawarma" as the default selected category
|
| 119 |
category = gr.Dropdown(list(menu_items.keys()), label="Select Category", value="Shawarma")
|
| 120 |
item = gr.Dropdown(list(menu_items["Shawarma"].keys()), label="Select Item")
|
| 121 |
+
quantity = gr.Slider(1, 10, step=1, label="Quantity", value=1)
|
| 122 |
|
| 123 |
# Update items dropdown based on selected category
|
| 124 |
def update_items(selected_category):
|
|
|
|
| 131 |
# Add to Cart button
|
| 132 |
add_button = gr.Button("Add to Cart")
|
| 133 |
cart_status = gr.Textbox(label="Status")
|
| 134 |
+
|
| 135 |
+
# Include index in headers for cart display
|
| 136 |
+
cart_display = gr.Dataframe(value=[], headers=["Index", "Category", "Item", "Quantity", "Price"], interactive=False)
|
| 137 |
total_price_display = gr.Number(label="Total Price", interactive=False)
|
| 138 |
|
| 139 |
# On Add to Cart button click
|
|
|
|
| 144 |
)
|
| 145 |
|
| 146 |
# Remove from Cart button
|
| 147 |
+
remove_index = gr.Number(label="Index to Remove", interactive=True, value=1, minimum=1)
|
| 148 |
remove_button = gr.Button("Remove from Cart")
|
| 149 |
|
| 150 |
+
# Update remove_button click to handle index removal gracefully
|
| 151 |
remove_button.click(
|
| 152 |
fn=remove_from_cart,
|
| 153 |
inputs=remove_index,
|
| 154 |
+
outputs=[cart_status, cart_display, total_price_display]
|
| 155 |
)
|
| 156 |
|
| 157 |
# Place Order button
|