Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 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=[
|
| 45 |
+
gr.Slider(18, 80, value=30, label="Age"),
|
| 46 |
+
gr.Radio(["Male", "Female"], label="Gender", value="Male"),
|
| 47 |
+
gr.Number(label="Weight (kg)", value=75),
|
| 48 |
+
gr.Number(label="Height (cm)", value=175),
|
| 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()
|