-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
442 lines (336 loc) · 17.9 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import numpy as np
import pickle
from flask import Flask, url_for, render_template, request, redirect, session, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail, Message
import base64
import tensorflow as tf
from PIL import Image
import cv2
import io
from keras.models import load_model
import h5py
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://hackathon_1shg_user:xf83AoqlmOiElqW3rgzgG4PhRG7oH6kQ@dpg-cjp2hlm1208c73c714tg-a.singapore-postgres.render.com:5432/sih'
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = 'reiqgeaklkpiadxe'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
db = SQLAlchemy(app)
mail = Mail(app)
heart_model = pickle.load(open('model/heart_disease_model.pkl', 'rb'))
covid_model = load_model('model/covid.model')
liver_model = pickle.load(open('model/liver_model.pkl', 'rb'))
asd_model = pickle.load(open('model/asd_model.pkl', 'rb'))
diabetes_model = pickle.load(open('model/diabetes_model.pkl', 'rb'))
label_dict = {0: 'Covid19 Negative', 1: 'Covid19 Positive'}
img_size = 100
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
patients = db.relationship('Patient', backref='doctor_user')
def init(self, username, password):
self.username = username
self.password = password
# class Patient(db.model)
class Patient(db.Model):
id = db.Column(db.Integer, primary_key=True)
doctor = db.Column(db.Integer, db.ForeignKey('user.id'))
email = db.Column(db.String(100), unique=True)
name = db.Column(db.String(100))
age = db.Column(db.String(10))
phone = db.Column(db.String(15))
liver_disease = db.Column(db.Integer, default=0)
heart_disease = db.Column(db.Integer, default=0)
diabetes = db.Column(db.Integer, default=0)
def predict(model, values, dic):
values = np.asarray(values)
return model.predict(values.reshape(1, -1))[0]
def preprocess(img):
img = np.array(img)
if (img.ndim == 3):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else:
gray = img
gray = gray/255
resized = cv2.resize(gray, (img_size, img_size))
reshaped = resized.reshape(1, img_size, img_size)
return reshaped
# url
@app.route('/', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
u = request.form['username']
p = request.form['password']
data = User.query.filter_by(username=u, password=p).first()
if data is not None:
session['logged_in'] = True
session['email'] = data.username
return redirect(url_for('admin'))
return render_template('login.html', message="Incorrect Details! If not registered.. Please first do so!")
@app.route('/admin', methods=['GET', 'POST'])
def admin():
return render_template('admin.html')
# try:
# print(session.get("email"))
# except Exception as e:
# print(e)
# if request.method == 'GET':
# return render_template('patient.html')
# doctor_id = User.query.filter_by(username=session.get("email")).first().id
# print(doctor_id)
# email = request.form['email']
# name = request.form['name']
# age = request.form['age']
# phone = request.form['phone']
# db.session.add(Patient(doctor=doctor_id,email=email, name=name,age=age,phone=phone))
# db.session.commit()
# return {"message":"patient created"}
@app.route('/uploadpatient', methods=['GET', 'POST'])
def uploadpatient():
try:
print(session.get("email"))
except Exception as e:
print(e)
if request.method == 'GET':
return render_template('uploadpatient.html')
doctor_id = User.query.filter_by(username=session.get("email")).first().id
print(doctor_id)
email = request.form['email']
name = request.form['name']
age = request.form['age']
phone = request.form['phone']
db.session.add(Patient(doctor=doctor_id, email=email,
name=name, age=age, phone=phone))
db.session.commit()
return redirect(url_for('patients'))
@app.route('/patients', methods=['GET', 'POST'])
def patients():
doctor_id = User.query.filter_by(username=session.get("email")).first()
patients = doctor_id.patients
print(patients)
return render_template('patients.html', patients=patients)
@app.route('/register/', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
try:
password = request.form['password']
cpassword = request.form['cpassword']
if password == cpassword:
db.session.add(
User(username=request.form['username'], password=request.form['password']))
db.session.commit()
return redirect(url_for('login'))
else:
return redirect(url_for('register'))
except:
return render_template('register.html', message="User Already Exists")
else:
return render_template('register.html')
from flask import jsonify
@app.route('/main', methods=['GET', 'POST'])
def main():
# render_template('main.html')
if request.method == 'GET':
print('get')
return render_template("main.html")
else:
try:
print(request.get_json())
patient_id = request.json['patient']
global patient_email
patient_email = request.json['email']
#
# patient_email = request.get_json().email
print(patient_id)
print(patient_email)
session["patient_id"] = patient_id
#return render_template('main.html')
return {"h":"me"}
# result = wikipedia.summary(query,sentences = 10)
# return render_template("main.html",result=result,query = query, pred = 1)
except:
print('except')
return render_template("main.html")
@app.route('/heart_disease')
def heart():
return render_template("heart.html")
@app.route('/liver_disese')
def liver():
return render_template("liver.html")
@app.route("/covid")
def covid():
return (render_template("covid.html"))
@app.route("/diabetes")
def diabetes():
return (render_template("diabetes.html"))
@app.route("/ASD")
def ASD():
return (render_template("ASD.html"))
@app.route("/predict_heart", methods=['POST', 'GET'])
def predictPage_heart():
try:
if request.method == 'POST':
to_predict_dict = request.form.to_dict()
to_predict_list = list(map(float, list(to_predict_dict.values())))
pred = predict(heart_model, to_predict_list, to_predict_dict)
try:
print(session)
patient_id = session.get('patient_id')
patient_id = int(patient_id)
print(patient_id)
print(type(patient_id))
patient = Patient.query.filter_by(id=patient_id).first()
print(patient)
if patient:
patient.heart_disease = int(pred)
db.session.commit()
except Exception as e:
print(e)
# patient = Patient.query.filter_by(id=patient_id).first()
# print(patient)
# if patient:
# patient.heart_disease = int(pred)
# db.session.commit()
return render_template('predict_heart.html', pred=pred)
# if patient:
# patient.heart_disease = pred
# db.session.commit()
except:
message = "Please enter valid Data! You may have missed some data to be filled!"
return render_template("heart.html", message=message)
@app.route("/email_heart", methods=['POST', 'GET'])
def email_heart_pos():
if request.method == 'POST':
msg = Message("Heart_results", sender='[email protected]',
recipients=[patient_email])
msg.body = "Great! Your heart looks to be in great condition! Here is what you can do to continue having a healthy heart\n 1. Take a 10-minute walk. If you don't exercise at all, a brief walk is a great way to start.\n\n2. Give yourself a lift. Lifting a hardcover book or a two-pound weight a few times a day can help tone your arm muscles.\n\n3. Eat one extra fruit or vegetable a day.\n\n4. Make breakfast count. Start the day with some fruit and a serving of whole grains, like oatmeal, bran flakes, or whole-wheat toast.\n\n5. Have a handful of nuts. Walnuts, almonds, peanuts, and other nuts are good for your heart.\n\n6. Sample the fruits of the sea. Eat fish or other types of seafood instead of red meat once a week. It's good for the heart, the brain, and the waistline.\n\n7. Breathe deeply. Try breathing slowly and deeply for a few minutes a day. It can help you relax.\n\n8. Wash your hands often. Scrubbing up with soap and water often during the day is a great way to protect your heart and health. The flu, pneumonia, and other infections can be very hard on the heart.\n\n9. Count your blessings. Taking a moment each day to acknowledge the blessings in your life is one way to start tapping into other positive emotions. These have been linked with better health, longer life, and greater well-being, just as their opposites — chronic anger, worry, and hostility."
mail.send(msg)
return render_template('predict_heart.html', pred=0, message="Mail sent")
@app.route("/email_heart_neg", methods=['POST', 'GET'])
def email_heart_neg():
if request.method == 'POST':
msg = Message("Heart_results", sender='[email protected]',
recipients=[patient_email])
msg.body = "Oopss!! There are chances you might be afflicted with a heart disease . Go get your clinical tests done and consult with your doctor asap. There is no need to panic. It is just a prediction which may be even be incorrect."
mail.send(msg)
return render_template('predict_heart.html', pred=1, message="Mail sent")
@app.route("/predict_liver", methods=['POST', 'GET'])
def predictPage_liver():
try:
if request.method == 'POST':
to_predict_dict = request.form.to_dict()
to_predict_list = list(map(float, list(to_predict_dict.values())))
pred = predict(liver_model, to_predict_list, to_predict_dict)
except:
message = "Please enter valid Data! You may have missed some data to be filled!"
return render_template("liver.html", message=message)
return render_template('predict_liver.html', pred=pred)
@app.route("/email_liver", methods=['POST', 'GET'])
def email_liver_pos():
if request.method == 'POST':
msg = Message("Liver_results", sender='[email protected]',
recipients=[patient_email])
msg.body = "GREAT! you are in good Health. Kudos! Your Liver seems healthy. You could follow the below suggestions for further"
mail.send(msg)
return render_template('predict_liver.html', pred=0, message="Mail sent")
@app.route("/email_liver_neg", methods=['POST', 'GET'])
def email_liver_neg():
if request.method == 'POST':
msg = Message("Liver_results", sender='[email protected]',
recipients=[patient_email])
msg.body = "Oopss!! There are chances you might be afflicted with a liver disorder . Go get your clinical tests done and consult with your doctor asap. There is no need to panic. It is just a prediction which may be even be incorrect."
mail.send(msg)
return render_template('predict_liver.html', pred=1, message="Mail sent")
@app.route("/predict_ASD", methods=['POST', 'GET'])
def predictPage_ASD():
try:
if request.method == 'POST':
to_predict_dict = request.form.to_dict()
print(to_predict_dict)
to_predict_list = list(map(float, list(to_predict_dict.values())))
print(to_predict_list)
pred = predict(asd_model, to_predict_list, to_predict_dict)
except:
message = "Please enter valid Data! You may have missed some data to be filled!"
return render_template("ASD.html", message=message)
return render_template('predict_ASD.html', pred=pred)
@app.route("/email_asd", methods=['POST', 'GET'])
def email_asd_pos():
if request.method == 'POST':
msg = Message("Autisum Disorder results",
sender='[email protected]', recipients=[patient_email])
msg.body = "Great! Your look to be in great condition! Here is what you can do to avoid this disorder. \n\nLive healthy. Have regular check-ups, eat well-balanced meals, and exercise. \nMake sure you have good prenatal care, and take all recommended vitamins and supplements. \nDon’t take drugs during pregnancy. Ask your doctor before you take any medication. This is especially true for some anti-seizure drugs. \nAvoid alcohol. Say “no” to that glass of wine -- and any kind of alcoholic beverage -- while you’re pregnant. Seek treatment for existing health conditions. If you've been diagnosed with celiac disease or PKU, follow your doctor’s advice for keeping them under control. \nGet vaccinated. Make sure you get the German measles (rubella) vaccine before you get pregnant. It can prevent rubella-associated autism."
mail.send(msg)
return render_template('predict_ASD.html', pred=0, message="Mail sent")
@app.route("/email_asd_neg", methods=['POST', 'GET'])
def email_asd_neg():
if request.method == 'POST':
msg = Message("Autisum Disorder results",
sender='[email protected]', recipients=[patient_email])
msg.body = "Oopss!! There are chances you might be suffering from Autism sprectrum disorder. Go get your clinical tests done and consult with your doctor asap. There is no need to panic. It is just a prediction which may be even be incorrect."
mail.send(msg)
return render_template('predict_ASD.html', pred=1, message="Mail sent")
@app.route("/predict_diabetes", methods=['POST', 'GET'])
def predictPage_diabetes():
try:
if request.method == 'POST':
to_predict_dict = request.form.to_dict()
to_predict_list = list(map(float, list(to_predict_dict.values())))
print(to_predict_list)
pred = predict(diabetes_model, to_predict_list, to_predict_dict)
print(pred)
except:
message = "Please enter valid Data! You may have missed some data to be filled!"
return render_template("diabetes.html", message=message)
return render_template('predict_diabetes.html', pred=pred)
@app.route("/email_diabetes", methods=['POST', 'GET'])
def email_di_pos():
if request.method == 'POST':
msg = Message("Diabetes_results", sender='[email protected]',
recipients=[patient_email])
msg.body = "Great! Your looks to be in great condition! Here is what you can do to continue being healthy heart\n 1. Take a 10-minute walk. If you don't exercise at all, a brief walk is a great way to start.\n\n2. Give yourself a lift. Lifting a hardcover book or a two-pound weight a few times a day can help tone your arm muscles.\n\n3. Eat one extra fruit or vegetable a day.\n\n4. Make breakfast count. Start the day with some fruit and a serving of whole grains, like oatmeal, bran flakes, or whole-wheat toast.\n\n5. Have a handful of nuts. Walnuts, almonds, peanuts, and other nuts are good for your heart.\n\n6. Sample the fruits of the sea. Eat fish or other types of seafood instead of red meat once a week. It's good for the heart, the brain, and the waistline.\n\n7. Breathe deeply. Try breathing slowly and deeply for a few minutes a day. It can help you relax.\n\n8. Wash your hands often. Scrubbing up with soap and water often during the day is a great way to protect your heart and health. The flu, pneumonia, and other infections can be very hard on the heart.\n\n9. Count your blessings. Taking a moment each day to acknowledge the blessings in your life is one way to start tapping into other positive emotions. These have been linked with better health, longer life, and greater well-being, just as their opposites — chronic anger, worry, and hostility."
mail.send(msg)
return render_template('predict_diabetes.html', pred=0, message="Mail sent")
@app.route("/email_diabetes_neg", methods=['POST', 'GET'])
def email_di_neg():
if request.method == 'POST':
msg = Message("Heart_results", sender='[email protected]',
recipients=[patient_email])
msg.body = "Oopss!! There are chances you might be suffering from diabetes. Go get your clinical tests done and consult with your doctor asap. There is no need to panic. It is just a prediction which may be even be incorrect."
mail.send(msg)
return render_template('predict_diabetes.html', pred=1, message="Mail sent")
@app.route("/predict_covid", methods=["POST"])
def predict_covid():
print('HERE')
message = request.get_json(force=True)
encoded = message['image']
decoded = base64.b64decode(encoded)
dataBytesIO = io.BytesIO(decoded)
dataBytesIO.seek(0)
image = Image.open(dataBytesIO)
test_image = preprocess(image)
prediction = covid_model.predict(test_image)
result = np.argmax(prediction, axis=1)[0]
accuracy = float(np.max(prediction, axis=1)[0])
label = label_dict[result]
print(prediction, result, accuracy)
response = {'prediction': {'result': label, 'accuracy': accuracy}}
return jsonify(response)
@app.route('/logout', methods=['GET', 'POST'])
def logout():
session['logged_in'] = False
return redirect(url_for('login'))
@app.route('/aboutus')
def aboutus():
return render_template("aboutus.html")
if __name__ == '__main__':
app.secret_key = "ThisIsNotASecret:p"
with app.app_context():
db.create_all()
app.run(debug=True, port=5002)