This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrest_proxy.py
90 lines (72 loc) · 2.83 KB
/
rest_proxy.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
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import grpc
from flask import Flask
from flask_restful import reqparse, Api, Resource, fields, marshal
import iris_pb2
import iris_pb2_grpc
app = Flask(__name__)
api = Api(app)
class HealthCheck(Resource):
"""
This class is used for health check.
"""
def get(self):
return {"status": "alive"}, 200
class RestProxy(Resource):
"""
This class is used for a proxy with REST to a gRPC server.
"""
# Define the inputs.
parser = reqparse.RequestParser()
parser.add_argument('sepal_length', type=float, help='sepal length', required=True)
parser.add_argument('sepal_width', type=float, help='sepal width', required=True)
parser.add_argument('petal_length', type=float, help='petal length', required=True)
parser.add_argument('petal_width', type=float, help='petal width', required=True)
parser.add_argument('threshold', type=float, help='threshold', required=False, default=0.5)
# Define the outputs.
resource_fields = {
'species': fields.String,
}
def __init__(self, host, port):
"""
:param host: host of gRPC server.
:param port: port of gRPC server.
"""
self.host = host
self.port = port
def post(self):
# Parse arguments by REST request.
args = self.__class__.parser.parse_args()
# Request to the gRPC server.
channel = grpc.insecure_channel('%s:%s' % (self.host, self.port))
stub = iris_pb2_grpc.IrisPredictorStub(channel)
request = iris_pb2.IrisPredictRequest(
sepal_length=args['sepal_length'],
sepal_width=args['sepal_width'],
petal_length=args['petal_length'],
petal_width=args['petal_width']
)
response = stub.PredictIrisSpecies(request)
# Return the results.
data = {
'species': response.species,
}
return marshal(data, self.__class__.resource_fields)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--host', help='REST server host', default='localhost', type=str)
parser.add_argument('--port', help='REST server port', default=5000, type=int)
parser.add_argument('--grpc_host', help='gRPC server host', default='0.0.0.0', type=str)
parser.add_argument('--grpc_port', help='gRPC server port', default=50052, type=int)
parser.add_argument('--debug', help='debug flag', default=False, type=bool)
args = parser.parse_args()
resource_class_kwargs = {
'host': args.grpc_host,
'port': args.grpc_port,
}
# Run flask app
api.add_resource(RestProxy, '/', resource_class_kwargs=resource_class_kwargs)
api.add_resource(HealthCheck, '/healthcheck')
app.run(host=args.host, port=args.port, debug=args.debug)