Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,7 @@ import numpy as np
|
|
| 7 |
import pickle
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
# MODEL DEFINITION (must match training code exactly)
|
| 12 |
-
# ============================================================================
|
| 13 |
|
| 14 |
class FoodIngredientClassifier(nn.Module):
|
| 15 |
def __init__(self, num_classes):
|
|
@@ -33,9 +31,7 @@ class FoodIngredientClassifier(nn.Module):
|
|
| 33 |
return self.backbone(x)
|
| 34 |
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
# LOAD MODEL
|
| 38 |
-
# ============================================================================
|
| 39 |
|
| 40 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 41 |
THRESHOLD = 0.5
|
|
@@ -52,9 +48,7 @@ def load_model():
|
|
| 52 |
|
| 53 |
model, mlb = load_model()
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
# TRANSFORM
|
| 57 |
-
# ============================================================================
|
| 58 |
|
| 59 |
transform = transforms.Compose([
|
| 60 |
transforms.Resize((224, 224)),
|
|
@@ -62,9 +56,7 @@ transform = transforms.Compose([
|
|
| 62 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 63 |
])
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
# INFERENCE FUNCTION
|
| 67 |
-
# ============================================================================
|
| 68 |
|
| 69 |
def predict(image, threshold):
|
| 70 |
if image is None:
|
|
@@ -96,13 +88,11 @@ def predict(image, threshold):
|
|
| 96 |
return "\n\n".join(output_lines)
|
| 97 |
|
| 98 |
|
| 99 |
-
#
|
| 100 |
-
# GRADIO UI
|
| 101 |
-
# ============================================================================
|
| 102 |
|
| 103 |
with gr.Blocks(title="Food Ingredient Detector") as demo:
|
| 104 |
gr.Markdown("""
|
| 105 |
-
#
|
| 106 |
Upload a photo of food and the model will identify its ingredients.
|
| 107 |
Built with a **ViT-B/16** backbone fine-tuned for multi-label ingredient classification.
|
| 108 |
""")
|
|
@@ -115,7 +105,7 @@ with gr.Blocks(title="Food Ingredient Detector") as demo:
|
|
| 115 |
label="Detection Threshold",
|
| 116 |
info="Lower = more ingredients detected, higher = only confident predictions"
|
| 117 |
)
|
| 118 |
-
predict_btn = gr.Button("
|
| 119 |
|
| 120 |
with gr.Column():
|
| 121 |
output = gr.Markdown(label="Results")
|
|
|
|
| 7 |
import pickle
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
# Model definition
|
|
|
|
|
|
|
| 11 |
|
| 12 |
class FoodIngredientClassifier(nn.Module):
|
| 13 |
def __init__(self, num_classes):
|
|
|
|
| 31 |
return self.backbone(x)
|
| 32 |
|
| 33 |
|
| 34 |
+
# Load model
|
|
|
|
|
|
|
| 35 |
|
| 36 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 37 |
THRESHOLD = 0.5
|
|
|
|
| 48 |
|
| 49 |
model, mlb = load_model()
|
| 50 |
|
| 51 |
+
# Transform
|
|
|
|
|
|
|
| 52 |
|
| 53 |
transform = transforms.Compose([
|
| 54 |
transforms.Resize((224, 224)),
|
|
|
|
| 56 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 57 |
])
|
| 58 |
|
| 59 |
+
# Inference Function
|
|
|
|
|
|
|
| 60 |
|
| 61 |
def predict(image, threshold):
|
| 62 |
if image is None:
|
|
|
|
| 88 |
return "\n\n".join(output_lines)
|
| 89 |
|
| 90 |
|
| 91 |
+
# UI
|
|
|
|
|
|
|
| 92 |
|
| 93 |
with gr.Blocks(title="Food Ingredient Detector") as demo:
|
| 94 |
gr.Markdown("""
|
| 95 |
+
# Food Ingredient Detector
|
| 96 |
Upload a photo of food and the model will identify its ingredients.
|
| 97 |
Built with a **ViT-B/16** backbone fine-tuned for multi-label ingredient classification.
|
| 98 |
""")
|
|
|
|
| 105 |
label="Detection Threshold",
|
| 106 |
info="Lower = more ingredients detected, higher = only confident predictions"
|
| 107 |
)
|
| 108 |
+
predict_btn = gr.Button("Detect Ingredients", variant="primary")
|
| 109 |
|
| 110 |
with gr.Column():
|
| 111 |
output = gr.Markdown(label="Results")
|