Skip to content

Commit 0cb7fe9

Browse files
authored
support paddle serving http deploy for text classification (#3378)
* add_http_deploy
1 parent 5070840 commit 0cb7fe9

File tree

10 files changed

+288
-25
lines changed

10 files changed

+288
-25
lines changed

applications/text_classification/hierarchical/deploy/paddle_serving/README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,37 @@ I0727 06:50:34.993671 43126 naive_executor.cc:102] --- skip [linear_75.tmp_1],
153153
[OP Object] init success
154154
```
155155

156-
#### 启动client测试
156+
#### 启动rpc client测试
157157
注意执行客户端请求时关闭代理,并根据实际情况修改server_url地址(启动服务所在的机器)
158158
```shell
159159
python rpc_client.py
160160
```
161161
输出打印如下:
162162
```
163-
text: 请问木竭胶囊能同高血压药、氨糖同时服吗?
164-
label: 3,37
163+
text: 消失的“外企光环”,5月份在华裁员900余人,香饽饽变“臭”了
164+
label: 组织关系,组织关系##裁员
165165
--------------------
166-
text: 低压100*高压140*头涨,想吃点降压药。谢谢!
167-
label: 0
166+
text: 卡车超载致使跨桥侧翻,没那么简单
167+
label: 灾害/意外,灾害/意外##坍/垮塌
168168
--------------------
169-
text: 脑穿通畸形易发人群有哪些
170-
label: 0,9
169+
text: 金属卡扣安装不到位,上海乐扣乐扣贸易有限公司将召回捣碎器1162件
170+
label: 产品行为,产品行为##召回
171+
--------------------
172+
```
173+
#### 启动http client测试
174+
注意执行客户端请求时关闭代理,并根据实际情况修改server_url地址(启动服务所在的机器)
175+
```shell
176+
python http_client.py
177+
```
178+
输出打印如下:
179+
```
180+
text: 消失的“外企光环”,5月份在华裁员900余人,香饽饽变“臭”了
181+
label: 组织关系,组织关系##裁员
182+
--------------------
183+
text: 卡车超载致使跨桥侧翻,没那么简单
184+
label: 灾害/意外,灾害/意外##坍/垮塌
185+
--------------------
186+
text: 金属卡扣安装不到位,上海乐扣乐扣贸易有限公司将召回捣碎器1162件
187+
label: 产品行为,产品行为##召回
171188
--------------------
172189
```

applications/text_classification/hierarchical/deploy/paddle_serving/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#rpc端口, rpc_port和http_port不允许同时为空。当rpc_port为空且http_port不为空时,会自动将rpc_port设置为http_port+1
2-
rpc_port: 7688
2+
rpc_port: 18090
33

44
#http端口, rpc_port和http_port不允许同时为空。当rpc_port可用且http_port为空时,不自动生成http_port
5-
http_port: 9998
5+
http_port: 9878
66

77
#worker_num, 最大并发数。
88
#当build_dag_each_worker=True时, 框架会创建worker_num个进程,每个进程内构建grpcSever和DAG
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import numpy as np
15+
from numpy import array
16+
import requests
17+
import json
18+
import sys
19+
20+
21+
class Runner(object):
22+
23+
def __init__(
24+
self,
25+
server_url: str,
26+
):
27+
self.server_url = server_url
28+
29+
def Run(self, text, label_list):
30+
sentence = np.array([t.encode('utf-8') for t in text], dtype=np.object_)
31+
sentence = sentence.__repr__()
32+
data = {"key": ["sentence"], "value": [sentence]}
33+
data = json.dumps(data)
34+
35+
ret = requests.post(url=self.server_url, data=data)
36+
ret = ret.json()
37+
for t, l in zip(text, eval(ret['value'][0])):
38+
print("text: ", t)
39+
label = ','.join([label_list[int(ll)] for ll in l.split(',')])
40+
print("label: ", label)
41+
print("--------------------")
42+
return
43+
44+
45+
if __name__ == "__main__":
46+
server_url = "http://127.0.0.1:9878/seq_cls/prediction"
47+
runner = Runner(server_url)
48+
text = [
49+
"消失的“外企光环”,5月份在华裁员900余人,香饽饽变“臭”了?", "卡车超载致使跨桥侧翻,没那么简单",
50+
"金属卡扣安装不到位,上海乐扣乐扣贸易有限公司将召回捣碎器1162件"
51+
]
52+
label_list = [
53+
'交往', '交往##会见', '交往##感谢', '交往##探班', '交往##点赞', '交往##道歉', '产品行为',
54+
'产品行为##上映', '产品行为##下架', '产品行为##发布', '产品行为##召回', '产品行为##获奖', '人生',
55+
'人生##产子/女', '人生##出轨', '人生##分手', '人生##失联', '人生##婚礼', '人生##庆生', '人生##怀孕',
56+
'人生##死亡', '人生##求婚', '人生##离婚', '人生##结婚', '人生##订婚', '司法行为', '司法行为##举报',
57+
'司法行为##入狱', '司法行为##开庭', '司法行为##拘捕', '司法行为##立案', '司法行为##约谈', '司法行为##罚款',
58+
'司法行为##起诉', '灾害/意外', '灾害/意外##地震', '灾害/意外##坍/垮塌', '灾害/意外##坠机',
59+
'灾害/意外##洪灾', '灾害/意外##爆炸', '灾害/意外##袭击', '灾害/意外##起火', '灾害/意外##车祸', '竞赛行为',
60+
'竞赛行为##夺冠', '竞赛行为##晋级', '竞赛行为##禁赛', '竞赛行为##胜负', '竞赛行为##退役', '竞赛行为##退赛',
61+
'组织关系', '组织关系##停职', '组织关系##加盟', '组织关系##裁员', '组织关系##解散', '组织关系##解约',
62+
'组织关系##解雇', '组织关系##辞/离职', '组织关系##退出', '组织行为', '组织行为##开幕', '组织行为##游行',
63+
'组织行为##罢工', '组织行为##闭幕', '财经/交易', '财经/交易##上市', '财经/交易##出售/收购',
64+
'财经/交易##加息', '财经/交易##涨价', '财经/交易##涨停', '财经/交易##融资', '财经/交易##跌停',
65+
'财经/交易##降价', '财经/交易##降息'
66+
]
67+
runner.Run(text, label_list)

applications/text_classification/hierarchical/deploy/paddle_serving/rpc_client.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,37 @@ def __init__(
2626
self.client = PipelineClient()
2727
self.client.connect([server_url])
2828

29-
def Run(self, data):
29+
def Run(self, data, label_list):
3030
data = np.array([x.encode('utf-8') for x in data], dtype=np.object_)
3131
ret = self.client.predict(feed_dict={"sentence": data})
3232
for d, l, in zip(data, eval(ret.value[0])):
3333
print("text: ", d)
34-
print("label: ", l)
34+
label = ','.join([label_list[int(ll)] for ll in l.split(',')])
35+
print("label: ", label)
3536
print("--------------------")
3637
return
3738

3839

3940
if __name__ == "__main__":
40-
server_url = "127.0.0.1:7688"
41+
server_url = "127.0.0.1:18090"
4142
runner = Runner(server_url)
42-
texts = [
43+
text = [
4344
"消失的“外企光环”,5月份在华裁员900余人,香饽饽变“臭”了?", "卡车超载致使跨桥侧翻,没那么简单",
4445
"金属卡扣安装不到位,上海乐扣乐扣贸易有限公司将召回捣碎器1162件"
4546
]
46-
runner.Run(texts)
47+
label_list = [
48+
'交往', '交往##会见', '交往##感谢', '交往##探班', '交往##点赞', '交往##道歉', '产品行为',
49+
'产品行为##上映', '产品行为##下架', '产品行为##发布', '产品行为##召回', '产品行为##获奖', '人生',
50+
'人生##产子/女', '人生##出轨', '人生##分手', '人生##失联', '人生##婚礼', '人生##庆生', '人生##怀孕',
51+
'人生##死亡', '人生##求婚', '人生##离婚', '人生##结婚', '人生##订婚', '司法行为', '司法行为##举报',
52+
'司法行为##入狱', '司法行为##开庭', '司法行为##拘捕', '司法行为##立案', '司法行为##约谈', '司法行为##罚款',
53+
'司法行为##起诉', '灾害/意外', '灾害/意外##地震', '灾害/意外##坍/垮塌', '灾害/意外##坠机',
54+
'灾害/意外##洪灾', '灾害/意外##爆炸', '灾害/意外##袭击', '灾害/意外##起火', '灾害/意外##车祸', '竞赛行为',
55+
'竞赛行为##夺冠', '竞赛行为##晋级', '竞赛行为##禁赛', '竞赛行为##胜负', '竞赛行为##退役', '竞赛行为##退赛',
56+
'组织关系', '组织关系##停职', '组织关系##加盟', '组织关系##裁员', '组织关系##解散', '组织关系##解约',
57+
'组织关系##解雇', '组织关系##辞/离职', '组织关系##退出', '组织行为', '组织行为##开幕', '组织行为##游行',
58+
'组织行为##罢工', '组织行为##闭幕', '财经/交易', '财经/交易##上市', '财经/交易##出售/收购',
59+
'财经/交易##加息', '财经/交易##涨价', '财经/交易##涨停', '财经/交易##融资', '财经/交易##跌停',
60+
'财经/交易##降价', '财经/交易##降息'
61+
]
62+
runner.Run(text, label_list)

applications/text_classification/multi_class/deploy/paddle_serving/README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ I0628 09:12:30.787542 74305 naive_executor.cc:102] --- skip [linear_147.tmp_1],
149149
150150
```
151151

