File size: 4,491 Bytes
fdf478b
b72c85c
 
6a5279a
7f2c5ae
71ef033
7f2c5ae
56dddfd
6a5279a
71ef033
 
 
 
 
 
ee19551
56dddfd
 
71ef033
 
 
 
3b51405
71ef033
ee19551
b72c85c
56dddfd
71ef033
 
b72c85c
75cb91d
71ef033
 
 
 
 
 
b72c85c
71ef033
 
b72c85c
 
 
71ef033
b72c85c
75cb91d
4a2c6aa
7eda077
71ef033
 
7eda077
71ef033
ee19551
92d7193
8f27092
7347eb1
71ef033
 
 
b72c85c
 
71ef033
 
 
 
8ec3d63
b72c85c
4a2c6aa
b72c85c
 
71ef033
 
 
b72c85c
 
 
 
71ef033
 
b72c85c
4a2c6aa
b72c85c
cb8a7c4
b72c85c
71ef033
b72c85c
71ef033
 
 
 
 
 
 
1778636
 
 
a7440e5
1778636
a7440e5
1778636
 
 
 
a7440e5
1778636
a7440e5
1778636
a7440e5
1778636
71ef033
 
bf20139
b72c85c
 
 
 
1778636
 
 
b72c85c
 
 
71ef033
b72c85c
71ef033
7347eb1
71ef033
 
 
 
 
 
 
 
 
b72c85c
1
2
3
4
5
6
7
8
9
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM  
import torch 

# --- Page Config ---
st.set_page_config(page_title="FitPlan AI", page_icon="πŸ‹", layout="wide")

# --- Custom Styling ---
st.markdown("""
   <style>
   .stApp { background-color: #0E1117; color: white; }
   [data-testid="stSidebar"] { background-color: #161B22 !important; }
   .stButton > button { width: 100%; border-radius: 8px; font-weight: bold; background-color: #00F260; color: black; }
   </style>
   """, unsafe_allow_html=True)

# --- Initialize Session State ---
if 'user_data' not in st.session_state:
   st.session_state.user_data = {
       'name': '', 'age': 25, 'height': 0.0, 'weight': 0.0,
       'goal': 'Build Muscle', 'level': 'Beginner', 'equip': [], 'gender': 'Male'
   }
if 'profile_complete' not in st.session_state:
   st.session_state.profile_complete = False

# --- Helper Functions ---
def calculate_bmi(w, h_cm):
   if h_cm > 0:
       return round(w / ((h_cm / 100) ** 2), 2)
   return 0

def bmi_category(bmi):
   if bmi < 18.5: return "Underweight"
   if bmi < 25: return "Normal"
   if bmi < 30: return "Overweight"
   return "Obese"


@st.cache_resource 
def load_model():
    tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base")
    model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
    return tokenizer, model

tokenizer, model = load_model()

# --- Sidebar Navigation ---
with st.sidebar:
   st.markdown("<h1 style='color: #00F260;'>πŸ‹ FitPlan AI</h1>", unsafe_allow_html=True)
   menu = st.radio("MENU", ["πŸ‘€ Profile", "πŸ“Š Dashboard"])

# --- NAVIGATION LOGIC ---

if menu == "πŸ‘€ Profile":
   st.title("πŸ‘€ User Profile")
   
   name = st.text_input("Name", value=st.session_state.user_data['name'])
   gender = st.selectbox("Gender", ["Male", "Female", "Other"])
   col1, col2 = st.columns(2)
   height = col1.number_input("Height (cm)", min_value=0.0, value=st.session_state.user_data['height'])
   weight = col2.number_input("Weight (kg)", min_value=0.0, value=st.session_state.user_data['weight'])
   
   goal = st.selectbox("Fitness Goal", ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"])
   equipment = st.multiselect("Equipment", ["Dumbbells", "Barbell", "Kettlebell", "Resistance Band", "Yoga Mat", "Full Gym", "No Equipment"])
   fitness_level = st.select_slider("Fitness Level", options=["Beginner", "Intermediate", "Advanced"])

   bmi = calculate_bmi(weight, height)

  
   if st.button(" Submit Profile"):
    
    if not name:
        st.error("Please enter your name.")
 
    elif height <= 0 or weight <= 0:
        st.error("Please enter valid height and weight.")
 
    elif not equipment:
        st.error("Please select at least one equipment option.")
 
    else:
        st.success(" Profile Submitted Successfully!")
        
        # Syncing data for dashboard
        st.session_state.user_data.update({
            'name': name, 'height': height, 'weight': weight, 
            'goal': goal, 'equip': equipment, 'level': fitness_level, 'gender': gender
        })
        st.session_state.profile_complete = True
 
        bmi_status = bmi_category(bmi)
        equipment_list = ", ".join(equipment)
 
        prompt = f"""
You are a certified professional fitness trainer.

Create a detailed 5-day workout plan.

User Information:
- Gender: {gender}
- BMI: {bmi:.2f} ({bmi_status})
- Goal: {goal}
- Fitness Level: {fitness_level}
- Equipment Available: {equipment_list}

Start directly with:

Day 1:
"""
 
        with st.spinner("Generating your AI workout plan..."):
            
            inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
 
            outputs = model.generate(
                **inputs,
                max_new_tokens=600,
                temperature=0.7,
                do_sample=True
            )
 
            result = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
 
        st.subheader(" Your Personalized Workout Plan")
        st.write(result)

elif menu == "πŸ“Š Dashboard":
   if not st.session_state.profile_complete:
       st.warning("Please complete your profile first.")
   else:
       ud = st.session_state.user_data
       bmi = calculate_bmi(ud['weight'], ud['height'])
       st.title(f"Welcome, {ud['name']}!")
       st.metric("Current BMI", f"{bmi}")
       st.write(f"**Current Goal:** {ud['goal']}")
       st.write(f"**Equipment Available:** {', '.join(ud['equip'])}")