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transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# -------------------------
|
| 8 |
+
# PAGE CONFIG
|
| 9 |
+
# -------------------------
|
| 10 |
+
st.set_page_config(page_title="FitPlan-AI", page_icon="💪")
|
| 11 |
+
|
| 12 |
+
st.title("💪 FitPlan-AI: Personalized Fitness Profile")
|
| 13 |
+
|
| 14 |
+
# -------------------------
|
| 15 |
+
# BMI FUNCTIONS
|
| 16 |
+
# -------------------------
|
| 17 |
+
def calculate_bmi(weight, height_cm):
|
| 18 |
+
height_m = height_cm / 100
|
| 19 |
+
return round(weight / (height_m ** 2), 2)
|
| 20 |
+
|
| 21 |
+
def get_category(bmi):
|
| 22 |
+
if bmi < 18.5:
|
| 23 |
+
return "Underweight"
|
| 24 |
+
elif bmi < 24.9:
|
| 25 |
+
return "Normal"
|
| 26 |
+
elif bmi < 29.9:
|
| 27 |
+
return "Overweight"
|
| 28 |
+
else:
|
| 29 |
+
return "Obese"
|
| 30 |
+
|
| 31 |
+
# -------------------------
|
| 32 |
+
# LOAD MODEL
|
| 33 |
+
# -------------------------
|
| 34 |
+
@st.cache_resource
|
| 35 |
+
def load_model():
|
| 36 |
+
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base")
|
| 37 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
|
| 38 |
+
return tokenizer, model
|
| 39 |
+
|
| 40 |
+
tokenizer, model = load_model()
|
| 41 |
+
|
| 42 |
+
# -------------------------
|
| 43 |
+
# FORM
|
| 44 |
+
# -------------------------
|
| 45 |
+
with st.form("fitness_form"):
|
| 46 |
+
|
| 47 |
+
st.subheader("Personal Information")
|
| 48 |
+
|
| 49 |
+
name = st.text_input("Full Name*", placeholder="Enter your name")
|
| 50 |
+
|
| 51 |
+
col1, col2 = st.columns(2)
|
| 52 |
+
with col1:
|
| 53 |
+
height = st.number_input("Height (cm)*", min_value=1.0, step=0.1)
|
| 54 |
+
with col2:
|
| 55 |
+
weight = st.number_input("Weight (kg)*", min_value=1.0, step=0.1)
|
| 56 |
+
|
| 57 |
+
st.subheader("Fitness Details")
|
| 58 |
+
|
| 59 |
+
goal = st.selectbox(
|
| 60 |
+
"Fitness Goal",
|
| 61 |
+
["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexibility"]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
level = st.radio(
|
| 65 |
+
"Fitness Level",
|
| 66 |
+
["Beginner", "Intermediate", "Advanced"]
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
equipment = st.multiselect(
|
| 70 |
+
"Available Equipment",
|
| 71 |
+
["Dumbbells", "Resistance Band", "Yoga Mat", "No Equipment",
|
| 72 |
+
"Kettlebell", "Pull-up Bar"]
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
submit = st.form_submit_button("Submit Profile")
|
| 76 |
+
|
| 77 |
+
# -------------------------
|
| 78 |
+
# HANDLE SUBMISSION
|
| 79 |
+
# -------------------------
|
| 80 |
+
if submit:
|
| 81 |
+
|
| 82 |
+
if not name:
|
| 83 |
+
st.error("Please enter your name.")
|
| 84 |
+
|
| 85 |
+
elif height <= 0 or weight <= 0:
|
| 86 |
+
st.error("Please enter valid height and weight.")
|
| 87 |
+
|
| 88 |
+
elif not equipment:
|
| 89 |
+
st.error("Please select at least one equipment option.")
|
| 90 |
+
|
| 91 |
+
else:
|
| 92 |
+
st.success("Profile Submitted Successfully!")
|
| 93 |
+
|
| 94 |
+
# Calculate BMI
|
| 95 |
+
bmi = calculate_bmi(weight, height)
|
| 96 |
+
bmi_status = get_category(bmi)
|
| 97 |
+
|
| 98 |
+
st.write(f"### 📊 Your BMI: {bmi} ({bmi_status})")
|
| 99 |
+
|
| 100 |
+
equipment_list = ", ".join(equipment)
|
| 101 |
+
|
| 102 |
+
# -------------------------
|
| 103 |
+
# GENERATE 5-DAY PLAN (LOOP METHOD)
|
| 104 |
+
# -------------------------
|
| 105 |
+
with st.spinner("Generating your 5-day workout plan..."):
|
| 106 |
+
|
| 107 |
+
full_plan = ""
|
| 108 |
+
|
| 109 |
+
for day in range(1, 6):
|
| 110 |
+
|
| 111 |
+
prompt = f"""
|
| 112 |
+
You are a certified professional fitness trainer.
|
| 113 |
+
Create a detailed workout plan for Day {day}.
|
| 114 |
+
Include:
|
| 115 |
+
- Warm-up
|
| 116 |
+
- 4 to 6 exercises
|
| 117 |
+
- Sets and reps
|
| 118 |
+
- Cool down
|
| 119 |
+
User Details:
|
| 120 |
+
BMI: {bmi} ({bmi_status})
|
| 121 |
+
Goal: {goal}
|
| 122 |
+
Fitness Level: {level}
|
| 123 |
+
Equipment Available: {equipment_list}
|
| 124 |
+
Start with:
|
| 125 |
+
Day {day}:
|
| 126 |
+
"""
|
| 127 |
+
|
| 128 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 129 |
+
|
| 130 |
+
outputs = model.generate(
|
| 131 |
+
**inputs,
|
| 132 |
+
max_new_tokens=350,
|
| 133 |
+
temperature=0.8,
|
| 134 |
+
do_sample=True,
|
| 135 |
+
repetition_penalty=1.1
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
result = tokenizer.decode(
|
| 139 |
+
outputs[0],
|
| 140 |
+
skip_special_tokens=True
|
| 141 |
+
).strip()
|
| 142 |
+
|
| 143 |
+
full_plan += result + "\n\n"
|
| 144 |
+
|
| 145 |
+
# -------------------------
|
| 146 |
+
# DISPLAY PLAN
|
| 147 |
+
# -------------------------
|
| 148 |
+
st.subheader("🏋️ Your Personalized 5-Day Workout Plan")
|
| 149 |
+
st.write(full_plan)
|