-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
147 lines (120 loc) · 3.42 KB
/
server.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
import argparse
import os
from flask import Blueprint, Flask, jsonify, request
from intent_classifier import load_intent_classifier
from model_package import ModelPackage
DEFAULT_MODEL_PATH = os.getenv("MODEL")
try:
from _version import VERSION
except ImportError:
VERSION = None
api = Blueprint("main", __name__)
models = ModelPackage()
@api.route("/ready")
def ready():
if not models.ready:
return "Not ready", 423
return "OK", 200
@api.route("/info")
def info():
return jsonify(
{
"models": models.info(),
"ready": models.ready,
"version": VERSION,
}
)
@api.route("/intent", methods=["POST"])
def intent():
if not request.is_json:
return (
jsonify(
{
"label": "BODY_MISSING",
"message": "Request doesn't have a body.",
}
),
400,
)
data = request.get_json()
if not isinstance(data, dict) or "text" not in data:
return (
jsonify(
{
"label": "TEXT_MISSING",
"message": '"text" missing from request body.',
}
),
400,
)
try:
intents = models.classify(data["text"], data.get("requested_model"))
return (
jsonify(
{
"intents": [{"label": label} for label in intents],
}
),
200,
)
# except ValueError as e:
# return (
# jsonify(
# {
# "label": "BAD_REQUEST",
# "message": f"Incorrect request parameters: {e}",
# }
# ),
# 400,
# )
except Exception as e:
return (
jsonify(
{
"label": "INTERNAL_ERROR",
"message": f"Something went wrong: {e}",
}
),
500,
)
def create_app(model_paths=DEFAULT_MODEL_PATH):
"""
Function to create a Flask app by loading the models specified.
:param model_paths: Paths to the model files.
This parameter can be a string or a list of strings;
each string can contain several semicolon-separated paths.
Default is DEFAULT_MODEL_PATH.
:return: The Flask app object.
:raises ValueError: If model_paths is not provided or is empty.
"""
if not model_paths:
raise ValueError("Please provide model path as a MODEL environment variable")
app = Flask(__name__)
app.register_blueprint(api)
if isinstance(model_paths, str):
model_paths = [model_paths]
for model_path in model_paths:
for model_path_split in model_path.split(":"):
models.add(load_intent_classifier(model_path_split))
return app
def main():
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
"--model",
type=str,
default=DEFAULT_MODEL_PATH,
required=not DEFAULT_MODEL_PATH,
nargs="+",
help="Path to model directory or file.",
)
arg_parser.add_argument(
"--port",
type=int,
default=os.getenv("PORT") or 8080,
help="Server port number.",
)
args = arg_parser.parse_args()
app = create_app(args.model)
app.run(port=args.port)
if __name__ == "__main__":
main()