test_pipeline_server.py 2.5 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 25
logging.basicConfig(
    format='%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
    datefmt='%Y-%m-%d %H:%M',
    level=logging.INFO)
B
barrierye 已提交
26

B
barrierye 已提交
27

B
barrierye 已提交
28
class ImdbOp(Op):
B
barrierye 已提交
29 30 31 32
    def load_user_resources(self):
        self.imdb_dataset = IMDBDataset()
        self.imdb_dataset.load_resource('imdb.vocab')

B
barrierye 已提交
33 34
    def preprocess(self, input_data):
        data = input_data.parse()
B
barrierye 已提交
35
        word_ids, _ = self.imdb_dataset.get_words_and_label(data['words'])
B
barrierye 已提交
36 37 38
        return {"words": word_ids}


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

B
barrierye 已提交
49

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

server = PipelineServer()
server.add_ops([read_op, bow_op, cnn_op, combine_op])
B
barrierye 已提交
78
#server.set_response_op(bow_op)
B
barrierye 已提交
79 80 81
server.set_response_op(combine_op)
server.prepare_server('config.yml')
server.run_server()