-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
372 lines (270 loc) · 13.3 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
# import dialogflow_v2 as df
from flask import Flask, request, jsonify, render_template, make_response
# import os
import requests
import json
import popo
from google.cloud.language import types
from google.cloud import language
from google.cloud.language import enums
import six
import stopwords
class Instance:
def __init__(self, app):
self.app = app
self.value_critical = 0
self.value_regular = 0
self.cache_questions = []
self.non_critical_only = []
self.req_result = None
self.stw_set = set(stopwords.stopword_list)
def increment_critical(self):
self.value_critical += 1
def get_current_count_critical(self):
return self.value_critical
def set_current_count_critical(self, val):
self.value_critical = val
def increment_regular(self):
self.value_regular += 1
def get_current_count_regular(self):
return self.value_regular
def set_current_count_regular(self, val):
self.value_regular = val
app = Flask(__name__)
sess_inst = Instance(app)
log = app.logger
@app.route("/")
def index():
return "cyka blyat"
cacheQuestions = []
@app.route("/core", methods=["POST"])
def webhook():
req = request.get_json()
try:
action = req.get("queryResult").get("action")
except AttributeError:
return "Something went wrong, Please try again. "
res = ""
if action == "learn":
res = learnSubjectActivity(req)
# This will be asked irrespective, so we will fetch all the questions irrespective
elif action == "request.recital.critical":
res = requestReciteActivity(req)
# This will be called when user says yes to the above
elif action == "confirm.start.recital":
res = startCriticalActivity(req)
# This is the feedback loop that is the result of the above two (for critical questions)
elif action == "answer.on.prompt":
res = processAnswerLoopForCritical(req)
# This will be called when user says no to request.recital.critical
elif action == "confirm.start.regular":
res = startRegularActivity(req)
# This is the feedback loop that is the result of the above two (for regular questions)
elif action == "answer.on.prompt.regular":
res = processAnswerLoopForRegular(req)
elif action == "stop.training":
res = sessionEnd(req)
print("Action:" + action)
print("Response:" + res)
return make_response(jsonify({"fulfillmentText": res}))
"""
This method is used to start the recital,
we send a request to the server, it fetches the questions
and we get a response saying the number of questions in this critical section
and a comfirmation prompting the user to start the recital
"""
def requestReciteActivity(req):
if not cacheQuestions:
req_url = "https://recall-bot.herokuapp.com/api/revise"
# call rastogi's endpoint
req_result = requests.get(req_url).content
print(json.loads(req_result))
result_mapped = popo.welcome_from_dict(json.loads(req_result))
for indiv_result in result_mapped:
qalist = indiv_result.qa
for qas in qalist:
print("Q:", qas.question)
print("A:", qas.answer)
sess_inst.cache_questions.append({"question": qas.question, "answer": qas.answer, "topic_id": indiv_result.id})
if indiv_result.priority == 0:
sess_inst.non_critical_only.append({"question": qas.question, "answer": qas.answer, "topic_id": indiv_result.id})
return " I have {0} questions in critical and {1} in regular, do you want to start critical?".format(len(sess_inst.cache_questions), len(sess_inst.non_critical_only))
# TODO:same session restart
return " I have {0} questions in critical and {1} in regular, do you want to start critical?".format(len(sess_inst.cache_questions), len(sess_inst.non_critical_only))
"""
This method inits the first question after the confirmation
Once we get the answer for the first we hook it to a loop
so we deal with that in processAnswerLoopForCritical
"""
def startCriticalActivity(req):
returnStr = "Ok, Here is Question {0}. ".format(sess_inst.get_current_count_critical() + 1)
if len(sess_inst.cache_questions)>0:
append = sess_inst.cache_questions[sess_inst.get_current_count_critical()]["question"]
returnStr += str(append)
sess_inst.increment_critical()
return returnStr
else:
return "No more questions available this session."
"""
Answer processor for ans 0--->n-1 and question hook for
questions 1--->n-1
Process answer here and modify appropriately
When we enter this method, get_current_count_critical already points towards the next question/answer pair
so to get the previous ones, we must do -1
"""
def processAnswerLoopForCritical(req):
parameters = req["queryResult"]["parameters"]["any"]
if not sess_inst.cache_questions or sess_inst.cache_questions and len(sess_inst.cache_questions) > 0 and sess_inst.get_current_count_critical() > len(sess_inst.cache_questions):
return "I am out of questions, your session has ended."
elif len(sess_inst.cache_questions)>=sess_inst.get_current_count_critical():
act_ans = sess_inst.cache_questions[sess_inst.get_current_count_critical() - 1]["answer"]
act_ans_entities_set = entities_text(act_ans)
act_ans_set = set([w.lower().replace(",", "").replace(".", "") for w in act_ans.split(" ")])
act_ans_set = act_ans_set.difference(sess_inst.stw_set)
user_ans = "" if not parameters else parameters
user_ans_entities_set = entities_text(user_ans)
user_ans_set = set([w.lower().replace(",", "").replace(".", "") for w in user_ans.split(" ")])
user_ans_set = user_ans_set.difference(sess_inst.stw_set)
print("act ans entity set", act_ans_entities_set)
print("user ans entity set", user_ans_entities_set)
print("act ans set", act_ans_set)
print("user ans set", user_ans_set)
print("entity intersection", act_ans_entities_set.intersection(user_ans_entities_set))
print("ans intersection", act_ans_set.intersection(user_ans_set))
drEntities = 1 if len(act_ans_entities_set) == 0 else len(act_ans_entities_set)
drAns = 1 if len(act_ans_set) == 0 else len(act_ans_set)
correct_entity_percent = float(len(act_ans_entities_set.intersection(user_ans_entities_set))) / drEntities
correct_answer_percent = float(len(act_ans_set.intersection(user_ans_set))) / drAns
if correct_entity_percent > 0.3 or correct_answer_percent > 0.2:
# TODO:replace this with answer accuracy from GCP NLP
returnStr = "Correct Answer"
normalized_percent = 1
else:
# Prints out the correct Answer
returnStr = "Your answer was {0}% accurate. {1}".format(correct_entity_percent, sess_inst.cache_questions[sess_inst.get_current_count_critical() - 1]["answer"])
normalized_percent = 0
# TODO:use the response.
topic_id = sess_inst.cache_questions[sess_inst.get_current_count_critical() - 1]["topic_id"]
dictToSend = {"score": normalized_percent}
req_url = "https://recall-bot.herokuapp.com/api/revise/" + topic_id
print(req_url)
res = requests.put(req_url, json=dictToSend)
if sess_inst.get_current_count_critical() < len(sess_inst.cache_questions):
# Appends the next question to be asked
next_ques = sess_inst.cache_questions[sess_inst.get_current_count_critical()]["question"]
append = ". Your next question, {0}".format(next_ques)
sess_inst.increment_critical()
else:
sess_inst.set_current_count_critical(0)
sess_inst.cache_questions.clear()
sess_inst.set_current_count_regular(0)
sess_inst.non_critical_only.clear()
sess_inst.req_result = None
append = ". I am out of questions, your session has ended."
print("Response ye hai bhau:->", res)
returnStr += append
return returnStr
return "No more questions available in this session."
def startRegularActivity(req):
returnStr = "Here are your questions for the regular section, "
returnStr += "Question {0}. ".format(sess_inst.get_current_count_regular() + 1)
if len(sess_inst.non_critical_only)>0:
append = sess_inst.non_critical_only[sess_inst.get_current_count_regular()]["question"]
returnStr += str(append)
sess_inst.increment_regular()
return returnStr
else:
return "No more questions available this session."
def processAnswerLoopForRegular(req):
parameters = req["queryResult"]["parameters"]["any"]
if not sess_inst.non_critical_only or sess_inst.non_critical_only and len(sess_inst.non_critical_only) > 0 and sess_inst.get_current_count_regular() > len(sess_inst.non_critical_only):
return "I am out of questions, your session has ended."
elif len(sess_inst.non_critical_only) >= sess_inst.get_current_count_regular():
act_ans = sess_inst.non_critical_only[sess_inst.get_current_count_regular() - 1]["answer"]
act_ans_entities_set = entities_text(act_ans)
act_ans_set = set([w.lower().replace(",", "").replace(".", "") for w in act_ans.split(" ")])
act_ans_set = act_ans_set.difference(sess_inst.stw_set)
user_ans = "" if not parameters else parameters
user_ans_entities_set = entities_text(user_ans)
user_ans_set = set([w.lower().replace(",", "").replace(".", "") for w in user_ans.split(" ")])
user_ans_set = user_ans_set.difference(sess_inst.stw_set)
print("act ans entity set", act_ans_entities_set)
print("user ans entity set", user_ans_entities_set)
print("act ans set", act_ans_set)
print("user ans set", user_ans_set)
print("entity intersection", act_ans_entities_set.intersection(user_ans_entities_set))
print("ans intersection", act_ans_set.intersection(user_ans_set))
drEntities = 1 if len(act_ans_entities_set) == 0 else len(act_ans_entities_set)
drAns = 1 if len(act_ans_set) == 0 else len(act_ans_set)
correct_entity_percent = float(len(act_ans_entities_set.intersection(user_ans_entities_set))) / drEntities
correct_answer_percent = float(len(act_ans_set.intersection(user_ans_set))) / drAns
if correct_entity_percent > 0.3 or correct_answer_percent > 0.2:
# TODO:replace this with answer accuracy from GCP NLP
returnStr = "Correct Answer"
normalized_percent = 1
else:
# Prints out the correct Answer
returnStr = "Your answer was {0}% accurate. {1}".format(correct_entity_percent, sess_inst.non_critical_only[sess_inst.get_current_count_regular() - 1]["answer"])
normalized_percent = 0
# TODO:use the response.
topic_id = sess_inst.non_critical_only[sess_inst.get_current_count_regular() - 1]["topic_id"]
dictToSend = {"score": normalized_percent}
req_url = "https://recall-bot.herokuapp.com/api/revise/" + topic_id
print(req_url)
res = requests.put(req_url, json=dictToSend)
if sess_inst.get_current_count_regular() < len(sess_inst.non_critical_only):
# Appends the next question to be asked
next_ques = sess_inst.non_critical_only[sess_inst.get_current_count_regular()]["question"]
append = ". Your next question, {0}".format(next_ques)
sess_inst.increment_regular()
else:
sess_inst.set_current_count_regular(0)
sess_inst.non_critical_only.clear()
sess_inst.set_current_count_critical(0)
sess_inst.cache_questions.clear()
sess_inst.req_result = None
append = ". I am out of questions, your session has ended."
print("Response ye hai bhau:->", res)
returnStr += append
return returnStr
return "No more questions available in this session."
"""
Discontinued/Half Implemented/Unused features
"""
def learnSubjectActivity(req):
parameters = req["queryResult"]["parameters"]["subject"]
print(json.dumps(req, indent=5))
print("Params are:")
print(json.dumps(parameters, indent=4))
# TODO:error handling for unknown or wrong
# TODO:add personalization
req_url = "https://recall-bot.herokuapp.com/api/"
# call rastogi's endpoint
subject_questions_json = requests.get(req_url).content
subject_questions_json = json.loads(subject_questions_json)
#
# debug
# print(subject_questions_json)
return " Your requested subject is " + parameters
def sessionEnd(req):
returnStr = "Buzzye"
return returnStr
"""
Helper methods for NLP
"""
def entities_text(text):
"""Detects entities in the text."""
client = language.LanguageServiceClient()
if isinstance(text, six.binary_type):
text = text.decode('utf-8')
# Instantiates a plain text document.
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
# Detects entities in the document. You can also analyze HTML with:
# document.type == enums.Document.Type.HTML
entities = client.analyze_entities(document).entities
identified_entities = [str(entity.name).lower() for entity in entities]
return set(identified_entities)
if __name__ == "__main__":
app.run(debug=True)