Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +48 -107
src/streamlit_app.py
CHANGED
|
@@ -1,144 +1,85 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import
|
| 3 |
import torch
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
-
# LOAD MODEL (
|
| 7 |
# -----------------------------
|
| 8 |
@st.cache_resource
|
| 9 |
def load_model():
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
# -----------------------------
|
| 17 |
-
#
|
| 18 |
# -----------------------------
|
| 19 |
-
st.
|
| 20 |
-
page_title="FitPlan AI",
|
| 21 |
-
page_icon="πͺ",
|
| 22 |
-
layout="centered"
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
st.title("πͺ FitPlan AI β User Fitness Profile")
|
| 26 |
-
|
| 27 |
-
# -----------------------------
|
| 28 |
-
# BMI FUNCTION
|
| 29 |
-
# -----------------------------
|
| 30 |
-
def calculate_bmi(height_cm, weight_kg):
|
| 31 |
-
height_m = height_cm / 100
|
| 32 |
-
return weight_kg / (height_m ** 2)
|
| 33 |
-
|
| 34 |
-
def bmi_category(bmi):
|
| 35 |
-
if bmi < 18.5:
|
| 36 |
-
return "Underweight"
|
| 37 |
-
elif bmi < 25:
|
| 38 |
-
return "Normal Weight"
|
| 39 |
-
elif bmi < 30:
|
| 40 |
-
return "Overweight"
|
| 41 |
-
else:
|
| 42 |
-
return "Obese"
|
| 43 |
-
|
| 44 |
-
# -----------------------------
|
| 45 |
-
# PERSONAL INFORMATION
|
| 46 |
-
# -----------------------------
|
| 47 |
-
st.header("π€ Personal Information")
|
| 48 |
-
|
| 49 |
-
name = st.text_input("Enter Your Name *")
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
min_value=1.0,
|
| 54 |
-
step=0.1
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
weight_kg = st.number_input(
|
| 58 |
-
"Weight (kg) *",
|
| 59 |
-
min_value=1.0,
|
| 60 |
-
step=0.1
|
| 61 |
-
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
"
|
| 65 |
-
["
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
# FITNESS DETAILS
|
| 70 |
-
# -----------------------------
|
| 71 |
-
st.header("ποΈ Fitness Details")
|
| 72 |
-
|
| 73 |
-
fitness_goal = st.selectbox(
|
| 74 |
"Fitness Goal",
|
| 75 |
-
["
|
| 76 |
)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
"Available Equipment",
|
| 80 |
-
["Dumbbells", "Resistance Band", "Yoga Mat", "No Equipment"]
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
fitness_level = st.radio(
|
| 84 |
"Fitness Level",
|
| 85 |
["Beginner", "Intermediate", "Advanced"]
|
| 86 |
)
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# -----------------------------
|
| 89 |
-
#
|
| 90 |
# -----------------------------
|
| 91 |
-
if st.button("
|
| 92 |
-
|
| 93 |
-
if not name:
|
| 94 |
-
st.error("Please enter your name.")
|
| 95 |
-
|
| 96 |
-
elif height_cm <= 0 or weight_kg <= 0:
|
| 97 |
-
st.error("Please enter valid height and weight.")
|
| 98 |
|
| 99 |
-
|
| 100 |
-
st.success("Profile Submitted Successfully!")
|
| 101 |
-
|
| 102 |
-
# Calculate BMI
|
| 103 |
-
bmi = calculate_bmi(height_cm, weight_kg)
|
| 104 |
-
bmi_status = bmi_category(bmi)
|
| 105 |
-
|
| 106 |
-
# -----------------------------
|
| 107 |
-
# AI PROMPT
|
| 108 |
-
# -----------------------------
|
| 109 |
-
prompt = f"""
|
| 110 |
You are a certified professional fitness trainer.
|
| 111 |
|
| 112 |
Create a structured 5-day workout plan.
|
| 113 |
|
| 114 |
User Details:
|
| 115 |
-
Name: {name}
|
| 116 |
Gender: {gender}
|
| 117 |
BMI: {bmi:.2f} ({bmi_status})
|
| 118 |
-
Goal: {
|
| 119 |
Fitness Level: {fitness_level}
|
| 120 |
-
Equipment: {
|
| 121 |
-
|
| 122 |
-
Include:
|
| 123 |
-
- Warmup
|
| 124 |
-
- Exercises with sets and reps
|
| 125 |
-
- Rest time
|
| 126 |
-
- Structured day-wise format
|
| 127 |
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
Day 1
|
| 131 |
"""
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
st.write(result)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
+
# LOAD MODEL (Correct Pipeline)
|
| 7 |
# -----------------------------
|
| 8 |
@st.cache_resource
|
| 9 |
def load_model():
|
| 10 |
+
generator = pipeline(
|
| 11 |
+
"text2text-generation", # β
Correct for FLAN-T5
|
| 12 |
+
model="google/flan-t5-base",
|
| 13 |
+
device=0 if torch.cuda.is_available() else -1
|
| 14 |
+
)
|
| 15 |
+
return generator
|
| 16 |
|
| 17 |
+
generator = load_model()
|
| 18 |
|
| 19 |
# -----------------------------
|
| 20 |
+
# STREAMLIT UI
|
| 21 |
# -----------------------------
|
| 22 |
+
st.title("ποΈ AI Fitness Workout Planner")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
gender = st.selectbox("Select Gender", ["Male", "Female"])
|
| 25 |
+
bmi = st.number_input("Enter BMI", min_value=10.0, max_value=50.0, value=22.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
bmi_status = st.selectbox(
|
| 28 |
+
"BMI Status",
|
| 29 |
+
["Underweight", "Normal Weight", "Overweight", "Obese"]
|
| 30 |
)
|
| 31 |
|
| 32 |
+
goal = st.selectbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
"Fitness Goal",
|
| 34 |
+
["Weight Loss", "Muscle Gain", "General Fitness", "Strength Training"]
|
| 35 |
)
|
| 36 |
|
| 37 |
+
fitness_level = st.selectbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
"Fitness Level",
|
| 39 |
["Beginner", "Intermediate", "Advanced"]
|
| 40 |
)
|
| 41 |
|
| 42 |
+
equipment_list = st.text_input(
|
| 43 |
+
"Available Equipment (comma separated)",
|
| 44 |
+
"Dumbbells, Yoga Mat"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
# -----------------------------
|
| 48 |
+
# GENERATE WORKOUT PLAN
|
| 49 |
# -----------------------------
|
| 50 |
+
if st.button("Generate Workout Plan"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
prompt = f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
You are a certified professional fitness trainer.
|
| 54 |
|
| 55 |
Create a structured 5-day workout plan.
|
| 56 |
|
| 57 |
User Details:
|
|
|
|
| 58 |
Gender: {gender}
|
| 59 |
BMI: {bmi:.2f} ({bmi_status})
|
| 60 |
+
Goal: {goal}
|
| 61 |
Fitness Level: {fitness_level}
|
| 62 |
+
Equipment: {equipment_list}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
Rules:
|
| 65 |
+
- Exactly 5 days (Day 1 to Day 5)
|
| 66 |
+
- 4 to 6 exercises per day
|
| 67 |
+
- Include Sets, Reps, Rest
|
| 68 |
+
- Vary exercises based on user details
|
| 69 |
+
- No explanation
|
| 70 |
+
- Only workout plan
|
| 71 |
|
| 72 |
+
Start directly from Day 1.
|
| 73 |
"""
|
| 74 |
|
| 75 |
+
with st.spinner("Generating personalized workout plan..."):
|
| 76 |
+
result = generator(
|
| 77 |
+
prompt,
|
| 78 |
+
max_length=700,
|
| 79 |
+
temperature=0.9, # β
Higher = more variation
|
| 80 |
+
do_sample=True
|
| 81 |
+
)
|
| 82 |
+
workout_plan = result[0]["generated_text"]
|
| 83 |
+
|
| 84 |
+
st.subheader("π
Your Personalized 5-Day Workout Plan")
|
| 85 |
+
st.text_area("", workout_plan, height=650)
|
|
|