yhraje commited on
Commit
8bcf9b2
·
verified ·
1 Parent(s): d65fcb5

Upload 4 files

Browse files
Files changed (4) hide show
  1. .env +2 -0
  2. app.py +104 -0
  3. nutrition_agent.py +34 -0
  4. requirements.txt +16 -0
.env ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ GOOGLE_API_KEY="AIzaSyDYaELCHIBCrMqOylBMumTyDtHwd4KgyzM"
2
+
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ st.set_page_config(page_title="Smartest AI Nutrition Assistant", layout="wide")
16
+
17
+ st.title("🥗 Smartest AI Nutrition Assistant")
18
+
19
+ # Tabs
20
+ tab1, tab2 = st.tabs(["📋 Get Personalized Diet Plan", "📷 Image-Based Nutrition Analysis"])
21
+
22
+ # ---------------- Tab 1: Personalized Diet Plan ----------------
23
+ with tab1:
24
+ st.subheader("📋 Fill the Form to Get Your Personalized Diet Plan")
25
+
26
+ with st.form("diet_form"):
27
+ name = st.text_input("What is your name?")
28
+ age = st.text_input("Age")
29
+ gender = st.selectbox("Gender", ["Male", "Female", "Other"])
30
+ diet_type = st.selectbox("Are you Vegetarian or Non-Vegetarian?", ["Vegetarian", "Non-Vegetarian"])
31
+ location = st.text_input("Geographical Location (e.g., India, US)")
32
+ health_issues = st.text_input("Any existing health complaints?")
33
+ health_goals = st.text_input("Your health/fitness goals?")
34
+ likes = st.text_input("Foods you like")
35
+ dislikes = st.text_input("Foods you dislike")
36
+ calories = st.text_input("Approximate daily calorie requirement (if known)")
37
+
38
+ submitted = st.form_submit_button("Generate Diet Plan")
39
+
40
+ if submitted:
41
+ user_profile = (
42
+ f"My name is {name}. I am {age} years old, {gender}, {diet_type}, living in {location}. "
43
+ f"I have the following health issues: {health_issues}. "
44
+ f"My goal is to {health_goals}. I like eating {likes} but dislike {dislikes}. "
45
+ f"My daily calorie need is {calories}. "
46
+ "Please generate a personalized meal plan broken into breakfast, lunch, snacks, dinner. "
47
+ "Also include a clear explanation of nutritional content and a conclusion for health advice."
48
+ )
49
+
50
+ with st.spinner("Generating your personalized meal plan..."):
51
+ result = nutrition_agent.run(user_profile)
52
+
53
+ # Clean output
54
+ if result:
55
+ if isinstance(result, str):
56
+ st.markdown(result)
57
+ elif hasattr(result, 'content'):
58
+ st.markdown(result.content)
59
+ else:
60
+ st.write(result)
61
+
62
+
63
+ # ---------------- Tab 2: Image-Based Nutrition Analysis ----------------
64
+ with tab2:
65
+ st.subheader("📷 Upload or Paste a Food Image(JPG/JPEG/PNG only) URL")
66
+
67
+ image = None # Initialize image variable
68
+ image_input_type = st.radio("Choose input method:", ["Upload Image", "Paste Image URL"])
69
+
70
+ if image_input_type == "Upload Image":
71
+ uploaded_file = st.file_uploader("Upload a food image", type=["png", "jpg", "jpeg"])
72
+ if uploaded_file:
73
+ try:
74
+ image = Image.open(uploaded_file)
75
+ except Exception as e:
76
+ st.error(f"Error opening image: {e}")
77
+ else:
78
+ image_url = st.text_input("Enter image URL")
79
+ if image_url:
80
+ try:
81
+ response = requests.get(image_url)
82
+ response.raise_for_status()
83
+ image = Image.open(BytesIO(response.content))
84
+ except Exception as e:
85
+ st.error(f"Error loading image: {e}")
86
+
87
+ if image is not None:
88
+ st.image(image, caption="Selected Image", use_column_width=True)
89
+
90
+ if st.button("Analyze Nutrition"):
91
+ prompt = (
92
+ "Analyze the nutritional content of the food shown in this image. "
93
+ "Identify the type of food, estimate calories, and mention if it is healthy or not."
94
+ )
95
+ try:
96
+ model = genai.GenerativeModel('gemini-2.5-flash-preview-04-17')
97
+ with st.spinner("Analyzing image..."):
98
+ result = model.generate_content([prompt, image])
99
+ st.markdown("### 🧠 Nutrition Analysis Result")
100
+ st.markdown(result.text)
101
+ except Exception as e:
102
+ st.error(f"Failed to analyze image: {e}")
103
+ else:
104
+ st.info("Please upload an image or paste a valid image URL to analyze.")
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
+ )
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Virtual Environment Creation:
2
+ py -3.11 -m venv myenv
3
+ .\myenv\Scripts\activate
4
+
5
+ #Install all the dependencies:
6
+ 1. pip install google-generativeai==0.4.0 Pillow==10.3.0 opencv-python==4.9.0.80 numpy==1.26.4 duckduckgo-search==4.4
7
+ 2. pip install streamlit==1.34.0 rich==13.7.1 python-dotenv==1.0.1 requests==2.31.0 phi==0.1.95 phidata==0.2.20
8
+ 3. pip install --upgrade phi google-generativeai
9
+ 4. pip install phidata
10
+
11
+ To run the app.py:
12
+
13
+ 1. streamlit run app.py
14
+ 2. if streamlit run app.py is not recongnised, Please do
15
+ pip install streamlit
16
+ streamlit run app.py