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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from nutrition_agent import nutrition_agent
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import requests
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import google.generativeai as genai
|
| 7 |
+
import os
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
# Load environment variables
|
| 11 |
+
load_dotenv()
|
| 12 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 13 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 14 |
+
|
| 15 |
+
# Enhanced page configuration
|
| 16 |
+
st.set_page_config(
|
| 17 |
+
page_title="π₯ Smartest AI Nutrition Assistant",
|
| 18 |
+
layout="wide",
|
| 19 |
+
initial_sidebar_state="collapsed",
|
| 20 |
+
page_icon="π₯"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Custom CSS for enhanced UI
|
| 24 |
+
st.markdown("""
|
| 25 |
+
<style>
|
| 26 |
+
/* Main background and theme */
|
| 27 |
+
.main {
|
| 28 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 29 |
+
padding: 2rem;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
/* Hide Streamlit branding */
|
| 33 |
+
#MainMenu {visibility: hidden;}
|
| 34 |
+
footer {visibility: hidden;}
|
| 35 |
+
header {visibility: hidden;}
|
| 36 |
+
|
| 37 |
+
/* Custom title styling */
|
| 38 |
+
.custom-title {
|
| 39 |
+
text-align: center;
|
| 40 |
+
color: white;
|
| 41 |
+
font-size: 3.5rem;
|
| 42 |
+
font-weight: 800;
|
| 43 |
+
margin-bottom: 1rem;
|
| 44 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
| 45 |
+
animation: fadeInDown 1s ease-out;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
.custom-subtitle {
|
| 49 |
+
text-align: center;
|
| 50 |
+
color: rgba(255,255,255,0.9);
|
| 51 |
+
font-size: 1.2rem;
|
| 52 |
+
margin-bottom: 2rem;
|
| 53 |
+
animation: fadeInUp 1s ease-out 0.3s both;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
/* Enhanced tabs */
|
| 57 |
+
.stTabs [data-baseweb="tab-list"] {
|
| 58 |
+
gap: 20px;
|
| 59 |
+
background: rgba(255,255,255,0.1);
|
| 60 |
+
border-radius: 25px;
|
| 61 |
+
padding: 10px;
|
| 62 |
+
backdrop-filter: blur(10px);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
.stTabs [data-baseweb="tab"] {
|
| 66 |
+
height: 60px;
|
| 67 |
+
padding: 0 30px;
|
| 68 |
+
background: rgba(255,255,255,0.2);
|
| 69 |
+
border-radius: 20px;
|
| 70 |
+
color: white;
|
| 71 |
+
font-weight: 600;
|
| 72 |
+
border: 1px solid rgba(255,255,255,0.3);
|
| 73 |
+
transition: all 0.3s ease;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
.stTabs [aria-selected="true"] {
|
| 77 |
+
background: white;
|
| 78 |
+
color: #667eea;
|
| 79 |
+
box-shadow: 0 5px 20px rgba(0,0,0,0.2);
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
/* Form styling */
|
| 83 |
+
.stForm {
|
| 84 |
+
background: white;
|
| 85 |
+
padding: 2rem;
|
| 86 |
+
border-radius: 20px;
|
| 87 |
+
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
|
| 88 |
+
margin: 20px 0;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
/* Fix label visibility */
|
| 92 |
+
.stForm label,
|
| 93 |
+
.stTextInput label,
|
| 94 |
+
.stSelectbox label,
|
| 95 |
+
.stTextArea label,
|
| 96 |
+
.stRadio label,
|
| 97 |
+
.stCheckbox label {
|
| 98 |
+
color: #333 !important;
|
| 99 |
+
font-weight: 600 !important;
|
| 100 |
+
font-size: 1rem !important;
|
| 101 |
+
margin-bottom: 8px !important;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
/* Fix section headings */
|
| 105 |
+
.stForm h4 {
|
| 106 |
+
color: #667eea !important;
|
| 107 |
+
font-weight: 700 !important;
|
| 108 |
+
margin-top: 1.5rem !important;
|
| 109 |
+
margin-bottom: 1rem !important;
|
| 110 |
+
border-bottom: 2px solid #e0e0e0;
|
| 111 |
+
padding-bottom: 0.5rem;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
/* Fix markdown text in forms */
|
| 115 |
+
.stForm .stMarkdown {
|
| 116 |
+
color: #333 !important;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
/* Fix radio button labels */
|
| 120 |
+
.stRadio > div > div > div > label {
|
| 121 |
+
color: #555 !important;
|
| 122 |
+
font-weight: 500 !important;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
/* Input field enhancements */
|
| 126 |
+
.stTextInput > div > div > input,
|
| 127 |
+
.stSelectbox > div > div > select,
|
| 128 |
+
.stTextArea > div > div > textarea {
|
| 129 |
+
border: 2px solid #e0e0e0;
|
| 130 |
+
border-radius: 10px;
|
| 131 |
+
padding: 12px;
|
| 132 |
+
font-size: 1rem;
|
| 133 |
+
transition: all 0.3s ease;
|
| 134 |
+
background: #f8f9fa;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
.stTextInput > div > div > input:focus,
|
| 138 |
+
.stSelectbox > div > div > select:focus,
|
| 139 |
+
.stTextArea > div > div > textarea:focus {
|
| 140 |
+
border-color: #667eea;
|
| 141 |
+
background: white;
|
| 142 |
+
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
/* Button enhancements */
|
| 146 |
+
.stButton > button {
|
| 147 |
+
width: 100%;
|
| 148 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 149 |
+
color: white;
|
| 150 |
+
border: none;
|
| 151 |
+
border-radius: 25px;
|
| 152 |
+
padding: 15px 30px;
|
| 153 |
+
font-size: 1.1rem;
|
| 154 |
+
font-weight: 600;
|
| 155 |
+
transition: all 0.3s ease;
|
| 156 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
.stButton > button:hover {
|
| 160 |
+
transform: translateY(-2px);
|
| 161 |
+
box-shadow: 0 10px 25px rgba(0,0,0,0.3);
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
/* Card styling for content */
|
| 165 |
+
.content-card {
|
| 166 |
+
background: white;
|
| 167 |
+
padding: 2rem;
|
| 168 |
+
border-radius: 20px;
|
| 169 |
+
box-shadow: 0 10px 30px rgba(0,0,0,0.1);
|
| 170 |
+
margin: 20px 0;
|
| 171 |
+
border-left: 5px solid #667eea;
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
/* Fix text color in content cards */
|
| 175 |
+
.content-card h3,
|
| 176 |
+
.content-card h4,
|
| 177 |
+
.content-card p,
|
| 178 |
+
.content-card div {
|
| 179 |
+
color: #333 !important;
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
/* Fix markdown headings */
|
| 183 |
+
.stMarkdown h3,
|
| 184 |
+
.stMarkdown h4,
|
| 185 |
+
.stMarkdown h5 {
|
| 186 |
+
color: #667eea !important;
|
| 187 |
+
font-weight: 700 !important;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
/* Fix all text content */
|
| 191 |
+
.stMarkdown p,
|
| 192 |
+
.stMarkdown div {
|
| 193 |
+
color: #555 !important;
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
/* Image upload area styling */
|
| 197 |
+
.stFileUploader > div {
|
| 198 |
+
border: 3px dashed #667eea;
|
| 199 |
+
border-radius: 15px;
|
| 200 |
+
padding: 2rem;
|
| 201 |
+
text-align: center;
|
| 202 |
+
background: rgba(102, 126, 234, 0.05);
|
| 203 |
+
transition: all 0.3s ease;
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
.stFileUploader > div:hover {
|
| 207 |
+
border-color: #764ba2;
|
| 208 |
+
background: rgba(102, 126, 234, 0.1);
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
/* Progress bar styling */
|
| 212 |
+
.stProgress > div > div > div > div {
|
| 213 |
+
background: linear-gradient(90deg, #667eea, #764ba2);
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
/* Alert styling */
|
| 217 |
+
.stAlert {
|
| 218 |
+
border-radius: 15px;
|
| 219 |
+
border: none;
|
| 220 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
/* Animation keyframes */
|
| 224 |
+
@keyframes fadeInDown {
|
| 225 |
+
from { opacity: 0; transform: translateY(-30px); }
|
| 226 |
+
to { opacity: 1; transform: translateY(0); }
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
@keyframes fadeInUp {
|
| 230 |
+
from { opacity: 0; transform: translateY(30px); }
|
| 231 |
+
to { opacity: 1; transform: translateY(0); }
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
/* Feature icons */
|
| 235 |
+
.feature-icon {
|
| 236 |
+
font-size: 2rem;
|
| 237 |
+
margin-bottom: 0.5rem;
|
| 238 |
+
display: block;
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
/* Grid layout for forms */
|
| 242 |
+
.form-grid {
|
| 243 |
+
display: grid;
|
| 244 |
+
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
| 245 |
+
gap: 20px;
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
/* Additional text visibility fixes */
|
| 249 |
+
.stApp > div > div > div > div {
|
| 250 |
+
color: inherit;
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
/* Force white background for form containers */
|
| 254 |
+
.element-container:has(.stForm) {
|
| 255 |
+
background: white;
|
| 256 |
+
border-radius: 20px;
|
| 257 |
+
padding: 1rem;
|
| 258 |
+
margin: 1rem 0;
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
/* Ensure all text in white containers is dark */
|
| 262 |
+
div[data-testid="stForm"] {
|
| 263 |
+
background: white !important;
|
| 264 |
+
color: #333 !important;
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
div[data-testid="stForm"] * {
|
| 268 |
+
color: #333 !important;
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
/* Override any inherited white text */
|
| 272 |
+
.stTextInput > label,
|
| 273 |
+
.stSelectbox > label,
|
| 274 |
+
.stTextArea > label {
|
| 275 |
+
color: #333 !important;
|
| 276 |
+
font-weight: 600 !important;
|
| 277 |
+
}
|
| 278 |
+
</style>
|
| 279 |
+
""", unsafe_allow_html=True)
|
| 280 |
+
|
| 281 |
+
# Enhanced Header
|
| 282 |
+
st.markdown('<h1 class="custom-title">π₯ Smartest AI Nutrition Assistant</h1>', unsafe_allow_html=True)
|
| 283 |
+
st.markdown('<p class="custom-subtitle">Your personal AI-powered nutritionist for healthier living</p>', unsafe_allow_html=True)
|
| 284 |
+
|
| 285 |
+
# Feature highlights
|
| 286 |
+
col1, col2, col3 = st.columns(3)
|
| 287 |
+
with col1:
|
| 288 |
+
st.markdown("""
|
| 289 |
+
<div style="text-align: center; color: white; padding: 20px;">
|
| 290 |
+
<span class="feature-icon">π―</span>
|
| 291 |
+
<h3>Personalized Plans</h3>
|
| 292 |
+
<p>Customized diet plans based on your unique profile</p>
|
| 293 |
+
</div>
|
| 294 |
+
""", unsafe_allow_html=True)
|
| 295 |
+
|
| 296 |
+
with col2:
|
| 297 |
+
st.markdown("""
|
| 298 |
+
<div style="text-align: center; color: white; padding: 20px;">
|
| 299 |
+
<span class="feature-icon">π±</span>
|
| 300 |
+
<h3>Smart Analysis</h3>
|
| 301 |
+
<p>AI-powered food image recognition and analysis</p>
|
| 302 |
+
</div>
|
| 303 |
+
""", unsafe_allow_html=True)
|
| 304 |
+
|
| 305 |
+
with col3:
|
| 306 |
+
st.markdown("""
|
| 307 |
+
<div style="text-align: center; color: white; padding: 20px;">
|
| 308 |
+
<span class="feature-icon">β‘</span>
|
| 309 |
+
<h3>Instant Results</h3>
|
| 310 |
+
<p>Get nutrition insights in seconds</p>
|
| 311 |
+
</div>
|
| 312 |
+
""", unsafe_allow_html=True)
|
| 313 |
+
|
| 314 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
| 315 |
+
|
| 316 |
+
# Enhanced Tabs
|
| 317 |
+
tab1, tab2 = st.tabs(["π Get Personalized Diet Plan", "π· Image-Based Nutrition Analysis"])
|
| 318 |
+
|
| 319 |
+
# ---------------- Tab 1: Enhanced Personalized Diet Plan ----------------
|
| 320 |
+
with tab1:
|
| 321 |
+
st.markdown('<div class="content-card">', unsafe_allow_html=True)
|
| 322 |
+
st.markdown("### π Create Your Personal Nutrition Profile")
|
| 323 |
+
st.markdown("Fill out the form below to receive a customized diet plan tailored to your needs and goals.")
|
| 324 |
+
|
| 325 |
+
with st.form("diet_form", clear_on_submit=False):
|
| 326 |
+
# Personal Information Section
|
| 327 |
+
st.markdown("#### π€ Personal Information")
|
| 328 |
+
col1, col2 = st.columns(2)
|
| 329 |
+
|
| 330 |
+
with col1:
|
| 331 |
+
name = st.text_input("Full Name", placeholder="Enter your full name")
|
| 332 |
+
age = st.text_input("Age", placeholder="e.g., 25")
|
| 333 |
+
gender = st.selectbox("Gender", ["Select...", "Male", "Female", "Other"])
|
| 334 |
+
|
| 335 |
+
with col2:
|
| 336 |
+
location = st.text_input("Location", placeholder="e.g., Mumbai, India")
|
| 337 |
+
diet_type = st.selectbox("Diet Preference", ["Select...", "Vegetarian", "Non-Vegetarian", "Vegan"])
|
| 338 |
+
calories = st.text_input("Daily Calorie Target", placeholder="e.g., 2000 (optional)")
|
| 339 |
+
|
| 340 |
+
st.markdown("#### π― Health & Goals")
|
| 341 |
+
col3, col4 = st.columns(2)
|
| 342 |
+
|
| 343 |
+
with col3:
|
| 344 |
+
health_issues = st.text_area("Health Conditions",
|
| 345 |
+
placeholder="Any existing health issues, allergies, or medical conditions",
|
| 346 |
+
height=100)
|
| 347 |
+
health_goals = st.text_area("Fitness Goals",
|
| 348 |
+
placeholder="e.g., Weight loss, muscle gain, better energy",
|
| 349 |
+
height=100)
|
| 350 |
+
|
| 351 |
+
with col4:
|
| 352 |
+
likes = st.text_area("Foods You Love",
|
| 353 |
+
placeholder="List your favorite foods and cuisines",
|
| 354 |
+
height=100)
|
| 355 |
+
dislikes = st.text_area("Foods to Avoid",
|
| 356 |
+
placeholder="Foods you dislike or want to avoid",
|
| 357 |
+
height=100)
|
| 358 |
+
|
| 359 |
+
# Enhanced submit button
|
| 360 |
+
submitted = st.form_submit_button("π Generate My Personal Diet Plan", use_container_width=True)
|
| 361 |
+
|
| 362 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 363 |
+
|
| 364 |
+
if submitted:
|
| 365 |
+
# Validation
|
| 366 |
+
if not name or not age or gender == "Select..." or diet_type == "Select...":
|
| 367 |
+
st.error("β οΈ Please fill in all required fields (Name, Age, Gender, Diet Preference)")
|
| 368 |
+
else:
|
| 369 |
+
user_profile = (
|
| 370 |
+
f"My name is {name}. I am {age} years old, {gender}, {diet_type}, living in {location}. "
|
| 371 |
+
f"I have the following health issues: {health_issues}. "
|
| 372 |
+
f"My goal is to {health_goals}. I like eating {likes} but dislike {dislikes}. "
|
| 373 |
+
f"My daily calorie need is {calories}. "
|
| 374 |
+
"Please generate a personalized meal plan broken into breakfast, lunch, snacks, dinner. "
|
| 375 |
+
"Also include a clear explanation of nutritional content and a conclusion for health advice."
|
| 376 |
+
)
|
| 377 |
+
|
| 378 |
+
with st.spinner("π Creating your personalized nutrition plan..."):
|
| 379 |
+
progress_bar = st.progress(0)
|
| 380 |
+
for i in range(100):
|
| 381 |
+
progress_bar.progress(i + 1)
|
| 382 |
+
|
| 383 |
+
result = nutrition_agent.run(user_profile)
|
| 384 |
+
progress_bar.empty()
|
| 385 |
+
|
| 386 |
+
# Enhanced result display
|
| 387 |
+
if result:
|
| 388 |
+
st.markdown('<div class="content-card">', unsafe_allow_html=True)
|
| 389 |
+
st.markdown("### π Your Personalized Diet Plan")
|
| 390 |
+
st.success("β
Your custom nutrition plan has been generated!")
|
| 391 |
+
|
| 392 |
+
if isinstance(result, str):
|
| 393 |
+
st.markdown(result)
|
| 394 |
+
elif hasattr(result, 'content'):
|
| 395 |
+
st.markdown(result.content)
|
| 396 |
+
else:
|
| 397 |
+
st.write(result)
|
| 398 |
+
|
| 399 |
+
# Add download button
|
| 400 |
+
st.download_button(
|
| 401 |
+
label="π₯ Download Diet Plan",
|
| 402 |
+
data=str(result),
|
| 403 |
+
file_name=f"{name}_diet_plan.txt",
|
| 404 |
+
mime="text/plain"
|
| 405 |
+
)
|
| 406 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 407 |
+
|
| 408 |
+
# ---------------- Tab 2: Enhanced Image-Based Nutrition Analysis ----------------
|
| 409 |
+
with tab2:
|
| 410 |
+
st.markdown('<div class="content-card">', unsafe_allow_html=True)
|
| 411 |
+
st.markdown("### π· Smart Food Analysis")
|
| 412 |
+
st.markdown("Upload a food image or paste an image URL to get instant nutritional analysis powered by AI.")
|
| 413 |
+
|
| 414 |
+
image = None
|
| 415 |
+
image_input_type = st.radio(
|
| 416 |
+
"Choose your preferred input method:",
|
| 417 |
+
["π Upload Image", "π Paste Image URL"],
|
| 418 |
+
horizontal=True
|
| 419 |
+
)
|
| 420 |
+
|
| 421 |
+
if image_input_type == "π Upload Image":
|
| 422 |
+
uploaded_file = st.file_uploader(
|
| 423 |
+
"Drop your food image here or click to browse",
|
| 424 |
+
type=["png", "jpg", "jpeg"],
|
| 425 |
+
help="Supported formats: PNG, JPG, JPEG"
|
| 426 |
+
)
|
| 427 |
+
if uploaded_file:
|
| 428 |
+
try:
|
| 429 |
+
image = Image.open(uploaded_file)
|
| 430 |
+
st.success("β
Image uploaded successfully!")
|
| 431 |
+
except Exception as e:
|
| 432 |
+
st.error(f"β Error opening image: {e}")
|
| 433 |
+
else:
|
| 434 |
+
image_url = st.text_input(
|
| 435 |
+
"Enter image URL",
|
| 436 |
+
placeholder="https://example.com/food-image.jpg",
|
| 437 |
+
help="Paste a direct link to your food image"
|
| 438 |
+
)
|
| 439 |
+
if image_url:
|
| 440 |
+
try:
|
| 441 |
+
with st.spinner("π Loading image from URL..."):
|
| 442 |
+
response = requests.get(image_url)
|
| 443 |
+
response.raise_for_status()
|
| 444 |
+
image = Image.open(BytesIO(response.content))
|
| 445 |
+
st.success("β
Image loaded successfully!")
|
| 446 |
+
except Exception as e:
|
| 447 |
+
st.error(f"β Error loading image: {e}")
|
| 448 |
+
|
| 449 |
+
if image is not None:
|
| 450 |
+
# Enhanced image display
|
| 451 |
+
col1, col2 = st.columns([2, 1])
|
| 452 |
+
|
| 453 |
+
with col1:
|
| 454 |
+
st.image(image, caption="π½οΈ Selected Food Image", use_column_width=True)
|
| 455 |
+
|
| 456 |
+
with col2:
|
| 457 |
+
st.markdown("#### π Analysis Options")
|
| 458 |
+
analysis_depth = st.selectbox(
|
| 459 |
+
"Analysis Detail Level",
|
| 460 |
+
["Basic Analysis", "Detailed Analysis", "Health Recommendations"]
|
| 461 |
+
)
|
| 462 |
+
|
| 463 |
+
include_calories = st.checkbox("Include Calorie Estimation", value=True)
|
| 464 |
+
include_health = st.checkbox("Include Health Rating", value=True)
|
| 465 |
+
include_ingredients = st.checkbox("Identify Ingredients", value=False)
|
| 466 |
+
|
| 467 |
+
if st.button("π¬ Analyze Food Nutrition", use_container_width=True):
|
| 468 |
+
# Enhanced prompt based on user selections
|
| 469 |
+
prompt_parts = ["Analyze the nutritional content of the food shown in this image."]
|
| 470 |
+
|
| 471 |
+
if analysis_depth == "Detailed Analysis":
|
| 472 |
+
prompt_parts.append("Provide detailed nutritional breakdown including macronutrients and micronutrients.")
|
| 473 |
+
elif analysis_depth == "Health Recommendations":
|
| 474 |
+
prompt_parts.append("Include health recommendations and dietary suggestions.")
|
| 475 |
+
|
| 476 |
+
if include_calories:
|
| 477 |
+
prompt_parts.append("Estimate the caloric content.")
|
| 478 |
+
|
| 479 |
+
if include_health:
|
| 480 |
+
prompt_parts.append("Rate the healthiness of this food on a scale of 1-10.")
|
| 481 |
+
|
| 482 |
+
if include_ingredients:
|
| 483 |
+
prompt_parts.append("Identify and list the main ingredients visible.")
|
| 484 |
+
|
| 485 |
+
prompt = " ".join(prompt_parts)
|
| 486 |
+
|
| 487 |
+
try:
|
| 488 |
+
model = genai.GenerativeModel('gemini-2.5-flash-preview-04-17')
|
| 489 |
+
with st.spinner("π€ AI is analyzing your food image..."):
|
| 490 |
+
progress_bar = st.progress(0)
|
| 491 |
+
for i in range(100):
|
| 492 |
+
progress_bar.progress(i + 1)
|
| 493 |
+
|
| 494 |
+
result = model.generate_content([prompt, image])
|
| 495 |
+
progress_bar.empty()
|
| 496 |
+
|
| 497 |
+
# Enhanced result display
|
| 498 |
+
st.markdown("---")
|
| 499 |
+
st.markdown("### π§ AI Nutrition Analysis Results")
|
| 500 |
+
st.success("β
Analysis complete!")
|
| 501 |
+
|
| 502 |
+
# Create expandable sections for better organization
|
| 503 |
+
with st.expander("π Detailed Analysis", expanded=True):
|
| 504 |
+
st.markdown(result.text)
|
| 505 |
+
|
| 506 |
+
# Add action buttons
|
| 507 |
+
col1, col2, col3 = st.columns(3)
|
| 508 |
+
with col1:
|
| 509 |
+
st.download_button(
|
| 510 |
+
label="π₯ Save Analysis",
|
| 511 |
+
data=result.text,
|
| 512 |
+
file_name="nutrition_analysis.txt",
|
| 513 |
+
mime="text/plain"
|
| 514 |
+
)
|
| 515 |
+
with col2:
|
| 516 |
+
if st.button("π Analyze Again"):
|
| 517 |
+
st.experimental_rerun()
|
| 518 |
+
with col3:
|
| 519 |
+
if st.button("π€ Share Results"):
|
| 520 |
+
st.info("π Copy the analysis text to share with others!")
|
| 521 |
+
|
| 522 |
+
except Exception as e:
|
| 523 |
+
st.error(f"β Analysis failed: {e}")
|
| 524 |
+
st.info("π‘ Try uploading a clearer image or check your internet connection.")
|
| 525 |
+
else:
|
| 526 |
+
# Enhanced placeholder
|
| 527 |
+
st.markdown("""
|
| 528 |
+
<div style="text-align: center; padding: 3rem; background: rgba(102, 126, 234, 0.05); border-radius: 15px; border: 2px dashed #667eea;">
|
| 529 |
+
<h3>π½οΈ Ready to Analyze Your Food?</h3>
|
| 530 |
+
<p>Upload an image or paste a URL to get started with AI-powered nutrition analysis!</p>
|
| 531 |
+
</div>
|
| 532 |
+
""", unsafe_allow_html=True)
|
| 533 |
+
|
| 534 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 535 |
+
|
| 536 |
+
# Enhanced Footer
|
| 537 |
+
st.markdown("---")
|
| 538 |
+
st.markdown("""
|
| 539 |
+
<div style="text-align: center; color: white; padding: 20px;">
|
| 540 |
+
<p>π‘ <strong>Pro Tip:</strong> For best results, use clear, well-lit images of your food!</p>
|
| 541 |
+
<p>π€ Powered by AI β’ π₯ Built for Health β’ β€οΈ Made with Streamlit</p>
|
| 542 |
+
</div>
|
| 543 |
+
""", unsafe_allow_html=True)
|
nutrition_agent.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from phi.agent import Agent
|
| 2 |
+
from phi.model.google import Gemini
|
| 3 |
+
import os
|
| 4 |
+
from phi.tools.duckduckgo import DuckDuckGo
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
import google.generativeai as genai
|
| 7 |
+
|
| 8 |
+
# Load API key
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Define the nutrition assistant agent
|
| 15 |
+
nutrition_agent = Agent(
|
| 16 |
+
name="nutrition_agent",
|
| 17 |
+
model=Gemini(id="gemini-2.5-flash-preview-04-17"),
|
| 18 |
+
#gemini-2.5-pro-exp-03-25
|
| 19 |
+
#gemini-2.5-pro-preview-05-06
|
| 20 |
+
tools=[DuckDuckGo()],
|
| 21 |
+
|
| 22 |
+
instructions=[
|
| 23 |
+
"You are the smartest AI Nutrition Assistant. ",
|
| 24 |
+
"Ask the user for their name, age, gender, dietary preference (veg/non-veg), location, "
|
| 25 |
+
"health complaints, fitness goals, food likes/dislikes, and calorie needs. "
|
| 26 |
+
"Then provide a detailed personalized diet plan (breakfast/lunch/dinner) "
|
| 27 |
+
"along with clear nutritional explanations tailored to their inputs.",
|
| 28 |
+
"Always respond in well-structured markdown format using clear headings like ### Breakfast, ### Lunch, etc.",
|
| 29 |
+
"Do not generate one big paragraph. Instead, use bullet points, headings, and whitespace for clarity."
|
| 30 |
+
|
| 31 |
+
],
|
| 32 |
+
show_tool_calls=True,
|
| 33 |
+
markdown=True
|
| 34 |
+
)
|