Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,20 +2,15 @@ import streamlit as st
|
|
| 2 |
from PIL import Image
|
| 3 |
import google.generativeai as genai
|
| 4 |
import os
|
| 5 |
-
import json
|
| 6 |
|
| 7 |
-
# Configure API key
|
| 8 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 9 |
|
| 10 |
-
# Page config
|
| 11 |
st.set_page_config(page_title="Smart Nutrition Analyzer", page_icon="🥗", layout="centered")
|
| 12 |
|
| 13 |
-
# Title & description
|
| 14 |
st.title("🥗 Smart Nutrition Analyzer")
|
| 15 |
st.caption("AI-Powered Food Recognition & Nutritional Breakdown")
|
| 16 |
st.write("Upload a food photo and receive a structured, professional nutritional analysis.")
|
| 17 |
|
| 18 |
-
# File uploader
|
| 19 |
uploaded_file = st.file_uploader("📸 Upload your meal photo", type=["png", "jpg", "jpeg"])
|
| 20 |
|
| 21 |
if uploaded_file:
|
|
@@ -25,31 +20,21 @@ if uploaded_file:
|
|
| 25 |
if st.button("Analyze Nutrition"):
|
| 26 |
with st.spinner("Analyzing..."):
|
| 27 |
prompt = """
|
| 28 |
-
You are a certified nutritionist. Analyze the uploaded food image and
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
"Sugar": "Number (g)",
|
| 42 |
-
"Sodium": "Number (mg)"
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
Guidelines:
|
| 46 |
-
- Only return data in this structure, no extra text.
|
| 47 |
-
- Be realistic with portion sizes.
|
| 48 |
-
- If unsure, provide the closest reasonable estimate.
|
| 49 |
"""
|
| 50 |
-
|
| 51 |
try:
|
| 52 |
-
# Generate response
|
| 53 |
model = genai.GenerativeModel("gemini-2.5-flash")
|
| 54 |
response = model.generate_content([prompt, image])
|
| 55 |
text = response.text.strip()
|
|
@@ -57,26 +42,29 @@ if uploaded_file:
|
|
| 57 |
if "This is not a food item." in text:
|
| 58 |
st.error(text)
|
| 59 |
else:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# Meal Items Section
|
| 64 |
st.subheader("🍽️ Meal Items")
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# Nutritional Summary Section
|
| 69 |
st.subheader("⚖️ Nutritional Summary")
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
st.success("Nutrition Analysis Complete!")
|
| 82 |
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import google.generativeai as genai
|
| 4 |
import os
|
|
|
|
| 5 |
|
|
|
|
| 6 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 7 |
|
|
|
|
| 8 |
st.set_page_config(page_title="Smart Nutrition Analyzer", page_icon="🥗", layout="centered")
|
| 9 |
|
|
|
|
| 10 |
st.title("🥗 Smart Nutrition Analyzer")
|
| 11 |
st.caption("AI-Powered Food Recognition & Nutritional Breakdown")
|
| 12 |
st.write("Upload a food photo and receive a structured, professional nutritional analysis.")
|
| 13 |
|
|
|
|
| 14 |
uploaded_file = st.file_uploader("📸 Upload your meal photo", type=["png", "jpg", "jpeg"])
|
| 15 |
|
| 16 |
if uploaded_file:
|
|
|
|
| 20 |
if st.button("Analyze Nutrition"):
|
| 21 |
with st.spinner("Analyzing..."):
|
| 22 |
prompt = """
|
| 23 |
+
You are a certified nutritionist. Analyze the uploaded food image and provide a professional nutritional breakdown.
|
| 24 |
+
Format it like this:
|
| 25 |
+
|
| 26 |
+
List of items: [Item 1] (quantity), [Item 2] (quantity)
|
| 27 |
+
Calories: [Number] kcal
|
| 28 |
+
Protein: [Number] g
|
| 29 |
+
Carbohydrates: [Number] g
|
| 30 |
+
Fats: [Number] g
|
| 31 |
+
Fiber: [Number] g
|
| 32 |
+
Sugar: [Number] g
|
| 33 |
+
Sodium: [Number] mg
|
| 34 |
+
|
| 35 |
+
Only return the information in this format, no extra text.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
"""
|
|
|
|
| 37 |
try:
|
|
|
|
| 38 |
model = genai.GenerativeModel("gemini-2.5-flash")
|
| 39 |
response = model.generate_content([prompt, image])
|
| 40 |
text = response.text.strip()
|
|
|
|
| 42 |
if "This is not a food item." in text:
|
| 43 |
st.error(text)
|
| 44 |
else:
|
| 45 |
+
lines = [line for line in text.split("\n") if line.strip()]
|
| 46 |
+
|
|
|
|
|
|
|
| 47 |
st.subheader("🍽️ Meal Items")
|
| 48 |
+
st.write(lines[0].replace("List of items:", "").strip())
|
| 49 |
+
|
|
|
|
|
|
|
| 50 |
st.subheader("⚖️ Nutritional Summary")
|
| 51 |
+
nutrients = {}
|
| 52 |
+
for line in lines[1:]:
|
| 53 |
+
if ":" in line:
|
| 54 |
+
key, value = line.split(":", 1)
|
| 55 |
+
nutrients[key.strip()] = value.strip()
|
| 56 |
+
|
| 57 |
+
if nutrients:
|
| 58 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 59 |
+
col1.metric("Calories", nutrients.get("Calories", "N/A"))
|
| 60 |
+
col2.metric("Protein", nutrients.get("Protein", "N/A"))
|
| 61 |
+
col3.metric("Carbs", nutrients.get("Carbohydrates", "N/A"))
|
| 62 |
+
col4.metric("Fats", nutrients.get("Fats", "N/A"))
|
| 63 |
+
|
| 64 |
+
col1, col2, col3 = st.columns(3)
|
| 65 |
+
col1.metric("Fiber", nutrients.get("Fiber", "N/A"))
|
| 66 |
+
col2.metric("Sugar", nutrients.get("Sugar", "N/A"))
|
| 67 |
+
col3.metric("Sodium", nutrients.get("Sodium", "N/A"))
|
| 68 |
|
| 69 |
st.success("Nutrition Analysis Complete!")
|
| 70 |
|