Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,37 +11,36 @@ def identify_dish(image_bytes):
|
|
11 |
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
|
12 |
dish_name = ""
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
31 |
return dish_name.strip()
|
32 |
|
33 |
-
# 2. Function to
|
34 |
-
def calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_level
|
35 |
-
# Calculate BMI
|
36 |
bmi = weight_kg / ((height_cm / 100) ** 2)
|
37 |
|
38 |
-
# Calculate BMR using Mifflin-St Jeor formula
|
39 |
if gender == "male":
|
40 |
bmr = 10 * weight_kg + 6.25 * height_cm - 5 * age + 5
|
41 |
else:
|
42 |
bmr = 10 * weight_kg + 6.25 * height_cm - 5 * age - 161
|
43 |
|
44 |
-
# Calculate TDEE (Total Daily Energy Expenditure)
|
45 |
activity_multipliers = {
|
46 |
"sedentary": 1.2,
|
47 |
"light": 1.375,
|
@@ -51,21 +50,18 @@ def calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_l
|
|
51 |
}
|
52 |
tdee = bmr * activity_multipliers[activity_level]
|
53 |
|
54 |
-
# Calculate Ideal Body Weight (IBW) using Hamwi method
|
55 |
if gender == "male":
|
56 |
ibw = 50 + (0.91 * (height_cm - 152.4))
|
57 |
else:
|
58 |
ibw = 45.5 + (0.91 * (height_cm - 152.4))
|
59 |
|
60 |
-
# Calculate Daily Caloric Needs
|
61 |
if weight_goal == "loss":
|
62 |
-
daily_caloric_needs = tdee - 500
|
63 |
elif weight_goal == "gain":
|
64 |
-
daily_caloric_needs = tdee + 500
|
65 |
else:
|
66 |
daily_caloric_needs = tdee
|
67 |
|
68 |
-
# Calculate macronutrient calories
|
69 |
protein_calories = daily_caloric_needs * 0.2
|
70 |
fat_calories = daily_caloric_needs * 0.25
|
71 |
carbohydrate_calories = daily_caloric_needs * 0.55
|
@@ -90,9 +86,7 @@ def generate_diet_plan(dish_name, calorie_intake_per_day, goal):
|
|
90 |
- Dish Name: {dish_name}
|
91 |
- Caloric Intake per Day: {calorie_intake_per_day} calories
|
92 |
- Goal: {goal}
|
93 |
-
|
94 |
"""
|
95 |
-
# Request from the dietitian model
|
96 |
response = client.chat_completion(
|
97 |
model="meta-llama/Meta-Llama-3-8B-Instruct",
|
98 |
messages=[{"role": "You are a certified Dietitian with 20 years of Experience", "content": user_input}],
|
@@ -112,31 +106,47 @@ weight_goal = st.sidebar.selectbox("Weight goal", ["loss", "gain", "maintain"])
|
|
112 |
activity_level = st.sidebar.selectbox("Activity level", ["sedentary", "light", "moderate", "active", "very active"])
|
113 |
time_frame = st.sidebar.number_input("Time frame to achieve goal (months)", min_value=1)
|
114 |
|
115 |
-
#
|
116 |
-
if
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
# CSS for styling
|
137 |
st.markdown("""
|
138 |
<style>
|
139 |
.stButton button { background-color: #4CAF50; color: white; }
|
|
|
140 |
.stContainer { border: 1px solid #ddd; padding: 20px; margin-bottom: 20px; }
|
|
|
|
|
|
|
141 |
</style>
|
142 |
""", unsafe_allow_html=True)
|
|
|
|
11 |
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
|
12 |
dish_name = ""
|
13 |
|
14 |
+
try:
|
15 |
+
for message in client.chat_completion(
|
16 |
+
model="meta-llama/Llama-3.2-11B-Vision-Instruct",
|
17 |
+
messages=[
|
18 |
+
{
|
19 |
+
"role": "You are a food identification expert who identifies dishes from images. Your task is to strictly return the names of the dishes present in the image. Only return the dish names if you have high Confidence Level and without additional explanation or description.",
|
20 |
+
"content": [
|
21 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}" }},
|
22 |
+
{"type": "text", "text": "Identify the dishes in the image and return only the names of the dishes."},
|
23 |
+
],
|
24 |
+
}
|
25 |
+
],
|
26 |
+
max_tokens=70,
|
27 |
+
stream=True,
|
28 |
+
):
|
29 |
+
if message.choices[0].delta.content:
|
30 |
+
dish_name += message.choices[0].delta.content
|
31 |
+
except:
|
32 |
+
st.error("Error processing the image. Please upload a valid image.")
|
33 |
return dish_name.strip()
|
34 |
|
35 |
+
# 2. Function to calculate metrics
|
36 |
+
def calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_level):
|
|
|
37 |
bmi = weight_kg / ((height_cm / 100) ** 2)
|
38 |
|
|
|
39 |
if gender == "male":
|
40 |
bmr = 10 * weight_kg + 6.25 * height_cm - 5 * age + 5
|
41 |
else:
|
42 |
bmr = 10 * weight_kg + 6.25 * height_cm - 5 * age - 161
|
43 |
|
|
|
44 |
activity_multipliers = {
|
45 |
"sedentary": 1.2,
|
46 |
"light": 1.375,
|
|
|
50 |
}
|
51 |
tdee = bmr * activity_multipliers[activity_level]
|
52 |
|
|
|
53 |
if gender == "male":
|
54 |
ibw = 50 + (0.91 * (height_cm - 152.4))
|
55 |
else:
|
56 |
ibw = 45.5 + (0.91 * (height_cm - 152.4))
|
57 |
|
|
|
58 |
if weight_goal == "loss":
|
59 |
+
daily_caloric_needs = tdee - 500
|
60 |
elif weight_goal == "gain":
|
61 |
+
daily_caloric_needs = tdee + 500
|
62 |
else:
|
63 |
daily_caloric_needs = tdee
|
64 |
|
|
|
65 |
protein_calories = daily_caloric_needs * 0.2
|
66 |
fat_calories = daily_caloric_needs * 0.25
|
67 |
carbohydrate_calories = daily_caloric_needs * 0.55
|
|
|
86 |
- Dish Name: {dish_name}
|
87 |
- Caloric Intake per Day: {calorie_intake_per_day} calories
|
88 |
- Goal: {goal}
|
|
|
89 |
"""
|
|
|
90 |
response = client.chat_completion(
|
91 |
model="meta-llama/Meta-Llama-3-8B-Instruct",
|
92 |
messages=[{"role": "You are a certified Dietitian with 20 years of Experience", "content": user_input}],
|
|
|
106 |
activity_level = st.sidebar.selectbox("Activity level", ["sedentary", "light", "moderate", "active", "very active"])
|
107 |
time_frame = st.sidebar.number_input("Time frame to achieve goal (months)", min_value=1)
|
108 |
|
109 |
+
# Submit Button
|
110 |
+
if st.sidebar.button("Submit"):
|
111 |
+
if image_file:
|
112 |
+
# Step 1: Identify the dish
|
113 |
+
image_bytes = image_file.read()
|
114 |
+
dish_name = identify_dish(image_bytes)
|
115 |
+
if dish_name:
|
116 |
+
st.write("### Results")
|
117 |
+
st.divider()
|
118 |
+
st.write("**Dish Name Identified:**")
|
119 |
+
st.success(dish_name)
|
120 |
+
|
121 |
+
# Step 2: Perform Calculations
|
122 |
+
metrics = calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_level)
|
123 |
+
st.divider()
|
124 |
+
st.write("**Calculated Metrics:**")
|
125 |
+
st.write(f"**BMI:** {metrics['BMI']:.2f}")
|
126 |
+
st.write(f"**BMR:** {metrics['BMR']:.2f} calories")
|
127 |
+
st.write(f"**TDEE:** {metrics['TDEE']:.2f} calories")
|
128 |
+
st.write(f"**Ideal Body Weight:** {metrics['IBW']:.2f} kg")
|
129 |
+
st.write(f"**Daily Caloric Needs:** {metrics['Daily Caloric Needs']:.2f} calories")
|
130 |
+
|
131 |
+
# Step 3: Generate Diet Plan
|
132 |
+
st.divider()
|
133 |
+
st.write("**Diet Plan Based on Dish & Goal:**")
|
134 |
+
diet_plan = generate_diet_plan(dish_name, metrics["Daily Caloric Needs"], weight_goal)
|
135 |
+
st.info(diet_plan)
|
136 |
+
else:
|
137 |
+
st.error("Unable to identify the dish. Please try again with a different image.")
|
138 |
+
else:
|
139 |
+
st.error("Please upload an image of the dish.")
|
140 |
|
141 |
# CSS for styling
|
142 |
st.markdown("""
|
143 |
<style>
|
144 |
.stButton button { background-color: #4CAF50; color: white; }
|
145 |
+
.stAlert { background-color: #f9f9f9; border: 1px solid #ddd; padding: 10px; }
|
146 |
.stContainer { border: 1px solid #ddd; padding: 20px; margin-bottom: 20px; }
|
147 |
+
.stSuccess { background-color: #e8f5e9; }
|
148 |
+
.stInfo { background-color: #e3f2fd; }
|
149 |
+
.stError { background-color: #ffebee; }
|
150 |
</style>
|
151 |
""", unsafe_allow_html=True)
|
152 |
+
|