DietGenerator / app.py
F24EE's picture
Create app.py
64bfe5c verified
import streamlit as st
# Function to calculate daily calorie needs based on the Mifflin-St Jeor Equation
def calculate_bmr(gender, age, weight, height):
if gender == "Male":
bmr = 10 * weight + 6.25 * height - 5 * age + 5
else:
bmr = 10 * weight + 6.25 * height - 5 * age - 161
return bmr
# Function to calculate daily calorie intake based on activity level
def calculate_calories(bmr, activity_level):
if activity_level == "Sedentary (little to no exercise)":
return bmr * 1.2
elif activity_level == "Lightly Active (light exercise or sports 1-3 days a week)":
return bmr * 1.375
elif activity_level == "Moderately Active (moderate exercise or sports 3-5 days a week)":
return bmr * 1.55
elif activity_level == "Very Active (hard exercise or sports 6-7 days a week)":
return bmr * 1.725
else:
return bmr * 1.9 # Extremely Active
# Streamlit UI setup
st.set_page_config(page_title="Personalized Diet Plan", layout="wide")
# Title and description
st.title("Personalized Diet Plan Generator")
st.markdown("### Generate your daily diet plan based on your information.")
# Create columns for layout
col1, col2, col3 = st.columns(3)
# Column 1: Gender selection
with col1:
gender = st.selectbox("Select your Gender:", ["Male", "Female"])
# Column 2: Age, Weight, and Height input
with col2:
age = st.slider("Enter your age:", 18, 100, 25)
weight = st.number_input("Enter your weight (kg):", value=60, min_value=30, max_value=200)
height = st.number_input("Enter your height (cm):", value=170, min_value=100, max_value=250)
# Column 3: Activity level selection
with col3:
activity_level = st.selectbox(
"Select your activity level:",
[
"Sedentary (little to no exercise)",
"Lightly Active (light exercise or sports 1-3 days a week)",
"Moderately Active (moderate exercise or sports 3-5 days a week)",
"Very Active (hard exercise or sports 6-7 days a week)",
"Extremely Active (very hard exercise or a physically demanding job)"
]
)
# Button to calculate diet plan
if st.button("Generate Diet Plan"):
if age and weight and height and gender and activity_level:
# Calculate BMR (Basal Metabolic Rate)
bmr = calculate_bmr(gender, age, weight, height)
# Calculate daily calorie needs based on activity level
calories_needed = calculate_calories(bmr, activity_level)
# Provide a suggested diet breakdown (50% carbs, 20% protein, 30% fats)
carbs = calories_needed * 0.50 / 4 # 4 calories per gram of carbs
protein = calories_needed * 0.20 / 4 # 4 calories per gram of protein
fats = calories_needed * 0.30 / 9 # 9 calories per gram of fats
st.markdown("### Your Daily Calorie Requirement: {:.2f} Calories".format(calories_needed))
st.markdown("### Suggested Macros Breakdown:")
st.write(f"**Carbohydrates**: {carbs:.2f} g")
st.write(f"**Proteins**: {protein:.2f} g")
st.write(f"**Fats**: {fats:.2f} g")
# Generate a sample diet plan (for simplicity, it's just an example)
st.markdown("### Example Diet Plan:")
st.write("""
**Breakfast**:
- Oatmeal with fruits and nuts (300 calories)
- 1 Boiled Egg (70 calories)
**Lunch**:
- Grilled Chicken Salad with Avocado (400 calories)
- Brown Rice (200 calories)
**Snack**:
- Greek Yogurt with Berries (200 calories)
**Dinner**:
- Grilled Salmon with Steamed Vegetables (400 calories)
- Sweet Potato (200 calories)
**Total Calories**: ~2000 calories (customize according to your needs)
""")
else:
st.error("Please fill in all the required fields.")
# Footer
st.markdown("---")
st.markdown("Created by [Your Name].")