This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
155 lines (108 loc) · 4.53 KB
/
app.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
import base64
from collections import namedtuple
import sys
from typing import Any, Dict
from flask import Flask, request, jsonify
from cassis import *
import spacy
from spacy.tokens import Doc
# Types
JsonDict = Dict[str, Any]
PredictionRequest = namedtuple("PredictionRequest", ["layer", "feature", "projectId", "document", "typeSystem"])
PredictionResponse = namedtuple("PredictionResponse", ["document"])
Document = namedtuple("Document", ["xmi", "documentId", "userId"])
# Constants
SENTENCE_TYPE = "de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence"
TOKEN_TYPE = "de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token"
IS_PREDICTION = "inception_internal_predicted"
# Models
model_name = sys.argv[1] if len(sys.argv) >= 2 else 'en'
nlp = spacy.load(model_name, disable=['parser'])
# Routes
app = Flask(__name__)
@app.route("/ner/predict", methods=["POST"])
def route_predict_ner():
json_data = request.get_json()
prediction_request = parse_prediction_request(json_data)
prediction_response = predict_ner(prediction_request)
result = jsonify(document=prediction_response.document)
return result
@app.route("/ner/train", methods=["POST"])
def route_train_ner():
# Return empty response
return ('', 204)
@app.route("/pos/predict", methods=["POST"])
def route_predict_pos():
json_data = request.get_json()
prediction_request = parse_prediction_request(json_data)
prediction_response = predict_pos(prediction_request)
result = jsonify(document=prediction_response.document)
return result
@app.route("/pos/train", methods=["POST"])
def route_train_pos():
# Return empty response
return ('', 204)
def parse_prediction_request(json_object: JsonDict) -> PredictionRequest:
metadata = json_object["metadata"]
document = json_object["document"]
layer = metadata["layer"]
feature = metadata["feature"]
projectId = metadata["projectId"]
xmi = document["xmi"]
documentId = document["documentId"]
userId = document["userId"]
typesystem = json_object["typeSystem"]
return PredictionRequest(layer, feature, projectId, Document(xmi, documentId, userId), typesystem)
# NLP
def predict_ner(prediction_request: PredictionRequest) -> PredictionResponse:
# Load the CAS and type system from the request
typesystem = load_typesystem(prediction_request.typeSystem)
cas = load_cas_from_xmi(prediction_request.document.xmi, typesystem=typesystem)
AnnotationType = typesystem.get_type(prediction_request.layer)
# Extract the tokens from the CAS and create a spacy doc from it
tokens = list(cas.select(TOKEN_TYPE))
words = [cas.get_covered_text(token) for token in tokens]
doc = Doc(nlp.vocab, words=words)
# Find the named entities
nlp.entity(doc)
# For every entity returned by spacy, create an annotation in the CAS
for ent in doc.ents:
fields = {'begin': tokens[ent.start].begin,
'end': tokens[ent.end - 1].end,
IS_PREDICTION: True,
prediction_request.feature: ent.label_}
annotation = AnnotationType(**fields)
cas.add_annotation(annotation)
xmi = cas.to_xmi()
return PredictionResponse(xmi)
def predict_pos(prediction_request: PredictionRequest) -> PredictionResponse:
# Load the CAS and type system from the request
typesystem = load_typesystem(prediction_request.typeSystem)
cas = load_cas_from_xmi(prediction_request.document.xmi, typesystem=typesystem)
AnnotationType = typesystem.get_type(prediction_request.layer)
# Extract the tokens from the CAS and create a spacy doc from it
tokens = list(cas.select(TOKEN_TYPE))
words = [cas.get_covered_text(token) for token in tokens]
doc = Doc(nlp.vocab, words=words)
# Do the tagging
nlp.tagger(doc)
# For every token, extract the POS tag and create an annotation in the CAS
for token in doc:
fields = {'begin': tokens[token.i].begin,
'end': tokens[token.i].end,
IS_PREDICTION: True,
prediction_request.feature: token.pos_}
annotation = AnnotationType(**fields)
cas.add_annotation(annotation)
xmi = cas.to_xmi()
return PredictionResponse(xmi)
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')
"""
# For debugging purposes, load a json file containing the request and process it.
import json
with open("predict.json", "rb") as f:
predict_json = json.load(f)
request = parse_prediction_request(predict_json)
predict_pos(request)
"""