152-
#### 启动client测试
152+
#### 启动rpc client测试
153153
注意执行客户端请求时关闭代理,并根据实际情况修改server_url地址(启动服务所在的机器)
154154
```shell
155155
python rpc_client.py
@@ -173,3 +173,28 @@ label: 病因分析
173173
--------------------
174174
175175
```
176+
177+
#### 启动http client测试
178+
注意执行客户端请求时关闭代理,并根据实际情况修改server_url地址(启动服务所在的机器)
179+
```shell
180+
python http_client.py
181+
```
182+
输出打印如下:
183+
```
184+
data: 黑苦荞茶的功效与作用及食用方法
185+
label: 功效作用
186+
--------------------
187+
data: 交界痣会凸起吗
188+
label: 疾病表述
189+
--------------------
190+
data: 检查是否能怀孕挂什么科
191+
label: 就医建议
192+
--------------------
193+
data: 鱼油怎么吃咬破吃还是直接咽下去
194+
label: 其他
195+
--------------------
196+
data: 幼儿挑食的生理原因是
197+
label: 病因分析
198+
--------------------
199+
200+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import numpy as np
15+
from numpy import array
16+
import requests
17+
import json
18+
import sys
19+
20+
21+
class Runner(object):
22+
23+
def __init__(
24+
self,
25+
server_url: str,
26+
):
27+
self.server_url = server_url
28+
29+
def Run(self, text, label_list):
30+
sentence = np.array([t.encode('utf-8') for t in text], dtype=np.object_)
31+
sentence = sentence.__repr__()
32+
data = {"key": ["sentence"], "value": [sentence]}
33+
data = json.dumps(data)
34+
35+
ret = requests.post(url=self.server_url, data=data)
36+
ret = ret.json()
37+
for t, l in zip(text, eval(ret['value'][0])):
38+
print("text: ", t)
39+
print("label: ", label_list[l])
40+
print("--------------------")
41+
return
42+
43+
44+
if __name__ == "__main__":
45+
server_url = "http://127.0.0.1:9878/seq_cls/prediction"
46+
runner = Runner(server_url)
47+
text = [
48+
"黑苦荞茶的功效与作用及食用方法", "交界痣会凸起吗", "检查是否能怀孕挂什么科", "鱼油怎么吃咬破吃还是直接咽下去",
49+
"幼儿挑食的生理原因是"
50+
]
51+
label_list = [
52+
'病情诊断', '治疗方案', '病因分析', '指标解读', '就医建议', '疾病表述', '后果表述', '注意事项', '功效作用',
53+
'医疗费用', '其他'
54+
]
55+
runner.Run(text, label_list)

applications/text_classification/multi_label/deploy/paddle_serving/README.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,41 @@ W0625 16:45:40.312942 40218 gpu_context.cc:278] Please NOTE: device: 3, GPU Comp
150150
W0625 16:45:40.316538 40218 gpu_context.cc:306] device: 3, cuDNN Version: 8.1.
151151
```
152152

