Skip to content

Commit

Permalink
Add windows serving support for faq finance (PaddlePaddle#3491)
Browse files Browse the repository at this point in the history
* Add windows serving support for faq finance

* Remove unused comments
  • Loading branch information
w5688414 authored Oct 17, 2022
1 parent 839b2d0 commit ce67b4f
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 14 deletions.
25 changes: 17 additions & 8 deletions applications/question_answering/faq_finance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,17 +443,10 @@ sh scripts/export_to_serving.sh
```

启动 Pipeline Server:
修改Tokenizer:

```
self.tokenizer = AutoTokenizer.from_pretrained('rocketqa-zh-base-query-encoder')
```
然后运行:

```
cd deploy/python/
python web_service.py
python web_service.py --model_name_or_path rocketqa-zh-base-query-encoder
```

启动客户端调用 Server, 使用 POST的方式:
Expand All @@ -479,6 +472,22 @@ list_data = [
python rpc_client.py
```

对于Windows用户,启动下面的Pipeline Server:

```
python web_service_windows.py --model_name_or_path rocketqa-zh-base-query-encoder
```

启动客户端调用 Server, 使用 POST的方式(Windows不支持RPC的调用方式),首先修改http_client.py中需要预测的样本:

```
data = {"feed": ["买了社保,是不是就不用买商业保险了?"], "fetch": ["output_embedding"]}
```
然后运行:
```
python http_client.py
```

### 4.5 问答系统整个流程

问答系统使用了Client Server的模式,即抽取向量的模型部署在服务端,然后启动客户端(Client)端去访问。
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) 2022 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.

import json
import base64
import time
import numpy as np
import requests

headers = {"Content-type": "application/json"}
url = "http://10.21.226.175:8080/ernie/prediction" # XXX取决于服务端YourService的初始化name参数

data = {"feed": ["买了社保,是不是就不用买商业保险了?"], "fetch": ["output_embedding"]}
data = json.dumps(data)
print(data)
r = requests.post(url=url, headers=headers, data=data)
print(r.json())
json_data = r.json()
data = np.array(json_data['result']['output_embedding'])
print(data.shape)
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@

import numpy as np
import sys
import argparse

from paddle_serving_server.web_service import WebService, Op

# yapf: disable
parser = argparse.ArgumentParser()
parser.add_argument('--model_name_or_path', default="rocketqa-zh-base-query-encoder", help="Select tokenizer name to for model")
args = parser.parse_args()
# yapf: enable


def convert_example(example,
tokenizer,
Expand All @@ -37,8 +44,7 @@ class ErnieOp(Op):

def init_op(self):
from paddlenlp.transformers import AutoTokenizer
self.tokenizer = AutoTokenizer.from_pretrained(
'rocketqa-zh-base-query-encoder')
self.tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path)

def preprocess(self, input_dicts, data_id, log_id):
from paddlenlp.data import Stack, Tuple, Pad
Expand All @@ -54,7 +60,7 @@ def preprocess(self, input_dicts, data_id, log_id):
batchify_fn = lambda samples, fn=Tuple(
Pad(axis=0, pad_val=self.tokenizer.pad_token_id, dtype="int64"
), # input
Pad(axis=0, pad_val=self.tokenizer.pad_token_id, dtype="int64"
Pad(axis=0, pad_val=self.tokenizer.pad_token_type_id, dtype="int64"
), # segment
): fn(samples)
input_ids, segment_ids = batchify_fn(examples)
Expand All @@ -77,6 +83,7 @@ def get_pipeline_response(self, read_op):
return ernie_op


ernie_service = ErnieService(name="ernie")
ernie_service.prepare_pipeline_config("config_nlp.yml")
ernie_service.run_service()
if __name__ == "__main__":
ernie_service = ErnieService(name="ernie")
ernie_service.prepare_pipeline_config("config_nlp.yml")
ernie_service.run_service()
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright (c) 2021 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.

import numpy as np
import sys
import argparse

from paddle_serving_server.web_service import WebService, Op

# yapf: disable
parser = argparse.ArgumentParser()
parser.add_argument('--model_name_or_path', default="rocketqa-zh-base-query-encoder", help="Select tokenizer name to for model")
args = parser.parse_args()
# yapf: enable


def convert_example(example,
tokenizer,
max_seq_length=512,
pad_to_max_seq_len=False):
result = []
for text in example:
encoded_inputs = tokenizer(text=text,
max_seq_len=max_seq_length,
pad_to_max_seq_len=pad_to_max_seq_len)
input_ids = encoded_inputs["input_ids"]
token_type_ids = encoded_inputs["token_type_ids"]
result += [input_ids, token_type_ids]
return result


class ErnieService(WebService):

def init_service(self):
from paddlenlp.transformers import AutoTokenizer
self.tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path)

def preprocess(self, feed=[], fetch=[]):
from paddlenlp.data import Stack, Tuple, Pad
print("input dict", feed)
batch_size = len(feed)
is_batch = True
examples = []
for i in range(batch_size):
input_ids, segment_ids = convert_example([feed[i]], self.tokenizer)
examples.append((input_ids, segment_ids))
batchify_fn = lambda samples, fn=Tuple(
Pad(axis=0, pad_val=self.tokenizer.pad_token_id, dtype="int64"
), # input
Pad(axis=0, pad_val=self.tokenizer.pad_token_type_id, dtype="int64"
), # segment
): fn(samples)
input_ids, segment_ids = batchify_fn(examples)
feed_dict = {}
feed_dict['input_ids'] = input_ids
feed_dict['token_type_ids'] = segment_ids
return feed_dict, fetch, is_batch

def postprocess(self, feed=[], fetch=[], fetch_map=None):
for key in fetch_map:
fetch_map[key] = fetch_map[key].tolist()
return fetch_map


if __name__ == "__main__":
ernie_service = ErnieService(name="ernie")
ernie_service.load_model_config("../../serving_server")
ernie_service.prepare_server(workdir="workdir", port=8080)
ernie_service.init_service()
ernie_service.run_debugger_service()
ernie_service.run_web_service()

0 comments on commit ce67b4f

Please sign in to comment.