Module_2 / src /streamlit_app.py
srustik123's picture
Update src/streamlit_app.py
0d57756 verified
raw
history blame
3.46 kB
import streamlit as st
from transformers import pipeline
def calculate_bmi(weight, height_cm):
# Convert height from cm to meters
height_m = height_cm / 100
# BMI formula: weight (kg) / (height in meters)^2
bmi = weight / (height_m ** 2)
return round(bmi, 2)
def get_category(bmi):
if bmi < 18.5:
return "Underweight"
elif 18.5 <= bmi < 24.9:
return "Normal"
elif 25 <= bmi < 29.9:
return "Overweight"
else:
return "Obese"
# App UI
st.set_page_config(page_title="FitPlan-AI", page_icon="๐Ÿ’ช")
st.title("๐Ÿ’ช FitPlan-AI: Personalized Fitness Profile")
def load_model():
return pipeline(
"text-generation",
model="google/flan-t5-base"
)
generator = load_model()
with st.form("fitness_form"):
st.header("Personal Information")
name = st.text_input("Full Name*", placeholder="Enter your name")
col1, col2 = st.columns(2)
with col1:
height = st.number_input("Height (cm)*", min_value=1.0, step=0.1)
with col2:
weight = st.number_input("Weight (kg)*", min_value=1.0, step=0.1)
st.header("Fitness Details")
goal = st.selectbox("Fitness Goal", ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"])
level = st.radio("Fitness Level", ["Beginner", "Intermediate", "Advanced"])
equipment = st.multiselect("Available Equipment", ["Dumbbells", "Resistance Band", "Yoga Mat", "No Equipment", "Kettlebell", "Pull-up Bar"])
submit = st.form_submit_button("Generate Profile")
if submit:
# Validation: Ensure name is not empty
if not name.strip():
st.error("Please enter your name.")
elif height <= 0 or weight <= 0:
st.error("Height and Weight must be positive values.")
else:
# BMI Logic
user_bmi = calculate_bmi(weight, height)
category = get_category(user_bmi)
# Display Results
st.success(f"Profile Created Successfully for {name}!")
st.metric(label="Your Calculated BMI", value=user_bmi)
st.info(f"Health Category: **{category}**")
# Summary
st.write(f"**Goal:** {goal} | **Level:** {level}")
st.write(f"**Equipment:** {', '.join(equipment) if equipment else 'None selected'}")
if st.button("Submit Profile"):
if not name:
st.error("Please enter your name.")
elif not equipment:
st.error("Please select at least one equipment option.")
elif bmi is None:
st.error("Please enter valid height and weight.")
else:
st.success("โœ… Profile Submitted Successfully!")
bmi_status = bmi_category(bmi)
equipment_list = ", ".join(equipment)
prompt = f"""
Generate a 5-day structured workout plan.
User Details:
Name: {name}
Gender: {gender}
BMI: {bmi:.2f} ({bmi_status})
Goal: {goal}
Fitness Level: {fitness_level}
Available Equipment: {equipment_list}
Requirements:
- Include warmup
- Include exercises with sets and reps
- Include rest time
- Adjust intensity based on BMI and fitness level
- Keep it structured day-wise
"""
with st.spinner("Generating your AI workout plan..."):
result = generator(prompt, max_new_tokens=400)[0]["generated_text"]
st.subheader("๐Ÿ‹๏ธ Your Personalized Workout Plan")
st.write(result)