This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
/
webserver.py
294 lines (249 loc) · 12.7 KB
/
webserver.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
# coding=utf-8
################################################################################
#
# Copyright (c) 2016 eBay Software Foundation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#################################################################################
#
# @Author: Mingkuan Liu
# @Email: [email protected]
# @Date: 2016-07-24
#
##################################################################################
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
# from builtins import str
from builtins import str
from builtins import range
import os
import codecs
import numpy as np
import tensorflow as tf
import data_utils
import sse_model
import text_encoder
import logging
from logging.handlers import RotatingFileHandler
from logging import handlers
import sys
from flask import Flask, request, jsonify
class FlaskApp(Flask):
def __init__(self, *args, **kwargs):
super(FlaskApp, self).__init__(*args, **kwargs)
self.model = 'Do my initialization work here, loading model and index ....'
self.model_type = os.environ.get("MODEL_TYPE", "classification")
self.model_dir = "models-" + self.model_type
self.indexFile = os.environ.get("INDEX_FILE", "targetEncodingIndex.tsv")
if not os.path.exists("./logs"):
os.makedirs("./logs", exist_ok=True)
log = logging.getLogger('')
log.setLevel(logging.DEBUG)
format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt='%m/%d/%Y %I:%M:%S %p')
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(format)
log.addHandler(ch)
fh = handlers.RotatingFileHandler('./logs/WebServerLog.txt', maxBytes=(1048576 * 20), backupCount=7)
fh.setFormatter(format)
log.addHandler(fh)
logging.info("In app class: Received flask appconfig is: " + os.environ.get('MODEL_TYPE', 'Default_classification') )
if not os.path.exists(self.model_dir):
logging.error('Model folder %s does not exist!!' % self.model_dir )
exit(-1)
if not os.path.exists(os.path.join(self.model_dir, self.indexFile)):
logging.error('Index File does not exist!!')
exit(-1)
# load full set targetSeqID data
if not os.path.exists(os.path.join(self.model_dir, 'vocabulary.txt')):
logging.error('Error!! Could not find vocabulary file for encoder in model folder.')
exit(-1)
self.encoder = text_encoder.SubwordTextEncoder(filename=os.path.join(self.model_dir, 'vocabulary.txt'))
# load full set target Index data
self.targetEncodings = []
self.targetIDs = []
self.targetIDNameMap = {}
idx = 0
for line in codecs.open(os.path.join(self.model_dir, self.indexFile), 'r', 'utf-8').readlines():
info = line.strip().split('\t')
if len(info) != 3:
logging.info('Error in targetIndexFile! %s' % line)
continue
tgtid, tgtseq, tgtEncoding = info[0], info[1], info[2]
self.targetIDs.append(tgtid)
self.targetEncodings.append([float(f) for f in tgtEncoding.strip().split(',')])
self.targetIDNameMap[tgtid] = tgtseq
idx += 1
self.targetEncodings = np.array(self.targetEncodings)
cfg = tf.ConfigProto(log_device_placement=False, allow_soft_placement=True)
self.sess = tf.Session(config=cfg)
#load model
self.modelConfigs = data_utils.load_model_configs(self.model_dir)
self.model = sse_model.SSEModel( self.modelConfigs )
ckpt = tf.train.get_checkpoint_state(self.model_dir)
if ckpt:
logging.info("loading model from %s" % ckpt.model_checkpoint_path)
self.model.saver.restore(self.sess, ckpt.model_checkpoint_path)
else:
logging.error('Error!!!Could not load any model from specified folder: %s' % self.model_dir)
exit(-1)
app = FlaskApp(__name__)
@app.route('/api/classify', methods=['GET'])
def classification():
#parse out classification task GET request parameters: e.g.: /api/classify?keywords=hello kitty sunglasses&?nbest=8
keywords = request.args.get('keywords')
if 'nbest' in request.args:
nbest = int(request.args.get('nbest'))
else:
nbest = 8
# inference tensorflow model
# Get token-ids for the input sentence.
source_tokens = app.encoder.encode(tf.compat.as_str(keywords).lower())
srclen = len(source_tokens)
max_seq_length = int(app.modelConfigs['max_seq_length'])
if srclen > max_seq_length - 2:
logging.info('Input sentence too long, max allowed is %d. Try to increase limit!!!!' % (max_seq_length))
source_tokens = [text_encoder.PAD_ID] + source_tokens[:max_seq_length - 2] + [text_encoder.EOS_ID]
else:
source_tokens = [text_encoder.PAD_ID] * (max_seq_length - srclen - 1) + source_tokens + [text_encoder.EOS_ID]
dict = app.model.get_source_encoding_feed_dict(np.array([source_tokens]))
#sourceEncodings = app.sess.run([app.model.src_seq_embedding], feed_dict=dict)
sourceEncodings = app.sess.run([app.model.norm_src_seq_embedding], feed_dict=dict)
sourceEncodings = np.vstack(sourceEncodings)
distances = np.dot(sourceEncodings, app.targetEncodings.T)
rankedScore, rankedIdx = data_utils.getSortedResults(distances)
top_confs = rankedScore[0][:nbest]
top_tgtIDs = [app.targetIDs[lbl] for lbl in rankedIdx[0][:nbest]]
top_tgtNames = [app.targetIDNameMap[id] for id in top_tgtIDs]
topResults = []
for idx in range(nbest):
logging.info('top%d: %s , %f , %s ' % (idx + 1, top_tgtIDs[idx], top_confs[idx], top_tgtNames[idx]))
entry={}
entry['targetCategoryId'] = top_tgtIDs[idx]
entry['targetCategoryName'] = top_tgtNames[idx]
entry['confidenceScore'] = float(top_confs[idx])
topResults.append(entry)
return jsonify( { 'ReqeustKeywords':keywords, 'ClassificationResults':topResults} )
@app.route('/api/search', methods=['GET'])
def relevanceRanking():
#parse out search ranking task GET request parameters: e.g.: /api/search?query=red nike shoes&?nbest=10
keywords = request.args.get('query')
if 'nbest' in request.args:
nbest = int(request.args.get('nbest'))
else:
nbest = 10
# inference tensorflow model
# Get token-ids for the input sentence.
source_tokens = app.encoder.encode(tf.compat.as_str(keywords).lower())
srclen = len(source_tokens)
max_seq_length = int(app.modelConfigs['max_seq_length'])
if srclen > max_seq_length - 2:
logging.info('Input sentence too long, max allowed is %d. Try to increase limit!!!!' % (max_seq_length))
source_tokens = [text_encoder.PAD_ID] + source_tokens[:max_seq_length - 2] + [text_encoder.EOS_ID]
else:
source_tokens = [text_encoder.PAD_ID] * (max_seq_length - srclen - 1) + source_tokens + [text_encoder.EOS_ID]
dict = app.model.get_source_encoding_feed_dict(np.array([source_tokens]))
sourceEncodings = app.sess.run([app.model.src_seq_embedding], feed_dict=dict)
#sourceEncodings = app.sess.run([app.model.norm_src_seq_embedding], feed_dict=dict)
sourceEncodings = np.vstack(sourceEncodings)
distances = np.dot(sourceEncodings, app.targetEncodings.T)
rankedScore, rankedIdx = data_utils.getSortedResults(distances)
top_confs = rankedScore[0][:nbest]
top_tgtIDs = [app.targetIDs[lbl] for lbl in rankedIdx[0][:nbest]]
top_tgtNames = [app.targetIDNameMap[id] for id in top_tgtIDs]
topResults = []
for idx in range(nbest):
logging.info('top%d: %s , %f , %s ' % (idx + 1, top_tgtIDs[idx], top_confs[idx], top_tgtNames[idx]))
entry={}
entry['ListingId'] = top_tgtIDs[idx]
entry['ListingTitle'] = top_tgtNames[idx]
entry['rankingScore'] = float(top_confs[idx])
topResults.append(entry)
return jsonify( { 'SearchQuery':keywords, 'SearchRankingResults':topResults} )
@app.route('/api/qna', methods=['GET'])
def questionAnswering():
#parse QnA task's GET request parameters: e.g.: /api/qna?question=how does secure pay work&?nbest=5
keywords = request.args.get('question')
if 'nbest' in request.args:
nbest = int(request.args.get('nbest'))
else:
nbest = 5
# inference tensorflow model
# Get token-ids for the input sentence.
source_tokens = app.encoder.encode(tf.compat.as_str(keywords).lower())
srclen = len(source_tokens)
max_seq_length = int(app.modelConfigs['max_seq_length'])
if srclen > max_seq_length - 2:
logging.info('Input sentence too long, max allowed is %d. Try to increase limit!!!!' % (max_seq_length))
source_tokens = [text_encoder.PAD_ID] + source_tokens[:max_seq_length - 2] + [text_encoder.EOS_ID]
else:
source_tokens = [text_encoder.PAD_ID] * (max_seq_length - srclen - 1) + source_tokens + [text_encoder.EOS_ID]
dict = app.model.get_source_encoding_feed_dict(np.array([source_tokens]))
sourceEncodings = app.sess.run([app.model.src_seq_embedding], feed_dict=dict)
#sourceEncodings = app.sess.run([app.model.norm_src_seq_embedding], feed_dict=dict)
sourceEncodings = np.vstack(sourceEncodings)
distances = np.dot(sourceEncodings, app.targetEncodings.T)
rankedScore, rankedIdx = data_utils.getSortedResults(distances)
top_confs = rankedScore[0][:nbest]
top_tgtIDs = [app.targetIDs[lbl] for lbl in rankedIdx[0][:nbest]]
top_tgtNames = [app.targetIDNameMap[id] for id in top_tgtIDs]
topResults = []
for idx in range(nbest):
logging.info('top%d: %s , %f , %s ' % (idx + 1, top_tgtIDs[idx], top_confs[idx], top_tgtNames[idx]))
entry={}
entry['answerDocId'] = top_tgtIDs[idx]
entry['answerContent'] = top_tgtNames[idx]
entry['confidenceScore'] = float(top_confs[idx])
topResults.append(entry)
return jsonify( { 'Question':keywords, 'Answers':topResults} )
@app.route('/api/crosslingual', methods=['GET'])
def crosslingualSearch():
#parse out cross-lingual search task GET request parameters: e.g.: /api/crosslingual?query=nike运动鞋&?nbest=10
keywords = request.args.get('query')
if 'nbest' in request.args:
nbest = int(request.args.get('nbest'))
else:
nbest = 10
# inference tensorflow model
# Get token-ids for the input sentence.
source_tokens = app.encoder.encode(tf.compat.as_str(keywords).lower())
srclen = len(source_tokens)
max_seq_length = int(app.modelConfigs['max_seq_length'])
if srclen > max_seq_length - 2:
logging.info('Input sentence too long, max allowed is %d. Try to increase limit!!!!' % (max_seq_length))
source_tokens = [text_encoder.PAD_ID] + source_tokens[:max_seq_length - 2] + [text_encoder.EOS_ID]
else:
source_tokens = [text_encoder.PAD_ID] * (max_seq_length - srclen - 1) + source_tokens + [text_encoder.EOS_ID]
dict = app.model.get_source_encoding_feed_dict(np.array([source_tokens]))
sourceEncodings = app.sess.run([app.model.src_seq_embedding], feed_dict=dict)
#sourceEncodings = app.sess.run([app.model.norm_src_seq_embedding], feed_dict=dict)
sourceEncodings = np.vstack(sourceEncodings)
distances = np.dot(sourceEncodings, app.targetEncodings.T)
rankedScore, rankedIdx = data_utils.getSortedResults(distances)
top_confs = rankedScore[0][:nbest]
top_tgtIDs = [app.targetIDs[lbl] for lbl in rankedIdx[0][:nbest]]
top_tgtNames = [app.targetIDNameMap[id] for id in top_tgtIDs]
topResults = []
for idx in range(nbest):
logging.info('top%d: %s , %f , %s ' % (idx + 1, top_tgtIDs[idx], top_confs[idx], top_tgtNames[idx]))
entry={}
entry['documentId'] = top_tgtIDs[idx]
entry['documentTitle'] = top_tgtNames[idx]
entry['confScore'] = float(top_confs[idx])
topResults.append(entry)
return jsonify( { 'CrossLingualQuery':keywords, 'SearchResults':topResults} )
@app.route('/', methods=['GET'])
def default():
return 'Sequence Semantic Embedding NLP toolkit demo webserver. \n For classification task, send GET request with URL of /api/classify?keywords=hello kitty sunglasses \n For search relevance ranking task, send GET request with /api/search?query=red nike shoes&?nbest=10 \n For question answering task, send GET request with /api/qna?question=how does secure pay work&?nbest=5 \n For cross-lingual search task, send GET request with /api/crosslingual?query=nike运动鞋&?nbest=10 \n'