-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapplication.py
54 lines (39 loc) · 1.36 KB
/
application.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
from flask import Flask, request, jsonify
from ner import get_flair, evaluate
import json
from errors import *
application = Flask(__name__)
@application.errorhandler(HTTPException)
def handle_exception(e):
response = e.get_response()
response.data = json.dumps({
"code": e.code,
"name": e.name,
"description": e.description,
})
response.content_type = "application/json"
return response
@application.route("/api/v1/parse", methods=["POST"])
def parse():
if request.content_type == "application/json":
body = json.loads(request.data) if request.data else {}
language = body["language"] if "language" in body.keys() else None
if not language:
raise LanguageMandatory()
flair = get_flair(language)
if not flair:
raise LanguageInvalid()
content = body["text"] if "text" in body.keys() else None
if not content:
raise TextMandatory()
return evaluate(flair, content)
else:
raise InvalidUsage()
@application.route("/api/v1/entities", methods=["GET"])
def entities():
return jsonify(["flair:person", "flair:location", "flair:organization"]), 200
@application.route("/app/v1/healthcheck", methods=["GET"])
def healthcheck():
return '', 200
if __name__ == "__main__":
application.run(host="0.0.0.0", port="5000")