forked from sd2001/-le-medicin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
373 lines (297 loc) · 11.2 KB
/
app.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
from flask import Flask,render_template,request,flash,redirect,url_for,g,session,Response
import random
import torch
import json
from model import NeuralNet
from basics import bag_of_words, tokenize, stem
from nearby import hospital_search,pharmacy_search
import pymongo
from pymongo import MongoClient
from flask_login import login_user,current_user
import time
from datetime import datetime
from datetime import date
from camera import VideoCamera
import numpy as np
import face_recognition as fr
import cv2
app=Flask(__name__)
app.secret_key='hello_mic'
client=MongoClient("mongodb+srv://swarnabha:<pass>@cluster0.yxfcm.mongodb.net/MIC-Silicon?retryWrites=true&w=majority")
db=client['Patient']
data=db.Prescriptions
@app.route("/")
def login():
if g.user:
return redirect(url_for('test'))
return render_template('login.html')
@app.route("/register")
def reg():
return render_template('register.html')
ds_factor=0.6
@app.route('/face_login')
def index():
# rendering webpage
return render_template('face.html')
def gen(camera):
while True:
#get camera frame
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/video_feed')
def video_feed():
return Response(gen(VideoCamera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
def capture():
video = cv2.VideoCapture(0)
face_cascade=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
ret, frame = video.read()
face_rects=face_cascade.detectMultiScale(frame,1.3,5)
my_image = fr.load_image_file("2.jpg")
my_face_encoding = fr.face_encodings(my_image)[0]
known_face_encondings = [my_face_encoding]
known_face_names = ["SD"]
rgb_frame = frame[:, :, ::-1]
face_locations = fr.face_locations(rgb_frame)
face_encodings = fr.face_encodings(rgb_frame, face_locations)
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
matches = fr.compare_faces(known_face_encondings, face_encoding)
name = "Unknown"
face_distances = fr.face_distance(known_face_encondings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
return True
return False
@app.route('/get_p')
def click():
res=capture()
if res==True:
session['user']="s@s.com"
return redirect(url_for('test'))
else:
flash("Face doesn't match/Not detected. Try Again")
return render_template("face.html")
@app.route("/d",methods=['POST'])
def doctor():
email_d=None
pass_d=None
email_d=request.form.get('doctor_email')
pass_d=request.form.get('doctor_pass')
if email_d=='d@d.com' and pass_d=='12345':
return redirect(url_for('doctor_home'))
elif email_d is None or pass_d is None:
flash("Please fill in both the fields")
return render_template('login.html',user="d")
else:
flash("Invalid Credentials for Doctor Login")
return render_template('login.html',user="d")
return render_template('login.html')
@app.route("/p",methods=['POST'])
def patient():
session.pop('user',None)
email_p=None
pass_p=None
email_p=request.form.get('patient_email')
pass_p=request.form.get('patient_pass')
if email_p=='s@s.com' and pass_p=='12345':
session['user']=email_p
return redirect(url_for('test'))
elif email_p is None or pass_p is None:
flash("Please fill in both the fields")
return render_template('login.html',user="p")
elif email_p!='s@s.com' or pass_p!='12345':
flash("Invalid credentials for Patient Login")
return render_template('login.html',user="p")
return render_template('login.html')
@app.route("/doctor_home")
def doctor_home():
return render_template('doctor_home.html')
@app.route("/doctor_vc",methods=['GET','POST'])
def doc_vc():
if request.method=='POST':
illness=request.form.get('details')
med=request.form.get('Med')
qnty=request.form.get('qnty')
name="Dr P.Gandhi"
today = date.today()
date1=today.strftime("%d/%m/%Y")
doc={'Doctor':name,
'Illness':illness,
'Medicine':med,
'Quantity':qnty,
'date':date1}
data.insert_one(doc)
return redirect(url_for('doctor_home'))
return render_template('doctor_vc.html')
@app.route("/tests")
def test():
if g.user:
return render_template('patient_home.html')
flash("Please login before continuing")
return render_template('login.html',user='g')
@app.route("/tests/chatbot")
def chat():
if g.user:
return render_template("Chatbot_temp.html")
flash("Please login before continuing")
return render_template('login.html',user='g')
@app.route("/tests/chatbot_get")
def chatbot():
device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
with open('intents.json','r') as f:
intents=json.load(f)
FILE="data.pth"
data=torch.load(FILE)
input_size = data["input_size"]
hidden_size = data["hidden_size"]
output_size = data["output_size"]
all_words = data['all_words']
tags = data['tags']
model_state = data["model_state"]
model = NeuralNet(input_size, hidden_size, output_size).to(device)
model.load_state_dict(model_state)
model.eval()
sentence = request.args.get("msg") #get data from input,we write js to index.html
sentence=tokenize(sentence)
X=bag_of_words(sentence,all_words)
X=X.reshape(1,X.shape[0])
X=torch.from_numpy(X)
output=model(X)
_,predicted=torch.max(output,dim=1)
tag=tags[predicted.item()]
bot="I do not know...try something different😊"
probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()]
print(prob.item())
if prob.item() < 0.75:
bot="I do not know...try something different😊"
elif prob.item() > 0.75:
for intent in intents['intents']:
if tag == intent["tag"]:
bot=random.choice(intent['responses'])
return bot
@app.route("/tests/self_assess",methods=['GET','POST'])
def self_assess():
if g.user:
if request.method=='POST':
score=50
fear=request.form.get('fear')
eat=request.form.get('eat')
confidence=request.form.get('confidence')
future=request.form.get('future')
relation=request.form.get('relation')
activity=request.form.get('activity')
score=score+int(fear)+int(eat)+int(confidence)+int(future)+int(relation)+int(activity)
mssg=""
if score>50 and score<100:
mssg="Great, make your days even better!"
elif score>100:
mssg="Awesome, thats the motivation we need!"
elif score<50 and score>0:
mssg="Try to stay motiavted always. We would recommend you to consult Dr A."
elif score<0:
mssg="Its highly important for you to consult Dr A, before something big happens."
flash(mssg)
return render_template('self-assessment2.html')
return render_template('self-assessment2.html')
flash("Please login before continuing")
return render_template('login.html',user='g')
@app.route("/tests/inkblot",methods=['GET','POST'])
def inkblot():
if g.user:
if request.method=='POST':
score=50
fear=request.form.get('1')
eat=request.form.get('2')
confidence=request.form.get('3')
future=request.form.get('4')
relation=request.form.get('5')
score=score+int(fear)+int(eat)+int(confidence)+int(future)+int(relation)
mssg=""
if score>=50 and score<100:
mssg="Great, make your days even better!"
elif score>=100:
mssg="Awesome, thats the motivation we need!"
elif score<50 and score>0:
mssg="Try to stay motivated always. We would recommend you to consult a doctor."
elif score<=0:
mssg="Its highly important for you to consult a doctor, before something big happens."
flash(mssg)
return render_template('inkblot.html')
return render_template('inkblot.html')
flash("Please login before continuing")
return render_template('login.html',user='g')
@app.route("/doctor_list")
def doctor_list():
return render_template('doctor_info.html')
@app.route("/video_patient/<string:name>")
def v_p(name):
if g.user:
return render_template('video_chat.html',name=name)
flash("Please login before continuing")
return render_template('login.html',user='g')
@app.route("/shop",methods=['GET','POST'])
def shop_search():
if g.user:
items=["A Tabs","B Tabs","C Tabs"]
qnty=[10,18,5]
price=[10,15,18]
res=False
if request.method=='POST':
search=request.form.get('search')
for i in range(0,len(items)):
if search.lower()==items[i].lower():
res=True
q=qnty[i]
p=price[i]
return render_template('shop2.html',res=res,q=q,p=p,item=search)
return render_template('shop2.html',res=res)
return render_template('shop.html')
flash("Please login before continuing")
return render_template('login.html',user='g')
@app.route("/shop/confirmation")
def confirm():
if g.user:
return render_template('payment_ack.html')
flash("Please login before continuing")
return render_template('login.html',user='g')
@app.route("/prescription")
def pres():
if g.user:
return render_template('prescription.html',p=data)
flash("Please login before continuing")
return render_template('login.html',user='g')
@app.route("/logout")
def logoute():
if g.user:
session.pop('user', None)
return render_template('login.html')
flash("Please login before continuing")
return render_template('login.html')
@app.route("/nearby",methods=['GET','POST'])
def neerby():
if g.user:
res=False
if request.method=='POST':
choice=request.form.get('choice')
locality=request.form.get('locality')
if choice=='Hospital':
names=hospital_search(locality)
res=True
else:
names=pharmacy_search(locality)
res=True
return render_template('hospital.html',res=res,names=names)
return render_template('hospital.html',res=res)
flash("Please login before continuing")
return render_template('login.html',user='g')
@app.before_request
def before_request():
g.user=None
det=[]
if 'user' in session:
g.user=session['user']
if __name__=="__main__":
app.run(debug=True)