srbhavya01 commited on
Commit
83a2aa2
Β·
verified Β·
1 Parent(s): 78d5fce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -54
app.py CHANGED
@@ -1,58 +1,132 @@
1
  import streamlit as st
2
-
3
- # -------------------------
4
- # Page Config
5
- # -------------------------
6
-
7
- st.set_page_config(page_title="FitPlan AI", page_icon="πŸ’ͺ")
8
-
9
- st.title("πŸ’ͺ FitPlan AI β€” Personalized Workout Generator")
10
-
11
- # -------------------------
12
- # User Inputs
13
- # -------------------------
14
-
15
- name = st.text_input("Enter Your Name")
16
-
17
- gender = st.selectbox("Gender", ["Male", "Female"])
18
-
19
- height = st.number_input("Height (cm)", min_value=0.0)
20
- weight = st.number_input("Weight (kg)", min_value=0.0)
21
-
22
- goal = st.selectbox(
23
- "Select Your Fitness Goal",
24
- ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"]
25
- )
26
-
27
- equipment = st.multiselect(
28
- "Available Equipment",
29
- ["Dumbbells", "Resistance Band", "Yoga Mat", "No Equipment"]
30
- )
31
-
32
- fitness_level = st.radio(
33
- "Fitness Level",
34
- ["Beginner", "Intermediate", "Advanced"]
35
- )
36
-
37
- # -------------------------
38
- # Generate Plan Button
39
- # -------------------------
40
-
41
- if st.button("Generate Workout Plan"):
42
-
43
- if not name or height <= 0 or weight <= 0:
44
- st.error("⚠ Please fill all required fields correctly!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  else:
46
-
47
- prompt, bmi, status = build_prompt(
48
- name, gender, height, weight, goal, fitness_level, equipment
49
- )
50
-
51
- st.write(f"### πŸ“Š BMI: {round(bmi,2)} ({status})")
52
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  with st.spinner("Generating your AI workout plan..."):
54
- result = generate_plan(prompt, tokenizer, model)
55
-
56
- st.success("πŸ† Your Personalized Workout Plan")
 
 
 
 
 
 
 
 
 
 
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'])}")