-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
model_server.py
70 lines (48 loc) · 1.55 KB
/
model_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
import os
import grpc
import tensorflow as tf
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2_grpc
from keras_image_helper import create_preprocessor
from flask import Flask, request, jsonify
server = os.getenv('TF_SERVING_HOST', 'localhost:8500')
channel = grpc.insecure_channel(server)
stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
preprocessor = create_preprocessor('xception', target_size=(299, 299))
labels = [
'dress',
'hat',
'longsleeve',
'outwear',
'pants',
'shirt',
'shoes',
'shorts',
'skirt',
't-shirt'
]
def np_to_protobuf(data):
return tf.make_tensor_proto(data, shape=data.shape)
def make_request(X):
pb_request = predict_pb2.PredictRequest()
pb_request.model_spec.name = 'clothing-model'
pb_request.model_spec.signature_name = 'serving_default'
pb_request.inputs['input_8'].CopyFrom(np_to_protobuf(X))
return pb_request
def process_response(pb_result):
pred = pb_result.outputs['dense_7'].float_val
result = {c: p for c, p in zip(labels, pred)}
return result
def apply_model(url):
X = preprocessor.from_url(url)
pb_request = make_request(X)
pb_result = stub.Predict(pb_request, timeout=20.0)
return process_response(pb_result)
app = Flask('clothing-model')
@app.route('/predict', methods=['POST'])
def predict():
url = request.get_json()
result = apply_model(url['url'])
return jsonify(result)
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=9696)