Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,13 @@
|
|
| 1 |
-
import os
|
| 2 |
-
# 1. Force legacy Keras BEFORE any other imports
|
| 3 |
-
os.environ["TF_USE_LEGACY_KERAS"] = "1"
|
| 4 |
-
|
| 5 |
import gradio as gr
|
| 6 |
import tensorflow as tf
|
| 7 |
-
import tf_keras as keras # Use the legacy loader
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
MODEL_PATH = "model/
|
| 13 |
-
model = keras.models.load_model(MODEL_PATH)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
LABELS = [
|
| 17 |
'apple_pie', 'baby_back_ribs', 'baklava', 'beef_carpaccio', 'beef_tartare', 'beet_salad', 'beignets',
|
| 18 |
'bibimbap', 'bread_pudding', 'breakfast_burrito', 'bruschetta', 'caesar_salad', 'cannoli', 'caprese_salad',
|
|
@@ -29,8 +24,6 @@ LABELS = [
|
|
| 29 |
'shrimp_and_grits', 'spaghetti_bolognese', 'spaghetti_carbonara', 'spring_rolls', 'steak', 'strawberry_shortcake',
|
| 30 |
'sushi', 'tacos', 'takoyaki', 'tiramisu', 'tuna_tartare', 'waffles'
|
| 31 |
]
|
| 32 |
-
|
| 33 |
-
# 4. Full Nutrition Database
|
| 34 |
NUTRITION_DB = {
|
| 35 |
'apple_pie': {'cal': 237, 'protein': 1.9, 'carbs': 34.0, 'fat': 11.0},
|
| 36 |
'baby_back_ribs': {'cal': 292, 'protein': 17.0, 'carbs': 0.0, 'fat': 24.0},
|
|
@@ -135,14 +128,13 @@ NUTRITION_DB = {
|
|
| 135 |
'waffles': {'cal': 291, 'protein': 8.0, 'carbs': 33.0, 'fat': 14.0}
|
| 136 |
}
|
| 137 |
|
| 138 |
-
# 5. Prediction Logic
|
| 139 |
def predict_nutrition(img):
|
| 140 |
if img is None:
|
| 141 |
return None, "Please upload an image."
|
| 142 |
-
|
| 143 |
-
# Preprocessing
|
| 144 |
img = Image.fromarray(img).resize((224, 224))
|
| 145 |
-
img_array = keras.preprocessing.image.img_to_array(img)
|
| 146 |
img_array = np.expand_dims(img_array, axis=0) / 255.0
|
| 147 |
|
| 148 |
# Prediction
|
|
@@ -163,7 +155,6 @@ def predict_nutrition(img):
|
|
| 163 |
nutri_markdown = f"""
|
| 164 |
### π₯ Nutrition Facts: {clean_name}
|
| 165 |
*(Estimated per 100g)*
|
| 166 |
-
|
| 167 |
| Nutrient | Amount |
|
| 168 |
| :--- | :--- |
|
| 169 |
| π₯ **Calories** | {nutri['cal']} kcal |
|
|
@@ -174,9 +165,9 @@ def predict_nutrition(img):
|
|
| 174 |
|
| 175 |
return confidences, nutri_markdown
|
| 176 |
|
| 177 |
-
#
|
| 178 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 179 |
-
gr.Markdown("# π Food-101
|
| 180 |
gr.Markdown("Identify food items and see their nutritional breakdown instantly.")
|
| 181 |
|
| 182 |
with gr.Row():
|
|
@@ -185,7 +176,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 185 |
submit_btn = gr.Button("Analyze Meal", variant="primary")
|
| 186 |
|
| 187 |
with gr.Column(scale=1):
|
|
|
|
| 188 |
output_chart = gr.Label(num_top_classes=3, label="Top 3 Predictions")
|
|
|
|
| 189 |
output_nutri = gr.Markdown(label="Nutrition Breakdown")
|
| 190 |
|
| 191 |
submit_btn.click(
|
|
@@ -194,5 +187,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 194 |
outputs=[output_chart, output_nutri]
|
| 195 |
)
|
| 196 |
|
|
|
|
|
|
|
|
|
|
| 197 |
if __name__ == "__main__":
|
| 198 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# 1. Load your model
|
| 7 |
+
MODEL_PATH = "model/best_food_model.keras"
|
| 8 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 9 |
|
| 10 |
+
# 2. Labels & Database (Make sure to include the full dictionary from previous turns)
|
| 11 |
LABELS = [
|
| 12 |
'apple_pie', 'baby_back_ribs', 'baklava', 'beef_carpaccio', 'beef_tartare', 'beet_salad', 'beignets',
|
| 13 |
'bibimbap', 'bread_pudding', 'breakfast_burrito', 'bruschetta', 'caesar_salad', 'cannoli', 'caprese_salad',
|
|
|
|
| 24 |
'shrimp_and_grits', 'spaghetti_bolognese', 'spaghetti_carbonara', 'spring_rolls', 'steak', 'strawberry_shortcake',
|
| 25 |
'sushi', 'tacos', 'takoyaki', 'tiramisu', 'tuna_tartare', 'waffles'
|
| 26 |
]
|
|
|
|
|
|
|
| 27 |
NUTRITION_DB = {
|
| 28 |
'apple_pie': {'cal': 237, 'protein': 1.9, 'carbs': 34.0, 'fat': 11.0},
|
| 29 |
'baby_back_ribs': {'cal': 292, 'protein': 17.0, 'carbs': 0.0, 'fat': 24.0},
|
|
|
|
| 128 |
'waffles': {'cal': 291, 'protein': 8.0, 'carbs': 33.0, 'fat': 14.0}
|
| 129 |
}
|
| 130 |
|
|
|
|
| 131 |
def predict_nutrition(img):
|
| 132 |
if img is None:
|
| 133 |
return None, "Please upload an image."
|
| 134 |
+
|
| 135 |
+
# Preprocessing
|
| 136 |
img = Image.fromarray(img).resize((224, 224))
|
| 137 |
+
img_array = tf.keras.preprocessing.image.img_to_array(img)
|
| 138 |
img_array = np.expand_dims(img_array, axis=0) / 255.0
|
| 139 |
|
| 140 |
# Prediction
|
|
|
|
| 155 |
nutri_markdown = f"""
|
| 156 |
### π₯ Nutrition Facts: {clean_name}
|
| 157 |
*(Estimated per 100g)*
|
|
|
|
| 158 |
| Nutrient | Amount |
|
| 159 |
| :--- | :--- |
|
| 160 |
| π₯ **Calories** | {nutri['cal']} kcal |
|
|
|
|
| 165 |
|
| 166 |
return confidences, nutri_markdown
|
| 167 |
|
| 168 |
+
# 3. Enhanced Gradio UI
|
| 169 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 170 |
+
gr.Markdown("# π Food-101 Deep Learning Classifier")
|
| 171 |
gr.Markdown("Identify food items and see their nutritional breakdown instantly.")
|
| 172 |
|
| 173 |
with gr.Row():
|
|
|
|
| 176 |
submit_btn = gr.Button("Analyze Meal", variant="primary")
|
| 177 |
|
| 178 |
with gr.Column(scale=1):
|
| 179 |
+
# Output 1: Top 3 Confidence Chart
|
| 180 |
output_chart = gr.Label(num_top_classes=3, label="Top 3 Predictions")
|
| 181 |
+
# Output 2: Nutrition Table
|
| 182 |
output_nutri = gr.Markdown(label="Nutrition Breakdown")
|
| 183 |
|
| 184 |
submit_btn.click(
|
|
|
|
| 187 |
outputs=[output_chart, output_nutri]
|
| 188 |
)
|
| 189 |
|
| 190 |
+
gr.Markdown("---")
|
| 191 |
+
gr.Markdown("*Note: This model is for educational purposes. For medical dietary tracking, please consult a professional.*")
|
| 192 |
+
|
| 193 |
if __name__ == "__main__":
|
| 194 |
demo.launch()
|