-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.py
62 lines (46 loc) · 1.42 KB
/
api.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
#!/usr/bin/env python3
from flask import Flask, request, jsonify
from flask_cors import CORS
from bert import Ner
app = Flask(__name__)
CORS(app)
models = {"nl1": Ner("models/nl1"),
"nl2": Ner("models/nl2"),
"indo1": Ner("models/indo1"),
"indo2": Ner("models/indo2"),
"spa2": Ner("models/spa2"),
"en2": Ner("models/en2")}
@app.route("/predict/", methods=['GET'])
def predict():
text = request.args.get('text')
model = request.args.get('model')
if len(text) >= 512:
return jsonify({"result": 'too large!'})
if model in models:
model = models.get(model)
else:
model = models.get('nl1')
# try:
out = model.predict(text)
text_parsed = ""
result = []
ne = ""
p_tag = ''
lookup_type = ''
for r in out:
text_parsed += r.get('word') + ' '
if r.get('tag') != 'O':
p_tag = r.get('tag')
ne += r.get('word') + ' '
lookup_type = p_tag
else:
if p_tag != 'O':
if not ne.strip() in [ne.get('ne') for ne in result]:
pos = text_parsed.find(ne.strip())
result.append({'ne': ne.strip(), 'type': lookup_type, 'pos' : pos})
p_tag = 'O'
#from pprint import pprint
#pprint(result)
return jsonify({"result": out})
if __name__ == "__main__":
app.run('0.0.0.0', port=8000)