-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplication.py
More file actions
257 lines (217 loc) · 9.77 KB
/
application.py
File metadata and controls
257 lines (217 loc) · 9.77 KB
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
from flask import Flask, render_template, redirect, url_for, session, request
from warrant import Cognito
from config import cognito_userpool_id, cognito_app_client_id
from models.bert.inference_bert import Inference
import wikipedia
from rake_nltk import Rake
from database.Database import Database
from datetime import datetime
from wikipedia.exceptions import PageError
# from botocore.errorfactory
inference = Inference()
app = Flask(__name__)
database = Database()
KEYWORD_TOP_K = 5
MIN_ANSWER_SCORE = 0
keyword_topk = 5
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if valid_login(request.form['username'], request.form['password']):
session['username'] = request.form.get('username')
return redirect(url_for('welcome'))
else:
error = 'Incorrect username and password'
app.logger.warning('Incorrect Username and password for user (%s)', request.form.get('username'))
return render_template('login.html', error=error)
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('login'))
@app.route('/signup', methods=['GET', "POST"])
def signup():
error = None
if request.method == 'POST':
if len(request.form['password']) < 8:
error = 'Password too short!'
else:
cognito = Cognito(user_pool_id=cognito_userpool_id, client_id=cognito_app_client_id)
cognito.add_base_attributes(email=request.form['email'])
try:
cognito.register(username=request.form['username'], password=request.form['password'])
_ = database.add_user(request.form['username'],request.form['password'],request.form['email'])
except Exception as e:
if e.response['Error']['Code'] == 'UsernameExistsException':
error = 'User Already Exists!'
else:
error = 'Unexpected Error: {}'.format(e.response['Error']['Message'])
return render_template('signup.html', error=error)
session['username'] = request.form['username']
return redirect(url_for('verification'))
return render_template('signup.html', error=error)
@app.route('/verification', methods=['GET', 'POST'])
def verification():
if 'username' in session:
username = session['username']
if request.method == 'POST':
cognito = Cognito(user_pool_id=cognito_userpool_id, client_id=cognito_app_client_id, username=username)
try:
cognito.confirm_sign_up(confirmation_code=request.form['vcode'])
return redirect(url_for('welcome'))
except Exception as e:
print(e)
error = 'Unable to verify, please try again'
return render_template('signup.html', error=error)
return render_template("verification.html")
else:
return redirect(url_for('login'))
@app.route('/', methods=['GET', 'POST'])
def welcome():
if 'username' in session:
return render_template('welcome.html', username=session['username'])
else:
return redirect(url_for('login'))
@app.route('/with_context', methods=['GET', 'POST'])
def with_context():
if 'username' in session:
if request.method == 'POST':
question = request.form['question']
if not question.endswith('?'):
question += '?'
context = request.form['passage']
qas = {
'question': question,
'context_list': [context]
}
results = inference.response(qas=qas)
if not results:
return redirect(url_for('result_no_answer'))
answer, score = results[0]
if not answer or score < MIN_ANSWER_SCORE:
return redirect(url_for('result_no_answer'))
user_id = database.get_id_by_name(session['username'])
database.update(user_id, str(datetime.now()), request.form['passage'], request.form['question'],answer)
return redirect(url_for('result', question=question, answer=answer))
return render_template('with_context.html', username=session['username'])
else:
return redirect(url_for('login'))
@app.route('/without_context', methods=['GET', 'POST'])
def without_context():
if 'username' in session:
if request.method == 'POST':
question = request.form['question']
if not question.endswith('?'):
question += '?'
rake = Rake()
rake.extract_keywords_from_text(question)
keywords = rake.get_ranked_phrases()[:5]
context_list = []
for keyword in keywords:
try:
context = wikipedia.page(keyword).summary
except PageError as e:
print(e)
continue
context_list += get_context_list(context=context)
if not context_list:
return redirect(url_for('result_no_answer'))
qas = {
'question': question,
'context_list': context_list
}
results = inference.response(qas=qas)
final_answer = ""
max_score = float("-inf")
final_context = ""
for i, result in enumerate(results):
answer, score = result
if score > max_score and answer:
final_answer = answer
max_score = score
final_context = context_list[i // 3]
if not final_answer or max_score < MIN_ANSWER_SCORE:
return redirect(url_for('result_no_answer'))
user_id = database.get_id_by_name(session['username'])
database.update(user_id, str(datetime.now()), final_context, request.form['question'], final_answer)
return redirect(url_for('result', question=request.form['question'], answer=final_answer))
else:
return render_template('without_context.html', username=session['username'])
else:
return redirect(url_for('login'))
@app.route('/result/<question>/<answer>', methods=['GET', 'POST'])
def result(question="", answer=""):
if 'username' in session:
return render_template('result_with_context.html', username=session['username'], question=question, answer=answer)
else:
return redirect(url_for('login'))
@app.route('/result_no_answer/', methods=['GET', 'POST'])
def result_no_answer():
if 'username' in session:
return render_template('result_no_answer.html', username=session['username'])
else:
return redirect(url_for('login'))
@app.route('/history', methods=['GET'])
def history():
if 'username' in session:
requested_history = database.get_history_list(name=session['username'], limit=5)
if requested_history == -1:
requested_history = [("This will not be shown", "You do not have any question history now", "Go to ask Nucleus something!")]
return render_template('history.html', username=session['username'], requested_history=requested_history)
else:
return redirect(url_for('login'))
@app.route('/history/<cnt>', methods=['GET'])
def history_count(cnt=5):
if 'username' in session:
requested_history = database.get_history_list(name=session['username'], limit=cnt)
if requested_history == -1:
requested_history = [("This will not be shown", "You do not have any question history now", "Go to ask Nucleus something!")]
return render_template('history_count.html', username=session['username'], requested_history=requested_history)
else:
return redirect(url_for('login'))
@app.route('/history_redirect', methods=['GET', 'POST'])
def history_redirect():
if 'username' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
return redirect(url_for('history_count', cnt=request.form['cnt']))
return render_template('history_redirect.html', username=session['username'])
@app.route('/feedback/<question>/<answer>', methods=['GET', 'POST'])
def feedback(question=None, answer=None):
if 'username' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
database.user_feedback(session['username'], question, answer, int(request.form['score']),
request.form['expected_answer'])
return redirect(url_for("thankyou", username=session['username']))
else:
return render_template('feedback.html', username=session['username'], question=question, answer=answer)
@app.route('/thankyou', methods=['GET'])
def thankyou():
if 'username' not in session:
return redirect(url_for('login'))
return render_template('thankyou.html', username=session['username'])
def valid_login(username, password):
cognito = Cognito(cognito_userpool_id, cognito_app_client_id, username=username)
try:
cognito.authenticate(password)
except Exception as e:
print(e)
return False
return True
def get_context_list(context, min_len=700):
context_list = []
assert context
p1, p2 = 0, 0
while p2 < len(context):
p2 += min_len
while p2 < len(context) and context[p2] != '.':
p2 += 1
p2 += 1
context_list.append(context[p1:p2])
p1 = p2
return context_list
if __name__ == '__main__':
app.debug = True
app.secret_key = '\xe3-\xe1\xf7\xfb\x91\xb1\x8c\xae\xf2\xc1BH\xe0/K~~%>ac\t\x01'
app.run()