test_pipeline_server.py 2.6 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, ReadOp
17
from paddle_serving_server.pipeline import PipelineServer
B
barrierye 已提交
18
import numpy as np
B
barrierye 已提交
19
import logging
B
barrierye 已提交
20
from paddle_serving_app.reader import IMDBDataset
B
barrierye 已提交
21

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

B
barrierye 已提交
28

B
barrierye 已提交
29 30 31 32 33 34 35 36 37 38 39 40
class ImdbOp(Op):
    def preprocess(self, input_data):
        data = input_data.parse()
        imdb_dataset = IMDBDataset()
        imdb_dataset.load_resource('imdb.vocab')
        word_ids, _ = imdb_dataset.get_words_and_label(data['words'])
        return {"words": word_ids}

    # def postprocess(self, fetch_data):
    # return {key: str(value) for key, value in fetch_data.items()}


B
barrierye 已提交
41 42
class CombineOp(Op):
    def preprocess(self, input_data):
43
        combined_prediction = 0
44 45
        for op_name, channeldata in input_data.items():
            data = channeldata.parse()
46 47
            logging.info("{}: {}".format(op_name, data["prediction"]))
            combined_prediction += data["prediction"]
B
barrierye 已提交
48
        data = {"prediction": str(combined_prediction / 2)}
B
barrierye 已提交
49 50
        return data

B
barrierye 已提交
51

B
barrierye 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
read_op = ReadOp()
bow_op = ImdbOp(
    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 = ImdbOp(
    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 已提交
71
combine_op = CombineOp(
B
barrierye 已提交
72 73 74 75
    name="combine",
    input_ops=[bow_op, cnn_op],
    concurrency=1,
    timeout=-1,
76
    retry=1)
B
barrierye 已提交
77 78 79 80 81 82 83

server = PipelineServer()
server.add_ops([read_op, bow_op, cnn_op, combine_op])
# server.set_response_op(bow_op)
server.set_response_op(combine_op)
server.prepare_server('config.yml')
server.run_server()