-
Notifications
You must be signed in to change notification settings - Fork 13
/
web_server.py
219 lines (184 loc) · 8.25 KB
/
web_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
__DEBUG__=False
import os
import sys, traceback
import argparse
import json
import pprint
if not __DEBUG__:
#WEB SERVER: FLASK
from flask import Flask, jsonify, abort, make_response, request, render_template, flash, redirect
from flask_cors import CORS, cross_origin
#WEB SERVER: TORNADO
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
#WEB SERVER: FALCON
#import falcon
#WEB SERVER: WAITRESS
#if os.name == 'nt':
# from waitress import serve
#GEVENT
from gevent.pywsgi import WSGIServer
from gevent import monkey
# need to patch sockets to make requests async
monkey.patch_all()
import requests
from multiprocessing import cpu_count
can_fork = hasattr(os, "fork")
#FLASK
app = Flask(__name__)
CORS(app)
import core
import __main__
REST_config = __main__.REST_config
current_version = "1.0"
instances={}
config={}
config["port"]=REST_config["port"]
config["route"]=REST_config["route"]
if "<version>" in config["route"]:
config["route"] = config["route"].replace("<version>", current_version)
print("Port: ", config["port"], "\tRoute: ", config["route"] )
if not __DEBUG__:
default_headers = {'Content-Type': 'application/json',
"Access-Control-Allow-Origin" :"*",
"Access-Control-Allow-Methods":"GET,PUT,POST,DELETE" ,
"Access-Control-Allow-Headers":"Content-Type"}
#FLASK & TORNADO-compatible
class LoggingMiddleware(object):
def __init__(self, app):
self._app = app
def __call__(self, environ, resp):
errorlog = environ['wsgi.errors']
pprint.pprint(('REQUEST', \
environ["HTTP_HOST"], \
environ["REMOTE_ADDR"], \
environ["REQUEST_METHOD"], \
environ["PATH_INFO"], \
environ["QUERY_STRING"]), \
stream=errorlog)
def log_response(status, headers, *args):
pprint.pprint(('RESPONSE', status #, headers
), stream=errorlog)
return resp(status, headers, *args)
return self._app(environ, log_response)
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
#####################
## alive ##
#####################
@app.route(config["route"]+'/alive', methods=['OPTIONS','GET'])
def alive():
if request.method == 'OPTIONS':
r = make_response("")
for key, value in default_headers.items():
r.headers.add(key, value)
return r, 200
if request.method == 'GET':
r = make_response("alive")
for key, value in default_headers.items():
r.headers.add(key, value)
return r, 200
#####################
## instances ##
#####################
@app.route(config["route"]+'/instances', methods=['OPTIONS','GET'])
def valid_instances():
if request.method == 'OPTIONS':
r = make_response("")
for key, value in default_headers.items():
r.headers.add(key, value)
return r, 200
if request.method == 'GET':
r = make_response(jsonify({"instances":instances}))
for key, value in default_headers.items():
r.headers.add(key, value)
return r, 200
#####################
## inference ##
#####################
@app.route(config["route"]+'/inference', methods=['OPTIONS','POST'])
def inference():
if request.method == 'OPTIONS':
r = make_response("")
for key, value in default_headers.items():
r.headers.add(key, value)
return r, 200
if request.method == 'POST':
response_dict={}
REST_instance = request.args.get('instance',default=None)
if REST_instance == None or REST_instance == "":
return make_response(jsonify({'error': 'Missing (or empty) arg called "instance"'}), 400)
if REST_instance not in instances:
return make_response(jsonify({'error': 'Provided "instance" is not a recognized one.'}), 400)
if not request.json:
request.get_json(force=True) #force to get the payload as JSON
request_content = request.json
if "text" not in request_content:
return make_response(jsonify({'error': 'Missing "text" field in POST body.'}), 400)
text = request_content["text"]
if text==None or text=="":
return make_response(jsonify({'error': 'Data field is empty: no "text" to process.'}), 400)
instance = instances[REST_instance]
response_dict["prediction"]= core.predict(instance, text)
response = jsonify(response_dict)
for key, value in default_headers.items():
response.headers.add(key, value)
return response, 200
#####################
## batch_inference ##
#####################
@app.route(config["route"]+'/batch_inference', methods=['OPTIONS','POST'])
def batch_inference():
if request.method == 'OPTIONS':
r = make_response("")
for key, value in default_headers.items():
r.headers.add(key, value)
return r, 200
if request.method == 'POST':
response_dict={}
REST_instance = request.args.get('instance',default=None)
if REST_instance == None or REST_instance == "":
return make_response(jsonify({'error': 'Missing (or empty) arg called "instance"'}), 400)
if REST_instance not in instances:
return make_response(jsonify({'error': 'Provided "instance" is not a recognized one.'}), 400)
if not request.json:
request.get_json(force=True) #force to get the payload as JSON
request_content = request.json
if "texts" not in request_content:
return make_response(jsonify({'error': 'Missing "texts" field in POST body.'}), 400)
texts = request_content["texts"]
if texts==None:
return make_response(jsonify({'error': 'Data field is empty: no "texts" to process.'}), 400)
instance = instances[REST_instance]
response_dict["predictions"]= core.batch_predict(instance, texts)
response = jsonify(response_dict)
for key, value in default_headers.items():
response.headers.add(key, value)
return response, 200
def run_server():
core.load_instances(REST_config, instances)
if __DEBUG__:
instance = instances["EN300Twitter"]
print( core.predict(instance,"why does tom cruise take so many times to figure things out in the movie edge of tomorrow , but gets it right 1 st time in mission impossible ?"))
print( core.batch_predict(instance, ["why does tom cruise take so many times to figure things out in the movie edge of tomorrow , but gets it right 1 st time in mission impossible ?", "that sucks . i am forced to listen to kpop on thursday class . i guess my cells in brain are going to die out . "]))
else: #if not __DEBUG__:
app.wsgi_app = LoggingMiddleware(app.wsgi_app)
#app.run(debug=True,
# host='0.0.0.0',
# port=configurations["port"],
# threaded=True,
# #threaded= (True if can_fork == False else False),processes =
# #(cpu_count() if can_fork else 1),
# use_reloader=False)
###TORNADO
##http_server = HTTPServer(WSGIContainer(app))
##http_server.listen(configurations["port"])
##IOLoop.current().start()
#GEVENT
print("Start gevent WSGI server")
# use gevent WSGI server instead of the Flask
http = WSGIServer(('', config["port"]), app.wsgi_app)
# TODO gracefully handle shutdown
http.serve_forever()