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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
"""
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
import streamlit as st
|
| 5 |
from groq import Groq
|
| 6 |
|
| 7 |
+
# ----------------------------
|
| 8 |
+
# CONFIG
|
| 9 |
+
# ----------------------------
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Diet Pal",
|
| 12 |
+
page_icon="π₯",
|
| 13 |
+
layout="centered"
|
| 14 |
+
)
|
| 15 |
|
| 16 |
+
# ----------------------------
|
| 17 |
+
# API KEY
|
| 18 |
+
# ----------------------------
|
| 19 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 20 |
client = Groq(api_key=GROQ_API_KEY)
|
| 21 |
|
| 22 |
+
# ----------------------------
|
| 23 |
+
# TITLE
|
| 24 |
+
# ----------------------------
|
| 25 |
+
st.title("π₯ Diet Pal")
|
| 26 |
+
st.caption("Your AI Nutrition Assistant | Recipes + Diet Plans")
|
| 27 |
+
|
| 28 |
+
# ----------------------------
|
| 29 |
+
# MAIN MENU
|
| 30 |
+
# ----------------------------
|
| 31 |
+
feature = st.radio(
|
| 32 |
+
"Choose an option:",
|
| 33 |
+
["π³ Recipe Generator", "π Diet Planner"]
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# ==================================================
|
| 37 |
+
# RECIPE GENERATOR
|
| 38 |
+
# ==================================================
|
| 39 |
+
if feature == "π³ Recipe Generator":
|
| 40 |
+
|
| 41 |
+
st.markdown("## π½οΈ Smart Recipe Generator")
|
| 42 |
+
|
| 43 |
+
ingredients = st.text_input(
|
| 44 |
+
"π₯ What ingredients do you have?",
|
| 45 |
+
placeholder="e.g. chicken, tomato, rice"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
cuisine = st.text_input(
|
| 49 |
+
"π What type of cuisine do you prefer?",
|
| 50 |
+
placeholder="Optional: Chinese, Pakistani, Italian..."
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
if st.button("π³ Generate Recipes"):
|
| 54 |
+
|
| 55 |
+
if ingredients.strip():
|
| 56 |
+
|
| 57 |
+
with st.spinner("Thinking of delicious recipes..."):
|
| 58 |
+
|
| 59 |
+
prompt = f"""
|
| 60 |
+
You are Diet Pal AI.
|
| 61 |
+
|
| 62 |
+
The user has these ingredients:
|
| 63 |
+
{ingredients}
|
| 64 |
+
|
| 65 |
+
Preferred cuisine:
|
| 66 |
+
{cuisine if cuisine else "Any"}
|
| 67 |
+
|
| 68 |
+
Generate 3 recipes:
|
| 69 |
+
|
| 70 |
+
1. Low calorie recipe
|
| 71 |
+
2. Moderate calorie recipe
|
| 72 |
+
3. High calorie recipe
|
| 73 |
+
|
| 74 |
+
For each recipe give:
|
| 75 |
+
|
| 76 |
+
- Recipe Name
|
| 77 |
+
- Estimated Calories
|
| 78 |
+
- Ingredients
|
| 79 |
+
- Steps
|
| 80 |
+
- Protein / Carbs / Fats
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
chat_completion = client.chat.completions.create(
|
| 84 |
+
messages=[
|
| 85 |
+
{"role": "user", "content": prompt}
|
| 86 |
+
],
|
| 87 |
+
model="llama-3.3-70b-versatile"
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
result = chat_completion.choices[0].message.content
|
| 91 |
+
|
| 92 |
+
st.success("π³ Recipes Generated!")
|
| 93 |
+
st.markdown("### π Your Recipes")
|
| 94 |
+
st.markdown(result)
|
| 95 |
+
|
| 96 |
+
else:
|
| 97 |
+
st.warning("Please enter ingredients first.")
|
| 98 |
+
|
| 99 |
+
# ==================================================
|
| 100 |
+
# DIET PLANNER
|
| 101 |
+
# ==================================================
|
| 102 |
+
elif feature == "π Diet Planner":
|
| 103 |
+
|
| 104 |
+
st.markdown("## π Personal Diet Planner")
|
| 105 |
+
|
| 106 |
+
goal = st.radio(
|
| 107 |
+
"π― What is your goal?",
|
| 108 |
+
["Lose Weight", "Gain Weight"]
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
age = st.number_input("π Age", min_value=10, max_value=100, value=20)
|
| 112 |
+
|
| 113 |
+
weight = st.number_input(
|
| 114 |
+
"βοΈ Weight (kg)",
|
| 115 |
+
min_value=20,
|
| 116 |
+
max_value=250,
|
| 117 |
+
value=70
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
height = st.number_input(
|
| 121 |
+
"π Height (cm)",
|
| 122 |
+
min_value=100,
|
| 123 |
+
max_value=250,
|
| 124 |
+
value=170
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
gender = st.selectbox(
|
| 128 |
+
"π§ Gender",
|
| 129 |
+
["Male", "Female"]
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
activity = st.selectbox(
|
| 133 |
+
"π Activity Level",
|
| 134 |
+
["Low", "Moderate", "High"]
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
allergies = st.text_input(
|
| 138 |
+
"π« Allergies / Foods to Avoid",
|
| 139 |
+
placeholder="e.g. nuts, milk, gluten"
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
meals = st.selectbox(
|
| 143 |
+
"π½οΈ Meals per Day",
|
| 144 |
+
[3, 4, 5, 6]
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
if st.button("π Generate Diet Plan"):
|
| 148 |
+
|
| 149 |
+
with st.spinner("Creating your custom plan..."):
|
| 150 |
+
|
| 151 |
+
prompt = f"""
|
| 152 |
+
You are Diet Pal AI nutrition expert.
|
| 153 |
+
|
| 154 |
+
Create a personalized one-day diet plan.
|
| 155 |
+
|
| 156 |
+
User Goal: {goal}
|
| 157 |
+
Age: {age}
|
| 158 |
+
Weight: {weight} kg
|
| 159 |
+
Height: {height} cm
|
| 160 |
+
Gender: {gender}
|
| 161 |
+
Activity Level: {activity}
|
| 162 |
+
Allergies: {allergies if allergies else "None"}
|
| 163 |
+
Meals Per Day: {meals}
|
| 164 |
+
|
| 165 |
+
Provide:
|
| 166 |
+
|
| 167 |
+
1. Recommended Daily Calories
|
| 168 |
+
2. Breakfast
|
| 169 |
+
3. Lunch
|
| 170 |
+
4. Dinner
|
| 171 |
+
5. Snacks
|
| 172 |
+
6. Water Intake
|
| 173 |
+
7. Helpful Tips
|
| 174 |
"""
|
| 175 |
+
|
| 176 |
+
chat_completion = client.chat.completions.create(
|
| 177 |
+
messages=[
|
| 178 |
+
{"role": "user", "content": prompt}
|
| 179 |
+
],
|
| 180 |
+
model="llama-3.3-70b-versatile"
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
result = chat_completion.choices[0].message.content
|
| 184 |
+
|
| 185 |
+
st.success("π₯ Diet Plan Ready!")
|
| 186 |
+
st.markdown("### π Your Plan")
|
| 187 |
+
st.markdown(result)
|
| 188 |
+
|
| 189 |
+
# ----------------------------
|
| 190 |
+
# FOOTER
|
| 191 |
+
# ----------------------------
|
| 192 |
+
st.markdown("---")
|
| 193 |
+
st.caption("Powered by Groq + Streamlit π")
|