Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
# Load AI model (FLAN-T5 for food recognition & substitution) | |
food_model = pipeline("text2text-generation", model="google/flan-t5-small") | |
# Function to get calorie data & substitute using AI | |
def get_food_substitute(food, portion_size): | |
# AI model processes input and provides a structured response | |
query = f"Suggest a lower-calorie substitute for {food}. Format: Original Food - Calories, Substitute - Calories" | |
response = food_model(query, max_length=100, truncation=True)[0]['generated_text'] | |
# Try to extract structured values from AI response | |
try: | |
parts = response.split(", ") | |
if len(parts) < 4: | |
return "AI model returned incomplete data. Try another food item." | |
original_food, original_calories, substitute, substitute_calories = parts | |
original_calories = float(original_calories.split(" ")[0]) * portion_size / 100 | |
substitute_calories = float(substitute_calories.split(" ")[0]) * portion_size / 100 | |
calories_saved = original_calories - substitute_calories | |
return ( | |
f"**Original Food:** {original_food} ({original_calories:.2f} kcal)\n" | |
f"**Suggested Substitute:** {substitute} ({substitute_calories:.2f} kcal)\n" | |
f"**Calories Saved:** {calories_saved:.2f} kcal" | |
) | |
except Exception as e: | |
return f"Error: AI model output format incorrect. Details: {str(e)}" | |
# Gradio UI | |
with gr.Blocks() as app: | |
gr.Markdown("# 🍽️ DeepCalorie - AI-Powered Food Substitute Finder") | |
gr.Markdown("Enter a food item, and get a **lower-calorie alternative** along with calories saved.") | |
with gr.Row(): | |
food_input = gr.Textbox(label="Enter Food/Dish") | |
portion_input = gr.Number(label="Portion Size (grams)", value=100) | |
submit_button = gr.Button("Find Substitute") | |
output = gr.Markdown() | |
submit_button.click(get_food_substitute, inputs=[food_input, portion_input], outputs=output) | |
# Run the app | |
if __name__ == "__main__": | |
app.launch() | |