-
Notifications
You must be signed in to change notification settings - Fork 30
/
predictor.py
39 lines (30 loc) · 920 Bytes
/
predictor.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
from flask import Flask
import flask
import spacy
import os
import json
import logging
#Load in model
nlp = spacy.load('en_core_web_sm')
# The flask app for serving predictions
app = Flask(__name__)
@app.route('/ping', methods=['GET'])
def ping():
# Check if the classifier was loaded correctly
health = nlp is not None
status = 200 if health else 404
return flask.Response(response= '\n', status=status, mimetype='application/json')
@app.route('/invocations', methods=['POST'])
def transformation():
#Process input
input_json = flask.request.get_json()
resp = input_json['input']
#NER
doc = nlp(resp)
entities = [(X.text, X.label_) for X in doc.ents]
# Transform predictions to JSON
result = {
'output': entities
}
resultjson = json.dumps(result)
return flask.Response(response=resultjson, status=200, mimetype='application/json')