This repository has been archived by the owner on Nov 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackend.py
73 lines (57 loc) · 2.85 KB
/
backend.py
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
# -*- coding: utf-8 -*-
'''
CheeseHacks Diet Reccomender
Created by: Team Cheesy-Weazies
Members: Mikhail Ortiz-Lunyov, Yixuan Zheng, Mia Chen, Sojung Lee
This program takes in the user's eating habits,
and then returns suggestions for healthier meals
as nessesary.
This program assumes that age is 19 or above.
External Sources:
Data Set:
https://github.com/soumillll/Diet-Recommendation-System/blob/main/input.csv
Calories Needed:
https://www.healthline.com/nutrition/how-many-calories-per-day
https://www.omnicalculator.com/health/bmr-harris-benedict-equation
'''
import pandas as pd
# Step 1: Take user's input is handled by Flask in views.py
# Step 2: Calculate calorie needs based on the user's parameters
def calculate_calorie_needs(gender, weight, height, age, activity_level):
# BMR calculation using Harris-Benedict equation
if gender.lower() == 'male':
bmr = 88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age)
else:
bmr = 447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age)
# Activity level multiplier
activity_multiplier = {
'sedentary': 1.2,
'lightly active': 1.375,
'moderately active': 1.55,
'very active': 1.725,
'extra active': 1.9
}
return bmr * activity_multiplier[activity_level.lower()]
# Step 3: Set up a diet plan based on the calorie need
def get_diet_plan(calorie_needs):
# Read the dataset
food_data = pd.read_csv(r'/Users/shuyizhang/Downloads/input.csv')
# Assuming there's an additional column 'Unit' in the dataset
food_data['Unit'] = food_data['Food_items'].apply(lambda x: 'ml' if 'juice' in x.lower() or 'milk' in x.lower() or 'tea' in x.lower() or 'coffee' in x.lower() else 'g')
# Define high and low quantity caps
high_cap = 700
low_cap = 300
# List of items to apply the low cap
low_cap_items = ['egg', 'butter', 'cheese']
# Calculate the quantity for each food item and update the 'Unit' column
food_data['Quantity'] = food_data.apply(lambda row: min(40 * (calorie_needs / row['Calories']), high_cap if row['Food_items'].lower() not in low_cap_items else low_cap), axis=1)
food_data['Unit'] = food_data.apply(lambda row: f"{row['Quantity']:.2f} {row['Unit']}", axis=1)
# Filter the food items that fit within the calorie need
suitable_foods = food_data[food_data['Calories'] <= calorie_needs]
# Constructing a diet plan
breakfast = suitable_foods[suitable_foods['Breakfast'] == 1].sample(2) # 2 items for breakfast
lunch = suitable_foods[suitable_foods['Lunch'] == 1].sample(2) # 2 items for lunch
dinner = suitable_foods[suitable_foods['Dinner'] == 1].sample(1) # 1 item for dinner
diet_plan = pd.concat([breakfast, lunch, dinner])
return diet_plan.to_dict('records')
# Step 4: Get feedback from the user is handled by Flask in views.py