Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +60 -115
src/streamlit_app.py
CHANGED
|
@@ -1,182 +1,127 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
# -------------------------
|
| 8 |
-
|
| 9 |
# PAGE CONFIG
|
| 10 |
-
|
| 11 |
# -------------------------
|
| 12 |
-
|
| 13 |
st.set_page_config(page_title="FitPlan-AI", page_icon="💪")
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
# -------------------------
|
| 18 |
-
|
| 19 |
# BMI FUNCTIONS
|
| 20 |
-
|
| 21 |
# -------------------------
|
| 22 |
-
|
| 23 |
def calculate_bmi(weight, height_cm):
|
| 24 |
-
|
| 25 |
height_m = height_cm / 100
|
| 26 |
-
|
| 27 |
return round(weight / (height_m ** 2), 2)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
def get_category(bmi):
|
| 32 |
-
|
| 33 |
if bmi < 18.5:
|
| 34 |
-
|
| 35 |
return "Underweight"
|
| 36 |
-
|
| 37 |
elif bmi < 24.9:
|
| 38 |
-
|
| 39 |
return "Normal"
|
| 40 |
-
|
| 41 |
elif bmi < 29.9:
|
| 42 |
-
|
| 43 |
return "Overweight"
|
| 44 |
-
|
| 45 |
else:
|
| 46 |
-
|
| 47 |
return "Obese"
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
# -------------------------
|
| 52 |
-
|
| 53 |
-
# LOAD AI MODEL
|
| 54 |
-
|
| 55 |
# -------------------------
|
| 56 |
-
|
| 57 |
@st.cache_resource
|
| 58 |
-
|
| 59 |
def load_model():
|
| 60 |
-
|
| 61 |
return pipeline(
|
| 62 |
-
|
| 63 |
-
"
|
| 64 |
-
|
| 65 |
-
model="google/flan-t5-base"
|
| 66 |
-
|
| 67 |
)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
generator = load_model()
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
# -------------------------
|
| 76 |
-
|
| 77 |
# UI STARTS
|
| 78 |
-
|
| 79 |
# -------------------------
|
| 80 |
-
|
| 81 |
st.title("💪 FitPlan-AI: Personalized Fitness Profile")
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
# Form to collect all inputs
|
| 86 |
-
|
| 87 |
with st.form("fitness_form"):
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
st.subheader("Personal Information")
|
| 92 |
-
|
| 93 |
name = st.text_input("Full Name*", placeholder="Enter your name")
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
col1, col2 = st.columns(2)
|
| 98 |
-
|
| 99 |
with col1:
|
| 100 |
-
|
| 101 |
height = st.number_input("Height (cm)*", min_value=1.0, step=0.1)
|
| 102 |
-
|
| 103 |
with col2:
|
| 104 |
-
|
| 105 |
weight = st.number_input("Weight (kg)*", min_value=1.0, step=0.1)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
st.subheader("Fitness Details")
|
| 110 |
|
| 111 |
goal = st.selectbox(
|
| 112 |
-
|
| 113 |
"Fitness Goal",
|
| 114 |
-
|
| 115 |
-
["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"]
|
| 116 |
-
|
| 117 |
)
|
| 118 |
|
| 119 |
-
level = st.radio(
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
equipment = st.multiselect(
|
| 122 |
-
|
| 123 |
"Available Equipment",
|
| 124 |
-
|
| 125 |
["Dumbbells", "Resistance Band", "Yoga Mat", "No Equipment", "Kettlebell", "Pull-up Bar"]
|
| 126 |
-
|
| 127 |
)
|
| 128 |
|
|
|
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
# SUBMIT BUTTON AT BOTTOM
|
| 134 |
|
| 135 |
-
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
# HANDLE SUBMISSION
|
| 140 |
-
# -------------------------
|
| 141 |
-
if submit:
|
| 142 |
-
# Validation (Indented from here downwards)
|
| 143 |
-
if not name.strip():
|
| 144 |
-
st.error("Please enter your name.")
|
| 145 |
-
elif height <= 0 or weight <= 0:
|
| 146 |
-
st.error("Height and Weight must be positive values.")
|
| 147 |
-
elif not equipment:
|
| 148 |
-
st.error("Please select at least one equipment option.")
|
| 149 |
-
else:
|
| 150 |
-
# Calculate BMI
|
| 151 |
-
user_bmi = calculate_bmi(weight, height)
|
| 152 |
-
bmi_status = get_category(user_bmi)
|
| 153 |
-
|
| 154 |
-
st.success(f"✅ Profile Created Successfully for {name}!")
|
| 155 |
-
st.metric("Your BMI", user_bmi)
|
| 156 |
-
st.info(f"Health Category: **{bmi_status}**")
|
| 157 |
-
|
| 158 |
-
equipment_list = ", ".join(equipment)
|
| 159 |
-
|
| 160 |
-
# Prepare AI prompt
|
| 161 |
-
prompt = f"""
|
| 162 |
-
Create a professional 5-day structured workout plan.
|
| 163 |
-
User Details:
|
| 164 |
-
Name: {name}
|
| 165 |
-
BMI: {user_bmi} ({bmi_status})
|
| 166 |
-
Goal: {goal}
|
| 167 |
-
Fitness Level: {level}
|
| 168 |
-
Equipment: {equipment_list}
|
| 169 |
-
Include:
|
| 170 |
-
- Warm-up
|
| 171 |
-
- Exercises with sets and reps
|
| 172 |
-
- Rest time
|
| 173 |
-
- Intensity adjustment
|
| 174 |
-
- Day-wise structure
|
| 175 |
-
"""
|
| 176 |
-
|
| 177 |
-
with st.spinner("Generating your AI workout plan..."):
|
| 178 |
-
result = generator(prompt, max_new_tokens=400)[0]["generated_text"]
|
| 179 |
-
|
| 180 |
-
st.subheader("🏋️ Your Personalized Workout Plan")
|
| 181 |
-
st.write(result)
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
|
|
|
|
|
|
| 4 |
# -------------------------
|
|
|
|
| 5 |
# PAGE CONFIG
|
|
|
|
| 6 |
# -------------------------
|
|
|
|
| 7 |
st.set_page_config(page_title="FitPlan-AI", page_icon="💪")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
# -------------------------
|
|
|
|
| 10 |
# BMI FUNCTIONS
|
|
|
|
| 11 |
# -------------------------
|
|
|
|
| 12 |
def calculate_bmi(weight, height_cm):
|
|
|
|
| 13 |
height_m = height_cm / 100
|
|
|
|
| 14 |
return round(weight / (height_m ** 2), 2)
|
| 15 |
|
|
|
|
|
|
|
| 16 |
def get_category(bmi):
|
|
|
|
| 17 |
if bmi < 18.5:
|
|
|
|
| 18 |
return "Underweight"
|
|
|
|
| 19 |
elif bmi < 24.9:
|
|
|
|
| 20 |
return "Normal"
|
|
|
|
| 21 |
elif bmi < 29.9:
|
|
|
|
| 22 |
return "Overweight"
|
|
|
|
| 23 |
else:
|
|
|
|
| 24 |
return "Obese"
|
| 25 |
|
|
|
|
|
|
|
| 26 |
# -------------------------
|
| 27 |
+
# LOAD FLAN-T5 MODEL
|
|
|
|
|
|
|
| 28 |
# -------------------------
|
|
|
|
| 29 |
@st.cache_resource
|
|
|
|
| 30 |
def load_model():
|
|
|
|
| 31 |
return pipeline(
|
| 32 |
+
task="text2text-generation",
|
| 33 |
+
model="google/flan-t5-base",
|
| 34 |
+
tokenizer="google/flan-t5-base"
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
|
|
|
|
|
|
|
| 37 |
generator = load_model()
|
| 38 |
|
|
|
|
|
|
|
| 39 |
# -------------------------
|
|
|
|
| 40 |
# UI STARTS
|
|
|
|
| 41 |
# -------------------------
|
|
|
|
| 42 |
st.title("💪 FitPlan-AI: Personalized Fitness Profile")
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
with st.form("fitness_form"):
|
| 45 |
|
|
|
|
|
|
|
| 46 |
st.subheader("Personal Information")
|
|
|
|
| 47 |
name = st.text_input("Full Name*", placeholder="Enter your name")
|
| 48 |
|
|
|
|
|
|
|
| 49 |
col1, col2 = st.columns(2)
|
|
|
|
| 50 |
with col1:
|
|
|
|
| 51 |
height = st.number_input("Height (cm)*", min_value=1.0, step=0.1)
|
|
|
|
| 52 |
with col2:
|
|
|
|
| 53 |
weight = st.number_input("Weight (kg)*", min_value=1.0, step=0.1)
|
| 54 |
|
|
|
|
|
|
|
| 55 |
st.subheader("Fitness Details")
|
| 56 |
|
| 57 |
goal = st.selectbox(
|
|
|
|
| 58 |
"Fitness Goal",
|
| 59 |
+
["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexibility"]
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
+
level = st.radio(
|
| 63 |
+
"Fitness Level",
|
| 64 |
+
["Beginner", "Intermediate", "Advanced"]
|
| 65 |
+
)
|
| 66 |
|
| 67 |
equipment = st.multiselect(
|
|
|
|
| 68 |
"Available Equipment",
|
|
|
|
| 69 |
["Dumbbells", "Resistance Band", "Yoga Mat", "No Equipment", "Kettlebell", "Pull-up Bar"]
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
+
submit = st.form_submit_button("Submit Profile")
|
| 73 |
|
| 74 |
+
# -------------------------
|
| 75 |
+
# HANDLE SUBMISSION
|
| 76 |
+
# -------------------------
|
| 77 |
+
if submit:
|
| 78 |
|
| 79 |
+
if not name.strip():
|
| 80 |
+
st.error("Please enter your name.")
|
|
|
|
| 81 |
|
| 82 |
+
elif height <= 0 or weight <= 0:
|
| 83 |
+
st.error("Height and Weight must be positive values.")
|
| 84 |
|
| 85 |
+
elif not equipment:
|
| 86 |
+
st.error("Please select at least one equipment option.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
else:
|
| 89 |
+
# Calculate BMI
|
| 90 |
+
user_bmi = calculate_bmi(weight, height)
|
| 91 |
+
bmi_status = get_category(user_bmi)
|
| 92 |
+
|
| 93 |
+
st.success(f"✅ Profile Created Successfully for {name}!")
|
| 94 |
+
st.metric("Your BMI", user_bmi)
|
| 95 |
+
st.info(f"Health Category: **{bmi_status}**")
|
| 96 |
+
|
| 97 |
+
equipment_list = ", ".join(equipment)
|
| 98 |
+
|
| 99 |
+
# FLAN-T5 works best with instruction-style prompts
|
| 100 |
+
prompt = f"""
|
| 101 |
+
Create a structured 5-day workout plan.
|
| 102 |
+
|
| 103 |
+
User Details:
|
| 104 |
+
Name: {name}
|
| 105 |
+
BMI: {user_bmi} ({bmi_status})
|
| 106 |
+
Goal: {goal}
|
| 107 |
+
Fitness Level: {level}
|
| 108 |
+
Equipment: {equipment_list}
|
| 109 |
+
|
| 110 |
+
Include:
|
| 111 |
+
- Warm-up
|
| 112 |
+
- Exercises with sets and reps
|
| 113 |
+
- Rest time
|
| 114 |
+
- Day-wise breakdown
|
| 115 |
+
- Safety advice
|
| 116 |
+
"""
|
| 117 |
+
|
| 118 |
+
with st.spinner("Generating your AI workout plan..."):
|
| 119 |
+
result = generator(
|
| 120 |
+
prompt,
|
| 121 |
+
max_new_tokens=400,
|
| 122 |
+
do_sample=True,
|
| 123 |
+
temperature=0.7
|
| 124 |
+
)[0]["generated_text"]
|
| 125 |
+
|
| 126 |
+
st.subheader("🏋️ Your Personalized Workout Plan")
|
| 127 |
+
st.write(result)
|