-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
287 lines (238 loc) · 8.94 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
# %%
# Retrieval
from subprocess import Popen, PIPE, STDOUT
import time
from elasticsearch import Elasticsearch
es_server = Popen(['/home/dr_lunars/elasticsearch-7.0.0/bin/elasticsearch'],stdout=PIPE, stderr=STDOUT)
time.sleep(30)
es = Elasticsearch("http://localhost:9200", timeout=300, max_retries=10, retry_on_timeout=True)
daily_score = 0
# %%
# DPR
from haystack.document_store.faiss import FAISSDocumentStore
document_store = FAISSDocumentStore.load(faiss_file_path="my_faiss", sql_url="sqlite:///my_doc_store.db", index="document")
from dpr_inference import DPR
model_path = '/home/dr_lunars/models/question_encoder-optimized-quantized.onnx'
tokenizer_path = "kykim/bert-kor-base"
dpr = DPR(
model_path=model_path,
tokenizer_path=tokenizer_path,
document_store=document_store
)
# %%
# Reader
tag_model_path = '/home/dr_lunars/models/tag-optimized-quantized.onnx'
tag_tokenizer_path = '/home/dr_lunars/models/tokenizers/tag_bert'
mrc_model_path = '/home/dr_lunars/models/electra_reader_small-optimized-quantized.onnx'
mrc_tokenizer_path = '/home/dr_lunars/models/tokenizers/koelectra_small'
from mrc_inference import MRC
from tag_inference import TagInference
tag_model = TagInference(
model_path=tag_model_path,
tokenizer_path=tag_tokenizer_path
)
mrc = MRC(
model_path=mrc_model_path,
tokenizer_path=mrc_tokenizer_path,
tag_predict_model=tag_model
)
# %%
# Rerank
def rerank(sparse_documents, dense_documents=None):
sparse_dict, dense_dict = {}, {}
for sparse_doc, dense_doc in zip(sparse_documents, dense_documents):
sparse_dict[sparse_doc['_source']['text']] = [sparse_doc['_score'], sparse_doc['_source']['title']]
dense_dict[dense_doc.text] = dense_doc.score*0.1
hybrid_docs = []
for sparse_text, sparse_score_title in sparse_dict.items():
hybrid_dict = {}
hybrid_dict['_source'] = {
'title': sparse_score_title[1],
'text': sparse_text
}
try:
hybrid_dict['_score'] = dense_dict[sparse_text] + sparse_score_title[0]
hybrid_docs.append(hybrid_dict)
except:
hybrid_dict['_score'] = sparse_score_title[0]
hybrid_docs.append(hybrid_dict)
hybrid_docs = sorted(hybrid_docs, key=lambda x: x['_score'], reverse=True)
return hybrid_docs
# %%
# Preprocess
from hanspell import spell_checker
from inko import Inko
def preprocess(question):
question = spell_checker.check(question).as_dict()['checked']
return question
myInko = Inko(allowDoubleConsonant=False)
# %%
# Postprocess
from konlpy.tag import Hannanum
from konlpy.tag import Kkma
from konlpy.tag import Komoran
from konlpy.tag import Okt
hannanum = Hannanum()
kkma = Kkma()
komoran = Komoran()
okt = Okt()
def postprocess(ans):
if hannanum.pos(ans)[-1][-1] in ['J']:
ans = ans[:-len(hannanum.pos(ans)[-1][0])]
elif kkma.pos(ans)[-1][-1] in ['JKS','JKC','JKG','JKO','JKM','JKI','JKQ','JC','JX']:
ans = ans[:-len(kkma.pos(ans)[-1][0])]
elif komoran.pos(ans)[-1][-1] in ['JKS','JKC','JKG','JKO','JKB','JKV','JKQ','JC','JX']:
ans = ans[:-len(komoran.pos(ans)[-1][0])]
elif okt.pos(ans)[-1][-1] in ['Josa']:
ans = ans[:-len(okt.pos(ans)[-1][0])]
return ans
postprocess('Loading...')
# %%
# Log
import os
if not os.path.isfile('Log.txt'):
f = open("Log.txt", 'a+')
f.write('question, answer\n')
f.close()
# %%
# Answer
import random
Answer = ['잘 모르겠어요...','정확한 답변을 찾지 못했어요...','조금 더 구체적으로 질문해주세요...']
# %%
# Flask
from flask import Flask, render_template, request
app = Flask(__name__,static_folder='/home/dr_lunars/ODQA-Demo-Site/static',template_folder='/home/dr_lunars/ODQA-Demo-Site/templates')
@app.route("/")
def home():
return render_template("index.html")
@app.route("/get")
def get_bot_response():
try:
question = preprocess(request.args.get('msg'))
questions = set([question,myInko.en2ko(question)])
global daily_score
ans_lst = []
for q in questions:
query = {
'query':{
'bool':{
'must':[
{'match':{'question':q}}
]
}
}
}
doc = es.search(index='chatter',body=query,size=1)['hits']['hits']
if doc != []:
doc = doc[0]
ans_lst.append((doc['_source']['answer'],doc['_score']))
if ans_lst != []:
ans_lst = sorted(ans_lst, key = lambda x : x[1], reverse=True)
if ans_lst[0][1] >= 4:
if daily_score == 3:
daily_score = 0
answer = '타조 챗봇은 간단한 일상 대화만 가능합니다. WIKI에서 찾을 수 있는 내용으로 질문해주세요.'
f = open("Log.txt", 'a+')
f.write(question+', '+answer+'\n')
f.close()
return answer
else:
daily_score += 1
answer = ans_lst[0][0]
f = open("Log.txt", 'a+')
f.write(question+', '+answer+'\n')
f.close()
return answer
daily_score = 0
ans_lst = []
for q in questions:
q = q.replace("?","")
if len(q.split()) == 1 or len(q.split()) == 2:
query = {
'query':{
'bool':{
'must':[
{'match':{'title': postprocess(q.split()[0])}}
]
}
}
}
doc = es.search(index='document',body=query,size=1)['hits']['hits']
if doc != []:
doc = doc[0]
ans_lst.append((doc['_source']['title'],doc['_score']))
if ans_lst != []:
ans_lst = sorted(ans_lst, key = lambda x : x[1], reverse=True)
answer = '질문이 너무 짧아 정확한 답변을 하기 어렵습니다. <a href="https://ko.wikipedia.org/wiki/' + ans_lst[0][0] + '" target="_blank">' + ans_lst[0][0] + '</a>을(를) 참고하세요.'
f = open("Log.txt", 'a+')
f.write(question+', '+answer+'\n')
f.close()
return answer
if len(q.split()) <= 2:
answer = '질문이 너무 짧아 정확한 답변을 하기 어렵습니다.'
f = open("Log.txt", 'a+')
f.write(question+', '+answer+'\n')
f.close()
return answer
ans_lst = []
for q in questions:
query = {
'query':{
'bool':{
'must':[
{'match':{'question':q}}
]
}
}
}
doc = es.search(index='qa',body=query,size=1)['hits']['hits']
if doc != []:
doc = doc[0]
ans_lst.append((doc['_source']['answer'],doc['_score']))
if ans_lst != []:
ans_lst = sorted(ans_lst, key = lambda x : x[1], reverse=True)
if ans_lst[0][1] >= 23:
answer = ans_lst[0][0] + ' 입니다.'
f = open("Log.txt", 'a+')
f.write(question+', '+answer+'\n')
f.close()
return answer
ans_lst = []
for q in questions:
query = {
'query':{
'bool':{
'must':[
{'match':{'text':q}}
]
}
}
}
sparse_docs = es.search(index='document',body=query,size=10)['hits']['hits']
dense_docs = dpr.get_documents(q, top_k=5)
doc = rerank(sparse_docs, dense_docs)
if doc != []:
max_scr = doc[0]['_score']
for i in range(len(doc)):
ans = mrc.get_answer(context=doc[i]['_source']['text'], question=q)
ans_lst.append((ans[0],ans[1]*doc[i]['_score']/max_scr))
if ans_lst != []:
ans_lst = sorted(ans_lst, key = lambda x : x[1], reverse=True)
if ans_lst[0][1] >= 0.5:
answer = postprocess(ans_lst[0][0]) + ' 입니다.'
else:
answer = Answer[random.randint(0,len(Answer)-1)]
else:
answer = '질문을 이해하지 못했어요...'
f = open("Log.txt", 'a+')
f.write(question+', '+answer+'\n')
f.close()
return answer
except:
answer = Answer[random.randint(0,len(Answer)-1)]
f = open("Log.txt", 'a+')
f.write(question+', '+answer+'\n')
f.close()
return answer
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)