-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTesting_4.py
237 lines (185 loc) · 8.16 KB
/
Testing_4.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
import joblib
import pandas as pd
import streamlit as st
import PIL
from sklearn.preprocessing import StandardScaler
# Load the dataset
data = pd.read_csv("USDA_KM.csv")
# Load the model and scaler
kmeans = joblib.load('kmeans.joblib')
# scaler = joblib.load('C:/Users/User/PycharmProjects/Python_Tutorial/scaler.joblib')
# Create a feature matrix
scaled_data = data[['Calories', 'TotalFat', 'Carbohydrate', 'Sugar', 'Protein']]
# Scale the data to improve model performance
scaler = StandardScaler()
# Scale the data to improve model performance
scaler.fit_transform(scaled_data)
# Define the diet_recommendation function
def diet_recommendation(calories):
# Calculate the recommended intake of macronutrients
Protein = calories * 0.15 / 4
TotalFat = calories * 0.3 / 9
Carbohydrate = calories * 0.4 / 4
Sugar = calories * 0.1 / 4
# Select foods from the same cluster as the target calorie intake
filtered_data = data[
data['cluster'] == kmeans.predict(scaler.transform([[calories, Protein, TotalFat, Carbohydrate, Sugar]]))[0]]
# Sort the foods by energy density
sorted_data = filtered_data.sort_values('Calories')
# Display the top 10 recommendations
return sorted_data.head(10)
print(diet_recommendation(100))
print(diet_recommendation(1000))
print(diet_recommendation(10000))
tab1, tab2, tab3, tab4 = st.tabs(["Page 1", "Page 2", "BMI Calculator", "Diet Recommendation"])
with tab1:
# Page 1
image = PIL.Image.open('OIP.jpg')
st.image(image, width=640)
st.title("Diet Recommendation System")
st.subheader("Please enter your personal information below:")
# Create text input for name
name = st.text_input("Name")
# Create numeric input for age
# age = st.number_input("Age", min_value=0, max_value=120)
age = st.slider("Age", 0, 120, 10)
# Create a select widget for gender
gender = st.selectbox("Gender", options=["Male", "Female"])
# Create numeric input for height
height = st.number_input("Height (cm)", min_value=0.0, max_value=300.0, step=0.1)
# Create numeric input for weight
weight = st.number_input("Weight (kg)", min_value=0.0, max_value=500.0, step=0.1)
# Create a select widget for physical activity level
activity_level = st.selectbox("Physical activity level", options=["Sedentary (little or no exercise)",
"Lightly Active (light exercise/sports 1-3 "
"days/week)",
"Moderately Active (moderate exercise/sports "
" 3-5 days/week)",
"Very Active (hard exercise/sports 6-7 days a "
"week)",
"Extra Active (very hard exercise/sports & a "
"physical job)"])
# Create button to navigate to Page 2
if st.button("Submit"):
with tab2:
# Page 2
st.title("Results")
st.subheader("Based on the information you provided, your results are as follows:")
# Calculate BMI
if height != 0:
bmi = weight / ((height / 100) ** 2)
bmi_status = ""
if bmi < 18.5:
bmi_status = "Underweight"
elif 18.5 <= bmi < 25:
bmi_status = "Normal weight"
elif 25 <= bmi < 30:
bmi_status = "Overweight"
else:
bmi_status = "Obese"
else:
bmi = 0
bmi_status = "Invalid input: height cannot be zero"
age = float(age)
weight = float(weight)
height = float(height)
# Create a dictionary for each physical activity level
activity_level_dict = {
"Sedentary": 1.2,
"Lightly Active": 1.375,
"Moderately Active": 1.55,
"Very Active": 1.725,
"Extra Active": 1.9
}
# Calculate total daily energy expenditure based on Harris-Benedict Equation
activity_level_value = activity_level_dict.get(activity_level, 1.2)
if gender == "Male":
bmr = 66.5 + (13.75 * weight) + (5.003 * height) - (6.75 * age)
else:
bmr = 655.1 + (9.563 * weight) + (1.850 * height) - (4.676 * age)
# Total Daily Energy Expenditure (tdee) in calories multiply Basal Metabolic Rate (bmr) by the
# appropriate activity factor
tdee = bmr * activity_level_value
st.session_state.tdee = round(tdee, 2)
# Display results in table form
results = [
("Name", name),
("Age", int(age)),
("Gender", gender),
("BMI", round(bmi, 2)),
("BMI Status", bmi_status),
("Total Daily Energy Expenditure in calories", st.session_state.tdee)
]
col1, col2 = st.columns(2)
for i, r in enumerate(results):
with col1:
st.write(f"{r[0]}:")
with col2:
st.write(r[1])
# Create button to navigate back to Page 1
if st.button("Back"):
st.session_state = tab1
# with tab1:
# st.experimental_rerun()
with tab3:
# Page 3
# Create a title for the app
st.title('Welcome to BMI Calculator 🧮')
# TAKE WEIGHT INPUT in kg
weight = st.number_input("Enter your weight (kg)")
# radio button to choose height format
status = st.radio('Select your height format: ',
('cm', 'meter', 'feet'))
# compare status value
if status == 'cm':
# take height input in centimeters
height = st.number_input('Centimeters')
try:
bmi = weight / ((height / 100) ** 2)
except:
st.caption(":red[Please enter valid input values.]")
elif status == 'meter':
# take height input in meters
height = st.number_input('Meters')
try:
bmi = weight / (height ** 2)
except:
st.caption(":red[Please enter valid input values.]")
else:
# take height input in feet
height = st.number_input('Feet')
try:
bmi = weight / ((height / 3.28) ** 2)
except:
st.caption(":red[Please enter valid input values.]")
if not st.button('Calculate BMI'):
pass
# check if the button is pressed or not
else:
# print the BMI INDEX
st.text("Your BMI Index is {}.".format(bmi))
# give the interpretation of BMI index
if bmi < 18.5:
st.warning("Underweight")
elif 18.5 <= bmi < 25:
st.success("Healthy")
elif 25 <= bmi < 30:
st.warning("Overweight")
else:
st.error("Obese")
with tab4:
# Page 4
# Retrieve the value of tdee from st.session_state
tdee = st.session_state.tdee
# Create recommend diet button on Page 2
# Create a button that triggers the diet_recommendation function based on user input
if st.button("Recommend Diet"):
# Display the recommendations in a table
st.info("Top 10 recommended foods:")
# Call the diet_recommendation function
recommendations = diet_recommendation(tdee)
st.write(recommendations)
recommendations_text = ''
for i in range(len(recommendations)):
recommendations_text += str(i + 1) + '. ' + recommendations.iloc[i]['Description'] + '\n'
st.write(recommendations_text)