-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
343 lines (198 loc) · 6.81 KB
/
main.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# Imports
from flask import render_template, request, Flask
from joblib import load
from pandas import read_csv
# Initializing App
app = Flask(__name__)
app.config["SECRET_KEY"] = "secret"
# Global Variables
model_health_insurance = load("Models/health_insurance_model.joblib")
scaler_health_insurance = load("Models/health_insurance_scaler.joblib")
pipeline_diabetes = load("Models/diabetes_pipeline.joblib")
symptom_desc = read_csv("Datasets/symptom_desc.csv").drop_duplicates()
symptom_desc['Disease'] = symptom_desc['Disease'].apply(lambda x: x.lower())
symptom_desc = list(symptom_desc.values)
symptom_precaution = read_csv("Datasets/symptom_precaution.csv").drop_duplicates()
symptom_precaution['Disease'] = symptom_precaution['Disease'].apply(lambda x: x.lower())
symptom_precaution = list(symptom_precaution.values)
foodsncals = list(read_csv("Datasets/foodsncals.csv").drop_duplicates().values)
foods = []
# Initilazing Foods
for i in foodsncals:
foods.append({
"name":i[0].lower(),
"serving":i[1],
"cals":i[2],
"cals_raw":int(i[2].split()[0])
})
# Helper functions
def get_symptom_description(symptom):
"""
Gets symptom's description given the symptom
"""
symptom = symptom.lower()
results = []
for i in symptom_desc:
for j in i:
if symptom in str(j):
results.append(list(i))
return results
def get_symptom_precaution(symptom):
"""
Gets symptom's cure(s) given the symptom
"""
symptom = symptom.lower()
results = []
for i in symptom_precaution:
for j in i:
if symptom in str(j):
results.append(list(i))
return results
def get_food_cals(food):
"""
Gets the calories of food given the food
"""
results = []
if type(food) == str:
food = food.lower()
for i in foods:
if food == i['name']:
results.append(i)
else:
for j in food:
j = j.lower()
for i in foods:
if j == i['name']:
results.append(i)
return results
# Home page
@app.route("/")
def home():
"""
Home Page
"""
return render_template("index.html")
# Credits Page
@app.route("/credits")
def credits():
"""
Credits Page
"""
return render_template("credits.html")
# Symptom Describer
@app.route("/symptom_describer", methods=["POST", "GET"])
def symptom_describer():
"""
Symptom Description Page
"""
if request.method == "POST":
symptom = str(request.form["disease"]).lower()
description = get_symptom_description(symptom)
return render_template("symptom_describer.html", description=description)
else:
return render_template("symptom_describer.html")
# Symptom Curer
@app.route("/symptom_curer", methods=["POST", "GET"])
def symptom_curer():
"""
Symptom Cure(s) Page
"""
if request.method == "POST":
symptom = str(request.form["disease"]).lower()
precuations = get_symptom_precaution(symptom)
return render_template("symptom_curer.html", cures=precuations)
else:
return render_template("symptom_curer.html")
# Diabetes predicter and logic
@app.route("/diabetes_predicter", methods=["POST", "GET"])
def diabetes_predicter():
"""
Predicts Diabetes
"""
if request.method == "POST":
pregnancies = int(request.form["pregnancies"])
glucose = float(request.form["glucose"])
diastolic_bp = float(request.form["diastolic_bp"])
skinthick = float(request.form["skinthick"])
insulin = int(request.form["insulin"])
bmi = float(request.form["bmi"])
diabetes_pedigree = float(request.form["diabetes_pedigree"])
age = int(request.form["age"])
data_to_predict = [[pregnancies, glucose, diastolic_bp, skinthick, insulin, bmi, diabetes_pedigree, age]]
prediction = int(pipeline_diabetes.predict(data_to_predict)[0])
print(prediction)
return render_template("diabetes_results.html", prediction=prediction)
else:
return render_template("diabetes_predicter.html")
# Health Insurance and logic
@app.route("/heath_insurance_predicter", methods=["GET", "POST"])
def health_insurance_predicter():
"""
Predicts calue of health insurance
"""
if request.method == "POST":
age = int(request.form['age'])
bmi = float(request.form['bmi'])
children = int(request.form['children'])
region = request.form['region'].lower()
gender = request.form['gender'].lower()
smoker = request.form['smoker'].lower()
region_se = 0
region_sw = 0
region_ne = 0
region_nw = 0
if region == "se":
region_se = 1
elif region == "sw":
region_sw = 1
elif region == "ne":
region_ne = 1
elif region == "nw":
region_nw = 1
if gender == "female":
gender = 1
else:
gender = 0
if smoker == "yes":
smoker = 1
else:
smoker = 0
data_to_predict = [[age, gender, bmi,
children, smoker, region_ne, region_nw, region_se, region_sw]]
data_to_predict = scaler_health_insurance.transform(data_to_predict)
prediction = model_health_insurance.predict(data_to_predict)
money = int(prediction)
print(money)
return render_template("health_insurance_results.html", prediction=money)
else:
return render_template("health_insurance_predicter.html")
# BMI Calculator
@app.route("/bmicalculator", methods=["POST", "GET"])
def bmi_calculator():
bmi = None
if request.method == "POST":
age = float(request.form["age"])
height = float(request.form["height"])
weight = float(request.form["weight"])
bmi = round(weight/(height*height)*10000, 2)
return render_template("bmi_calculator.html", bmi=bmi)
# Calorie Calculator
@app.route("/calcalc", methods=["POST", "GET"])
def calorie_calc():
results = None
summed_cals = []
expected_cals = 0
if request.method == "POST":
edible = request.form["edible"]
expected_cals = int(request.form["calories"])
edible = [x.strip() for x in edible.split(',')]
if len(edible) == 1:
edible = edible[0]
results = get_food_cals(edible)
if len(results) > 1:
for i in results:
summed_cals.append(i['cals_raw'])
return render_template("cal_calc.html", results=results, summed_cals=sum(summed_cals), cals_left=expected_cals-sum(summed_cals), expected_cals=expected_cals)
# Running App
if __name__ == "__main__":
app.run(debug=True)