-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
412 lines (336 loc) · 11.7 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
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
import os
from datetime import datetime
import boto3
from authlib.integrations.flask_client import OAuth
from bson import ObjectId
from flask import redirect, send_file, url_for
from flask import session
from pymongo import MongoClient
import numpy as np
from flask import Flask, request, render_template
import pickle
import math
from s3_demo import download_file, upload_file
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from config import GOOGLE_MAIL_PASSWORD as x
app = Flask(__name__)
app.secret_key = '!secret'
app.config.from_object('config')
title = "TODO"
heading = "TODO"
BUCKET = "youngly"
s3 = boto3.client('s3')
CONF_URL = 'https://accounts.google.com/.well-known/openid-configuration'
oauth = OAuth(app)
oauth.register(
name='google',
server_metadata_url=CONF_URL,
client_kwargs={
'scope': 'openid email profile'
}
)
client = MongoClient("mongodb://127.0.0.1:27017") # host uri
db = client.mymongodb # Select the database
todos = db.todo # Select the collection name
notes = db.note
voice = db.voice
image = db.image
def redirect_url():
return request.args.get('next') or \
request.referrer or \
url_for('index')
@app.route("/list")
def lists():
# Display the all Tasks
useri = session.get('user')
if useri:
todos_l = todos.find({"user.email": useri['email']})
notes_l = notes.find({"user.email": useri['email']})
else:
todos_l = todos.find()
notes_l = notes.find()
a1 = "active"
return render_template('index.html', a1=a1, todos=todos_l, t=title, h=heading, user=useri, notes=notes_l)
@app.route("/diabetes")
def diabetes():
useri = session.get('user')
if useri:
return render_template('diabetes.html')
else:
return render_template('need.html')
@app.route("/recipe")
def recipe():
useri = session.get('user')
if useri:
return render_template('recipe.html')
else:
return render_template('need.html')
@app.route("/bmi")
def bmi():
useri = session.get('user')
if useri:
return render_template('bmi.html')
else:
return render_template('need.html')
@app.route("/listn")
def listsn():
# Display the all notes
useri = session.get('user')
if useri:
todos_l = todos.find({"user.email": useri['email']})
notes_l = notes.find({"user.email": useri['email']})
else:
todos_l = todos.find()
notes_l = notes.find()
a1 = "active"
return render_template('index.html', a1=a1, t=title, todos=todos_l, h=heading, user=useri, notes=notes_l)
@app.route("/completed")
def completed():
# Display the Completed Tasks
useri = session.get('user')
todos_l = todos.find({"done": "yes"})
a3 = "active"
return render_template('index.html', a3=a3, todos=todos_l, t=title, h=heading, user=useri)
@app.route("/done")
def done():
# Done-or-not ICON
id = request.values.get("_id")
task = todos.find({"_id": ObjectId(id)})
if task[0]["done"] == "yes":
todos.update({"_id": ObjectId(id)}, {"$set": {"done": "no"}})
else:
todos.update({"_id": ObjectId(id)}, {"$set": {"done": "yes"}})
redir = redirect_url()
return redirect(redir)
@app.route("/action", methods=['POST'])
def action():
# Adding a Task
name = request.values.get("name")
desc = request.values.get("desc")
date = request.values.get("date")
pr = request.values.get("pr")
user = session.get('user')
todos.insert({"name": name, "desc": desc, "date": date, "pr": pr, "done": "no", "user": user})
return redirect("/list")
@app.route("/notestore", methods=['POST'])
def note():
# Adding a Notes
note = request.values.get("note")
user = session.get('user')
notes.insert({"note": note, "user": user})
return redirect("/listn")
@app.route("/remove")
def remove():
# Deleting a Task with various references
key = request.values.get("_id")
todos.remove({"_id": ObjectId(key)})
return redirect("/")
@app.route("/remind")
def remind():
# Deleting a Task with various references
key = request.values.get("_id")
x = todos.find({"_id": ObjectId(key)})
s = "Hi "
for doc in x:
s = s + doc['user']['given_name'] + " task " + doc['name'] + " is added on " + doc['date']
sender(s, doc['user']['email'])
return redirect("/")
@app.route("/remindnote")
def remindn():
# Deleting a Note with various references
key = request.values.get("_id")
x = notes.find({"_id": ObjectId(key)})
s = "Hi "
for doc in x:
s = s + doc['user']['given_name'] + " note " + doc['note'] + " is added"
sender(s, doc['user']['email'])
return redirect("/")
def sender(mail_content, email):
# The mail addresses and password
mail_content = mail_content
sender_address = '[email protected]' # your mail id like [email protected]
sender_pass = x # 16 digit 2 factor gmail password
receiver_address = email # mail id to send the mail
# Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'task added' # The subject line
# The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
# Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) # use gmail with port
session.starttls() # enable security
session.login(sender_address, sender_pass) # login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
@app.route("/removen")
def removen():
# Deleting a note with various references
key = request.values.get("_id")
notes.remove({"_id": ObjectId(key)})
return redirect("/")
@app.route("/update")
def update():
# update task
id = request.values.get("_id")
useri = session.get('user')
task = todos.find({"_id": ObjectId(id)})
return render_template('update.html', tasks=task, h=heading, t=title, user=useri)
@app.route("/action3", methods=['POST'])
def action3():
# Updating a Task with various references
name = request.values.get("name")
desc = request.values.get("desc")
date = request.values.get("date")
pr = request.values.get("pr")
id = request.values.get("_id")
todos.update({"_id": ObjectId(id)}, {'$set': {"name": name, "desc": desc, "date": date, "pr": pr}})
return redirect("/")
@app.route("/search", methods=['GET'])
def search():
# Searching a Task with various references
useri = session.get('user')
key = request.values.get("key")
refer = request.values.get("refer")
if key == "_id":
todos_l = todos.find({refer: ObjectId(key)})
else:
todos_l = todos.find({refer: key})
return render_template('searchlist.html', todos=todos_l, t=title, h=heading, user=useri)
@app.route('/', methods=['POST', 'GET'])
def homepage():
useri = session.get('user')
if request.method == "POST":
f = request.files['audio_data']
x = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
x = str(x)
with open(x + ".wav", 'wb') as audio:
f.save(audio)
upload_file(f"{x + '.wav'}", BUCKET)
voice.insert({"voicename": x + '.wav', "user": useri})
os.remove(x + '.wav')
print('file uploaded successfully')
return render_template('storage.html', request="POST")
else:
if useri:
todos_l = todos.find({"user.email": useri['email']})
notes_l = notes.find({"user.email": useri['email']})
else:
todos_l = todos.find()
notes_l = notes.find()
a2 = "active"
return render_template('index.html', a2=a2, todos=todos_l, t=title, h=heading, user=useri, notes=notes_l)
@app.route('/login')
def login():
redirect_uri = url_for('auth', _external=True)
return oauth.google.authorize_redirect(redirect_uri)
@app.route('/auth')
def auth():
token = oauth.google.authorize_access_token()
user = oauth.google.parse_id_token(token)
session['user'] = user
return redirect('/')
@app.route('/logout')
def logout():
session.pop('user', None)
return render_template('bye.html')
@app.route("/storage")
def storage():
contents = []
v = []
useri = session.get('user')
voice_l = voice.find({"user.email": useri['email']})
for i in voice_l:
v.append(i['voicename'])
for key in s3.list_objects(Bucket='youngly')['Contents']:
if key['Key'] in v:
contents.append(key['Key'])
return render_template('storage.html', user=useri, contents=contents)
@app.route("/upload", methods=['POST'])
def upload():
useri = session.get('user')
if request.method == "POST":
f = request.files['file']
f.save(f.filename)
upload_file(f"{f.filename}", BUCKET)
voice.insert({"voicename": f.filename, "user": useri})
return redirect("/storage")
@app.route("/download/<filename>", methods=['GET'])
def download(filename):
if request.method == 'GET':
output = download_file(filename, BUCKET)
return send_file(output, as_attachment=True)
@app.route("/storageimage")
def storagei():
contents = []
v = []
useri = session.get('user')
voice_l = voice.find({"user.email": useri['email']})
for i in voice_l:
v.append(i['imangeename'])
for key in s3.list_objects(Bucket='youngly')['Contents']:
if key['Key'] in v:
contents.append(key['Key'])
return render_template('storage.html', user=useri, contents=contents)
@app.route("/uploadi", methods=['POST'])
def uploadi():
relationname = request.values.get("relationname")
useri = session.get('user')
if request.method == "POST":
f = request.files['file']
f.save(f.filename)
upload_file(f"{f.filename}", BUCKET)
image.insert({"voicename": f.filename, "user": useri, "relationname": relationname})
return redirect("/storage")
@app.route('/p')
def home():
return render_template('prediction.html')
@app.route('/pi')
def homed():
return render_template('predictiond.html')
def sigmoid(x):
return 1 / (1 + math.exp(-x))
# Load the scaler
fp = open(r"static/models/scaler.bin", "rb")
scaler = pickle.load(fp)
fp.close()
# Load LinearSVM
fp = open(r"static/models/LinearSVM.bin", "rb")
model1 = pickle.load(fp)
fp.close()
# Load LogisticRegression
fp = open(r"static/models/LogisticRegression.bin", "rb")
model2 = pickle.load(fp)
fp.close()
model = pickle.load(open('static/models/model.pkl', 'rb'))
@app.route('/predict', methods=['POST'])
def predict():
"""
For rendering results on HTML GUI
"""
int_features = [[float(x) for x in request.form.values()]]
final_features = scaler.transform(int_features)
# output = model.predict(final_features)
# Now convert linear svm's prediction to probability
out1 = sigmoid(model1.decision_function(final_features))
# Get prediction probability from logistic regression
out2 = model2.predict_proba(final_features)[:, 1]
# Their average is the final output probability
final_output = np.mean((out1, out2), axis=0)
# output = prediction.reshape(-1, 1)
output = (str(round(final_output[0] * 100, 2)))
return render_template('prediction.html', prediction_text='Chance of alzheimer disease {}%'.format(output))
@app.route('/predictd', methods=['POST'])
def predictd():
int_features = [[float(x) for x in request.form.values()]]
out1 = model.predict(int_features)
if out1:
return render_template('predictiond.html', prediction_text='You might have Diabetes')
else:
return render_template('predictiond.html', prediction_text='You might not have Diabetes')
if __name__ == '__main__':
app.run(debug=True)