-
Notifications
You must be signed in to change notification settings - Fork 8
/
nlu_predict.py
executable file
·61 lines (56 loc) · 2.5 KB
/
nlu_predict.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
import keras
import matchzoo as mz
import pandas as pd
import numpy as np
import sys
import json
from flask import request, Blueprint, jsonify
predict_main = Blueprint('predict', __name__)
ROOT_PATH = sys.path[0] + '/'
@predict_main.route('/<string:model>', methods=['POST'])
def DSSM(model):
request_data = json.loads(request.data.decode('utf-8'))
q = request_data['text1']
d = request_data['text2']
train_id = request_data['train_id']
df = pd.DataFrame(data={'text_left': [q],
'text_right': [d],
'label': [0]})
preprocessor_suffix = '.' + model + '_preprocessor'
preprocessor = mz.load_preprocessor(ROOT_PATH + 'matchzoo_temp_files/preprocessors/' + train_id + preprocessor_suffix)
predict_pack = mz.pack(df)
predict_pack_processed = preprocessor.transform(predict_pack)
keras.backend.clear_session()
model_suffix = '.' + model + '_model'
model = mz.load_model(ROOT_PATH + 'matchzoo_temp_files/models/' + train_id + model_suffix)
predict_score = float(model.predict(predict_pack_processed[:10].unpack()[0])[0][0])
ret_dict = {
'score': predict_score
}
'''
if model != 'drmm':
model_suffix = '.' + model + '_model'
model = mz.load_model(ROOT_PATH + 'matchzoo_temp_files/models/' + train_id + model_suffix)
predict_score = float(model.predict(predict_pack_processed[:10].unpack()[0])[0][0])
ret_dict = {
'score': predict_score
}
else:
glove_embedding = mz.datasets.embeddings.load_glove_embedding(dimension=300)
embedding_matrix = glove_embedding.build_matrix(preprocessor.context['vocab_unit'].state['term_index'])
l2_norm = np.sqrt((embedding_matrix * embedding_matrix).sum(axis=1))
embedding_matrix = embedding_matrix / l2_norm[:, np.newaxis]
pred_generator = mz.HistogramDataGenerator(data_pack=predict_pack_processed,
embedding_matrix=embedding_matrix,
bin_size=30,
hist_mode='LCH')
test_x, test_y = pred_generator[:]
keras.backend.clear_session()
model_suffix = '.' + model + '_model'
model = mz.load_model(ROOT_PATH + 'matchzoo_temp_files/models/' + train_id + model_suffix)
predict_score = float(model.predict(test_x)[0][0])
ret_dict = {
'score': predict_score
}
'''
return jsonify(ret_dict)