Skip to content

Commit

Permalink
add the paddlenlp server
Browse files Browse the repository at this point in the history
  • Loading branch information
wawltor committed Nov 18, 2022
1 parent e002b0d commit f12a0d8
Show file tree
Hide file tree
Showing 45 changed files with 2,099 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 基于PaddleNLP SimpleServing 的服务化部署

## 目录
- [环境准备](#环境准备)
- [Server启动服务](#Server服务启动)
- [其他参数设置](#其他参数设置)

## 环境准备
使用有SimpleServing功能的PaddleNLP版本
```shell
pip install paddlenlp --upgrade
```
## Server服务启动
### 分类任务启动
#### 启动 分类 Server 服务
```bash
paddlenlp server server:app --host 0.0.0.0 --port 8189
```

#### 分类任务发送服务
```bash
python client.py
```


## 其他参数设置
可以在client端设置 `max_seq_len`, `batch_size`, `prob_limit` 参数
```python
data = {
'data': {
'text': texts,
},
'parameters': {
'max_seq_len': args.max_seq_len,
'batch_size': args.batch_size,
'prob_limit': args.prob_limit
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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 argparse
import requests
import json

# yapf: disable
parser = argparse.ArgumentParser()
parser.add_argument("--max_seq_len", default=128, type=int, help="The maximum total input sequence length after tokenization.")
parser.add_argument("--batch_size", default=1, type=int, help="Batch size per GPU/CPU for predicting.")
parser.add_argument("--prob_limit", default=0.5, type=float, help="The limitation of probability for the label.")
args = parser.parse_args()
# yapf: enable

url = "http://0.0.0.0:8189/models/cls_hierarchical"
headers = {"Content-Type": "application/json"}

if __name__ == "__main__":
texts = [
'请问木竭胶囊能同高血压药、氨糖同时服吗?', '低压100*高压140*头涨,想吃点降压药。谢谢!', '脑穿通畸形易发人群有哪些',
'幼儿乱吃丙硫氧嘧啶片怎么办,我也不知道她吃了几片', '如果是可以降血糖的话,血糖值7点多的大概需要吃几个疗程?'
]
data = {
'data': {
'text': texts,
},
'parameters': {
'max_seq_len': args.max_seq_len,
'batch_size': args.batch_size,
'prob_limit': args.prob_limit
}
}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
print(r.text)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.

from paddlenlp import SimpleServer
from paddlenlp.server import CustomModelHandler, MultiLabelClassificationPostHandler

app = SimpleServer()
app.register('cls_hierarchical',
model_path="../../export",
tokenizer_name='ernie-3.0-medium-zh',
model_handler=CustomModelHandler,
post_handler=MultiLabelClassificationPostHandler)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 基于PaddleNLP SimpleServing 的服务化部署

## 目录
- [环境准备](#环境准备)
- [Server启动服务](#Server服务启动)
- [其他参数设置](#其他参数设置)

## 环境准备
使用有SimpleServing功能的PaddleNLP版本
```shell
pip install paddlenlp >= 2.4.4
```
## Server服务启动
### 分类任务启动
#### 启动分类 Server 服务
```bash
paddlenlp server server:app --host 0.0.0.0 --port 8189
```

#### 启动分类 Client 服务
```bash
python client.py
```


## 其他参数设置
可以在client端设置 `max_seq_len`, `batch_size` 参数
```python
data = {
'data': {
'text': texts,
'text_pair': text_pairs if len(text_pairs) > 0 else None
},
'parameters': {
'max_seq_len': args.max_seq_len,
'batch_size': args.batch_size
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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 argparse
import requests
import json

# yapf: disable
parser = argparse.ArgumentParser()
parser.add_argument("--max_seq_len", default=128, type=int, help="The maximum total input sequence length after tokenization.")
parser.add_argument("--batch_size", default=1, type=int, help="Batch size per GPU/CPU for predicting.")
args = parser.parse_args()
# yapf: enable

url = "http://0.0.0.0:8189/models/cls_multi_class"
headers = {"Content-Type": "application/json"}

if __name__ == "__main__":
texts = [
'黑苦荞茶的功效与作用及食用方法', '交界痣会凸起吗', '检查是否能怀孕挂什么科', '鱼油怎么吃咬破吃还是直接咽下去',
'幼儿挑食的生理原因是'
]
data = {
'data': {
'text': texts,
},
'parameters': {
'max_seq_len': args.max_seq_len,
'batch_size': args.batch_size
}
}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
result_json = json.loads(r.text)
print(result_json)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.

from paddlenlp import SimpleServer
from paddlenlp.server import CustomModelHandler, MultiClassificationPostHandler

app = SimpleServer()
app.register('cls_multi_class',
model_path="../../export",
tokenizer_name='ernie-3.0-medium-zh',
model_handler=CustomModelHandler,
post_handler=MultiClassificationPostHandler)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 基于PaddleNLP SimpleServing 的服务化部署

## 目录
- [环境准备](#环境准备)
- [Server启动服务](#Server服务启动)
- [其他参数设置](#其他参数设置)

## 环境准备
使用有SimpleServing功能的PaddleNLP版本
```shell
pip install paddlenlp --upgrade
```
## Server服务启动
### 分类任务启动
#### 启动 分类 Server 服务
```bash
paddlenlp server server:app --host 0.0.0.0 --port 8189
```

#### 分类任务发送服务
```bash
python client.py
```

## 其他参数设置
可以在client端设置 `max_seq_len`, `batch_size`, `prob_limit` 参数
```python
data = {
'data': {
'text': texts,
},
'parameters': {
'max_seq_len': args.max_seq_len,
'batch_size': args.batch_size,
'prob_limit': args.prob_limit
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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 argparse
import requests
import json

# yapf: disable
parser = argparse.ArgumentParser()
parser.add_argument("--max_seq_len", default=128, type=int, help="The maximum total input sequence length after tokenization.")
parser.add_argument("--batch_size", default=1, type=int, help="Batch size per GPU/CPU for predicting.")
parser.add_argument("--prob_limit", default=0.5, type=float, help="The limitation of probability for the label.")
args = parser.parse_args()
# yapf: enable

url = "http://0.0.0.0:8189/models/cls_multi_label"
headers = {"Content-Type": "application/json"}

if __name__ == "__main__":
texts = [
'原、被告另购置橱柜、碗架、电磁炉、电饭锅各一个归原告王某某所有。',
'于是原告到儿子就读的幼儿园进行探望,被告碰见后对原告破口大骂,还不让儿子叫原告妈妈,而叫被告现在的妻子做妈妈。',
'由我全额出资购买的联想台式电脑,我均依次放弃。'
]
data = {
'data': {
'text': texts,
},
'parameters': {
'max_seq_len': args.max_seq_len,
'batch_size': args.batch_size,
'prob_limit': args.prob_limit
}
}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
print(json.loads(r.text))
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.

from paddlenlp import SimpleServer
from paddlenlp.server import CustomModelHandler, MultiLabelClassificationPostHandler

app = SimpleServer()
app.register('cls_multi_label',
model_path="../../export",
tokenizer_name='ernie-3.0-medium-zh',
model_handler=CustomModelHandler,
post_handler=MultiLabelClassificationPostHandler)
11 changes: 11 additions & 0 deletions model_zoo/ernie-health/cblue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,22 @@ $ python train_spo.py --batch_size 12 --max_seq_length 300 --learning_rate 6e-5
使用动态图训练结束之后,还可以将动态图参数导出成静态图参数,用于部署推理等,具体代码见export_model.py。静态图参数保存在`output_path`指定路径中。

运行方式:
1. 分类任务静态图模型导出
```shell
python export_model.py --train_dataset CHIP-CDN-2C --params_path=./checkpoint/model_900/ --output_path=./export
```

2. SPO任务静态图模型导出
```shell
python export_model.py --train_dataset CMeIE --params_path=./checkpoint/model_900/ --output_path=./export
```

3. NER任务静态图模型导出
```shell
python export_model.py --train_dataset CMeEE --params_path=./checkpoint/model_1500/ --output_path=./export
```

**NOTICE**: train_dataset分类任务选择填上训练数据集名称,params_path选择最好参数的模型的路径。

[1] CBLUE: A Chinese Biomedical Language Understanding Evaluation Benchmark [pdf](https://arxiv.org/abs/2106.08087) [git](https://github.com/CBLUEbenchmark/CBLUE) [web](https://tianchi.aliyun.com/specials/promotion/2021chinesemedicalnlpleaderboardchallenge)

Expand Down
Loading

0 comments on commit f12a0d8

Please sign in to comment.