Upload 5 files
Browse files- FinalProj_RESNET-Official.ipynb +0 -0
- app (2).py +87 -0
- deployment_assets.joblib +3 -0
- requirements (1).txt +5 -0
- resnet_grocery_model_scripted.pt +3 -0
FinalProj_RESNET-Official.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app (2).py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 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 all images and return budget status
|
| 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 |
+
# Budget logic
|
| 54 |
+
if total_cost <= budget:
|
| 55 |
+
budget_status = f"π’ Within budget! Total = β±{total_cost:.2f} / β±{budget:.2f}"
|
| 56 |
+
else:
|
| 57 |
+
budget_status = f"π΄ Over budget! Total = β±{total_cost:.2f} / β±{budget:.2f}"
|
| 58 |
+
|
| 59 |
+
return results, budget_status
|
| 60 |
+
|
| 61 |
+
# Gradio App
|
| 62 |
+
with gr.Blocks(title="Grocery Item Classifier") as app:
|
| 63 |
+
gr.Markdown("## π Grocery Classifier with Budget Tracker")
|
| 64 |
+
|
| 65 |
+
image_input = gr.Image(type="pil", label="Drop or upload one image")
|
| 66 |
+
image_list_state = gr.State([])
|
| 67 |
+
|
| 68 |
+
add_btn = gr.Button("β Add Image")
|
| 69 |
+
gallery_output = gr.Gallery(label="πΌοΈ Uploaded Images", columns=3)
|
| 70 |
+
|
| 71 |
+
budget_input = gr.Number(label="π° Your Budget (β±)", value=500.0, precision=2)
|
| 72 |
+
classify_btn = gr.Button("β
Classify & Track")
|
| 73 |
+
budget_output = gr.Textbox(label="π Budget Status")
|
| 74 |
+
|
| 75 |
+
add_btn.click(
|
| 76 |
+
fn=add_image,
|
| 77 |
+
inputs=[image_input, image_list_state],
|
| 78 |
+
outputs=[image_list_state, gallery_output]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
classify_btn.click(
|
| 82 |
+
fn=classify_and_track,
|
| 83 |
+
inputs=[image_list_state, budget_input],
|
| 84 |
+
outputs=[gallery_output, budget_output]
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
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 (1).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
|