情感分析服务化部署之SimpleServing
[toc]
# 官方介绍文档
- https://github.com/PaddlePaddle/PaddleNLP/tree/develop/applications/sentiment_analysis/unified_sentiment_extraction (opens new window)
- https://github.com/PaddlePaddle/PaddleNLP/blob/develop/docs/server.md (opens new window)
- https://github.com/PaddlePaddle/PaddleNLP/tree/develop/applications/text_classification/multi_class/deploy/simple_serving (opens new window)
# 启动服务端
由于我机器paddlenlp
没有配置环境变量,我们使用绝对路径运行/home/aistudio/paddlenlp2.6.0/bin/paddlenlp
,默认启动脚本在安装包的bin/
目录下
进入deploy目录
cd /home/aistudio/PaddleNLP/applications/sentiment_analysis/unified_sentiment_extraction/deploy
1
deploy讲解,该目录下有两个文件,一个server.py 一个client.py
server.py
命令中启动的服务就是这个文件from paddlenlp import SimpleServer, Taskflow # The schema changed to your defined schema schema = [{"评价维度": ["观点词", "情感倾向[正向,负向,未提及]"]}] # define taskflow to perform sentiment analysis # 这里可以添加参数,指定我们自己模型的位置,task_path="/opt/model/nlp/model_best" senta = Taskflow("sentiment_analysis", schema=schema, model="uie-senta-base") # define your server app = SimpleServer() app.register_taskflow("taskflow/senta", senta)
1
2
3
4
5
6
7
8
9
10
client.py
客户端调用Demoimport json import requests url = "http://0.0.0.0:8189/taskflow/senta" headers = {"Content-Type": "application/json"} texts = ["蛋糕味道不错,店家的服务也很热情"] data = { "data": { "text": texts, } } r = requests.post(url=url, headers=headers, data=json.dumps(data)) datas = json.loads(r.text) print(datas)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 启动命令
/home/aistudio/paddlenlp2.6.0/bin/paddlenlp server server:app --workers 1 --host 0.0.0.0 --port 8189
1
我们也可以不进入deploy目录运行,通过--app_dir
目录指定目录即可
/home/aistudio/paddlenlp2.6.0/bin/paddlenlp server server:app --workers 1 --host 0.0.0.0 --port 8189 --app_dir /home/aistudio/PaddleNLP/applications/sentiment_analysis/unified_sentiment_extraction/deploy
1
启动成功内如如下
$ /home/aistudio/paddlenlp2.6.0/bin/paddlenlp server server:app --workers 1 --host 0.0.0.0 --port 8189 --app_dir /home/aistudio/PaddleNLP/applications/sentiment_analysis/unified_sentiment_extraction/deploy
[2023-09-13 15:45:45,991] [ INFO] - starting to PaddleNLP SimpleServer...
[2023-09-13 15:45:45,991] [ INFO] - The PaddleNLP SimpleServer is starting, backend component uvicorn arguments as follows:
[2023-09-13 15:45:45,991] [ INFO] - the starting argument [host]=0.0.0.0
[2023-09-13 15:45:45,992] [ INFO] - the starting argument [port]=8189
[2023-09-13 15:45:45,992] [ INFO] - the starting argument [log_level]=None
[2023-09-13 15:45:45,992] [ INFO] - the starting argument [workers]=1
[2023-09-13 15:45:45,992] [ INFO] - the starting argument [limit_concurrency]=None
[2023-09-13 15:45:45,992] [ INFO] - the starting argument [limit_max_requests]=None
[2023-09-13 15:45:45,992] [ INFO] - the starting argument [timeout_keep_alive]=15
[2023-09-13 15:45:45,992] [ INFO] - the starting argument [app_dir]=/home/aistudio/PaddleNLP/applications/sentiment_analysis/unified_sentiment_extraction/deploy
[2023-09-13 15:45:45,992] [ INFO] - the starting argument [reload]=False
[2023-09-13 15:45:52,612] [ INFO] - We are using <class 'paddlenlp.transformers.ernie.tokenizer.ErnieTokenizer'> to load '/home/aistudio/.paddlenlp/taskflow/sentiment_analysis/uie-senta-base'.
[2023-09-13 15:45:52,642] [ INFO] - Taskflow request [path]=/taskflow/senta is genereated.
INFO: Started server process [1964]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8189 (Press CTRL+C to quit)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 参数说明
--host: 启动服务化的IP地址,通常可以设置成 0.0.0.0
--port:启动服务化的网络端口
--workers: 接收服务化的进程数,默认为1
--log_level:服务化输出日志的级别,默认为 info 级别
--limit_concurrency:服务化能接受的并发数目,默认为None, 没有限制
--timeout_keep_alive:保持服务化连接的时间,默认为15s
--app_dir:服务化本地的路径,默认为服务化启动的位置
--reload: 当 app_dir的服务化相关配置和代码发生变化时,是否重启server,默认为False
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 客户端调用
# 多条数据调用
在 texts 传入多条数据即可,返回的数据也是一个数组,对应上数组位置即可
import json
import requests
url = "http://0.0.0.0:8189/taskflow/senta"
headers = {"Content-Type": "application/json"}
texts = ["蛋糕味道不错,店家的服务也很热情","服务员不热情,服务态度不好,下次不来了"]
data = {
"data": {
"text": texts,
}
}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
datas = json.loads(r.text)
print(datas)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
结果
{
"result": [
{
"评价维度": [
{
"end": 4,
"probability": 0.8804405736499632,
"relations": {
"情感倾向[正向,负向,未提及]": [
{
"probability": 0.9998830584822258,
"text": "正向"
}
],
"观点词": [
{
"end": 6,
"probability": 0.9943114289210939,
"start": 4,
"text": "不错"
}
]
},
"start": 2,
"text": "味道"
},
{
"end": 12,
"probability": 0.995827464498646,
"relations": {
"情感倾向[正向,负向,未提及]": [
{
"probability": 0.9999358663076237,
"text": "正向"
}
],
"观点词": [
{
"end": 16,
"probability": 0.9947389572457865,
"start": 14,
"text": "热情"
}
]
},
"start": 10,
"text": "服务"
}
]
},
{
"评价维度": [
{
"end": 2,
"probability": 0.9997505104257698,
"relations": {
"情感倾向[正向,负向,未提及]": [
{
"probability": 0.9999443299277857,
"text": "正向"
}
],
"观点词": [
{
"end": 4,
"probability": 0.999357408362016,
"start": 3,
"text": "好"
}
]
},
"start": 0,
"text": "蛋糕"
},
{
"end": 12,
"probability": 0.9861312855031521,
"relations": {
"情感倾向[正向,负向,未提及]": [
{
"probability": 0.9996660628408236,
"text": "负向"
}
],
"观点词": [
{
"end": 14,
"probability": 0.3098063392794046,
"start": 12,
"text": "不好"
}
]
},
"start": 10,
"text": "服务"
}
]
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 自定义schema
调用
默认我们再server.py代码中指定了schema
,同时在Client调用时也可以支持定义 schema
传入到client请求中,可以快速切换 schema
这
import json
import requests
url = "http://0.0.0.0:8189/taskflow/senta"
headers = {"Content-Type": "application/json"}
texts = ["蛋糕味道不错,店家的服务也很热情","服务员不热情,服务态度不好,下次不来了"]
data = {
"data": {
"text": texts,
},
"parameters": {
"schema": ["观点词"] # 自定义schema
}
}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
datas = json.loads(r.text)
print(datas)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
返回结果
{
"result": [
{
"观点词": [
{
"end": 16,
"probability": 0.9935872307286964,
"start": 14,
"text": "热情"
},
{
"end": 6,
"probability": 0.9989313489162441,
"start": 4,
"text": "不错"
}
]
},
{
"观点词": [
{
"end": 13,
"probability": 0.9952653178475401,
"start": 11,
"text": "不好"
},
{
"end": 6,
"probability": 0.5518675358377116,
"start": 3,
"text": "不热情"
}
]
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
上次更新: 2023/12/13, 14:39:09