Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,28 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
# -----------------------------
|
| 4 |
-
# PRODUCT DATA
|
| 5 |
# -----------------------------
|
| 6 |
|
| 7 |
products = [
|
| 8 |
-
{
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"image": "https://upload.wikimedia.org/wikipedia/commons/1/15/Red_Apple.jpg",
|
| 13 |
-
"best_seller": True
|
| 14 |
-
},
|
| 15 |
-
{
|
| 16 |
-
"id": 2,
|
| 17 |
-
"name": "Milk 1L",
|
| 18 |
-
"price": 60,
|
| 19 |
-
"image": "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a4/Milk_glass.jpg/800px-Milk_glass.jpg",
|
| 20 |
-
"best_seller": False
|
| 21 |
-
},
|
| 22 |
-
{
|
| 23 |
-
"id": 3,
|
| 24 |
-
"name": "Potato Chips",
|
| 25 |
-
"price": 20,
|
| 26 |
-
"image": "https://upload.wikimedia.org/wikipedia/commons/6/69/Potato-Chips.jpg",
|
| 27 |
-
"best_seller": True
|
| 28 |
-
},
|
| 29 |
-
{
|
| 30 |
-
"id": 4,
|
| 31 |
-
"name": "Bananas",
|
| 32 |
-
"price": 50,
|
| 33 |
-
"image": "https://upload.wikimedia.org/wikipedia/commons/8/8a/Banana-Single.jpg",
|
| 34 |
-
"best_seller": False
|
| 35 |
-
},
|
| 36 |
]
|
| 37 |
|
| 38 |
-
cart = []
|
| 39 |
-
|
| 40 |
# -----------------------------
|
| 41 |
# FUNCTIONS
|
| 42 |
# -----------------------------
|
| 43 |
|
| 44 |
-
def add_to_cart(product_id):
|
| 45 |
for p in products:
|
| 46 |
if p["id"] == product_id:
|
| 47 |
cart.append(p)
|
| 48 |
break
|
| 49 |
-
return view_cart()
|
| 50 |
|
| 51 |
-
def view_cart():
|
| 52 |
if not cart:
|
| 53 |
return "π Cart is empty"
|
| 54 |
|
|
@@ -61,59 +35,50 @@ def view_cart():
|
|
| 61 |
text += f"\nTotal: βΉ{total}"
|
| 62 |
return text
|
| 63 |
|
| 64 |
-
def
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
return render_products()
|
| 68 |
|
| 69 |
# -----------------------------
|
| 70 |
# UI
|
| 71 |
# -----------------------------
|
| 72 |
|
| 73 |
-
with gr.Blocks(
|
| 74 |
-
.product-card {
|
| 75 |
-
border: 1px solid #ddd;
|
| 76 |
-
padding: 10px;
|
| 77 |
-
border-radius: 10px;
|
| 78 |
-
text-align: center;
|
| 79 |
-
}
|
| 80 |
-
.best {
|
| 81 |
-
color: red;
|
| 82 |
-
font-weight: bold;
|
| 83 |
-
}
|
| 84 |
-
""") as demo:
|
| 85 |
|
| 86 |
-
gr.
|
| 87 |
|
| 88 |
-
|
| 89 |
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
product_area.clear()
|
| 97 |
-
for p in products:
|
| 98 |
-
with gr.Row():
|
| 99 |
-
img = gr.Image(value=p["image"], height=150, show_label=False)
|
| 100 |
-
with gr.Column():
|
| 101 |
-
title = p["name"]
|
| 102 |
-
if p["best_seller"]:
|
| 103 |
-
title += " β Best Seller"
|
| 104 |
|
| 105 |
-
|
| 106 |
-
gr.Markdown(f"**Price: βΉ{p['price']}**")
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
lambda pid=p["id"]: add_to_cart(pid),
|
| 112 |
-
outputs=cart_output
|
| 113 |
-
)
|
| 114 |
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
|
| 118 |
|
| 119 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
# -----------------------------
|
| 4 |
+
# PRODUCT DATA
|
| 5 |
# -----------------------------
|
| 6 |
|
| 7 |
products = [
|
| 8 |
+
{"id": 1, "name": "Fresh Apples", "price": 120, "best": True},
|
| 9 |
+
{"id": 2, "name": "Milk 1L", "price": 60, "best": False},
|
| 10 |
+
{"id": 3, "name": "Potato Chips", "price": 20, "best": True},
|
| 11 |
+
{"id": 4, "name": "Bananas", "price": 50, "best": False},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
]
|
| 13 |
|
|
|
|
|
|
|
| 14 |
# -----------------------------
|
| 15 |
# FUNCTIONS
|
| 16 |
# -----------------------------
|
| 17 |
|
| 18 |
+
def add_to_cart(product_id, cart):
|
| 19 |
for p in products:
|
| 20 |
if p["id"] == product_id:
|
| 21 |
cart.append(p)
|
| 22 |
break
|
| 23 |
+
return cart, view_cart(cart)
|
| 24 |
|
| 25 |
+
def view_cart(cart):
|
| 26 |
if not cart:
|
| 27 |
return "π Cart is empty"
|
| 28 |
|
|
|
|
| 35 |
text += f"\nTotal: βΉ{total}"
|
| 36 |
return text
|
| 37 |
|
| 38 |
+
def sort_products():
|
| 39 |
+
products.sort(key=lambda x: x["price"])
|
| 40 |
+
return "Products sorted by price (Low β High)"
|
|
|
|
| 41 |
|
| 42 |
# -----------------------------
|
| 43 |
# UI
|
| 44 |
# -----------------------------
|
| 45 |
|
| 46 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
cart_state = gr.State([])
|
| 49 |
|
| 50 |
+
gr.Markdown("# π AI Grocery Store")
|
| 51 |
|
| 52 |
+
sort_btn = gr.Button("π Sort by Price (Low β High)")
|
| 53 |
+
sort_output = gr.Textbox(show_label=False)
|
| 54 |
|
| 55 |
+
with gr.Row():
|
| 56 |
+
for p in products:
|
| 57 |
+
with gr.Column():
|
| 58 |
+
title = p["name"]
|
| 59 |
+
if p["best"]:
|
| 60 |
+
title += " β Best Seller"
|
| 61 |
|
| 62 |
+
gr.Markdown(f"### {title}")
|
| 63 |
+
gr.Markdown(f"**Price: βΉ{p['price']}**")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
btn = gr.Button("Add to Cart")
|
|
|
|
| 66 |
|
| 67 |
+
btn.click(
|
| 68 |
+
add_to_cart,
|
| 69 |
+
inputs=[gr.Number(value=p["id"], visible=False), cart_state],
|
| 70 |
+
outputs=[cart_state, None]
|
| 71 |
+
)
|
| 72 |
|
| 73 |
+
cart_box = gr.Textbox(label="π Cart", lines=8)
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# Update cart display whenever state changes
|
| 76 |
+
cart_state.change(
|
| 77 |
+
view_cart,
|
| 78 |
+
inputs=cart_state,
|
| 79 |
+
outputs=cart_box
|
| 80 |
+
)
|
| 81 |
|
| 82 |
+
sort_btn.click(sort_products, outputs=sort_output)
|
| 83 |
|
| 84 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|