Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +56 -55
src/streamlit_app.py
CHANGED
|
@@ -1,18 +1,17 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# -------------------------
|
| 5 |
# PAGE CONFIG (MUST BE FIRST)
|
| 6 |
-
# -------------------------
|
| 7 |
st.set_page_config(page_title="FitPlan-AI", page_icon="💪")
|
| 8 |
|
| 9 |
-
# -------------------------
|
| 10 |
-
# BMI
|
| 11 |
-
# -------------------------
|
| 12 |
def calculate_bmi(weight, height_cm):
|
| 13 |
height_m = height_cm / 100
|
| 14 |
-
|
| 15 |
-
return round(bmi, 2)
|
| 16 |
|
| 17 |
def get_category(bmi):
|
| 18 |
if bmi < 18.5:
|
|
@@ -24,26 +23,26 @@ def get_category(bmi):
|
|
| 24 |
else:
|
| 25 |
return "Obese"
|
| 26 |
|
| 27 |
-
# -------------------------
|
| 28 |
-
#
|
| 29 |
-
# -------------------------
|
| 30 |
@st.cache_resource
|
| 31 |
def load_model():
|
| 32 |
return pipeline(
|
| 33 |
-
"text2text-generation", #
|
| 34 |
model="google/flan-t5-base"
|
| 35 |
)
|
| 36 |
|
| 37 |
generator = load_model()
|
| 38 |
|
| 39 |
-
# -------------------------
|
| 40 |
-
# UI
|
| 41 |
-
# -------------------------
|
| 42 |
st.title("💪 FitPlan-AI: Personalized Fitness Profile")
|
| 43 |
|
| 44 |
with st.form("fitness_form"):
|
| 45 |
|
| 46 |
-
st.
|
| 47 |
|
| 48 |
name = st.text_input("Full Name*", placeholder="Enter your name")
|
| 49 |
|
|
@@ -53,7 +52,7 @@ with st.form("fitness_form"):
|
|
| 53 |
with col2:
|
| 54 |
weight = st.number_input("Weight (kg)*", min_value=1.0, step=0.1)
|
| 55 |
|
| 56 |
-
st.
|
| 57 |
|
| 58 |
goal = st.selectbox(
|
| 59 |
"Fitness Goal",
|
|
@@ -72,52 +71,54 @@ with st.form("fitness_form"):
|
|
| 72 |
|
| 73 |
submit = st.form_submit_button("Generate Workout Plan")
|
| 74 |
|
| 75 |
-
# -------------------------
|
| 76 |
-
#
|
| 77 |
-
# -------------------------
|
| 78 |
if submit:
|
| 79 |
|
| 80 |
# Validation
|
| 81 |
if name.strip() == "":
|
| 82 |
st.error("Please enter your name.")
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
st.error("Height and Weight must be positive values.")
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
st.error("Please select at least one equipment option.")
|
|
|
|
| 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 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
st.write(result)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# -------------------------
|
| 5 |
# PAGE CONFIG (MUST BE FIRST)
|
| 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:
|
|
|
|
| 23 |
else:
|
| 24 |
return "Obese"
|
| 25 |
|
| 26 |
+
# -------------------------
|
| 27 |
+
# LOAD MODEL (Correct Pipeline)
|
| 28 |
+
# -------------------------
|
| 29 |
@st.cache_resource
|
| 30 |
def load_model():
|
| 31 |
return pipeline(
|
| 32 |
+
"text2text-generation", # Correct for FLAN-T5
|
| 33 |
model="google/flan-t5-base"
|
| 34 |
)
|
| 35 |
|
| 36 |
generator = load_model()
|
| 37 |
|
| 38 |
+
# -------------------------
|
| 39 |
+
# UI STARTS
|
| 40 |
+
# -------------------------
|
| 41 |
st.title("💪 FitPlan-AI: Personalized Fitness Profile")
|
| 42 |
|
| 43 |
with st.form("fitness_form"):
|
| 44 |
|
| 45 |
+
st.subheader("Personal Information")
|
| 46 |
|
| 47 |
name = st.text_input("Full Name*", placeholder="Enter your name")
|
| 48 |
|
|
|
|
| 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",
|
|
|
|
| 71 |
|
| 72 |
submit = st.form_submit_button("Generate Workout Plan")
|
| 73 |
|
| 74 |
+
# -------------------------
|
| 75 |
+
# HANDLE SUBMISSION
|
| 76 |
+
# -------------------------
|
| 77 |
if submit:
|
| 78 |
|
| 79 |
# Validation
|
| 80 |
if name.strip() == "":
|
| 81 |
st.error("Please enter your name.")
|
| 82 |
+
st.stop()
|
| 83 |
|
| 84 |
+
if height <= 0 or weight <= 0:
|
| 85 |
st.error("Height and Weight must be positive values.")
|
| 86 |
+
st.stop()
|
| 87 |
|
| 88 |
+
if len(equipment) == 0:
|
| 89 |
st.error("Please select at least one equipment option.")
|
| 90 |
+
st.stop()
|
| 91 |
|
| 92 |
+
# Calculate BMI
|
| 93 |
+
user_bmi = calculate_bmi(weight, height)
|
| 94 |
+
bmi_status = get_category(user_bmi)
|
| 95 |
+
|
| 96 |
+
st.success(f"✅ Profile Created Successfully for {name}!")
|
| 97 |
+
st.metric("Your BMI", user_bmi)
|
| 98 |
+
st.info(f"Health Category: **{bmi_status}**")
|
| 99 |
+
|
| 100 |
+
equipment_list = ", ".join(equipment)
|
| 101 |
+
|
| 102 |
+
prompt = f"""
|
| 103 |
+
Create a professional 5-day structured workout plan.
|
| 104 |
+
|
| 105 |
+
User Details:
|
| 106 |
+
Name: {name}
|
| 107 |
+
BMI: {user_bmi} ({bmi_status})
|
| 108 |
+
Goal: {goal}
|
| 109 |
+
Fitness Level: {level}
|
| 110 |
+
Equipment: {equipment_list}
|
| 111 |
+
|
| 112 |
+
Include:
|
| 113 |
+
- Warm-up
|
| 114 |
+
- Exercises with sets and reps
|
| 115 |
+
- Rest time
|
| 116 |
+
- Intensity adjustment
|
| 117 |
+
- Day-wise structure
|
| 118 |
+
"""
|
| 119 |
+
|
| 120 |
+
with st.spinner("Generating your AI workout plan..."):
|
| 121 |
+
result = generator(prompt, max_new_tokens=400)[0]["generated_text"]
|
| 122 |
+
|
| 123 |
+
st.subheader("🏋️ Your Personalized Workout Plan")
|
| 124 |
+
st.write(result)
|
|
|