-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrun_http_api_serve.py
51 lines (34 loc) · 1.2 KB
/
run_http_api_serve.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
import sys
from flask import Flask, request, jsonify
from flask_cors import CORS
from tokenizer_tools.tagset.NER.BILUO import BILUOSequenceEncoderDecoder
decoder = BILUOSequenceEncoderDecoder()
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
app.config['DEBUG'] = True
CORS(app)
from tensorflow.contrib import predictor
export_dir = '/Users/howl/PyCharmProjects/seq2annotation_ner_on_people_daily/evaluate/results/saved_model/1543901916'
predict_fn = predictor.from_saved_model(export_dir)
@app.route("/parse", methods=['GET'])
def single_tokenizer():
text_msg = request.args.get('q')
print(text_msg)
predictions = predict_fn(
{
'words': [[i for i in text_msg]],
'words_len': [len(text_msg)]
}
)
print(predictions['tags'])
tags_seq = [i.decode() for i in predictions['tags'][0]]
offset_list = decoder.decode_to_offset(tags_seq)
print(offset_list)
response = {
'text': text_msg,
'spans': [{'start': i[0], 'end': i[1], 'type': i[2]} for i in offset_list],
'ents': list({i[2].lower() for i in offset_list})
}
return jsonify(response)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)