forked from meshde/EruditeX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_server.py
More file actions
161 lines (119 loc) · 4.24 KB
/
test_server.py
File metadata and controls
161 lines (119 loc) · 4.24 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
import subprocess
import os
# import nltk.data
import threading
from flask import Flask
from flask import Response
from flask import request
from flask import jsonify
from werkzeug import secure_filename
# from Helpers import file_extraction as filer
# from Helpers import deployment_utils as deploy
# from IR import infoRX
# from Models import abcnn_model
# from Models import AnsSelect
# from Models import DT_RNN
class EdXServer():
status = {}
def __init__(self):
self.file = ''
self.context = []
self.query = ''
@classmethod
def update(cls, value):
cls.status = value
def get_file(self, filename):
# print(filename)
self.file = os.path.join(app2.config['UPLOAD_FOLDER'] + filename)
# self.context = filer.extract_file_contents(self.file)
# print(self.context)
if len(self.context) > 0:
return True
return True #TODO: remove before deploy
def get_query(self, query):
self.query = query
# print(self.query)
try:
import time
answers = [('ground', 0.872), ('bat', 0.655), ('catch', 0.317), ('bowler', 0.012), ('catch', 0.317), ('bowler', 0.012)]
ans_list = []
for x in answers:
ans_list.append({'word':x[0], 'score': x[1]})
# Filter top 5 paras using Info Retrieval
# self.update('Ranking Paragraphs using Information Retrieval..')
self.update({'val': 'Ranking Paragraphs using Information Retrieval.'})
# para_select = infoRX.retrieve_info(self.context, self.query)
# para_sents = []
# tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
t_end = time.time() + 3
while time.time() < t_end:
status = 'Selecting Answer Sentences..'
self.update({'val': 'Sentences selected by IR Module', 'answers': ans_list})
# for para in para_select:
# para_sents.extend(tokenizer.tokenize(para))
# print('Sentences selected by IR Module:')
# print(para_sents)
t_end = time.time() + 3
while time.time() < t_end:
status = 'Selecting Answer Sentences..'
self.update({'val': 'Ranking Candidate Answer Sentences.'})
self.update('Selecting Answer Sentences..')
print(status)
# Select Ans Sents - ABCNN
# ans_sents = abcnn.ans_select(query, para_sents)
print('Sentences scored by Sentence Selection Module:')
# print(ans_sents)
t_end = time.time() + 3
while time.time() < t_end:
status = 'Generating VDT and extracting Answer..'
self.update('Generating VDT and extracting Answer..')
# best_ans, score, answers = deploy.extract_answer_from_sentences(ans_sents, query)
except:
return {'answers': [{'word': 'ERROR', 'score': 'QA Subsystem failure.'}]}
self.update('false')
answers = [('ground', 0.872), ('bat', 0.655), ('catch', 0.317), ('bowler', 0.012)]
ans_list = []
for x in answers:
ans_list.append({'word':x[0], 'score': x[1]})
ans_dict = {'answers': ans_list}
return ans_dict
app = Flask(__name__)
app2 = Flask(__name__)
server = EdXServer()
# abcnn = abcnn_model()
# ans_select = AnsSelect()
# dt_rnn = DT_RNN()
app2.config['UPLOAD_FOLDER'] = os.path.join('./data/uploads/')
@app2.route('/filed',methods=['POST'])
def filer():
# data = request.get_json(force=True)
# filename = data['filename']
# file = data['file']
f = request.files['file']
f.save(os.path.join(app2.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
if server.get_file(f.filename):
resp = Response('File uploaded. Context Ready.')
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app2.route('/status', methods=['POST'])
def status():
print(EdXServer.status)
resp = jsonify({'status': EdXServer.status})
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route('/query',methods=['POST'])
def queried():
query = request.get_json(force=True)['query']
# resp = Response(server.get_query(query))
resp = jsonify(server.get_query(query))
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
def start1(port):
app.run(port=port)
def start2(port):
app2.run(port=port)
if __name__ == '__main__':
t1 = threading.Thread(target=start1, args=(5001,))
t2 = threading.Thread(target=start2, args=(5000,))
t1.start()
t2.start()