Tomertg commited on
Commit
7d003a9
Β·
verified Β·
1 Parent(s): 89f9505

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +284 -29
app.py CHANGED
@@ -5,40 +5,296 @@ import pandas as pd
5
  # 1. Load the trained model
6
  model = joblib.load('fitness_model.joblib')
7
 
8
- # 2. Define the prediction function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def predict_plan(age, gender, weight, height, goal, equipment, injury, experience):
10
- # Create DataFrame with exact column names as trained
11
  input_data = pd.DataFrame({
12
- 'Age': [age],
13
- 'Gender': [gender],
14
- 'Weight_kg': [weight],
15
- 'Height_cm': [height],
16
- 'Goal': [goal],
17
- 'Equipment': [equipment],
18
- 'Injury': [injury],
19
- 'Experience': [experience]
20
  })
21
 
22
- # Predict
23
- prediction = model.predict(input_data)[0]
24
 
25
- # Generate Smart Tip based on logic
26
- tip = f"Goal: {goal}. "
27
 
 
 
28
  if injury != 'None':
29
- tip += f"Note: Because of your {injury} injury, this plan avoids high impact stress on that area."
30
  elif goal == 'Muscle Gain':
31
- tip += "Focus on progressive overload and sufficient protein intake."
32
  elif goal == 'Weight Loss':
33
- tip += "Combine this workout with a slight caloric deficit for best results."
34
- elif goal == 'Strength':
35
- tip += "Rest periods of 3-5 minutes between sets are recommended for strength."
36
- else:
37
- tip += "Stay consistent and listen to your body!"
38
 
39
- return prediction, tip
40
 
