Spaces:
Sleeping
Sleeping
Commit
·
827e896
1
Parent(s):
db3c5d0
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,105 @@
|
|
| 1 |
-
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import joblib
|
| 4 |
-
from
|
| 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 |
-
user_input =
|
| 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 |
-
if __name__ ==
|
| 104 |
-
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
from collections import OrderedDict
|
| 5 |
+
|
| 6 |
+
# Load models and encoders
|
| 7 |
+
food_model = joblib.load("goal_classifier.pkl")
|
| 8 |
+
exercise_model = joblib.load("exercise_classifier.pkl")
|
| 9 |
+
encoders = joblib.load("encoders.pkl")
|
| 10 |
+
df = pd.read_csv("fitness_meal_plan_with_exercises.csv")
|
| 11 |
+
|
| 12 |
+
# Load individual encoders
|
| 13 |
+
le_gender = encoders['gender']
|
| 14 |
+
le_workout = encoders['workout']
|
| 15 |
+
le_goal = encoders['goal']
|
| 16 |
+
le_exercise = encoders['exercise']
|
| 17 |
+
preprocessor = encoders['preprocessor']
|
| 18 |
+
|
| 19 |
+
def calculate_bmi(weight_kg, height_cm):
|
| 20 |
+
return weight_kg / ((height_cm / 100) ** 2)
|
| 21 |
+
|
| 22 |
+
def get_meal_plan(week, day):
|
| 23 |
+
filtered = df[(df['Week'] == week) & (df['Day'] == day)]
|
| 24 |
+
if not filtered.empty:
|
| 25 |
+
row = filtered.iloc[0]
|
| 26 |
+
return OrderedDict([
|
| 27 |
+
("Breakfast", {"Meal": row["Breakfast"], "Calories": int(row["Calories_Breakfast"])}),
|
| 28 |
+
("Snack_1", {"Meal": row["Snack_1"], "Calories": int(row["Calories_Snack_1"])}),
|
| 29 |
+
("Lunch", {"Meal": row["Lunch"], "Calories": int(row["Calories_Lunch"])}),
|
| 30 |
+
("Snack_2", {"Meal": row["Snack_2"], "Calories": int(row["Calories_Snack_2"])}),
|
| 31 |
+
("Dinner", {"Meal": row["Dinner"], "Calories": int(row["Calories_Dinner"])}),
|
| 32 |
+
])
|
| 33 |
+
return OrderedDict()
|
| 34 |
+
|
| 35 |
+
def get_exercise(week, day):
|
| 36 |
+
filtered = df[(df['Week'] == week) & (df['Day'] == day)]
|
| 37 |
+
if not filtered.empty:
|
| 38 |
+
row = filtered.iloc[0]
|
| 39 |
+
try:
|
| 40 |
+
row['Exercise_Name'] = le_exercise.inverse_transform([row['Exercise_Name']])[0]
|
| 41 |
+
except:
|
| 42 |
+
pass
|
| 43 |
+
return row[['Exercise_Name', 'Exercise_Description', 'Exercise_Duration']].to_dict()
|
| 44 |
+
return {}
|
| 45 |
+
|
| 46 |
+
def recommend(choice, gender, age, height_cm, weight_kg, workout_history, goal, week, day):
|
| 47 |
+
try:
|
| 48 |
+
# Encode inputs
|
| 49 |
+
user_input = {
|
| 50 |
+
'Gender': le_gender.transform([gender])[0],
|
| 51 |
+
'Age': age,
|
| 52 |
+
'Height_cm': height_cm,
|
| 53 |
+
'Weight_kg': weight_kg,
|
| 54 |
+
'Workout_History': le_workout.transform([workout_history])[0],
|
| 55 |
+
'Goal': le_goal.transform([goal])[0],
|
| 56 |
+
'Week': week,
|
| 57 |
+
'Day': day
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
user_input['BMI'] = calculate_bmi(weight_kg, height_cm)
|
| 61 |
+
|
| 62 |
+
# Prepare for prediction
|
| 63 |
+
user_df = pd.DataFrame([user_input])
|
| 64 |
+
user_X = preprocessor.transform(user_df)
|
| 65 |
+
|
| 66 |
+
# Optionally use models (if needed for future logic)
|
| 67 |
+
food_model.predict(user_X)
|
| 68 |
+
exercise_model.predict(user_X)
|
| 69 |
+
|
| 70 |
+
# Output
|
| 71 |
+
if choice == "meal":
|
| 72 |
+
meal_plan = get_meal_plan(week, day)
|
| 73 |
+
return {
|
| 74 |
+
"Meal_Plan": meal_plan,
|
| 75 |
+
"Total_Calories": sum(meal["Calories"] for meal in meal_plan.values())
|
| 76 |
+
}
|
| 77 |
+
elif choice == "exercise":
|
| 78 |
+
return {"Exercise": get_exercise(week, day)}
|
| 79 |
+
else:
|
| 80 |
+
return {"error": "Invalid choice. Must be 'meal' or 'exercise'"}
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return {"error": str(e)}
|
| 83 |
+
|
| 84 |
+
# Gradio interface
|
| 85 |
+
demo = gr.Interface(
|
| 86 |
+
fn=recommend,
|
| 87 |
+
inputs=[
|
| 88 |
+
gr.Radio(choices=["meal", "exercise"], label="Choice"),
|
| 89 |
+
gr.Radio(choices=["Male", "Female"], label="Gender"),
|
| 90 |
+
gr.Slider(10, 80, step=1, label="Age"),
|
| 91 |
+
gr.Slider(100, 220, step=1, label="Height (cm)"),
|
| 92 |
+
gr.Slider(30, 200, step=1, label="Weight (kg)"),
|
| 93 |
+
gr.Dropdown(choices=le_workout.classes_.tolist(), label="Workout History"),
|
| 94 |
+
gr.Dropdown(choices=le_goal.classes_.tolist(), label="Goal"),
|
| 95 |
+
gr.Slider(1, 4, step=1, label="Week"),
|
| 96 |
+
gr.Slider(1, 7, step=1, label="Day")
|
| 97 |
+
],
|
| 98 |
+
outputs=gr.JSON(label="Recommendation"),
|
| 99 |
+
title="Fitness Meal & Exercise Recommendation System",
|
| 100 |
+
description="Select your info and receive a personalized meal plan or exercise for the chosen week & day."
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
demo.launch()
|
| 105 |
+
|