atdamole commited on
Commit
e88bda6
·
verified ·
1 Parent(s): be18e79

Upload 6 files

Browse files
FinalProj_RESNET-Official.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
FinalProj_Try1.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
app (1).py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from torchvision import transforms
4
+ from PIL import Image
5
+ import joblib
6
+
7
+ # Load model and metadata
8
+ model = torch.jit.load("resnet_grocery_model_scripted.pt", map_location=torch.device("cpu"))
9
+ model.eval()
10
+
11
+ assets = joblib.load("deployment_assets.joblib")
12
+ transform = assets['transform']
13
+ class_names = assets['class_names']
14
+
15
+ # Price list
16
+ items = ['Bisconni Chocolate Chip Cookies 46.8gm', 'Coca Cola Can 250ml', 'Colgate Maximum Cavity Protection 75gm',
17
+ 'Fanta 500ml', 'Fresher Guava Nectar 500ml', 'Fruita Vitals Red Grapes 200ml', 'Islamabad Tea 238gm',
18
+ 'Kolson Slanty Jalapeno 18gm', 'Kurkure Chutney Chaska 62gm', 'LU Candi Biscuit 60gm', 'LU Oreo Biscuit 19gm',
19
+ 'LU Prince Biscuit 55.2gm', 'Lays Masala 34gm', 'Lays Wavy Mexican Chili 34gm', 'Lifebuoy Total Protect Soap 96gm',
20
+ 'Lipton Yellow Label Tea 95gm', 'Meezan Ultra Rich Tea 190gm', 'Peek Freans Sooper Biscuit 13.2gm',
21
+ 'Safeguard Bar Soap Pure White 175gm', 'Shezan Apple 250ml', 'Sunsilk Shampoo Soft - Smooth 160ml',
22
+ 'Super Crisp BBQ 30gm', 'Supreme Tea 95gm', 'Tapal Danedar 95gm', 'Vaseline Healthy White Lotion 100ml']
23
+
24
+ prices = [55.20, 31.75, 90.00, 63.50, 50.00, 35.00, 150.00, 15.00, 25.00, 30.00, 10.00, 30.00, 20.00, 20.00, 44.50, 100.00,
25
+ 200.00, 10.00, 70.00, 25.00, 120.00, 15.00, 100.00, 100.00, 120.00]
26
+
27
+ pricelist = dict(zip(items, prices))
28
+
29
+ # Add image to state
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 and calculate total
36
+ def classify_and_track(images, budget):
37
+ results = []
38
+ total_cost = 0
39
+
40
+ for img in images:
41
+ if isinstance(img, Image.Image):
42
+ input_tensor = transform(img).unsqueeze(0)
43
+ with torch.no_grad():
44
+ outputs = model(input_tensor)
45
+ predicted_index = torch.argmax(outputs, dim=1).item()
46
+ predicted_class = class_names[predicted_index]
47
+ price = pricelist.get(predicted_class, 0)
48
+ total_cost += price
49
+ results.append((img, f"{predicted_class} - ₱{price:.2f}"))
50
+ else:
51
+ results.append((None, "Invalid image"))
52
+
53
+ if total_cost <= budget:
54
+ budget_status = f"🟢 Within budget! Total = ₱{total_cost:.2f} / ₱{budget:.2f}"
55
+ else:
56
+ budget_status = f"🔴 Over budget! Total = ₱{total_cost:.2f} / ₱{budget:.2f}"
57
+
58
+ return results, budget_status
59
+
60
+ # Clear state
61
+ def clear_all():
62
+ return [], [], ""
63
+
64
+ # Launch app
65
+ theme = gr.themes.Base(
66
+ primary_hue="green",
67
+ secondary_hue="orange",
68
+ font=["Poppins", "sans-serif"]
69
+ )
70
+
71
+ with gr.Blocks(theme=theme, title="Grocery Item Classifier") as app:
72
+ gr.Markdown("## 🛒 Grocery Classifier with Budget Tracker")
73
+ gr.Markdown("Upload grocery images, classify them, and track spending against your budget.")
74
+
75
+ image_list_state = gr.State([])
76
+
77
+ with gr.Row():
78
+ with gr.Column(scale=1):
79
+ image_input = gr.Image(type="pil", label="📷 Upload Image")
80
+ add_btn = gr.Button("➕ Add Image")
81
+ clear_btn = gr.Button("🧹 Clear All")
82
+ budget_input = gr.Number(label="💰 Your Budget (₱)", value=500.0, precision=2)
83
+ classify_btn = gr.Button("✅ Classify & Track")
84
+
85
+ with gr.Column(scale=2):
86
+ gallery_output = gr.Gallery(label="🖼️ Uploaded Images", columns=3, height="auto")
87
+ budget_output = gr.Textbox(label="📊 Budget Status", interactive=False)
88
+
89
+ add_btn.click(
90
+ fn=add_image,
91
+ inputs=[image_input, image_list_state],
92
+ outputs=[image_list_state, gallery_output]
93
+ )
94
+
95
+ classify_btn.click(
96
+ fn=classify_and_track,
97
+ inputs=[image_list_state, budget_input],
98
+ outputs=[gallery_output, budget_output]
99
+ )
100
+
101
+ clear_btn.click(
102
+ fn=clear_all,
103
+ outputs=[image_list_state, gallery_output, budget_output]
104
+ )
105
+
106
+ app.launch()
deployment_assets.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c63520922d21bebbb1936d928998d62a99a169d094d59e4a0815caa7f7c2704b
3
+ size 1678
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ torch
3
+ torchvision
4
+ joblib
5
+ Pillow
resnet_grocery_model_scripted.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0e70f385e2a24a4c62514f88a9ead2580743340a1355c28bbfdf89c2a5a76743
3
+ size 44892341