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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Calorie & Macronutrient Nutrition Calculator
|
| 3 |
+
Hugging Face Spaces Deployment Version
|
| 4 |
+
No APIs | No ML | Pure Python Logic
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# ==============================
|
| 11 |
+
# Calculation Functions
|
| 12 |
+
# ==============================
|
| 13 |
+
|
| 14 |
+
def calculate_bmr(age, gender, weight, height):
|
| 15 |
+
"""Calculate Basal Metabolic Rate using Mifflin-St Jeor equation."""
|
| 16 |
+
if gender == "Male":
|
| 17 |
+
return 10 * weight + 6.25 * height - 5 * age + 5
|
| 18 |
+
else:
|
| 19 |
+
return 10 * weight + 6.25 * height - 5 * age - 161
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def get_activity_factor(activity_level):
|
| 23 |
+
"""Return activity multiplier."""
|
| 24 |
+
factors = {
|
| 25 |
+
"Sedentary": 1.2,
|
| 26 |
+
"Light": 1.375,
|
| 27 |
+
"Moderate": 1.55,
|
| 28 |
+
"Active": 1.725,
|
| 29 |
+
"Very Active": 1.9
|
| 30 |
+
}
|
| 31 |
+
return factors.get(activity_level, 1.2)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def adjust_calories(tdee, goal):
|
| 35 |
+
"""Adjust calories based on goal."""
|
| 36 |
+
if goal == "Weight Loss":
|
| 37 |
+
return tdee - 500
|
| 38 |
+
elif goal == "Muscle Gain":
|
| 39 |
+
return tdee + 300
|
| 40 |
+
else:
|
| 41 |
+
return tdee
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def macro_distribution(goal):
|
| 45 |
+
"""Return macro percentages."""
|
| 46 |
+
ratios = {
|
| 47 |
+
"Weight Loss": (0.40, 0.30, 0.30),
|
| 48 |
+
"Maintenance": (0.50, 0.25, 0.25),
|
| 49 |
+
"Muscle Gain": (0.50, 0.30, 0.20)
|
| 50 |
+
}
|
| 51 |
+
return ratios.get(goal)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def calculate_macros(calories, goal):
|
| 55 |
+
"""Calculate macro grams and calories."""
|
| 56 |
+
carb_pct, protein_pct, fat_pct = macro_distribution(goal)
|
| 57 |
+
|
| 58 |
+
carb_cal = calories * carb_pct
|
| 59 |
+
protein_cal = calories * protein_pct
|
| 60 |
+
fat_cal = calories * fat_pct
|
| 61 |
+
|
| 62 |
+
carb_g = carb_cal / 4
|
| 63 |
+
protein_g = protein_cal / 4
|
| 64 |
+
fat_g = fat_cal / 9
|
| 65 |
+
|
| 66 |
+
return carb_g, protein_g, fat_g, carb_cal, protein_cal, fat_cal
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def calculate_bmi(weight, height):
|
| 70 |
+
"""Calculate BMI and classification."""
|
| 71 |
+
height_m = height / 100
|
| 72 |
+
bmi = weight / (height_m ** 2)
|
| 73 |
+
|
| 74 |
+
if bmi < 18.5:
|
| 75 |
+
category = "Underweight"
|
| 76 |
+
elif 18.5 <= bmi < 25:
|
| 77 |
+
category = "Normal weight"
|
| 78 |
+
elif 25 <= bmi < 30:
|
| 79 |
+
category = "Overweight"
|
| 80 |
+
else:
|
| 81 |
+
category = "Obese"
|
| 82 |
+
|
| 83 |
+
return bmi, category
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def meal_suggestions(goal):
|
| 87 |
+
"""Provide meal suggestions."""
|
| 88 |
+
suggestions = {
|
| 89 |
+
"Weight Loss": (
|
| 90 |
+
"3โ4 meals per day",
|
| 91 |
+
"Focus on high-protein meals and fiber-rich foods. Spread protein evenly."
|
| 92 |
+
),
|
| 93 |
+
"Maintenance": (
|
| 94 |
+
"3โ5 meals per day",
|
| 95 |
+
"Maintain balanced meals with carbs, protein and healthy fats."
|
| 96 |
+
),
|
| 97 |
+
"Muscle Gain": (
|
| 98 |
+
"4โ6 meals per day",
|
| 99 |
+
"Eat protein every 3โ4 hours. Include carbs around workouts."
|
| 100 |
+
)
|
| 101 |
+
}
|
| 102 |
+
return suggestions.get(goal)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# ==============================
|
| 106 |
+
# Main App Logic
|
| 107 |
+
# ==============================
|
| 108 |
+
|
| 109 |
+
def nutrition_calculator(age, gender, weight, height, activity, goal):
|
| 110 |
+
|
| 111 |
+
# Input validation
|
| 112 |
+
if age < 15 or age > 100:
|
| 113 |
+
return "โ Age must be between 15 and 100."
|
| 114 |
+
if weight < 20 or weight > 200:
|
| 115 |
+
return "โ Weight must be between 20 and 200 kg."
|
| 116 |
+
if height < 100 or height > 250:
|
| 117 |
+
return "โ Height must be between 100 and 250 cm."
|
| 118 |
+
|
| 119 |
+
bmr = calculate_bmr(age, gender, weight, height)
|
| 120 |
+
tdee = bmr * get_activity_factor(activity)
|
| 121 |
+
adjusted_calories = adjust_calories(tdee, goal)
|
| 122 |
+
|
| 123 |
+
carb_g, protein_g, fat_g, carb_cal, protein_cal, fat_cal = calculate_macros(adjusted_calories, goal)
|
| 124 |
+
bmi, bmi_category = calculate_bmi(weight, height)
|
| 125 |
+
meals, tip = meal_suggestions(goal)
|
| 126 |
+
|
| 127 |
+
result = f"""
|
| 128 |
+
## ๐ Nutrition Results
|
| 129 |
+
|
| 130 |
+
### ๐ฅ Daily Energy
|
| 131 |
+
- **BMR:** {bmr:.0f} kcal/day
|
| 132 |
+
- **TDEE:** {tdee:.0f} kcal/day
|
| 133 |
+
- **Target Calories ({goal}):** {adjusted_calories:.0f} kcal/day
|
| 134 |
+
|
| 135 |
+
---
|
| 136 |
+
|
| 137 |
+
### ๐ Macronutrient Breakdown
|
| 138 |
+
|
| 139 |
+
| Macro | Grams | Calories |
|
| 140 |
+
|-------|-------|----------|
|
| 141 |
+
| Carbohydrates | {carb_g:.0f} g | {carb_cal:.0f} kcal |
|
| 142 |
+
| Protein | {protein_g:.0f} g | {protein_cal:.0f} kcal |
|
| 143 |
+
| Fat | {fat_g:.0f} g | {fat_cal:.0f} kcal |
|
| 144 |
+
|
| 145 |
+
---
|
| 146 |
+
|
| 147 |
+
### ๐งฎ Body Mass Index (BMI)
|
| 148 |
+
- **BMI:** {bmi:.1f}
|
| 149 |
+
- **Category:** {bmi_category}
|
| 150 |
+
|
| 151 |
+
---
|
| 152 |
+
|
| 153 |
+
### ๐ฝ๏ธ Meal Recommendation
|
| 154 |
+
- Suggested Meals Per Day: **{meals}**
|
| 155 |
+
- Tip: {tip}
|
| 156 |
+
|
| 157 |
+
---
|
| 158 |
+
|
| 159 |
+
โ ๏ธ This calculator provides general guidance only.
|
| 160 |
+
"""
|
| 161 |
+
|
| 162 |
+
return result
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
# ==============================
|
| 166 |
+
# Gradio Interface
|
| 167 |
+
# ==============================
|
| 168 |
+
|
| 169 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 170 |
+
|
| 171 |
+
gr.Markdown("# ๐ฅ Calorie & Macronutrient Calculator")
|
| 172 |
+
gr.Markdown("Estimate your daily calorie needs, macros, BMI and meal suggestions.")
|
| 173 |
+
|
| 174 |
+
with gr.Row():
|
| 175 |
+
with gr.Column():
|
| 176 |
+
age = gr.Number(label="Age", value=25)
|
| 177 |
+
gender = gr.Radio(["Male", "Female"], label="Gender", value="Male")
|
| 178 |
+
weight = gr.Number(label="Weight (kg)", value=70)
|
| 179 |
+
height = gr.Number(label="Height (cm)", value=170)
|
| 180 |
+
activity = gr.Dropdown(
|
| 181 |
+
["Sedentary", "Light", "Moderate", "Active", "Very Active"],
|
| 182 |
+
label="Activity Level",
|
| 183 |
+
value="Moderate"
|
| 184 |
+
)
|
| 185 |
+
goal = gr.Dropdown(
|
| 186 |
+
["Weight Loss", "Maintenance", "Muscle Gain"],
|
| 187 |
+
label="Goal",
|
| 188 |
+
value="Maintenance"
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
calculate_btn = gr.Button("Calculate")
|
| 192 |
+
clear_btn = gr.Button("Reset")
|
| 193 |
+
|
| 194 |
+
with gr.Column():
|
| 195 |
+
output = gr.Markdown()
|
| 196 |
+
|
| 197 |
+
calculate_btn.click(
|
| 198 |
+
nutrition_calculator,
|
| 199 |
+
inputs=[age, gender, weight, height, activity, goal],
|
| 200 |
+
outputs=output
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
clear_btn.click(lambda: "", outputs=output)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
# Required for Hugging Face Spaces
|
| 207 |
+
if __name__ == "__main__":
|
| 208 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 209 |
+
|