Spaces:
Sleeping
Sleeping
File size: 8,145 Bytes
827e896 8529f50 827e896 8529f50 cd20d52 db9559c 8529f50 827e896 8529f50 827e896 8529f50 827e896 8529f50 827e896 8529f50 827e896 8529f50 827e896 cd20d52 8529f50 cd20d52 2de204c db9559c 827e896 | 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | import gradio as gr
import pandas as pd
import joblib
from collections import OrderedDict
# Load models and encoders
food_model = joblib.load("goal_classifier.pkl")
exercise_model = joblib.load("exercise_classifier.pkl")
encoders = joblib.load("encoder.pkl")
df = pd.read_csv("fitness_meal_plan_with_exercises.csv")
# Load individual encoders
le_gender = encoders['gender']
le_workout = encoders['workout']
le_goal = encoders['goal']
le_exercise = encoders['exercise']
preprocessor = encoders['preprocessor']
def calculate_bmi(weight_kg, height_cm):
return weight_kg / ((height_cm / 100) ** 2)
def get_meal_plan(week, day):
filtered = df[(df['Week'] == week) & (df['Day'] == day)]
if not filtered.empty:
row = filtered.iloc[0]
return OrderedDict([
("Breakfast", {"Meal": row["Breakfast"], "Calories": int(row["Calories_Breakfast"])}),
("Snack_1", {"Meal": row["Snack_1"], "Calories": int(row["Calories_Snack_1"])}),
("Lunch", {"Meal": row["Lunch"], "Calories": int(row["Calories_Lunch"])}),
("Snack_2", {"Meal": row["Snack_2"], "Calories": int(row["Calories_Snack_2"])}),
("Dinner", {"Meal": row["Dinner"], "Calories": int(row["Calories_Dinner"])}),
])
return OrderedDict()
def get_exercise(week, day):
filtered = df[(df['Week'] == week) & (df['Day'] == day)]
if not filtered.empty:
row = filtered.iloc[0]
try:
row['Exercise_Name'] = le_exercise.inverse_transform([row['Exercise_Name']])[0]
except:
pass
return row[['Exercise_Name', 'Exercise_Description', 'Exercise_Duration']].to_dict()
return {}
def recommend(choice, gender, age, height_cm, weight_kg, workout_history, goal, week, day):
try:
# Encode inputs
user_input = {
'Gender': le_gender.transform([gender])[0],
'Age': age,
'Height_cm': height_cm,
'Weight_kg': weight_kg,
'Workout_History': le_workout.transform([workout_history])[0],
'Goal': le_goal.transform([goal])[0],
'Week': week,
'Day': day
}
user_input['BMI'] = calculate_bmi(weight_kg, height_cm)
# Prepare for prediction
user_df = pd.DataFrame([user_input])
user_X = preprocessor.transform(user_df)
# Optionally use models (if needed for future logic)
food_model.predict(user_X)
exercise_model.predict(user_X)
# Output
if choice == "meal":
meal_plan = get_meal_plan(week, day)
return {
"Meal_Plan": meal_plan,
"Total_Calories": sum(meal["Calories"] for meal in meal_plan.values())
}
elif choice == "exercise":
return {"Exercise": get_exercise(week, day)}
else:
return {"error": "Invalid choice. Must be 'meal' or 'exercise'"}
except Exception as e:
return {"error": str(e)}
# Gradio interface
demo = gr.Interface(
fn=recommend,
inputs=[
gr.Radio(choices=["meal", "exercise"], label="Choice"),
gr.Radio(choices=["Male", "Female"], label="Gender"),
gr.Slider(10, 80, step=1, label="Age"),
gr.Slider(100, 220, step=1, label="Height (cm)"),
gr.Slider(30, 200, step=1, label="Weight (kg)"),
gr.Dropdown(choices=le_workout.classes_.tolist(), label="Workout History"),
gr.Dropdown(choices=le_goal.classes_.tolist(), label="Goal"),
gr.Slider(1, 4, step=1, label="Week"),
gr.Slider(1, 7, step=1, label="Day")
],
outputs=gr.JSON(label="Recommendation"),
title="Fitness Meal & Exercise Recommendation System",
description="Select your info and receive a personalized meal plan or exercise for the chosen week & day."
)
if __name__ == "__main__":
demo.launch()
# import gradio as gr
# import pandas as pd
# import joblib
# from collections import OrderedDict
# # Load models
# food_model = joblib.load("goal_classifier.pkl")
# exercise_model = joblib.load("exercise_classifier.pkl")
# # Load individual encoders & preprocessor
# le_gender = joblib.load("le_gender.pkl")
# le_workout = joblib.load("le_workout.pkl")
# le_goal = joblib.load("le_goal.pkl")
# le_exercise = joblib.load("exercise.pkl")
# preprocessor = joblib.load("preprocessor.pkl")
# # Load dataset
# df = pd.read_csv("fitness_meal_plan_with_exercises.csv")
# # Reverse map exercise ID to name
# exercise_reverse_mapping = {v: k for k, v in le_exercise.items()}
# def calculate_bmi(weight_kg, height_cm):
# return weight_kg / ((height_cm / 100) ** 2)
# def get_meal_plan(week, day):
# filtered = df[(df['Week'] == week) & (df['Day'] == day)]
# if not filtered.empty:
# row = filtered.iloc[0]
# return OrderedDict([
# ("Breakfast", {"Meal": row["Breakfast"], "Calories": int(row["Calories_Breakfast"])}),
# ("Snack_1", {"Meal": row["Snack_1"], "Calories": int(row["Calories_Snack_1"])}),
# ("Lunch", {"Meal": row["Lunch"], "Calories": int(row["Calories_Lunch"])}),
# ("Snack_2", {"Meal": row["Snack_2"], "Calories": int(row["Calories_Snack_2"])}),
# ("Dinner", {"Meal": row["Dinner"], "Calories": int(row["Calories_Dinner"])}),
# ])
# return OrderedDict()
# def get_exercise(week, day):
# filtered = df[(df['Week'] == week) & (df['Day'] == day)]
# if not filtered.empty:
# row = filtered.iloc[0]
# exercise_id = row['Exercise_ID']
# exercise_name = exercise_reverse_mapping.get(exercise_id, "Unknown Exercise")
# return {
# "Exercise_Name": exercise_name,
# "Exercise_Description": row["Exercise_Description"],
# "Exercise_Duration": row["Exercise_Duration"]
# }
# return {}
# def recommend(choice, gender, age, height_cm, weight_kg, workout_history, goal, week, day):
# try:
# user_input = {
# 'Gender': le_gender.transform([gender])[0],
# 'Age': age,
# 'Height_cm': height_cm,
# 'Weight_kg': weight_kg,
# 'Workout_History': le_workout.transform([workout_history])[0],
# 'Goal': le_goal.transform([goal])[0],
# 'Week': week,
# 'Day': day
# }
# user_input['BMI'] = calculate_bmi(weight_kg, height_cm)
# # Prepare for prediction
# user_df = pd.DataFrame([user_input])
# user_X = preprocessor.transform(user_df)
# # Predict (not used yet, can be incorporated)
# _ = food_model.predict(user_X)
# _ = exercise_model.predict(user_X)
# if choice == "meal":
# meal_plan = get_meal_plan(week, day)
# return {
# "Meal_Plan": meal_plan,
# "Total_Calories": sum(meal["Calories"] for meal in meal_plan.values())
# }
# elif choice == "exercise":
# return {"Exercise": get_exercise(week, day)}
# else:
# return {"error": "Invalid choice. Must be 'meal' or 'exercise'"}
# except Exception as e:
# return {"error": str(e)}
# # Gradio interface
# demo = gr.Interface(
# fn=recommend,
# inputs=[
# gr.Radio(choices=["meal", "exercise"], label="Choice"),
# gr.Radio(choices=["Male", "Female"], label="Gender"),
# gr.Slider(10, 80, step=1, label="Age"),
# gr.Slider(100, 220, step=1, label="Height (cm)"),
# gr.Slider(30, 200, step=1, label="Weight (kg)"),
# gr.Dropdown(choices=le_workout.classes_.tolist(), label="Workout History"),
# gr.Dropdown(choices=le_goal.classes_.tolist(), label="Goal"),
# gr.Slider(1, 4, step=1, label="Week"),
# gr.Slider(1, 7, step=1, label="Day")
# ],
# outputs=gr.JSON(label="Recommendation"),
# title="Fitness Meal & Exercise Recommendation System",
# description="Select your details to receive a personalized meal or exercise plan for the selected week and day."
# )
# if __name__ == "__main__":
# demo.launch()
|