senta_web_service.py 3.0 KB
Newer Older
M
MRXLT 已提交
1
#encoding=utf-8
M
MRXLT 已提交
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.

M
MRXLT 已提交
16
from paddle_serving_server.web_service import WebService
M
MRXLT 已提交
17
from paddle_serving_client import Client
M
MRXLT 已提交
18
from paddle_serving_app.reader import LACReader, SentaReader
M
MRXLT 已提交
19 20
import os
import sys
W
wangjiawei04 已提交
21
import numpy as np
M
MRXLT 已提交
22 23 24 25
#senta_web_service.py
from paddle_serving_server.web_service import WebService
from paddle_serving_client import Client
from paddle_serving_app.reader import LACReader, SentaReader
M
MRXLT 已提交
26 27


M
MRXLT 已提交
28 29 30
class SentaService(WebService):
    #初始化lac模型预测服务
    def init_lac_client(self, lac_port, lac_client_config):
M
MRXLT 已提交
31 32
        self.lac_reader = LACReader()
        self.senta_reader = SentaReader()
M
MRXLT 已提交
33 34 35
        self.lac_client = Client()
        self.lac_client.load_client_config(lac_client_config)
        self.lac_client.connect(["127.0.0.1:{}".format(lac_port)])
M
MRXLT 已提交
36

M
MRXLT 已提交
37
    #定义senta模型预测服务的预处理,调用顺序:lac reader->lac模型预测->预测结果后处理->senta reader
M
MRXLT 已提交
38
    def preprocess(self, feed=[], fetch=[]):
M
MRXLT 已提交
39
        feed_batch = []
W
wangjiawei04 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53
        words_lod = [0]
        for ins in feed:
            if "words" not in ins:
                raise ("feed data error!")
            feed_data = self.lac_reader.process(ins["words"])
            words_lod.append(words_lod[-1] + len(feed_data))
            feed_batch.append(np.array(feed_data).reshape(len(feed_data), 1))
        words = np.concatenate(feed_batch, axis=0)

        lac_result = self.lac_client.predict(
            feed={"words": words,
                  "words.lod": words_lod},
            fetch=["crf_decode"],
            batch=True)
M
MRXLT 已提交
54
        result_lod = lac_result["crf_decode.lod"]
W
wangjiawei04 已提交
55 56
        feed_batch = []
        words_lod = [0]
M
MRXLT 已提交
57 58 59 60 61
        for i in range(len(feed)):
            segs = self.lac_reader.parse_result(
                feed[i]["words"],
                lac_result["crf_decode"][result_lod[i]:result_lod[i + 1]])
            feed_data = self.senta_reader.process(segs)
W
wangjiawei04 已提交
62 63 64 65 66 67
            feed_batch.append(np.array(feed_data).reshape(len(feed_data), 1))
            words_lod.append(words_lod[-1] + len(feed_data))
        return {
            "words": np.concatenate(feed_batch),
            "words.lod": words_lod
        }, fetch
M
MRXLT 已提交
68 69


M
MRXLT 已提交
70
senta_service = SentaService(name="senta")
M
MRXLT 已提交
71 72 73
senta_service.load_model_config("senta_bilstm_model")
senta_service.prepare_server(workdir="workdir")
senta_service.init_lac_client(
W
wangjiawei04 已提交
74 75
    lac_port=9300,
    lac_client_config="lac/lac_model/serving_server_conf.prototxt")
M
MRXLT 已提交
76 77
senta_service.run_rpc_service()
senta_service.run_web_service()