rlgondong commited on
Commit
8516823
Β·
verified Β·
1 Parent(s): ee43fd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -26,13 +26,17 @@ prices = [55.20, 31.75, 90.00, 63.50, 50.00, 35.00, 150.00, 15.00, 25.00, 30.00,
26
 
27
  pricelist = dict(zip(items, prices))
28
 
29
- # Add uploaded image to state list
30
  def add_image(new_img, image_list):
31
  if new_img:
32
  image_list.append(new_img)
33
  return image_list, image_list
34
 
35
- # Classify uploaded images and generate receipt
 
 
 
 
36
  def classify_and_track(images, budget):
37
  results = []
38
  total_cost = 0
@@ -60,16 +64,17 @@ def classify_and_track(images, budget):
60
  else:
61
  results.append((None, "Invalid image"))
62
 
 
63
  receipt_lines.append(" ==============================")
64
  receipt_lines.append(f" TOTAL:{'':>20}β‚±{total_cost:>7.2f}")
65
  receipt_lines.append(f" BUDGET:{'':>19}β‚±{budget:>7.2f}")
 
66
  receipt_lines.append(" ==============================")
67
  if total_cost <= budget:
68
  receipt_lines.append(" βœ… You're within budget! πŸŽ‰")
69
  receipt_lines.append(" πŸ›οΈ Happy Shopping!")
70
  else:
71
  receipt_lines.append(" ❌ Over budget! Try removing items 😬")
72
-
73
  receipt_lines.append(" ==============================")
74
  receipt_lines.append(" THANK YOU FOR SHOPPING WITH US!")
75
  receipt_lines.append(" Come back for smarter buys! 🧾")
@@ -91,20 +96,21 @@ custom_theme = gr.themes.Base(
91
  with gr.Blocks(theme=custom_theme) as iface:
92
  gr.Markdown(
93
  """
94
- <div style="text-align:center">
95
- <h1>🧾 ScanCart 🧾</h1>
96
- <p>Upload images of your grocery items, and ScanCart will identify them,<br>
97
- calculate the total cost, and check if it fits within your budget.<br><br>
98
  It's like having a smart shopping assistant! πŸ›’πŸ“Έ</p>
99
  </div>
100
- """
 
101
  )
102
 
103
  with gr.Row():
104
  with gr.Column(scale=1):
105
  image_input = gr.Image(type="pil", label="Upload Item Image", height=224)
106
  add_btn = gr.Button("Add Image βž•")
107
- budget_input = gr.Number(label="Your Budget (β‚±)", value=500, precision=2)
 
108
  classify_btn = gr.Button("Generate Receipt 🧾")
109
 
110
  with gr.Column(scale=2):
@@ -114,13 +120,19 @@ with gr.Blocks(theme=custom_theme) as iface:
114
  image_list_state = gr.State([])
115
 
116
  add_btn.click(
117
- fn=add_image,
118
  inputs=[image_input, image_list_state],
119
  outputs=[image_list_state, gallery_output]
120
  )
121
 
 
 
 
 
 
 
122
  classify_btn.click(
123
- fn=classify_and_track,
124
  inputs=[image_list_state, budget_input],
125
  outputs=[gallery_output, receipt_output]
126
  )
 
26
 
27
  pricelist = dict(zip(items, prices))
28
 
29
+ # Functions
30
  def add_image(new_img, image_list):
31
  if new_img:
32
  image_list.append(new_img)
33
  return image_list, image_list
34
 
35
+ def remove_last_image(image_list):
36
+ if image_list:
37
+ image_list.pop()
38
+ return image_list, image_list
39
+
40
  def classify_and_track(images, budget):
41
  results = []
42
  total_cost = 0
 
64
  else:
65
  results.append((None, "Invalid image"))
66
 
67
+ remaining = budget - total_cost
68
  receipt_lines.append(" ==============================")
69
  receipt_lines.append(f" TOTAL:{'':>20}β‚±{total_cost:>7.2f}")
70
  receipt_lines.append(f" BUDGET:{'':>19}β‚±{budget:>7.2f}")
71
+ receipt_lines.append(f" REMAINING:{'':>16}β‚±{remaining:>7.2f}")
72
  receipt_lines.append(" ==============================")
73
  if total_cost <= budget:
74
  receipt_lines.append(" βœ… You're within budget! πŸŽ‰")
75
  receipt_lines.append(" πŸ›οΈ Happy Shopping!")
76
  else:
77
  receipt_lines.append(" ❌ Over budget! Try removing items 😬")
 
78
  receipt_lines.append(" ==============================")
79
  receipt_lines.append(" THANK YOU FOR SHOPPING WITH US!")
80
  receipt_lines.append(" Come back for smarter buys! 🧾")
 
96
  with gr.Blocks(theme=custom_theme) as iface:
97
  gr.Markdown(
98
  """
99
+ <div style="text-align:center;">
100
+ <h1 style="font-size: 3em;">ScanCart 🧾</h1>
101
+ <p style="font-size: 1.4em;">Upload images of your grocery items, and ScanCart will identify them, calculate the total cost, and check if it fits within your budget.<br>
 
102
  It's like having a smart shopping assistant! πŸ›’πŸ“Έ</p>
103
  </div>
104
+ """,
105
+ elem_id="title"
106
  )
107
 
108
  with gr.Row():
109
  with gr.Column(scale=1):
110
  image_input = gr.Image(type="pil", label="Upload Item Image", height=224)
111
  add_btn = gr.Button("Add Image βž•")
112
+ remove_btn = gr.Button("Remove Last Item πŸ—‘οΈ")
113
+ budget_input = gr.Number(label="Your Budget (β‚±)", value=500.0, precision=2)
114
  classify_btn = gr.Button("Generate Receipt 🧾")
115
 
116
  with gr.Column(scale=2):
 
120
  image_list_state = gr.State([])
121
 
122
  add_btn.click(
123
+ add_image,
124
  inputs=[image_input, image_list_state],
125
  outputs=[image_list_state, gallery_output]
126
  )
127
 
128
+ remove_btn.click(
129
+ remove_last_image,
130
+ inputs=image_list_state,
131
+ outputs=[image_list_state, gallery_output]
132
+ )
133
+
134
  classify_btn.click(
135
+ classify_and_track,
136
  inputs=[image_list_state, budget_input],
137
  outputs=[gallery_output, receipt_output]
138
  )