Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +75 -45
src/streamlit_app.py
CHANGED
|
@@ -1,43 +1,68 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
return pipeline("text-generation", model="google/flan-t5-base")
|
| 8 |
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
st.title("πͺ FitPlan AI - Fitness Profile & BMI Calculator")
|
| 13 |
|
| 14 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# -------------------------
|
| 17 |
-
#
|
| 18 |
# -------------------------
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# -------------------------
|
| 28 |
-
#
|
| 29 |
# -------------------------
|
| 30 |
|
| 31 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
goal = st.selectbox(
|
| 34 |
-
"
|
| 35 |
["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"]
|
| 36 |
)
|
| 37 |
|
| 38 |
equipment = st.multiselect(
|
| 39 |
"Available Equipment",
|
| 40 |
-
["Dumbbells", "Resistance Band", "Yoga Mat", "
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
fitness_level = st.radio(
|
|
@@ -49,10 +74,9 @@ fitness_level = st.radio(
|
|
| 49 |
# BMI Functions
|
| 50 |
# -------------------------
|
| 51 |
|
| 52 |
-
def calculate_bmi(weight,
|
| 53 |
-
height_m =
|
| 54 |
-
|
| 55 |
-
return round(bmi, 2)
|
| 56 |
|
| 57 |
def bmi_category(bmi):
|
| 58 |
if bmi < 18.5:
|
|
@@ -65,45 +89,51 @@ def bmi_category(bmi):
|
|
| 65 |
return "Obese"
|
| 66 |
|
| 67 |
# -------------------------
|
| 68 |
-
#
|
| 69 |
# -------------------------
|
| 70 |
|
| 71 |
-
if st.button("
|
| 72 |
|
| 73 |
-
# Validation
|
| 74 |
if not name:
|
| 75 |
-
st.error("
|
| 76 |
elif height_cm <= 0 or weight_kg <= 0:
|
| 77 |
-
st.error("Enter valid height
|
| 78 |
elif not equipment:
|
| 79 |
-
st.error("Select
|
| 80 |
else:
|
| 81 |
-
st.success("β
Profile Submitted Successfully!")
|
| 82 |
-
|
| 83 |
-
# Calculate BMI
|
| 84 |
bmi = calculate_bmi(weight_kg, height_cm)
|
| 85 |
bmi_status = bmi_category(bmi)
|
| 86 |
|
| 87 |
-
st.
|
| 88 |
|
| 89 |
equipment_list = ", ".join(equipment)
|
| 90 |
|
| 91 |
prompt = f"""
|
| 92 |
-
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
BMI: {bmi} ({bmi_status})
|
| 98 |
-
Goal: {goal}
|
| 99 |
-
Fitness Level: {fitness_level}
|
| 100 |
-
Available Equipment: {equipment_list}
|
| 101 |
|
| 102 |
-
|
| 103 |
-
"""
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
st.write(result)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# -------------------------
|
| 6 |
+
# Page Config FIRST
|
| 7 |
+
# -------------------------
|
|
|
|
| 8 |
|
| 9 |
+
st.set_page_config(page_title="FitPlan AI", page_icon="πͺ")
|
| 10 |
|
| 11 |
+
# π¨ Background Style
|
|
|
|
| 12 |
|
| 13 |
+
st.markdown(
|
| 14 |
+
"""
|
| 15 |
+
<style>
|
| 16 |
+
.stApp {
|
| 17 |
+
background-image: url("https://images.unsplash.com/photo-1554284126-aa88f22d8b74");
|
| 18 |
+
background-size: cover;
|
| 19 |
+
}
|
| 20 |
+
</style>
|
| 21 |
+
""",
|
| 22 |
+
unsafe_allow_html=True
|
| 23 |
+
)
|
| 24 |
|
| 25 |
# -------------------------
|
| 26 |
+
# Load Model (Small for HF)
|
| 27 |
# -------------------------
|
| 28 |
|
| 29 |
+
@st.cache_resource
|
| 30 |
+
def load_model():
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 32 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
|
| 33 |
+
return tokenizer, model
|
| 34 |
|
| 35 |
+
tokenizer, model = load_model()
|
| 36 |
|
| 37 |
+
# -------------------------
|
| 38 |
+
# App Title
|
| 39 |
+
# -------------------------
|
| 40 |
+
|
| 41 |
+
st.title("πͺ FitPlan AI - BMI Calculator & Workout Planner")
|
| 42 |
+
st.write("Enter your details to get AI workout plan.")
|
| 43 |
|
| 44 |
# -------------------------
|
| 45 |
+
# Personal Info
|
| 46 |
# -------------------------
|
| 47 |
|
| 48 |
+
name = st.text_input("Enter Your Name *")
|
| 49 |
+
gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
| 50 |
+
height_cm = st.number_input("Height (cm)", min_value=0.0)
|
| 51 |
+
weight_kg = st.number_input("Weight (kg)", min_value=0.0)
|
| 52 |
+
|
| 53 |
+
# -------------------------
|
| 54 |
+
# Fitness Details
|
| 55 |
+
# -------------------------
|
| 56 |
|
| 57 |
goal = st.selectbox(
|
| 58 |
+
"Fitness Goal",
|
| 59 |
["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"]
|
| 60 |
)
|
| 61 |
|
| 62 |
equipment = st.multiselect(
|
| 63 |
"Available Equipment",
|
| 64 |
+
["Dumbbells", "Resistance Band", "Yoga Mat", "Skipping Rope",
|
| 65 |
+
"Weight Plates", "Cycling", "Inclined Bench", "Pullups Bar", "No Equipment"]
|
| 66 |
)
|
| 67 |
|
| 68 |
fitness_level = st.radio(
|
|
|
|
| 74 |
# BMI Functions
|
| 75 |
# -------------------------
|
| 76 |
|
| 77 |
+
def calculate_bmi(weight, height):
|
| 78 |
+
height_m = height / 100
|
| 79 |
+
return round(weight / (height_m ** 2), 2)
|
|
|
|
| 80 |
|
| 81 |
def bmi_category(bmi):
|
| 82 |
if bmi < 18.5:
|
|
|
|
| 89 |
return "Obese"
|
| 90 |
|
| 91 |
# -------------------------
|
| 92 |
+
# Generate Plan
|
| 93 |
# -------------------------
|
| 94 |
|
| 95 |
+
if st.button("Generate Workout Plan"):
|
| 96 |
|
|
|
|
| 97 |
if not name:
|
| 98 |
+
st.error("Enter your name")
|
| 99 |
elif height_cm <= 0 or weight_kg <= 0:
|
| 100 |
+
st.error("Enter valid height & weight")
|
| 101 |
elif not equipment:
|
| 102 |
+
st.error("Select equipment")
|
| 103 |
else:
|
|
|
|
|
|
|
|
|
|
| 104 |
bmi = calculate_bmi(weight_kg, height_cm)
|
| 105 |
bmi_status = bmi_category(bmi)
|
| 106 |
|
| 107 |
+
st.success(f"BMI: {bmi} ({bmi_status})")
|
| 108 |
|
| 109 |
equipment_list = ", ".join(equipment)
|
| 110 |
|
| 111 |
prompt = f"""
|
| 112 |
+
Create a 5-day workout plan.
|
| 113 |
|
| 114 |
+
Include warmup, exercises, sets, reps, rest time.
|
| 115 |
+
|
| 116 |
+
Day 1 β Chest & Triceps: Bench Press 4Γ8β10, Incline Press 3Γ10, Dips 3Γ12, Tricep Extensions 3Γ12.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
Day 2 β Back & Biceps: Pull-ups 4Γ8β10, Rows 4Γ10, Band Rows 3Γ12, Bicep Curls 3Γ12.
|
|
|
|
| 119 |
|
| 120 |
+
Day 3 β Legs: Squats 4Γ8β10, Lunges 3Γ12/leg, Romanian Deadlift 3Γ10, Calf Raises 4Γ15.
|
| 121 |
+
|
| 122 |
+
Day 4 β Shoulders & Core: Overhead Press 4Γ8β10, Lateral Raises 3Γ12, Plank 3Γ60s, Leg Raises 3Γ12.
|
| 123 |
+
|
| 124 |
+
Day 5 β Cardio & Conditioning: 25-min Run, Skipping 3Γ2min, Burpees 3Γ12, Mountain Climbers 3Γ30s.
|
| 125 |
+
|
| 126 |
+
"""
|
| 127 |
|
| 128 |
+
with st.spinner("Generating workout plan..."):
|
| 129 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 130 |
+
outputs = model.generate(
|
| 131 |
+
**inputs,
|
| 132 |
+
max_new_tokens=400,
|
| 133 |
+
temperature=0.7,
|
| 134 |
+
do_sample=True
|
| 135 |
+
)
|
| 136 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 137 |
+
|
| 138 |
+
st.subheader("ποΈ Your Workout Plan")
|
| 139 |
st.write(result)
|