-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·585 lines (444 loc) · 17.6 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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
from flask import (
Flask,
render_template,
request,
redirect,
url_for,
flash,
jsonify,
session,
)
import os
import pathlib
import textwrap
import google.generativeai as genai
from IPython.display import display
from IPython.display import Markdown
import numpy as np
import torch
import torch.nn as nn
from argparse import Namespace
import pickle
from flask_sqlalchemy import SQLAlchemy
import bcrypt
from dotenv import load_dotenv
app = Flask(__name__)
load_dotenv()
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"
db = SQLAlchemy(app)
app.secret_key = "secret_key"
class BertForSequenceRegression(nn.Module):
def __init__(self, path, num_marks=2):
super(BertForSequenceRegression, self).__init__()
self.num_marks = num_marks
with open(path, "rb") as f:
self.bert = pickle.load(f)
self.hidden_1 = nn.Linear(2 * config.hidden_size, 2 * config.hidden_size)
self.notline_1 = nn.ReLU()
self.dropout_1 = nn.Dropout(config.hidden_dropout_prob)
self.hidden_2 = nn.Linear(2 * config.hidden_size, config.hidden_size)
self.notline_2 = nn.ReLU()
self.dropout_2 = nn.Dropout(config.hidden_dropout_prob)
self.hidden = nn.Linear(config.hidden_size, 128)
self.regres = nn.Linear(128, num_marks)
def forward(
self,
input_ids,
content_vector,
token_type_ids=None,
attention_mask=None,
labels=None,
):
output_bert = self.bert(
input_ids, token_type_ids, attention_mask
).last_hidden_state.mean(1)
h_concat = torch.cat((content_vector, output_bert), dim=1)
hidden_vec_1 = self.hidden_1(h_concat)
hidden_drop_1 = self.dropout_1(hidden_vec_1)
hidden_vecn_1 = self.notline_1(hidden_drop_1)
hidden_vec_2 = self.hidden_2(hidden_vecn_1)
hidden_drop_2 = self.dropout_2(hidden_vec_2)
hidden_vecn_2 = self.notline_2(hidden_drop_2)
hidden = self.hidden(hidden_vecn_2)
marks = self.regres(hidden)
return marks
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
role = db.Column(db.String(100), default="student")
def __init__(self, email, password, name, role):
self.name = name
self.email = email
self.password = bcrypt.hashpw(
password.encode("utf-8"), bcrypt.gensalt()
).decode("utf-8")
self.role = role
def check_password(self, password):
return bcrypt.checkpw(password.encode("utf-8"), self.password.encode("utf-8"))
not_adhd_path = "Models/Not ADHD/Debarta/deberta_model.pkl"
adhd_path = "Models/ADHD/Debarta/deberta_model.pkl"
config = Namespace(hidden_dropout_prob=0.05, hidden_size=768)
model_notadhd_debarta = BertForSequenceRegression(not_adhd_path)
model_adhd_debarta = BertForSequenceRegression(adhd_path)
# Define the device to be CPU
device = torch.device("cpu")
# Load the tokenizer using pickle
with open("Models/Not ADHD/Debarta/bert_tokenizer.pkl", "rb") as f:
tokenizer = pickle.load(f)
# Load the trained model
model_notadhd_debarta = BertForSequenceRegression(
not_adhd_path
).cpu() # Ensure model is on CPU
model_notadhd_debarta.load_state_dict(
torch.load(
"Models/Not ADHD/Debarta/deb_model_3.pt",
map_location=device,
)
)
model_notadhd_debarta.eval()
model_adhd_debarta = BertForSequenceRegression(
adhd_path
).cpu() # Ensure model is on CPU
model_adhd_debarta.load_state_dict(
torch.load(
"Models/ADHD/Debarta/adhd_deb_model_3.pt",
map_location=device,
)
)
model_adhd_debarta.eval()
api_key = os.getenv("GOOGLE_API_KEY")
os.environ["GOOGLE_API_KEY"] = api_key
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
model = genai.GenerativeModel("gemini-pro")
def convert_to_positive(score):
return 1 / (1 + np.exp(-score))
def to_markdown(text):
text = text.replace("•", " *")
return textwrap.indent(text, "> ", predicate=lambda _: True)
with app.app_context():
db.create_all()
@app.route("/")
def index():
return render_template("home.html")
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "POST":
# handle request
name = request.form["name"]
email = request.form["email"]
password = request.form["password"]
role = request.form["role"]
new_user = User(name=name, email=email, password=password, role=role)
db.session.add(new_user)
db.session.commit()
if role == "teacher":
return redirect("/loginTeacher")
return redirect("/loginStudent")
return render_template("register.html")
@app.route("/loginTeacher", methods=["GET", "POST"])
def loginTeacher():
if request.method == "POST":
email = request.form["email"]
password = request.form["password"]
role = "teacher"
if role == "teacher":
user = User.query.filter_by(email=email).first()
if user and user.check_password(password):
session["email"] = user.email
return redirect("/teacher")
else:
return render_template("loginTeacher.html", error="Invalid user")
else:
return render_template("loginTeacher.html", error="Invalid user")
return render_template("loginTeacher.html")
@app.route("/loginStudent", methods=["GET", "POST"])
def loginStudent():
if request.method == "POST":
email = request.form["email"]
password = request.form["password"]
role = "student"
if role == "student":
user = User.query.filter_by(email=email).first()
if user and user.check_password(password):
session["email"] = user.email
return redirect("/student")
else:
return render_template("login.html", error="Invalid user")
else:
return render_template("login.html", error="Invalid user")
return render_template("loginStudent.html")
@app.route("/logout")
def logout():
session.pop("email", None)
return redirect("/login")
@app.route("/aboutus")
def about_us():
return render_template("aboutus.html")
@app.route("/teacher")
def teacher():
if session["email"]:
user = User.query.filter_by(email=session["email"]).first()
return render_template("teacher.html")
return redirect("/login")
@app.route("/student")
def student():
if session["email"]:
user = User.query.filter_by(email=session["email"]).first()
return render_template("student.html")
return redirect("/login")
@app.route("/adhd")
def adhd():
return render_template("adhd.html")
@app.route("/notadhd/submit_summary", methods=["POST"])
def submit_summary_notadhd():
summary = request.json.get("summary", "")
question = "What are the grammatical and sentence structure errors in the following Summary and how Can one imporve it? Explain for children in grades 2-12."
summary = '"' + summary + '"'
complete_response = question + "\n" + summary
# Tokenize the text
tokens = tokenizer.tokenize(summary)
token_index = (
[tokenizer.cls_token_id]
+ tokenizer.convert_tokens_to_ids(tokens)
+ [tokenizer.sep_token_id]
)
# Pad or truncate the token index
max_length = 260
if len(token_index) < max_length:
pad = [tokenizer.pad_token_id] * (max_length - len(token_index))
token_index = token_index + pad
else:
token_index = token_index[:max_length]
# Convert token index to tensor
data_vector = torch.LongTensor(token_index).unsqueeze(dim=0).to(device)
# Generate content vector (random for illustration)
content_vector = torch.randn(1, 768).to(device)
# Generate attention mask
attention_mask = (data_vector != 0).long()
# Perform prediction
with torch.no_grad():
scores = model_notadhd_debarta(
data_vector, content_vector, attention_mask=attention_mask
)
# Parse the output tensor to extract content and wording scores
content_score, wording_score = scores[:, 0], scores[:, 1]
# Convert content and wording scores to positive values
positive_content_score = convert_to_positive(content_score.item())
positive_wording_score = convert_to_positive(wording_score.item())
response = model.generate_content(complete_response)
markdown_content = to_markdown(response.text)
response_data = {
"markdown_content": markdown_content,
"content_score": positive_content_score,
"wording_score": positive_wording_score,
}
# Redirect to the results page with the markdown_content as a query parameter
return jsonify(response_data), 200, {"Content-Type": "application/json"}
@app.route("/adhd/submit_summary", methods=["POST"])
def submit_summary_adhd():
summary = request.json.get("summary", "")
question = "What are the grammatical and sentence structure errors in the following Summary and how Can one imporve it? Explain for children in grades 2-12."
summary = '"' + summary + '"'
complete_response = question + "\n" + summary
# Tokenize the text
tokens = tokenizer.tokenize(summary)
token_index = (
[tokenizer.cls_token_id]
+ tokenizer.convert_tokens_to_ids(tokens)
+ [tokenizer.sep_token_id]
)
# Pad or truncate the token index
max_length = 260
if len(token_index) < max_length:
pad = [tokenizer.pad_token_id] * (max_length - len(token_index))
token_index = token_index + pad
else:
token_index = token_index[:max_length]
# Convert token index to tensor
data_vector = torch.LongTensor(token_index).unsqueeze(dim=0).to(device)
# Generate content vector (random for illustration)
content_vector = torch.randn(1, 768).to(device)
# Generate attention mask
attention_mask = (data_vector != 0).long()
# Perform prediction
with torch.no_grad():
scores = model_adhd_debarta(
data_vector, content_vector, attention_mask=attention_mask
)
# Parse the output tensor to extract content and wording scores
content_score, wording_score = scores[:, 0], scores[:, 1]
# Convert content and wording scores to positive values
positive_content_score = convert_to_positive(content_score.item())
positive_wording_score = convert_to_positive(wording_score.item())
response = model.generate_content(complete_response)
markdown_content = to_markdown(response.text)
response_data = {
"markdown_content": markdown_content,
"content_score": positive_content_score,
"wording_score": positive_wording_score,
}
# Redirect to the results page with the markdown_content as a query parameter
return jsonify(response_data), 200, {"Content-Type": "application/json"}
@app.route("/teachernotadhd/submit_summary", methods=["POST"])
def submit_summary_teacher_notadhd():
summary = request.json.get("summary", "")
question = "What are the grammatical and sentence structure errors in the following Summary and how Can one imporve it? Explain for children in grades 2-12."
summary = '"' + summary + '"'
complete_response = question + "\n" + summary
# Tokenize the text
tokens = tokenizer.tokenize(summary)
token_index = (
[tokenizer.cls_token_id]
+ tokenizer.convert_tokens_to_ids(tokens)
+ [tokenizer.sep_token_id]
)
# Pad or truncate the token index
max_length = 260
if len(token_index) < max_length:
pad = [tokenizer.pad_token_id] * (max_length - len(token_index))
token_index = token_index + pad
else:
token_index = token_index[:max_length]
# Convert token index to tensor
data_vector = torch.LongTensor(token_index).unsqueeze(dim=0).to(device)
# Generate content vector (random for illustration)
content_vector = torch.randn(1, 768).to(device)
# Generate attention mask
attention_mask = (data_vector != 0).long()
# Perform prediction
with torch.no_grad():
scores = model_notadhd_debarta(
data_vector, content_vector, attention_mask=attention_mask
)
# Parse the output tensor to extract content and wording scores
content_score, wording_score = scores[:, 0], scores[:, 1]
# Convert content and wording scores to positive values
positive_content_score = convert_to_positive(content_score.item())
positive_wording_score = convert_to_positive(wording_score.item())
response = model.generate_content(complete_response)
markdown_content = to_markdown(response.text)
response_data = {
"markdown_content": markdown_content,
"content_score": positive_content_score,
"wording_score": positive_wording_score,
}
# Redirect to the results page with the markdown_content as a query parameter
return jsonify(response_data), 200, {"Content-Type": "application/json"}
@app.route("/teacheradhd/submit_summary", methods=["POST"])
def submit_summary_teacher_adhd():
summary = request.json.get("summary", "")
question = "What are the grammatical and sentence structure errors in the following Summary and how Can one imporve it? Explain for children in grades 2-12."
summary = '"' + summary + '"'
complete_response = question + "\n" + summary
# Tokenize the text
tokens = tokenizer.tokenize(summary)
token_index = (
[tokenizer.cls_token_id]
+ tokenizer.convert_tokens_to_ids(tokens)
+ [tokenizer.sep_token_id]
)
# Pad or truncate the token index
max_length = 260
if len(token_index) < max_length:
pad = [tokenizer.pad_token_id] * (max_length - len(token_index))
token_index = token_index + pad
else:
token_index = token_index[:max_length]
# Convert token index to tensor
data_vector = torch.LongTensor(token_index).unsqueeze(dim=0).to(device)
# Generate content vector (random for illustration)
content_vector = torch.randn(1, 768).to(device)
# Generate attention mask
attention_mask = (data_vector != 0).long()
# Perform prediction
with torch.no_grad():
scores = model_notadhd_debarta(
data_vector, content_vector, attention_mask=attention_mask
)
# Parse the output tensor to extract content and wording scores
content_score, wording_score = scores[:, 0], scores[:, 1]
# Convert content and wording scores to positive values
positive_content_score = convert_to_positive(content_score.item())
positive_wording_score = convert_to_positive(wording_score.item())
response = model.generate_content(complete_response)
markdown_content = to_markdown(response.text)
response_data = {
"markdown_content": markdown_content,
"content_score": positive_content_score,
"wording_score": positive_wording_score,
}
# Redirect to the results page with the markdown_content as a query parameter
return jsonify(response_data), 200, {"Content-Type": "application/json"}
@app.route("/resultsadhd")
def resultsadhd():
# Get the markdown_content from the query parameters
markdown_content = request.args.get("markdown_content", "")
content_score = request.args.get("content_score", "")
wording_score = request.args.get("wording_score", "")
# print(content_score, wording_score)
# print(markdown_content)
# Render the template with the values
return render_template(
"resultsadhd.html",
markdown_content=markdown_content,
content_score=content_score,
wording_score=wording_score,
)
@app.route("/resultsnotadhd")
def resultsnotadhd():
# Get the markdown_content from the query parameters
# Get the markdown_content from the query parameters
markdown_content = request.args.get("markdown_content", "")
content_score = request.args.get("content_score", "")
wording_score = request.args.get("wording_score", "")
# print(content_score, wording_score)
# print(markdown_content)
return render_template(
"resultsnotadhd.html",
markdown_content=markdown_content,
content_score=content_score,
wording_score=wording_score,
)
@app.route("/teacherresultadhd")
def teacherresultadhd():
markdown_content = request.args.get("markdown_content", "")
content_score = request.args.get("content_score", "")
wording_score = request.args.get("wording_score", "")
return render_template(
"teacherresultadhd.html",
markdown_content=markdown_content,
content_score=content_score,
wording_score=wording_score,
)
@app.route("/teacherresultnotadhd")
def teacherresultnotadhd():
markdown_content = request.args.get("markdown_content", "")
content_score = request.args.get("content_score", "")
wording_score = request.args.get("wording_score", "")
# print(content_score, wording_score)
# print(markdown_content)
return render_template(
"teacherresultnotadhd.html",
markdown_content=markdown_content,
content_score=content_score,
wording_score=wording_score,
)
@app.route("/notadhd")
def notadhd():
return render_template("notadhd.html")
@app.route("/quiz")
def quiz():
return render_template("quiz.html")
@app.route("/startquiz")
def startquiz():
return render_template("startquiz.html")
@app.route("/teacheradhd")
def teacheradhd():
return render_template("teacheradhd.html")
@app.route("/teachernotadhd")
def teachernotadhd():
return render_template("teachernotadhd.html")
if __name__ == "__main__":
app.run(debug=True)