Neha555Altaf's picture
Create app.py
0bc52f9 verified
Raw
History Blame Contribute Delete
1.68 kB
import gradio as gr
# Sample food nutrient database
nutrient_db = {
"egg": {"calories": 78, "protein": 6, "carbs": 0.6, "fat": 5},
"rice": {"calories": 200, "protein": 4, "carbs": 45, "fat": 0.4},
"banana": {"calories": 89, "protein": 1.1, "carbs": 23, "fat": 0.3},
"apple": {"calories": 52, "protein": 0.3, "carbs": 14, "fat": 0.2},
"milk": {"calories": 103, "protein": 8, "carbs": 12, "fat": 2.4},
"bread": {"calories": 66, "protein": 2, "carbs": 12, "fat": 1}
}
def analyze_food(text):
text = text.lower()
total = {"calories": 0, "protein": 0, "carbs": 0, "fat": 0}
found_items = []
for item in nutrient_db:
if item in text:
found_items.append(item)
total["calories"] += nutrient_db[item]["calories"]
total["protein"] += nutrient_db[item]["protein"]
total["carbs"] += nutrient_db[item]["carbs"]
total["fat"] += nutrient_db[item]["fat"]
if not found_items:
return "❌ Sorry! No known food found. Please try common names like egg, rice, banana."
result = f"βœ… Nutrient Summary for: {', '.join(found_items)}\n\n"
result += f"🍽️ Calories: {total['calories']} kcal\n"
result += f"πŸ₯š Protein: {total['protein']} g\n"
result += f"🍞 Carbs: {total['carbs']} g\n"
result += f"🧈 Fat: {total['fat']} g"
return result
demo = gr.Interface(
fn=analyze_food,
inputs=gr.Textbox(placeholder="Type your food: e.g. 'I had rice and egg'", lines=2),
outputs="text",
title="πŸ₯— Food Nutrient Analyzer",
description="Enter 1 or more food items (e.g. egg, banana, rice) and get their total nutrition."
)
demo.launch()