Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,6 @@ def classify_and_track(images, budget):
|
|
| 35 |
total_cost = 0
|
| 36 |
receipt_lines = []
|
| 37 |
|
| 38 |
-
# Receipt Header with some emojis and stylized spacing
|
| 39 |
receipt_lines.append(" π SCANCART SUPERMARKET π")
|
| 40 |
receipt_lines.append(" π Online Smart Checkout")
|
| 41 |
receipt_lines.append(" ==============================")
|
|
@@ -53,7 +52,6 @@ def classify_and_track(images, budget):
|
|
| 53 |
total_cost += price
|
| 54 |
results.append((img, f"{predicted_class} - β±{price:.2f}"))
|
| 55 |
|
| 56 |
-
# Limit item name length and add cool formatting
|
| 57 |
item_name = (predicted_class[:22] + '...') if len(predicted_class) > 25 else predicted_class
|
| 58 |
receipt_lines.append(f" {item_name:<25} β±{price:>6.2f}")
|
| 59 |
else:
|
|
@@ -63,61 +61,20 @@ def classify_and_track(images, budget):
|
|
| 63 |
receipt_lines.append(f" TOTAL:{'':>20}β±{total_cost:>7.2f}")
|
| 64 |
receipt_lines.append(f" BUDGET:{'':>19}β±{budget:>7.2f}")
|
| 65 |
receipt_lines.append(" ==============================")
|
| 66 |
-
|
| 67 |
-
# Fun dynamic message depending on budget
|
| 68 |
if total_cost <= budget:
|
| 69 |
-
receipt_lines.append(" β
|
| 70 |
receipt_lines.append(" ποΈ Happy Shopping!")
|
| 71 |
else:
|
| 72 |
-
receipt_lines.append(" β
|
| 73 |
-
receipt_lines.append(" β οΈ Try removing some items.")
|
| 74 |
|
| 75 |
receipt_lines.append(" ==============================")
|
| 76 |
-
receipt_lines.append("
|
| 77 |
-
receipt_lines.append("
|
| 78 |
|
| 79 |
receipt_text = "\n".join(receipt_lines)
|
| 80 |
return results, receipt_text
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
custom_theme = gr.themes.Base(
|
| 84 |
-
primary_hue="rose",
|
| 85 |
-
secondary_hue="amber",
|
| 86 |
-
neutral_hue="stone",
|
| 87 |
-
font=[gr.themes.GoogleFont("Montserrat")]
|
| 88 |
-
).set(
|
| 89 |
-
body_background_fill="#fff0f0",
|
| 90 |
-
body_text_color="#4a4a4a",
|
| 91 |
-
input_background_fill="#fff5f5",
|
| 92 |
-
button_primary_background_fill="#ff4d6d",
|
| 93 |
-
button_primary_text_color="#fff",
|
| 94 |
-
button_secondary_background_fill="#ffb347",
|
| 95 |
-
button_secondary_text_color="#5a2a00"
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
# Custom CSS for hover effect and scroll for gallery
|
| 99 |
-
custom_css = """
|
| 100 |
-
.gr-gallery-item img {
|
| 101 |
-
border-radius: 15px;
|
| 102 |
-
transition: transform 0.3s ease;
|
| 103 |
-
}
|
| 104 |
-
.gr-gallery-item:hover img {
|
| 105 |
-
transform: scale(1.1);
|
| 106 |
-
box-shadow: 0 8px 20px rgba(255,77,109,0.6);
|
| 107 |
-
cursor: pointer;
|
| 108 |
-
}
|
| 109 |
-
.gr-textbox textarea {
|
| 110 |
-
font-family: 'Helvetica', Courier, monospace;
|
| 111 |
-
background-color: #fff8f8;
|
| 112 |
-
border: 2px solid #ff4d6d;
|
| 113 |
-
border-radius: 10px;
|
| 114 |
-
padding: 12px;
|
| 115 |
-
font-size: 14px;
|
| 116 |
-
line-height: 1.4;
|
| 117 |
-
}
|
| 118 |
-
"""
|
| 119 |
-
|
| 120 |
-
# Gradio Interface
|
| 121 |
with gr.Blocks(theme=custom_theme) as iface:
|
| 122 |
gr.Markdown(
|
| 123 |
"""
|
|
@@ -127,30 +84,31 @@ with gr.Blocks(theme=custom_theme) as iface:
|
|
| 127 |
"""
|
| 128 |
)
|
| 129 |
|
|
|
|
|
|
|
| 130 |
with gr.Row():
|
| 131 |
with gr.Column(scale=1):
|
| 132 |
-
image_input = gr.Image(type="pil", label="
|
| 133 |
-
add_btn = gr.Button("
|
| 134 |
-
budget_input = gr.Number(label="
|
| 135 |
-
classify_btn = gr.Button("
|
| 136 |
-
budget_input.tooltip = "Set your shopping budget here to get feedback on spending!"
|
| 137 |
|
| 138 |
with gr.Column(scale=2):
|
| 139 |
-
gallery_output = gr.Gallery(label="
|
| 140 |
-
receipt_output = gr.Textbox(label="
|
| 141 |
|
| 142 |
image_list_state = gr.State([])
|
| 143 |
|
| 144 |
add_btn.click(
|
| 145 |
-
|
| 146 |
inputs=[image_input, image_list_state],
|
| 147 |
outputs=[image_list_state, gallery_output]
|
| 148 |
)
|
| 149 |
|
| 150 |
classify_btn.click(
|
| 151 |
-
|
| 152 |
inputs=[image_list_state, budget_input],
|
| 153 |
outputs=[gallery_output, receipt_output]
|
| 154 |
)
|
| 155 |
|
| 156 |
-
|
|
|
|
| 35 |
total_cost = 0
|
| 36 |
receipt_lines = []
|
| 37 |
|
|
|
|
| 38 |
receipt_lines.append(" π SCANCART SUPERMARKET π")
|
| 39 |
receipt_lines.append(" π Online Smart Checkout")
|
| 40 |
receipt_lines.append(" ==============================")
|
|
|
|
| 52 |
total_cost += price
|
| 53 |
results.append((img, f"{predicted_class} - β±{price:.2f}"))
|
| 54 |
|
|
|
|
| 55 |
item_name = (predicted_class[:22] + '...') if len(predicted_class) > 25 else predicted_class
|
| 56 |
receipt_lines.append(f" {item_name:<25} β±{price:>6.2f}")
|
| 57 |
else:
|
|
|
|
| 61 |
receipt_lines.append(f" TOTAL:{'':>20}β±{total_cost:>7.2f}")
|
| 62 |
receipt_lines.append(f" BUDGET:{'':>19}β±{budget:>7.2f}")
|
| 63 |
receipt_lines.append(" ==============================")
|
|
|
|
|
|
|
| 64 |
if total_cost <= budget:
|
| 65 |
+
receipt_lines.append(" β
You're within budget! π")
|
| 66 |
receipt_lines.append(" ποΈ Happy Shopping!")
|
| 67 |
else:
|
| 68 |
+
receipt_lines.append(" β Over budget! Try removing items π¬")
|
|
|
|
| 69 |
|
| 70 |
receipt_lines.append(" ==============================")
|
| 71 |
+
receipt_lines.append(" THANK YOU FOR SHOPPING WITH US!")
|
| 72 |
+
receipt_lines.append(" Come back for smarter buys! π§Ύ")
|
| 73 |
|
| 74 |
receipt_text = "\n".join(receipt_lines)
|
| 75 |
return results, receipt_text
|
| 76 |
|
| 77 |
+
# ποΈ Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
with gr.Blocks(theme=custom_theme) as iface:
|
| 79 |
gr.Markdown(
|
| 80 |
"""
|
|
|
|
| 84 |
"""
|
| 85 |
)
|
| 86 |
|
| 87 |
+
|
| 88 |
+
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column(scale=1):
|
| 91 |
+
image_input = gr.Image(type="pil", label="Upload Item Image", height=224)
|
| 92 |
+
add_btn = gr.Button("Add Image β")
|
| 93 |
+
budget_input = gr.Number(label="Your Budget (β±)", value=500, precision=2)
|
| 94 |
+
classify_btn = gr.Button("Generate Receipt π§Ύ")
|
|
|
|
| 95 |
|
| 96 |
with gr.Column(scale=2):
|
| 97 |
+
gallery_output = gr.Gallery(label="Cart Preview", columns=3, height="auto", object_fit="contain")
|
| 98 |
+
receipt_output = gr.Textbox(label="Receipt", lines=20, interactive=False, show_copy_button=True)
|
| 99 |
|
| 100 |
image_list_state = gr.State([])
|
| 101 |
|
| 102 |
add_btn.click(
|
| 103 |
+
add_image,
|
| 104 |
inputs=[image_input, image_list_state],
|
| 105 |
outputs=[image_list_state, gallery_output]
|
| 106 |
)
|
| 107 |
|
| 108 |
classify_btn.click(
|
| 109 |
+
classify_and_track,
|
| 110 |
inputs=[image_list_state, budget_input],
|
| 111 |
outputs=[gallery_output, receipt_output]
|
| 112 |
)
|
| 113 |
|
| 114 |
+
demo.launch()
|