-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
39 lines (26 loc) · 1.08 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
import json
import simple_http_server.server as server
from simple_http_server import JSONBody
from simple_http_server import request_map
from simple_http_server import Response
from trollaganda import setup
from parse_args import parseArgs
predictor = None
def setupPredictor():
global predictor
args = parseArgs()
predictor = setup(args=args)
@request_map("/predict", method=["POST"])
def my_ctrl2(data = JSONBody()):
predictions = predictor.predict(messages=data["messages"])
results = list(map(lambda x: True if x[0] else False, predictions))
resultsJSON = json.dumps(results)
return Response(status_code=200, headers={"Access-Control-Allow-Origin": "*", "Content-Type": "application/json", "Access-Control-Allow-Headers": "*"}, body=resultsJSON)
@request_map("/predict", method=["OPTIONS"])
def my_ctrl1():
return Response(status_code=200, headers={"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "*", "Content-Type": "application/json"})
def main(*args):
setupPredictor()
server.start()
if __name__ == "__main__":
main()