aayanb09 commited on
Commit
0045512
·
verified ·
1 Parent(s): 21518d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -14,13 +14,13 @@ DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
  class FoodIngredientClassifier(nn.Module):
15
  def __init__(self, num_classes):
16
  super().__init__()
17
-
18
  self.backbone = models.vit_b_16(
19
  weights=models.ViT_B_16_Weights.DEFAULT
20
  )
21
-
22
  num_features = self.backbone.heads.head.in_features
23
-
24
  self.backbone.heads = nn.Sequential(
25
  nn.Dropout(0.5),
26
  nn.Linear(num_features, 1024),
@@ -33,14 +33,15 @@ class FoodIngredientClassifier(nn.Module):
33
  nn.Dropout(0.3),
34
  nn.Linear(512, num_classes)
35
  )
36
-
37
  def forward(self, x):
38
  return self.backbone(x)
39
 
 
40
  # -------------------------
41
- # LOAD CHECKPOINT
42
  # -------------------------
43
- # Add sklearn MultiLabelBinarizer to safe globals before loading
44
  torch.serialization.add_safe_globals([MultiLabelBinarizer])
45
 
46
  checkpoint = torch.load("model.pth", map_location=DEVICE)
@@ -73,26 +74,27 @@ transform = transforms.Compose([
73
  def predict(image):
74
  image = image.convert("RGB")
75
  img_tensor = transform(image).unsqueeze(0).to(DEVICE)
76
-
77
  with torch.no_grad():
78
  output = model(img_tensor)
79
  probs = torch.sigmoid(output).cpu().numpy()[0]
80
-
81
  pred_indices = np.where(probs > threshold)[0]
82
  ingredients = mlb.classes_[pred_indices]
83
  confidences = probs[pred_indices]
84
-
85
  results = sorted(
86
  [(ing, float(conf)) for ing, conf in zip(ingredients, confidences)],
87
  key=lambda x: x[1],
88
  reverse=True
89
  )
90
-
91
  if not results:
92
  return {"No ingredient detected": 1.0}
93
-
94
  return {k: v for k, v in results}
95
 
 
96
  # -------------------------
97
  # GRADIO INTERFACE
98
  # -------------------------
 
14
  class FoodIngredientClassifier(nn.Module):
15
  def __init__(self, num_classes):
16
  super().__init__()
17
+
18
  self.backbone = models.vit_b_16(
19
  weights=models.ViT_B_16_Weights.DEFAULT
20
  )
21
+
22
  num_features = self.backbone.heads.head.in_features
23
+
24
  self.backbone.heads = nn.Sequential(
25
  nn.Dropout(0.5),
26
  nn.Linear(num_features, 1024),
 
33
  nn.Dropout(0.3),
34
  nn.Linear(512, num_classes)
35
  )
36
+
37
  def forward(self, x):
38
  return self.backbone(x)
39
 
40
+
41
  # -------------------------
42
+ # LOAD CHECKPOINT - FIXED VERSION
43
  # -------------------------
44
+ # Add the sklearn class to safe globals before loading
45
  torch.serialization.add_safe_globals([MultiLabelBinarizer])
46
 
47
  checkpoint = torch.load("model.pth", map_location=DEVICE)
 
74
  def predict(image):
75
  image = image.convert("RGB")
76
  img_tensor = transform(image).unsqueeze(0).to(DEVICE)
77
+
78
  with torch.no_grad():
79
  output = model(img_tensor)
80
  probs = torch.sigmoid(output).cpu().numpy()[0]
81
+
82
  pred_indices = np.where(probs > threshold)[0]
83
  ingredients = mlb.classes_[pred_indices]
84
  confidences = probs[pred_indices]
85
+
86
  results = sorted(
87
  [(ing, float(conf)) for ing, conf in zip(ingredients, confidences)],
88
  key=lambda x: x[1],
89
  reverse=True
90
  )
91
+
92
  if not results:
93
  return {"No ingredient detected": 1.0}
94
+
95
  return {k: v for k, v in results}
96
 
97
+
98
  # -------------------------
99
  # GRADIO INTERFACE
100
  # -------------------------