aayanb09 commited on
Commit
0d8dfe4
·
verified ·
1 Parent(s): 167e8c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -17
app.py CHANGED
@@ -12,7 +12,7 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
  print(f"Using device: {device}")
13
 
14
  # -----------------------------
15
- # Model definition (MATCHES TRAINING)
16
  # -----------------------------
17
  class FoodIngredientClassifier(nn.Module):
18
  def __init__(self, num_classes):
@@ -31,7 +31,7 @@ class FoodIngredientClassifier(nn.Module):
31
  return self.backbone(x)
32
 
33
  # -----------------------------
34
- # Load checkpoint (PyTorch 2.6+ safe)
35
  # -----------------------------
36
  checkpoint = torch.load(
37
  "best_model.pth",
@@ -69,7 +69,7 @@ def clean_name(name):
69
  return name.replace("_", " ").title()
70
 
71
  # -----------------------------
72
- # Prediction function (BULLETPROOF)
73
  # -----------------------------
74
  def predict(image, threshold):
75
  if image is None:
@@ -85,14 +85,14 @@ def predict(image, threshold):
85
  logits = model(input_tensor)
86
  probs = torch.sigmoid(logits).cpu().numpy()[0]
87
 
88
- # Threshold-based predictions
89
  results = {
90
  clean_name(class_names[i]): float(probs[i])
91
  for i in range(len(probs))
92
  if probs[i] >= threshold
93
  }
94
 
95
- # 🔒 Fallback: always return top 5
96
  if not results:
97
  top_idx = np.argsort(probs)[-5:][::-1]
98
  results = {
@@ -100,13 +100,10 @@ def predict(image, threshold):
100
  for i in top_idx
101
  }
102
 
103
- # Sort by confidence
104
- results = dict(sorted(results.items(), key=lambda x: x[1], reverse=True))
105
-
106
- return results
107
 
108
  # -----------------------------
109
- # Gradio UI
110
  # -----------------------------
111
  iface = gr.Interface(
112
  fn=predict,
@@ -122,13 +119,11 @@ iface = gr.Interface(
122
  ],
123
  outputs=gr.JSON(label="Detected Ingredients"),
124
  title="Food Ingredient Detection (Multi-Label)",
125
- description=(
126
- "Upload a food image to detect **multiple ingredients at once**.\n\n"
127
- "This is a **multi-label ResNet-50 model** using sigmoid outputs."
128
- ),
129
- theme=gr.themes.Soft(),
130
- allow_flagging="never"
131
  )
132
 
 
 
 
133
  if __name__ == "__main__":
134
- iface.launch(enable_queue=True)
 
12
  print(f"Using device: {device}")
13
 
14
  # -----------------------------
15
+ # Model definition
16
  # -----------------------------
17
  class FoodIngredientClassifier(nn.Module):
18
  def __init__(self, num_classes):
 
31
  return self.backbone(x)
32
 
33
  # -----------------------------
34
+ # Load checkpoint
35
  # -----------------------------
36
  checkpoint = torch.load(
37
  "best_model.pth",
 
69
  return name.replace("_", " ").title()
70
 
71
  # -----------------------------
72
+ # Prediction function
73
  # -----------------------------
74
  def predict(image, threshold):
75
  if image is None:
 
85
  logits = model(input_tensor)
86
  probs = torch.sigmoid(logits).cpu().numpy()[0]
87
 
88
+ # Threshold-based results
89
  results = {
90
  clean_name(class_names[i]): float(probs[i])
91
  for i in range(len(probs))
92
  if probs[i] >= threshold
93
  }
94
 
95
+ # Fallback: always return top 5
96
  if not results:
97
  top_idx = np.argsort(probs)[-5:][::-1]
98
  results = {
 
100
  for i in top_idx
101
  }
102
 
103
+ return dict(sorted(results.items(), key=lambda x: x[1], reverse=True))
 
 
 
104
 
105
  # -----------------------------
106
+ # Gradio Interface (NO deprecated args)
107
  # -----------------------------
108
  iface = gr.Interface(
109
  fn=predict,
 
119
  ],
120
  outputs=gr.JSON(label="Detected Ingredients"),
121
  title="Food Ingredient Detection (Multi-Label)",
122
+ description="Upload a food image to detect multiple ingredients using a ResNet-50 model."
 
 
 
 
 
123
  )
124
 
125
+ # -----------------------------
126
+ # Launch (Gradio 6.x style)
127
+ # -----------------------------
128
  if __name__ == "__main__":
129
+ iface.launch(theme=gr.themes.Soft())