Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +40 -109
src/streamlit_app.py
CHANGED
|
@@ -1,113 +1,44 @@
|
|
| 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 |
-
# BMI FUNCTIONS
|
| 10 |
-
# -------------------------
|
| 11 |
-
def calculate_bmi(weight, height_cm):
|
| 12 |
-
height_m = height_cm / 100
|
| 13 |
-
return round(weight / (height_m ** 2), 2)
|
| 14 |
-
|
| 15 |
-
def get_category(bmi):
|
| 16 |
-
if bmi < 18.5:
|
| 17 |
-
return "Underweight"
|
| 18 |
-
elif bmi < 24.9:
|
| 19 |
-
return "Normal"
|
| 20 |
-
elif bmi < 29.9:
|
| 21 |
-
return "Overweight"
|
| 22 |
-
else:
|
| 23 |
-
return "Obese"
|
| 24 |
-
|
| 25 |
# -------------------------
|
| 26 |
-
# LOAD AI MODEL
|
| 27 |
-
# -------------------------
|
| 28 |
-
@st.cache_resource
|
| 29 |
-
def load_model():
|
| 30 |
-
return pipeline(
|
| 31 |
-
"text2text-generation", # Correct for FLAN-T5
|
| 32 |
-
model="google/flan-t5-base"
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
generator = load_model()
|
| 36 |
-
|
| 37 |
-
# -------------------------
|
| 38 |
-
# UI STARTS
|
| 39 |
-
# -------------------------
|
| 40 |
-
st.title("💪 FitPlan-AI: Personalized Fitness Profile")
|
| 41 |
-
|
| 42 |
-
# Form to collect all inputs
|
| 43 |
-
with st.form("fitness_form"):
|
| 44 |
-
|
| 45 |
-
st.subheader("Personal Information")
|
| 46 |
-
name = st.text_input("Full Name*", placeholder="Enter your name")
|
| 47 |
-
|
| 48 |
-
col1, col2 = st.columns(2)
|
| 49 |
-
with col1:
|
| 50 |
-
height = st.number_input("Height (cm)*", min_value=1.0, step=0.1)
|
| 51 |
-
with col2:
|
| 52 |
-
weight = st.number_input("Weight (kg)*", min_value=1.0, step=0.1)
|
| 53 |
-
|
| 54 |
-
st.subheader("Fitness Details")
|
| 55 |
-
goal = st.selectbox(
|
| 56 |
-
"Fitness Goal",
|
| 57 |
-
["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"]
|
| 58 |
-
)
|
| 59 |
-
level = st.radio("Fitness Level", ["Beginner", "Intermediate", "Advanced"])
|
| 60 |
-
equipment = st.multiselect(
|
| 61 |
-
"Available Equipment",
|
| 62 |
-
["Dumbbells", "Resistance Band", "Yoga Mat", "No Equipment", "Kettlebell", "Pull-up Bar"]
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
# -------------------------
|
| 66 |
-
# SUBMIT BUTTON AT BOTTOM
|
| 67 |
-
# -------------------------
|
| 68 |
-
submit = st.form_submit_button("Submit Profile")
|
| 69 |
-
|
| 70 |
-
# -------------------------
|
| 71 |
# HANDLE SUBMISSION
|
| 72 |
# -------------------------
|
| 73 |
if submit:
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# -------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
# HANDLE SUBMISSION
|
| 3 |
# -------------------------
|
| 4 |
if submit:
|
| 5 |
+
# Validation (Indented from here downwards)
|
| 6 |
+
if not name.strip():
|
| 7 |
+
st.error("Please enter your name.")
|
| 8 |
+
elif height <= 0 or weight <= 0:
|
| 9 |
+
st.error("Height and Weight must be positive values.")
|
| 10 |
+
elif not equipment:
|
| 11 |
+
st.error("Please select at least one equipment option.")
|
| 12 |
+
else:
|
| 13 |
+
# Calculate BMI
|
| 14 |
+
user_bmi = calculate_bmi(weight, height)
|
| 15 |
+
bmi_status = get_category(user_bmi)
|
| 16 |
+
|
| 17 |
+
st.success(f"✅ Profile Created Successfully for {name}!")
|
| 18 |
+
st.metric("Your BMI", user_bmi)
|
| 19 |
+
st.info(f"Health Category: **{bmi_status}**")
|
| 20 |
+
|
| 21 |
+
equipment_list = ", ".join(equipment)
|
| 22 |
+
|
| 23 |
+
# Prepare AI prompt
|
| 24 |
+
prompt = f"""
|
| 25 |
+
Create a professional 5-day structured workout plan.
|
| 26 |
+
User Details:
|
| 27 |
+
Name: {name}
|
| 28 |
+
BMI: {user_bmi} ({bmi_status})
|
| 29 |
+
Goal: {goal}
|
| 30 |
+
Fitness Level: {level}
|
| 31 |
+
Equipment: {equipment_list}
|
| 32 |
+
Include:
|
| 33 |
+
- Warm-up
|
| 34 |
+
- Exercises with sets and reps
|
| 35 |
+
- Rest time
|
| 36 |
+
- Intensity adjustment
|
| 37 |
+
- Day-wise structure
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
with st.spinner("Generating your AI workout plan..."):
|
| 41 |
+
result = generator(prompt, max_new_tokens=400)[0]["generated_text"]
|
| 42 |
+
|
| 43 |
+
st.subheader("🏋️ Your Personalized Workout Plan")
|
| 44 |
+
st.write(result)
|