-
Notifications
You must be signed in to change notification settings - Fork 1
/
web_app.py
40 lines (32 loc) · 1.23 KB
/
web_app.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
from flask import Flask, request, jsonify, current_app
from protos import infer_pb2_grpc
from utils.label import ServiceClient, image_preprocess
from google.protobuf.json_format import MessageToDict
app = Flask(__name__)
app.config['predict'] = ServiceClient(
infer_pb2_grpc, 'InferenceStub', 'localhost', 50051)
@app.route('/predict', methods=["POST"])
def predict():
res = {"message": "", "results": []}
if request.json:
req_dict = request.get_json()
try:
# convert image to protobuffer
image = image_preprocess(req_dict)
except Exception as e:
current_app.logger.error(f'pre handler image error: {str(e)}')
res['message'] = str(e)
return jsonify(res)
# put to predict
try:
remote_results = app.config['predict'].Predict(image)
res['results'] = MessageToDict(remote_results)['results']
except Exception as e:
current_app.logger.error(e.details)
res['message'] = f"inference failed: {e.code()}"
return jsonify(res)
else:
res['message'] = 'please post JSON format data'
return jsonify(res)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)