File size: 1,682 Bytes
0bc52f9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 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() |