test_pipeline_server.py 3.7 KB
Newer Older
B
barrierye 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=doc-string-missing

B
barrierye 已提交
16
from paddle_serving_server.pipeline import Op, RequestOp, ResponseOp
17
from paddle_serving_server.pipeline import PipelineServer
B
barrierye 已提交
18 19
from paddle_serving_server.pipeline.proto import pipeline_service_pb2
from paddle_serving_server.pipeline.channel import ChannelDataEcode
B
barrierye 已提交
20
import numpy as np
B
barrierye 已提交
21
import logging
B
barrierye 已提交
22
from paddle_serving_app.reader import IMDBDataset
B
barrierye 已提交
23

B
barrierye 已提交
24 25
_LOGGER = logging.getLogger(__name__)

B
barrierye 已提交
26 27 28
logging.basicConfig(
    format='%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
    datefmt='%Y-%m-%d %H:%M',
B
barrierye 已提交
29
    level=logging.DEBUG)
B
barrierye 已提交
30

B
barrierye 已提交
31

B
barrierye 已提交
32
class ImdbRequestOp(RequestOp):
B
barrierye 已提交
33 34 35 36
    def load_user_resources(self):
        self.imdb_dataset = IMDBDataset()
        self.imdb_dataset.load_resource('imdb.vocab')

B
barrierye 已提交
37 38 39 40 41 42 43 44 45
    def unpack_request_package(self, request):
        dictdata = {}
        for idx, key in enumerate(request.key):
            if key != "words":
                continue
            words = request.value[idx]
            word_ids, _ = self.imdb_dataset.get_words_and_label(words)
            dictdata[key] = np.array(word_ids)
        return dictdata
B
barrierye 已提交
46 47


B
barrierye 已提交
48 49
class CombineOp(Op):
    def preprocess(self, input_data):
50
        combined_prediction = 0
B
barrierye 已提交
51 52
        for op_name, data in input_data.items():
            _LOGGER.info("{}: {}".format(op_name, data["prediction"]))
53
            combined_prediction += data["prediction"]
B
barrierye 已提交
54
        data = {"prediction": combined_prediction / 2}
B
barrierye 已提交
55 56
        return data

B
barrierye 已提交
57

B
barrierye 已提交
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
class ImdbResponseOp(ResponseOp):
    # Here ImdbResponseOp is consistent with the default ResponseOp implementation
    def pack_response_package(self, channeldata):
        resp = pipeline_service_pb2.Response()
        resp.ecode = channeldata.ecode
        if resp.ecode == ChannelDataEcode.OK.value:
            feed = channeldata.parse()
            # ndarray to string
            for name, var in feed.items():
                resp.value.append(var.__repr__())
                resp.key.append(name)
        else:
            resp.error_info = channeldata.error_info
        return resp


read_op = ImdbRequestOp()
bow_op = Op(name="bow",
            input_ops=[read_op],
            server_endpoints=["127.0.0.1:9393"],
            fetch_list=["prediction"],
            client_config="imdb_bow_client_conf/serving_client_conf.prototxt",
            concurrency=1,
            timeout=-1,
            retry=1)
cnn_op = Op(name="cnn",
            input_ops=[read_op],
            server_endpoints=["127.0.0.1:9292"],
            fetch_list=["prediction"],
            client_config="imdb_cnn_client_conf/serving_client_conf.prototxt",
            concurrency=1,
            timeout=-1,
            retry=1)
B
barrierye 已提交
91
combine_op = CombineOp(
B
barrierye 已提交
92 93 94 95
    name="combine",
    input_ops=[bow_op, cnn_op],
    concurrency=1,
    timeout=-1,
96
    retry=1)
B
barrierye 已提交
97

B
barrierye 已提交
98 99 100 101 102 103 104 105 106
# fetch output of bow_op
# response_op = ImdbResponseOp(input_ops=[bow_op])

# fetch output of combine_op
response_op = ImdbResponseOp(input_ops=[combine_op])

# use default ResponseOp implementation
# response_op = ResponseOp(input_ops=[combine_op])

B
barrierye 已提交
107
server = PipelineServer()
B
barrierye 已提交
108
server.set_response_op(response_op)
B
barrierye 已提交
109 110
server.prepare_server('config.yml')
server.run_server()