41
- # 3. Create the Gradio Interface
42
  iface = gr.Interface(
43
  fn=predict_plan,
44
  inputs=[
@@ -49,16 +305,15 @@ iface = gr.Interface(
49
  gr.Dropdown(['Weight Loss', 'Muscle Gain', 'Endurance', 'General Health', 'Strength', 'Athletic Performance'], label="Goal", value="General Health"),
50
  gr.Dropdown(['Gym Membership', 'Home Dumbbells', 'Bodyweight Only', 'Full Home Gym (Rack+Barbell)'], label="Equipment", value="Gym Membership"),
51
  gr.Dropdown(['None', 'Knee', 'Back', 'Shoulder', 'Ankle'], label="Injury", value="None"),
52
- gr.Radio(['Beginner', 'Intermediate', 'Advanced'], label="Experience", value="Beginner")
53
  ],
54
  outputs=[
55
- gr.Textbox(label="Recommended Workout Plan", type="text"),
56
- gr.Textbox(label="AI Coach Tip", type="text")
57
  ],
58
- title="SmartFit AI - Workout Recommender",
59
- description="Enter your profile details to get a machine-learning optimized workout plan based on 10,000 synthetic profiles.",
60
  theme="soft"
61
  )
62
 
63
- # 4. Launch App
64
  iface.launch()
 
5
  # 1. Load the trained model
6
  model = joblib.load('fitness_model.joblib')
7
 
8
+ # 2. COMPLETE Workout Database (Covers ALL 19 plans from the dataset)
9
+ workout_database = {
10
+ # --- Injury Friendly / Rehab ---
11
+ "Lower Body Power & Stationary Bike": """
12
+ 🎯 GOAL: Legs & Cardio (Shoulder Injury Safe)
13
+
14
+ WARM-UP:
15
+ - 5 min Light Cycling
16
+ - Leg Swings (20 per leg)
17
+
18
+ THE WORKOUT:
19
+ 1. Leg Press (Machine): 3 sets x 12-15 reps (Moderate weight)
20
+ 2. Goblet Squats (Hold weight at chest): 3 sets x 10 reps
21
+ 3. Walking Lunges: 3 sets x 12 steps per leg
22
+ 4. Seated Leg Extensions: 3 sets x 15 reps
23
+ 5. Standing Calf Raises: 4 sets x 20 reps
24
+
25
+ CARDIO FINISHER:
26
+ - Stationary Bike: 15 mins total (30 sec sprint / 30 sec slow)
27
+ """,
28
+
29
+ "Seated Upper Body Strength & Rowing": """
30
+ 🎯 GOAL: Upper Body Power (Ankle/Leg Injury Safe)
31
+
32
+ WARM-UP:
33
+ - 5 min Rowing Machine (Light pace)
34
+ - Arm Circles & Torso Twists
35
+
36
+ THE WORKOUT (All Seated):
37
+ 1. Seated Dumbbell Overhead Press: 3 sets x 10 reps
38
+ 2. Seated Chest Press (Machine): 3 sets x 12 reps
39
+ 3. Lat Pulldowns (Wide Grip): 3 sets x 12 reps
40
+ 4. Seated Bicep Curls (Dumbbells): 3 sets x 12 reps
41
+ 5. Seated Tricep Overhead Extension: 3 sets x 15 reps
42
+
43
+ CORE:
44
+ - Seated Russian Twists (feet hooked): 3 sets x 20 reps
45
+ """,
46
+
47
+ "Upper Body Calisthenics & Core": """
48
+ 🎯 GOAL: Bodyweight Strength (Knee Injury Safe)
49
+
50
+ WARM-UP:
51
+ - Arm Swings, Wrist Rotations
52
+
53
+ THE WORKOUT:
54
+ 1. Push-ups (Classic or Knees): 3 sets x MAX reps
55
+ 2. Pull-ups (or Bodyweight Rows under table): 3 sets x 6-10 reps
56
+ 3. Dips (Chair or Bar): 3 sets x 10 reps
57
+ 4. Plank Hold: 3 sets x 45-60 seconds
58
+ 5. Side Plank: 3 sets x 30 sec per side
59
+ 6. Supermans (Lower back): 3 sets x 15 reps
60
+ """,
61
+
62
+ "Core Stability, Yoga & Walking": """
63
+ 🎯 GOAL: Active Recovery & Back Health (Back Injury Safe)
64
+
65
+ WARM-UP:
66
+ - 5 min Slow Walk
67
+
68
+ YOGA FLOW (Hold each for 30-45 sec):
69
+ 1. Cat-Cow Stretch (Mobilize spine)
70
+ 2. Bird-Dog (Stabilize core): 3 sets x 10 per side
71
+ 3. Child's Pose (Rest lower back)
72
+ 4. Glute Bridges: 3 sets x 15 reps
73
+ 5. Cobra Stretch (Gentle extension)
74
+
75
+ CARDIO:
76
+ - Walking: 30 minutes brisk pace (Outdoor or Treadmill 1% incline)
77
+ """,
78
+
79
+ # --- Strength & Muscle Building ---
80
+ "Push-Pull-Legs Split (Hypertrophy)": """
81
+ 🎯 GOAL: Maximum Muscle Growth (Advanced)
82
+
83
+ TODAY'S SESSION: PUSH (Chest, Shoulders, Triceps)
84
+ 1. Barbell Bench Press: 4 sets x 8 reps (Heavy)
85
+ 2. Incline Dumbbell Press: 3 sets x 10 reps
86
+ 3. Overhead Military Press: 3 sets x 10 reps
87
+ 4. Lateral Raises (Dumbbells): 4 sets x 15 reps (Burnout)
88
+ 5. Cable Tricep Pushdowns: 3 sets x 12 reps
89
+ """,
90
+
91
+ "5x5 Powerlifting Program": """
92
+ 🎯 GOAL: Pure Strength (Powerlifting Style)
93
+
94
+ PROTOCOL: Rest 3-5 mins between sets. Lift Heavy.
95
+
96
+ THE BIG 3:
97
+ 1. Barbell Squat: 5 sets x 5 reps
98
+ 2. Bench Press: 5 sets x 5 reps
99
+ 3. Deadlift: 1 set x 5 reps (Very Heavy)
100
+
101
+ ACCESSORY WORK:
102
+ - Pull-ups: 3 sets x Failure
103
+ - Dips: 3 sets x Failure
104
+ """,
105
+
106
+ "Starting Strength (3x5)": """
107
+ 🎯 GOAL: Beginner Strength Foundation
108
+
109
+ PROTOCOL: Focus on perfect form. Add weight each workout.
110
+
111
+ THE WORKOUT:
112
+ 1. Squat: 3 sets x 5 reps
113
+ 2. Overhead Press: 3 sets x 5 reps
114
+ 3. Deadlift: 1 set x 5 reps
115
+ """,
116
+
117
+ "Dumbbell Upper/Lower Split": """
118
+ 🎯 GOAL: Balanced Physique with Dumbbells
119
+
120
+ TODAY: UPPER BODY
121
+ 1. Dumbbell Floor Press: 3 sets x 10 reps
122
+ 2. Single-Arm Dumbbell Row: 3 sets x 12 per side
123
+ 3. Dumbbell Shoulder Press: 3 sets x 10 reps
124
+ 4. Dumbbell Shrugs: 3 sets x 15 reps
125
+ 5. Bicep Curls vs Skullcrushers (Superset): 3 sets x 12 reps
126
+ """,
127
+
128
+ "3-Day Full Body Compound Lifts": """
129
+ 🎯 GOAL: Efficiency (3 days/week)
130
+
131
+ THE WORKOUT:
132
+ 1. Goblet Squat: 3 sets x 10 reps
133
+ 2. Push-ups: 3 sets x 15 reps
134
+ 3. Dumbbell Lunges: 3 sets x 10 per leg
135
+ 4. Dumbbell Shoulder Press: 3 sets x 10
136
+ 5. Plank: 3 sets x 60 sec
137
+ """,
138
+
139
+ # --- Cardio & Athletic Performance ---
140
+ "Outdoor Running (5k-10k Prep)": """
141
+ 🎯 GOAL: Endurance Running
142
+
143
+ TODAY'S RUN: Interval Training
144
+ - Warm-up: 5 min Brisk Walk + Dynamic Stretching
145
+ - Run 1 km Easy Pace
146
+ - Interval: 4 rounds of [500m Fast / 2 mins Walk]
147
+ - Run 1 km Easy Pace to finish
148
+ - Cool down: Static Stretching (Calves, Hamstrings)
149
+
150
+ TOTAL DISTANCE: Approx 4-5 km
151
+ """,
152
+
153
+ "Treadmill Intervals & Circuit Training": """
154
+ 🎯 GOAL: Fat Burn & Cardio
155
+
156
+ TREADMILL BLOCK (15 mins):
157
+ - 5 min Warmup walk
158
+ - 10 rounds of: 30 sec Sprint / 30 sec Rest (Stand on rails)
159
+
160
+ CIRCUIT BLOCK (Repeat 3 times):
161
+ 1. 15 Kettlebell/Dumbbell Swings
162
+ 2. 10 Pushups
163
+ 3. 15 Box Jumps (or Step ups)
164
+ 4. Rest 1 min
165
+ """,
166
+
167
+ "Sprints & Plyometrics": """
168
+ 🎯 GOAL: Explosive Speed & Power
169
+
170
+ WARM-UP (Critical): 10 mins jogging, high knees, butt kicks.
171
+
172
+ PLYOMETRICS:
173
+ 1. Box Jumps: 3 sets x 6 reps (Max height)
174
+ 2. Broad Jumps: 3 sets x 5 reps
175
+
176
+ SPRINTS:
177
+ - 4 x 40 meter sprints (100% effort)
178
+ - Rest 2 mins between sprints
179
+ """,
180
+
181
+ "Jump Rope & Burpee Intervals": """
182
+ 🎯 GOAL: High Intensity Cardio (No Equipment)
183
+
184
+ FORMAT: 30 sec work / 15 sec rest. Repeat for 20 mins.
185
+
186
+ THE LOOPS:
187
+ 1. Jump Rope (Regular bounce)
188
+ 2. Burpees
189
+ 3. Jump Rope (High knees)
190
+ 4. Mountain Climbers
191
+ 5. Jump Rope (Double unders or fast)
192
+ 6. Air Squats
193
+ (Repeat cycle)
194
+ """,
195
+
196
+ "Metabolic Resistance Training (Weights + Cardio)": """
197
+ 🎯 GOAL: Burn Fat while Toning
198
+
199
+ CIRCUIT (Do exercises back-to-back, rest 90s after full round. Do 4 Rounds):
200
+ 1. Dumbbell Thrusters (Squat + Press): 15 reps
201
+ 2. Renegade Rows (Plank + Row): 10 per side
202
+ 3. Mountain Climbers: 30 reps
203
+ 4. Kettlebell Swings: 20 reps
204
+ """,
205
+
206
+ # --- Functional & General Health ---
207
+ "Full Body Dumbbell Functional Flow": """
208
+ 🎯 GOAL: Daily movement & health
209
+
210
+ THE WORKOUT:
211
+ 1. Dumbbell Deadlifts: 3 sets x 12 reps
212
+ 2. Z-Press (Seated on floor shoulder press): 3 sets x 10
213
+ 3. Reverse Lunges: 3 sets x 10 per leg
214
+ 4. Farmer's Carries: 3 sets x 40 steps
215
+ 5. Bird-Dogs: 3 sets x 12 reps
216
+ """,
217
+
218
+ "Daily Yoga & Bodyweight Basics": """
219
+ 🎯 GOAL: Flexibility & Core
220
+
221
+ FLOW SEQUENCE (Repeat 2 times):
222
+ 1. Sun Salutation A (5 rounds)
223
+ 2. Warrior II pose (Hold 30 sec per side)
224
+ 3. Tree Pose (Balance): 30 sec per side
225
+ 4. Bodyweight Squats: 15 reps
226
+ 5. Downward Dog -> Plank flow: 10 reps
227
+ """,
228
+
229
+ "Advanced Calisthenics Progression": """
230
+ 🎯 GOAL: Bodyweight Mastery
231
+
232
+ SKILL WORK:
233
+ 1. Handstand Hold (Wall assisted): 3 sets x 30 sec
234
+ 2. L-Sit Practice: 3 sets x 10-15 sec
235
+
236
+ STRENGTH:
237
+ 1. Pistol Squats (or assisted): 3 sets x 5 per leg
238
+ 2. Archer Pushups: 3 sets x 8 per side
239
+ 3. Wide Grip Pullups: 3 sets x 8
240
+ """,
241
+
242
+ "HIIT with Dumbbells (Tabata Style)": """
243
+ 🎯 GOAL: Fast Fat Burn
244
+
245
+ TABATA PROTOCOL: 20 sec Work / 10 sec Rest. 8 Rounds per exercise.
246
+
247
+ EXERCISE 1: Dumbbell Goblet Squats (4 mins)
248
+ -- Rest 1 min --
249
+ EXERCISE 2: Dumbbell Push Press (4 mins)
250
+ -- Rest 1 min --
251
+ EXERCISE 3: Russian Twists (4 mins)
252
+ """,
253
+
254
+ "CrossFit Style WODs": """
255
+ 🎯 GOAL: Functional Fitness
256
+
257
+ WOD (Workout of the Day): "The Chipper"
258
+ For Time (Complete as fast as possible):
259
+ - 50 Air Squats
260
+ - 40 Sit-ups
261
+ - 30 Dumbbell Snatches (alternate arms)
262
+ - 20 Push-ups
263
+ - 10 Burpees
264
+
265
+ Time Cap: 15 mins. Go!
266
+ """
267
+ }
268
+
269
+ default_plan_text = "Please select a plan or adjust inputs."
270
+
271
+ # 3. Prediction Function
272
  def predict_plan(age, gender, weight, height, goal, equipment, injury, experience):
273
+ # Prepare Input
274
  input_data = pd.DataFrame({
275
+ 'Age': [age], 'Gender': [gender], 'Weight_kg': [weight],
276
+ 'Height_cm': [height], 'Goal': [goal], 'Equipment': [equipment],
277
+ 'Injury': [injury], 'Experience': [experience]
 
 
 
 
 
278
  })
279
 
280
+ # Predict Label
281
+ plan_name = model.predict(input_data)[0]
282
 
283
+ # Get Detailed Text (Lookup in dictionary)
284
+ full_routine = workout_database.get(plan_name, f"Routine for {plan_name} coming soon!")
285
 
286
+ # Generate Tip
287
+ tip = f"Plan Selected: {plan_name}\n"
288
  if injury != 'None':
289
+ tip += f"⚠️ SAFETY MODE: Adjusted for {injury} injury. Avoid heavy impact."
290
  elif goal == 'Muscle Gain':
291
+ tip += "πŸ’ͺ TIP: Eat 20g protein post-workout."
292
  elif goal == 'Weight Loss':
293
+ tip += "πŸ”₯ TIP: Keep heart rate up during rests."
 
 
 
 
294
 
295
+ return full_routine, tip
296
 
297
+ # 4. Interface
298
  iface = gr.Interface(
299
  fn=predict_plan,
300
  inputs=[
 
305
  gr.Dropdown(['Weight Loss', 'Muscle Gain', 'Endurance', 'General Health', 'Strength', 'Athletic Performance'], label="Goal", value="General Health"),
306
  gr.Dropdown(['Gym Membership', 'Home Dumbbells', 'Bodyweight Only', 'Full Home Gym (Rack+Barbell)'], label="Equipment", value="Gym Membership"),
307
  gr.Dropdown(['None', 'Knee', 'Back', 'Shoulder', 'Ankle'], label="Injury", value="None"),
308
+ gr.Radio(['Beginner', 'Intermediate', 'Advanced'], label="Experience", value="Intermediate")
309
  ],
310
  outputs=[
311
+ gr.Textbox(label="Your Personalized Routine", lines=15),
312
+ gr.Textbox(label="AI Coach Notes")
313
  ],
314
+ title="SmartFit AI πŸ‹οΈβ€β™‚οΈ",
315
+ description="Get a safe, goal-oriented workout plan generated by AI.",
316
  theme="soft"
317
  )
318
 
 
319
  iface.launch()