Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,132 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
# ------
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
st.
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
else:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
with st.spinner("Generating your AI workout plan..."):
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
st.write(result)
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# --- Page Config ---
|
| 6 |
+
st.set_page_config(page_title="FitPlan AI", page_icon="π", layout="wide")
|
| 7 |
+
|
| 8 |
+
# --- Custom Styling ---
|
| 9 |
+
st.markdown("""
|
| 10 |
+
<style>
|
| 11 |
+
.stApp { background-color: #0E1117; color: white; }
|
| 12 |
+
[data-testid="stSidebar"] { background-color: #161B22 !important; }
|
| 13 |
+
.stButton > button { width: 100%; border-radius: 8px; font-weight: bold; background-color: #00F260; color: black; }
|
| 14 |
+
</style>
|
| 15 |
+
""", unsafe_allow_html=True)
|
| 16 |
+
|
| 17 |
+
# --- Initialize Session State ---
|
| 18 |
+
if 'user_data' not in st.session_state:
|
| 19 |
+
st.session_state.user_data = {
|
| 20 |
+
'name': '', 'age': 25, 'height': 0.0, 'weight': 0.0,
|
| 21 |
+
'goal': 'Build Muscle', 'level': 'Beginner', 'equip': [], 'gender': 'Male'
|
| 22 |
+
}
|
| 23 |
+
if 'profile_complete' not in st.session_state:
|
| 24 |
+
st.session_state.profile_complete = False
|
| 25 |
+
|
| 26 |
+
# --- Helper Functions ---
|
| 27 |
+
def calculate_bmi(w, h_cm):
|
| 28 |
+
if h_cm > 0:
|
| 29 |
+
return round(w / ((h_cm / 100) ** 2), 2)
|
| 30 |
+
return 0
|
| 31 |
+
|
| 32 |
+
def bmi_category(bmi):
|
| 33 |
+
if bmi < 18.5: return "Underweight"
|
| 34 |
+
if bmi < 25: return "Normal"
|
| 35 |
+
if bmi < 30: return "Overweight"
|
| 36 |
+
return "Obese"
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@st.cache_resource
|
| 40 |
+
def load_model():
|
| 41 |
+
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base")
|
| 42 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
|
| 43 |
+
return tokenizer, model
|
| 44 |
+
|
| 45 |
+
tokenizer, model = load_model()
|
| 46 |
+
|
| 47 |
+
# --- Sidebar Navigation ---
|
| 48 |
+
with st.sidebar:
|
| 49 |
+
st.markdown("<h1 style='color: #00F260;'>π FitPlan AI</h1>", unsafe_allow_html=True)
|
| 50 |
+
menu = st.radio("MENU", ["π€ Profile", "π Dashboard"])
|
| 51 |
+
|
| 52 |
+
# --- NAVIGATION LOGIC ---
|
| 53 |
+
|
| 54 |
+
if menu == "π€ Profile":
|
| 55 |
+
st.title("π€ User Profile")
|
| 56 |
+
|
| 57 |
+
name = st.text_input("Name", value=st.session_state.user_data['name'])
|
| 58 |
+
gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
| 59 |
+
col1, col2 = st.columns(2)
|
| 60 |
+
height = col1.number_input("Height (cm)", min_value=0.0, value=st.session_state.user_data['height'])
|
| 61 |
+
weight = col2.number_input("Weight (kg)", min_value=0.0, value=st.session_state.user_data['weight'])
|
| 62 |
+
|
| 63 |
+
goal = st.selectbox("Fitness Goal", ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"])
|
| 64 |
+
equipment = st.multiselect("Equipment", ["Dumbbells", "Barbell", "Kettlebell", "Resistance Band", "Yoga Mat", "Full Gym", "No Equipment"])
|
| 65 |
+
fitness_level = st.select_slider("Fitness Level", options=["Beginner", "Intermediate", "Advanced"])
|
| 66 |
+
|
| 67 |
+
bmi = calculate_bmi(weight, height)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
if st.button(" Submit Profile"):
|
| 71 |
+
|
| 72 |
+
if not name:
|
| 73 |
+
st.error("Please enter your name.")
|
| 74 |
+
|
| 75 |
+
elif height <= 0 or weight <= 0:
|
| 76 |
+
st.error("Please enter valid height and weight.")
|
| 77 |
+
|
| 78 |
+
elif not equipment:
|
| 79 |
+
st.error("Please select at least one equipment option.")
|
| 80 |
+
|
| 81 |
else:
|
| 82 |
+
st.success(" Profile Submitted Successfully!")
|
| 83 |
+
|
| 84 |
+
# Syncing data for dashboard
|
| 85 |
+
st.session_state.user_data.update({
|
| 86 |
+
'name': name, 'height': height, 'weight': weight,
|
| 87 |
+
'goal': goal, 'equip': equipment, 'level': fitness_level, 'gender': gender
|
| 88 |
+
})
|
| 89 |
+
st.session_state.profile_complete = True
|
| 90 |
+
|
| 91 |
+
bmi_status = bmi_category(bmi)
|
| 92 |
+
equipment_list = ", ".join(equipment)
|
| 93 |
+
|
| 94 |
+
prompt = f"""
|
| 95 |
+
You are a certified professional fitness trainer.
|
| 96 |
+
Create a detailed 5-day workout plan.
|
| 97 |
+
User Information:
|
| 98 |
+
- Gender: {gender}
|
| 99 |
+
- BMI: {bmi:.2f} ({bmi_status})
|
| 100 |
+
- Goal: {goal}
|
| 101 |
+
- Fitness Level: {fitness_level}
|
| 102 |
+
- Equipment Available: {equipment_list}
|
| 103 |
+
Start directly with:
|
| 104 |
+
Day 1:
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
with st.spinner("Generating your AI workout plan..."):
|
| 108 |
+
|
| 109 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 110 |
+
|
| 111 |
+
outputs = model.generate(
|
| 112 |
+
**inputs,
|
| 113 |
+
max_new_tokens=600,
|
| 114 |
+
temperature=0.7,
|
| 115 |
+
do_sample=True
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
| 119 |
+
|
| 120 |
+
st.subheader(" Your Personalized Workout Plan")
|
| 121 |
st.write(result)
|
| 122 |
+
|
| 123 |
+
elif menu == "π Dashboard":
|
| 124 |
+
if not st.session_state.profile_complete:
|
| 125 |
+
st.warning("Please complete your profile first.")
|
| 126 |
+
else:
|
| 127 |
+
ud = st.session_state.user_data
|
| 128 |
+
bmi = calculate_bmi(ud['weight'], ud['height'])
|
| 129 |
+
st.title(f"Welcome, {ud['name']}!")
|
| 130 |
+
st.metric("Current BMI", f"{bmi}")
|
| 131 |
+
st.write(f"**Current Goal:** {ud['goal']}")
|
| 132 |
+
st.write(f"**Equipment Available:** {', '.join(ud['equip'])}")
|