Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load AI model (FLAN-T5 for
|
| 5 |
food_model = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 6 |
|
| 7 |
# Function to get calorie data & substitute using AI
|
| 8 |
def get_food_substitute(food, portion_size):
|
| 9 |
-
# AI model processes input and provides a
|
| 10 |
-
query = f"Suggest a
|
| 11 |
-
response = food_model(query, max_length=
|
| 12 |
|
| 13 |
-
#
|
| 14 |
try:
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
original_calories = float(original_calories.split(" ")[0]) * portion_size / 100
|
| 17 |
substitute_calories = float(substitute_calories.split(" ")[0]) * portion_size / 100
|
| 18 |
calories_saved = original_calories - substitute_calories
|
| 19 |
|
| 20 |
-
|
| 21 |
-
result = (
|
| 22 |
f"**Original Food:** {original_food} ({original_calories:.2f} kcal)\n"
|
| 23 |
f"**Suggested Substitute:** {substitute} ({substitute_calories:.2f} kcal)\n"
|
| 24 |
f"**Calories Saved:** {calories_saved:.2f} kcal"
|
| 25 |
)
|
| 26 |
-
except:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
return result
|
| 30 |
|
| 31 |
# Gradio UI
|
| 32 |
with gr.Blocks() as app:
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load AI model (FLAN-T5 for food recognition & substitution)
|
| 5 |
food_model = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 6 |
|
| 7 |
# Function to get calorie data & substitute using AI
|
| 8 |
def get_food_substitute(food, portion_size):
|
| 9 |
+
# AI model processes input and provides a structured response
|
| 10 |
+
query = f"Suggest a lower-calorie substitute for {food}. Format: Original Food - Calories, Substitute - Calories"
|
| 11 |
+
response = food_model(query, max_length=100, truncation=True)[0]['generated_text']
|
| 12 |
|
| 13 |
+
# Try to extract structured values from AI response
|
| 14 |
try:
|
| 15 |
+
parts = response.split(", ")
|
| 16 |
+
if len(parts) < 4:
|
| 17 |
+
return "AI model returned incomplete data. Try another food item."
|
| 18 |
+
|
| 19 |
+
original_food, original_calories, substitute, substitute_calories = parts
|
| 20 |
original_calories = float(original_calories.split(" ")[0]) * portion_size / 100
|
| 21 |
substitute_calories = float(substitute_calories.split(" ")[0]) * portion_size / 100
|
| 22 |
calories_saved = original_calories - substitute_calories
|
| 23 |
|
| 24 |
+
return (
|
|
|
|
| 25 |
f"**Original Food:** {original_food} ({original_calories:.2f} kcal)\n"
|
| 26 |
f"**Suggested Substitute:** {substitute} ({substitute_calories:.2f} kcal)\n"
|
| 27 |
f"**Calories Saved:** {calories_saved:.2f} kcal"
|
| 28 |
)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Error: AI model output format incorrect. Details: {str(e)}"
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Gradio UI
|
| 33 |
with gr.Blocks() as app:
|