153-
#### 启动client测试
153+
#### 启动rpc client测试
154154
注意执行客户端请求时关闭代理,并根据实际情况修改server_url地址(启动服务所在的机器)
155155
```shell
156156
python rpc_client.py
157157
```
158158
输出打印如下:
159159
```
160160
data: 五松新村房屋是被告婚前购买的;
161-
label: 10
161+
label: 婚前个人财产
162162
--------------------
163163
data: 被告于2016年3月将车牌号为皖B×××××出售了2.7万元,被告通过原告偿还了齐荷花人民币2.6万元,原、被告尚欠齐荷花2万元。
164-
label: 2,9
164+
label: 有夫妻共同财产,有夫妻共同债务
165165
--------------------
166166
data: 2、判令被告返还借婚姻索取的现金33万元,婚前个人存款10万元;
167-
label: 10
167+
label: 婚前个人财产
168168
--------------------
169169
data: 一、判决原告于某某与被告杨某某离婚;
170-
label: 8,11
170+
label: 准予离婚,法定离婚
171+
```
172+
#### 启动http client测试
173+
注意执行客户端请求时关闭代理,并根据实际情况修改server_url地址(启动服务所在的机器)
174+
```shell
175+
python http_client.py
176+
```
177+
输出打印如下:
178+
```
179+
data: 五松新村房屋是被告婚前购买的;
180+
label: 婚前个人财产
181+
--------------------
182+
data: 被告于2016年3月将车牌号为皖B×××××出售了2.7万元,被告通过原告偿还了齐荷花人民币2.6万元,原、被告尚欠齐荷花2万元。
183+
label: 有夫妻共同财产,有夫妻共同债务
184+
--------------------
185+
data: 2、判令被告返还借婚姻索取的现金33万元,婚前个人存款10万元;
186+
label: 婚前个人财产
187+
--------------------
188+
data: 一、判决原告于某某与被告杨某某离婚;
189+
label: 准予离婚,法定离婚
171190
```

