charantejapolavarapu commited on
Commit
a4b156f
Β·
verified Β·
1 Parent(s): b177e30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -73
app.py CHANGED
@@ -1,54 +1,28 @@
1
  import gradio as gr
2
 
3
  # -----------------------------
4
- # PRODUCT DATA (WORKING IMAGES)
5
  # -----------------------------
6
 
7
  products = [
8
- {
9
- "id": 1,
10
- "name": "Fresh Apples",
11
- "price": 120,
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 sort_low_to_high():
65
- global products
66
- products = sorted(products, key=lambda x: x["price"])
67
- return render_products()
68
 
69
  # -----------------------------
70
  # UI
71
  # -----------------------------
72
 
73
- with gr.Blocks(css="""
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.Markdown("# πŸ›’ AI Grocery Store (Amazon Style UI)")
87
 
88
- sort_btn = gr.Button("πŸ“Š Sort by Price (Low to High)")
89
 
90
- product_area = gr.Column()
 
91
 
92
- cart_output = gr.Textbox(label="πŸ›’ Cart", lines=10)
 
 
 
 
 
93
 
94
- def render_products():
95
- with product_area:
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
- gr.Markdown(f"### {title}")
106
- gr.Markdown(f"**Price: β‚Ή{p['price']}**")
107
 
108
- btn = gr.Button("Add to Cart")
 
 
 
 
109
 
110
- btn.click(
111
- lambda pid=p["id"]: add_to_cart(pid),
112
- outputs=cart_output
113
- )
114
 
115
- sort_btn.click(sort_low_to_high, outputs=None)
 
 
 
 
 
116
 
117
- render_products()
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)