senta_web_service.py 3.5 KB
Newer Older
M
MRXLT 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 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.

from paddle_serving_server_gpu.web_service import WebService
from paddle_serving_client import Client
M
MRXLT 已提交
17
from paddle_serving_app import LACReader, SentaReader
M
MRXLT 已提交
18 19
import os
import sys
M
MRXLT 已提交
20
from multiprocessing import Process
M
MRXLT 已提交
21 22 23


class SentaService(WebService):
M
MRXLT 已提交
24
    def set_config(
M
MRXLT 已提交
25 26 27 28 29 30 31 32
            self,
            lac_model_path,
            lac_dict_path,
            senta_dict_path, ):
        self.lac_model_path = lac_model_path
        self.lac_client_config_path = lac_model_path + "/serving_server_conf.prototxt"
        self.lac_dict_path = lac_dict_path
        self.senta_dict_path = senta_dict_path
M
MRXLT 已提交
33 34 35 36
        self.show = False

    def show_detail(self, show=False):
        self.show = show
M
MRXLT 已提交
37

M
MRXLT 已提交
38
    def start_lac_service(self):
39 40
        if not os.path.exists('./lac_serving'):
            os.mkdir("./lac_serving")
M
MRXLT 已提交
41
        os.chdir('./lac_serving')
M
MRXLT 已提交
42
        self.lac_port = self.port + 100
M
MRXLT 已提交
43
        r = os.popen(
M
MRXLT 已提交
44 45
            "python -m paddle_serving_server.serve --model {} --port {} &".
            format("../" + self.lac_model_path, self.lac_port))
M
MRXLT 已提交
46 47 48 49 50
        os.chdir('..')

    def init_lac_service(self):
        ps = Process(target=self.start_lac_service())
        ps.start()
M
MRXLT 已提交
51
        self.init_lac_client()
M
MRXLT 已提交
52 53 54 55 56 57 58 59

    def lac_predict(self, feed_data):
        lac_result = self.lac_client.predict(
            feed={"words": feed_data}, fetch=["crf_decode"])
        return lac_result

    def init_lac_client(self):
        self.lac_client = Client()
M
MRXLT 已提交
60
        self.lac_client.load_client_config(self.lac_client_config_path)
M
MRXLT 已提交
61
        self.lac_client.connect(["127.0.0.1:{}".format(self.lac_port)])
M
MRXLT 已提交
62 63

    def init_lac_reader(self):
M
MRXLT 已提交
64
        self.lac_reader = LACReader()
M
MRXLT 已提交
65 66

    def init_senta_reader(self):
M
MRXLT 已提交
67
        self.senta_reader = SentaReader()
M
MRXLT 已提交
68

M
MRXLT 已提交
69
    def preprocess(self, feed=[], fetch=[]):
M
MRXLT 已提交
70
        feed_data = self.lac_reader.process(feed[0]["words"])
M
MRXLT 已提交
71 72 73
        if self.show:
            print("---- lac reader ----")
            print(feed_data)
M
MRXLT 已提交
74
        lac_result = self.lac_predict(feed_data)
M
MRXLT 已提交
75 76 77
        if self.show:
            print("---- lac out ----")
            print(lac_result)
M
MRXLT 已提交
78
        segs = self.lac_reader.parse_result(feed[0]["words"],
M
MRXLT 已提交
79
                                            lac_result["crf_decode"])
M
MRXLT 已提交
80 81 82
        if self.show:
            print("---- lac parse ----")
            print(segs)
M
MRXLT 已提交
83
        feed_data = self.senta_reader.process(segs)
M
MRXLT 已提交
84 85 86
        if self.show:
            print("---- senta reader ----")
            print("feed_data", feed_data)
B
barrierye 已提交
87
        return [{"words": feed_data}], fetch
M
MRXLT 已提交
88 89


M
MRXLT 已提交
90
senta_service = SentaService(name="senta")
M
MRXLT 已提交
91
senta_service.show_detail(False)
M
MRXLT 已提交
92
senta_service.set_config(
M
MRXLT 已提交
93 94
    lac_model_path="./lac_model",
    lac_dict_path="./lac_dict",
M
MRXLT 已提交
95
    senta_dict_path="./vocab.txt")
M
MRXLT 已提交
96
senta_service.load_model_config(sys.argv[1])
M
MRXLT 已提交
97 98
senta_service.prepare_server(
    workdir=sys.argv[2], port=int(sys.argv[3]), device="cpu")
M
MRXLT 已提交
99 100 101
senta_service.init_lac_reader()
senta_service.init_senta_reader()
senta_service.init_lac_service()
M
MRXLT 已提交
102 103
senta_service.run_rpc_service()
senta_service.run_web_service()