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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# -----------------------------
|
| 4 |
+
# Core data and logic
|
| 5 |
+
# -----------------------------
|
| 6 |
+
|
| 7 |
+
# Same inventory as in the notebook
|
| 8 |
+
Inventory = [
|
| 9 |
+
[1, "Apples", 250],
|
| 10 |
+
[2, "Cherry", 650],
|
| 11 |
+
[3, "Chickoo", 50],
|
| 12 |
+
[4, "Grapes", 90],
|
| 13 |
+
[5, "Mangoes", 150],
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
# Global cart (Space will run single-process)
|
| 17 |
+
YourCart = [] # each item is [product_id, name, price_per_kg]
|
| 18 |
+
|
| 19 |
+
def add_item_to_cart(item_id: int):
|
| 20 |
+
"""Add one unit of the given product ID to the cart."""
|
| 21 |
+
try:
|
| 22 |
+
item = Inventory[item_id - 1]
|
| 23 |
+
except IndexError:
|
| 24 |
+
return "Invalid item ID. Please enter a valid Product ID."
|
| 25 |
+
|
| 26 |
+
YourCart.append(item)
|
| 27 |
+
return f"The item {item[1]} has been added to the cart, and your shopping cart has been updated."
|
| 28 |
+
|
| 29 |
+
def remove_item_from_cart(cart_index: int):
|
| 30 |
+
"""Remove item by 1-based index from the cart."""
|
| 31 |
+
if not YourCart:
|
| 32 |
+
return "Invalid cart index or cart is empty."
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
removed_item = YourCart.pop(cart_index - 1)
|
| 36 |
+
return f"The item {removed_item[1]} has been removed from the cart, and your shopping cart has been updated."
|
| 37 |
+
except IndexError:
|
| 38 |
+
return "Invalid cart index or cart is empty."
|
| 39 |
+
|
| 40 |
+
def display_cart(cart):
|
| 41 |
+
"""Return a formatted string of the cart contents."""
|
| 42 |
+
if not cart:
|
| 43 |
+
return "Your cart is empty!!!\nWhy not buy Fruits from us ..."
|
| 44 |
+
|
| 45 |
+
lines = []
|
| 46 |
+
lines.append("Your Cart\n")
|
| 47 |
+
lines.append("Index Name Quantity Price (Total)\n")
|
| 48 |
+
lines.append("--------------------------------------------------")
|
| 49 |
+
|
| 50 |
+
unique_items = list({tuple(i) for i in cart})
|
| 51 |
+
total_cost = 0
|
| 52 |
+
|
| 53 |
+
for idx, item in enumerate(unique_items, start=1):
|
| 54 |
+
quantity = cart.count(list(item))
|
| 55 |
+
name = item[1]
|
| 56 |
+
price_per_kg = item[2]
|
| 57 |
+
price_total = price_per_kg * quantity
|
| 58 |
+
total_cost += price_total
|
| 59 |
+
lines.append(
|
| 60 |
+
f"{idx:<7} {name:<14} {quantity:<9} {price_total:>10.2f}"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
lines.append("--------------------------------------------------")
|
| 64 |
+
lines.append(f"Total Cost: {total_cost:.2f}")
|
| 65 |
+
return "\n".join(lines)
|
| 66 |
+
|
| 67 |
+
def total_cost(cart):
|
| 68 |
+
"""Compute total cost as plain string."""
|
| 69 |
+
if not cart:
|
| 70 |
+
return "Total cost: 0.00"
|
| 71 |
+
s = sum(item[2] for item in cart)
|
| 72 |
+
return f"Total cost: {s:.2f}"
|
| 73 |
+
|
| 74 |
+
# -----------------------------
|
| 75 |
+
# Gradio interface wrapper
|
| 76 |
+
# -----------------------------
|
| 77 |
+
|
| 78 |
+
def gradio_interface(item_id, cart_index):
|
| 79 |
+
"""
|
| 80 |
+
Wrapper matching your notebook logic:
|
| 81 |
+
- If item_id is provided, add that product.
|
| 82 |
+
- If cart_index is provided, remove that entry.
|
| 83 |
+
- Always return: cart_display, add_message, remove_message, total_cost_message.
|
| 84 |
+
"""
|
| 85 |
+
add_msg = ""
|
| 86 |
+
remove_msg = ""
|
| 87 |
+
|
| 88 |
+
# Normalize None to 0 (Gradio Number can return None)
|
| 89 |
+
if item_id is not None and item_id > 0:
|
| 90 |
+
add_msg = add_item_to_cart(int(item_id))
|
| 91 |
+
|
| 92 |
+
if cart_index is not None and cart_index > 0:
|
| 93 |
+
remove_msg = remove_item_from_cart(int(cart_index))
|
| 94 |
+
|
| 95 |
+
cart_display = display_cart(YourCart)
|
| 96 |
+
cost_msg = total_cost(YourCart)
|
| 97 |
+
|
| 98 |
+
return cart_display, add_msg, remove_msg, cost_msg
|
| 99 |
+
|
| 100 |
+
# -----------------------------
|
| 101 |
+
# Build Gradio Blocks UI
|
| 102 |
+
# -----------------------------
|
| 103 |
+
|
| 104 |
+
with gr.Blocks() as demo:
|
| 105 |
+
gr.Markdown("# Fruit Shopping Cart System")
|
| 106 |
+
|
| 107 |
+
with gr.Row():
|
| 108 |
+
itemid_input = gr.Number(
|
| 109 |
+
label="Enter Product ID to Add to Cart",
|
| 110 |
+
interactive=True
|
| 111 |
+
)
|
| 112 |
+
cartindex_input = gr.Number(
|
| 113 |
+
label="Enter Index ID to Remove from Cart",
|
| 114 |
+
interactive=True
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
with gr.Row():
|
| 118 |
+
add_button = gr.Button("Add to Cart")
|
| 119 |
+
remove_button = gr.Button("Remove from Cart")
|
| 120 |
+
|
| 121 |
+
cart_display_output = gr.Textbox(
|
| 122 |
+
label="Your Cart",
|
| 123 |
+
interactive=False,
|
| 124 |
+
lines=12
|
| 125 |
+
)
|
| 126 |
+
add_message_output = gr.Textbox(
|
| 127 |
+
label="Add Item Message",
|
| 128 |
+
interactive=False,
|
| 129 |
+
lines=3
|
| 130 |
+
)
|
| 131 |
+
remove_message_output = gr.Textbox(
|
| 132 |
+
label="Remove Item Message",
|
| 133 |
+
interactive=False,
|
| 134 |
+
lines=3
|
| 135 |
+
)
|
| 136 |
+
total_cost_output = gr.Textbox(
|
| 137 |
+
label="Total Cost",
|
| 138 |
+
interactive=False,
|
| 139 |
+
lines=2
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Add item: pass itemid_input and a dummy 0 for cart_index
|
| 143 |
+
add_button.click(
|
| 144 |
+
fn=gradio_interface,
|
| 145 |
+
inputs=[itemid_input, gr.Number(visible=False, value=0)],
|
| 146 |
+
outputs=[
|
| 147 |
+
cart_display_output,
|
| 148 |
+
add_message_output,
|
| 149 |
+
remove_message_output,
|
| 150 |
+
total_cost_output,
|
| 151 |
+
],
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
# Remove item: pass a dummy 0 for item_id and cartindex_input
|
| 155 |
+
remove_button.click(
|
| 156 |
+
fn=gradio_interface,
|
| 157 |
+
inputs=[gr.Number(visible=False, value=0), cartindex_input],
|
| 158 |
+
outputs=[
|
| 159 |
+
cart_display_output,
|
| 160 |
+
add_message_output,
|
| 161 |
+
remove_message_output,
|
| 162 |
+
total_cost_output,
|
| 163 |
+
],
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
# This is the entrypoint Hugging Face Spaces expects
|
| 167 |
+
if __name__ == "__main__":
|
| 168 |
+
demo.launch()
|