-
Notifications
You must be signed in to change notification settings - Fork 1
/
srv.py
54 lines (45 loc) · 1.72 KB
/
srv.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
import random
from srv_predictor import Predictor
from flask import Flask, request, jsonify, make_response
import os
import time
predictor = Predictor(
os.environ.get('SG_SEG_DATASET_PATH'),
os.environ.get('SG_REAL_DATASET_PATH'),
os.environ.get('SG_MODEL_PATH'),
os.environ.get('SG_SEG_COLORS_JSON'),
os.environ.get('SG_CLF_PATH'),
)
app = Flask(__name__)
latest_request_ptr = [time.time()]
@app.route("/infer", methods=['POST', 'OPTIONS'])
def infer():
latest_request_ptr[0] = time.time()
if request.method == "OPTIONS":
return _build_cors_preflight_response()
elif request.method == "POST":
content = request.json
if content['action'] == 'infer':
in_img = content['img'].split(',')[-1]
raw_z = content['z']
return _corsify_actual_response(jsonify(predictor.infer(in_img, raw_z)))
elif content['action'] == 'example':
raw_z = content['z'] if 'z' in content else [random.gauss(0, 1) for _ in range(512)]
return _corsify_actual_response(jsonify(predictor.example(raw_z)))
else:
return _corsify_actual_response(jsonify({'error': 'unknown action'}))
@app.route("/health", methods=['GET'])
def ping():
return "OK"
@app.route("/latest_request", methods=['GET'])
def latest_request():
return str(latest_request_ptr[0])
def _build_cors_preflight_response():
response = make_response()
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "*"
response.headers["Access-Control-Allow-Headers"] = "*"
return response
def _corsify_actual_response(response):
response.headers.add("Access-Control-Allow-Origin", "*")
return response