applications/text_classification/multi_label/deploy/paddle_serving/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
rpc_port: 18090
33

44
#http端口, rpc_port和http_port不允许同时为空。当rpc_port可用且http_port为空时,不自动生成http_port
5-
http_port: 5594
5+
http_port: 9878
66

77
#worker_num, 最大并发数。
88
#当build_dag_each_worker=True时, 框架会创建worker_num个进程,每个进程内构建grpcSever和DAG
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import numpy as np
15+
from numpy import array
16+
import requests
17+
import json
18+
import sys
19+
20+
21+
class Runner(object):
22+
23+
def __init__(
24+
self,
25+
server_url: str,
26+
):
27+
self.server_url = server_url
28+
29+
def Run(self, text, label_list):
30+
sentence = np.array([t.encode('utf-8') for t in text], dtype=np.object_)
31+
sentence = sentence.__repr__()
32+
data = {"key": ["sentence"], "value": [sentence]}
33+
data = json.dumps(data)
34+
35+
ret = requests.post(url=self.server_url, data=data)
36+
ret = ret.json()
37+
for t, l in zip(text, eval(ret['value'][0])):
38+
print("text: ", t)
39+
label = ','.join([label_list[int(ll)] for ll in l.split(',')])
40+
print("label: ", label)
41+
print("--------------------")
42+
return
43+
44+
45+
if __name__ == "__main__":
46+
server_url = "http://127.0.0.1:9878/seq_cls/prediction"
47+
runner = Runner(server_url)
48+
text = [
49+
"五松新村房屋是被告婚前购买的;",
50+
"被告于2016年3月将车牌号为皖B×××××出售了2.7万元,被告通过原告偿还了齐荷花人民币2.6万元,原、被告尚欠齐荷花2万元。",
51+
"2、判令被告返还借婚姻索取的现金33万元,婚前个人存款10万元;", "一、判决原告于某某与被告杨某某离婚;"
52+
]
53+
label_list = [
54+
'婚后有子女', '限制行为能力子女抚养', '有夫妻共同财产', '支付抚养费', '不动产分割', '婚后分居', '二次起诉离婚',
55+
'按月给付抚养费', '准予离婚', '有夫妻共同债务', '婚前个人财产', '法定离婚', '不履行家庭义务', '存在非婚生子',
56+
'适当帮助', '不履行离婚协议', '损害赔偿', '感情不和分居满二年', '子女随非抚养权人生活', '婚后个人财产'
57+
]
58+
runner.Run(text, label_list)

0 commit comments

Comments
 (0)