AlaaElsayed commited on
Commit
827e896
·
1 Parent(s): db3c5d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -104
app.py CHANGED
@@ -1,104 +1,105 @@
1
- from flask import Flask, request, jsonify, make_response
2
- import pandas as pd
3
- import joblib
4
- from flask_cors import CORS
5
- from collections import OrderedDict
6
- import json
7
-
8
- app = Flask(__name__)
9
- CORS(app) # Enables cross-origin requests (e.g., from Flutter)
10
-
11
- # Load models and encoders
12
- food_model = joblib.load("goal_classifier.pkl")
13
- exercise_model = joblib.load("exercise_classifier.pkl")
14
- encoders = joblib.load("encoders.pkl")
15
- df = pd.read_csv("fitness_meal_plan_with_exercises.csv")
16
-
17
- # Load individual encoders
18
- le_gender = encoders['gender']
19
- le_workout = encoders['workout']
20
- le_goal = encoders['goal']
21
- le_exercise = encoders['exercise']
22
- preprocessor = encoders['preprocessor']
23
-
24
- # --- Utilities ---
25
- def calculate_bmi(weight_kg, height_cm):
26
- return weight_kg / ((height_cm / 100) ** 2)
27
-
28
- def get_meal_plan(week, day):
29
- filtered = df[(df['Week'] == week) & (df['Day'] == day)]
30
- if not filtered.empty:
31
- row = filtered.iloc[0]
32
- return OrderedDict([
33
- ("Breakfast", {"Meal": row["Breakfast"], "Calories": int(row["Calories_Breakfast"])}),
34
- ("Snack_1", {"Meal": row["Snack_1"], "Calories": int(row["Calories_Snack_1"])}),
35
- ("Lunch", {"Meal": row["Lunch"], "Calories": int(row["Calories_Lunch"])}),
36
- ("Snack_2", {"Meal": row["Snack_2"], "Calories": int(row["Calories_Snack_2"])}),
37
- ("Dinner", {"Meal": row["Dinner"], "Calories": int(row["Calories_Dinner"])})
38
- ])
39
- return OrderedDict()
40
-
41
- def get_exercise(week, day):
42
- filtered = df[(df['Week'] == week) & (df['Day'] == day)]
43
- if not filtered.empty:
44
- row = filtered.iloc[0]
45
- # Decode label if encoded
46
- try:
47
- row['Exercise_Name'] = le_exercise.inverse_transform([row['Exercise_Name']])[0]
48
- except:
49
- pass
50
- return row[['Exercise_Name', 'Exercise_Description', 'Exercise_Duration']].to_dict()
51
- return {}
52
-
53
- # --- Main API Endpoint ---
54
- @app.route("/recommend", methods=["POST"])
55
- def recommend():
56
- data = request.get_json()
57
-
58
- try:
59
- # Encode inputs
60
- user_input = {
61
- 'Gender': le_gender.transform([data['Gender']])[0],
62
- 'Age': data['Age'],
63
- 'Height_cm': data['Height_cm'],
64
- 'Weight_kg': data['Weight_kg'],
65
- 'Workout_History': le_workout.transform([data['Workout_History']])[0],
66
- 'Goal': le_goal.transform([data['Goal']])[0],
67
- 'Week': data['Week'],
68
- 'Day': data['Day']
69
- }
70
-
71
- # Add BMI for potential future use
72
- user_input['BMI'] = calculate_bmi(user_input['Weight_kg'], user_input['Height_cm'])
73
-
74
- # Prepare input for prediction (not used in current response)
75
- user_df = pd.DataFrame([user_input])
76
- user_X = preprocessor.transform(user_df)
77
-
78
- # Perform predictions (optional if not needed in response)
79
- food_model.predict(user_X)
80
- exercise_model.predict(user_X)
81
-
82
- # Return either meal or exercise plan
83
- if data['choice'] == 'meal':
84
- meal_plan = get_meal_plan(user_input['Week'], user_input['Day'])
85
- response_data = {
86
- "Meal_Plan": meal_plan,
87
- "Total_Calories": sum(meal["Calories"] for meal in meal_plan.values())
88
- }
89
-
90
- elif data['choice'] == 'exercise':
91
- exercise = get_exercise(user_input['Week'], user_input['Day'])
92
- response_data = {"Exercise": exercise}
93
-
94
- else:
95
- return jsonify({"error": "Invalid choice. Must be 'meal' or 'exercise'"}), 400
96
-
97
- return make_response(json.dumps(response_data, ensure_ascii=False), 200, {'Content-Type': 'application/json'})
98
-
99
- except Exception as e:
100
- return jsonify({'error': str(e)}), 400
101
-
102
- # --- Run the Flask app ---
103
- if __name__ == '__main__':
104
- app.run(host='0.0.0.0', port=5000, debug=True)
 
 
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
+