情感分析服务化部署之构建Docker服务
[toc]
# 基础信息
阿里云镜像服务:https://cr.console.aliyun.com/cn-hangzhou/instances (opens new window)
# 目的
将情感分析服务直接Docker化,一键部署
# 步骤
# docker下载镜像
docker pull ubuntu
1
[root@bigdata-24-251 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ocr 2.5 214a28072b2d 10 days ago 5.72GB
registry.cn-hangzhou.aliyuncs.com/jast-zsh/ocr 2.5 214a28072b2d 10 days ago 5.72GB
ocr v2.5 b3c53723a4e0 10 days ago 5.72GB
ubuntu latest c6b84b685f35 4 weeks ago 77.8MB
1
2
3
4
5
6
2
3
4
5
6
进入容器
$ docker run -it ubuntu /bin/bash
1
进入根目录
$ cd
1
安装wget
$ apt-get update && apt-get install wget
$ wget -b https://repo.anaconda.com/archive/Anaconda3-2023.07-2-Linux-x86_64.sh && tail -f wget-log
$ chmod 755 Anaconda3-2023.07-2-Linux-x86_64.sh
$ sh Anaconda3-2023.07-2-Linux-x86_64.sh
...
Please, press ENTER to continue
>>> # 回车
Do you accept the license terms? [yes|no]
[no] >>> # 输入yes
Anaconda3 will now be installed into this location:
/root/anaconda3
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
[/root/anaconda3] >>> # 回车
...
Do you wish the installer to initialize Anaconda3
by running conda init? [yes|no]
[no] >>> # 输入yes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
安装依赖
apt-get install -y vim
1
# 新增下面配置
vim /etc/profile
export PATH=$PATH:/root/anaconda3/bin
1
2
3
2
3
安装激活环境
source activate
conda create --name paddlenlp python=3.8 --channel https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda activate paddlenlp
1
2
3
2
3
安装paddlenlp的python依赖
pip install --force-reinstall paddlepaddle==2.5.0rc1 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install --use-pep517 paddlenlp==2.6.0 -i https://mirror.baidu.com/pypi/simple
pip install wordcloud==1.8.2.2 -i https://mirror.baidu.com/pypi/simple
1
2
3
2
3
下载源码
$ apt-get install git
$ git clone -b v2.6.0rc https://gitee.com/paddlepaddle/PaddleNLP.git
$ mv PaddleNLP/ /opt/
$ cd /opt/
1
2
3
4
2
3
4
到此相关依赖都安装好了。如果使用官方基础模型,可以直接打包了
# 提交镜像
commit提交镜像
# 查看我们刚刚运行的容器 CONTAINER ID
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b74fd7e81c4 ubuntu "/bin/bash" 27 minutes ago Up 27 minutes awesome_satoshi
# 对容器进行 commit
$ docker commit -a "jast" -m "基础模型" 0b74fd7e81c4 nlp-basic:1.0.0
ee35087cca5c9c169b7bba61d5d6fe7eb3736dc554037d6a128245c36a855a7c
1
2
3
4
5
6
7
2
3
4
5
6
7
查看我们commit的镜像
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nlp-basic v1.0.1 d3b936ced524 16 minutes ago 9.7GB
1
2
3
2
3
tag镜像
# 查看容器IMAGE ID
docker images
# 容器打上TAG
docker tag ee35087cca5c registry.cn-hangzhou.aliyuncs.com/jast-zsh/nlp:1.0.0
# 登录阿里云镜像服务
$ docker login --username=xxxx registry.cn-hangzhou.aliyuncs.com
# 提交镜像到阿里云镜像服务
$ docker push registry.cn-hangzhou.aliyuncs.com/jast-zsh/nlp:1.0.0
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 下载镜像并应用
docker pull registry.cn-hangzhou.aliyuncs.com/jast-zsh/nlp:1.0.0
1
下载完成后,通过docker images
查看
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/jast-zsh/nlp 1.0.0 ee35087cca5c 19 hours ago 9.7GB
1
2
3
2
3
运行进入容器
$ docker run -it ee35087cca5c /bin/bash
root@84161121405b:~# source /etc/profile
root@84161121405b:~# source activate
(base) root@84161121405b:~# conda activate paddlenlp
(paddlenlp) root@84161121405b:~#
1
2
3
4
5
2
3
4
5
验证安装的版本
(paddlenlp) root@84161121405b:~# pip list |grep paddle
paddle-bfloat 0.1.7
paddle2onnx 1.0.9
paddlefsl 1.1.0
paddlenlp 2.6.0
paddlepaddle 2.5.0rc1
1
2
3
4
5
6
2
3
4
5
6
执行Python代码,测试默认模型
from paddlenlp import Taskflow
schema = [{'评价维度': ['观点词', '情感倾向[正向,负向,未提及]']}]
senta = Taskflow("sentiment_analysis", model="uie-senta-base", schema=schema)
senta("文本内容")
1
2
3
4
2
3
4
# 挂载模型目录
实际应用中我们会需要指定我们自己的模型,通过 -v
指定即可
docker run -it -v /opt/model/nlp:/opt/model/nlp ee35087cca5c /bin/bash
1
# 端口映射
容器启动服务后需要在服务器访问,这里我们通过- p
进行端口映射
docker run -it -p 18889:18889 -v /opt/model/nlp:/opt/model/nlp ee35087cca5c /bin/bash
1
# 指定运行容器名称
docker run -it --name="nlp" -p 18889:18889 -v /opt/model/nlp:/opt/model/nlp ee35087cca5c /bin/bash
1
# 启动服务
先运行服务
docker run -it --name="nlp" -p 18889:18889 -v /opt/model/nlp:/opt/model/nlp ee35087cca5c /bin/bash
1
进入目录
cd /opt/PaddleNLP/applications/sentiment_analysis/unified_sentiment_extraction/deploy
1
source一下环境
source /etc/profile
source activate
conda activate paddlenlp
1
2
3
2
3
# 启动官方模型服务
paddlenlp server server:app --workers 1 --host 0.0.0.0 --port 18889
1
成功日志如下
INFO: Started server process [98]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:18889 (Press CTRL+C to quit)
1
2
3
4
2
3
4
发送请求测试
import json
import requests
url = "http://172.16.24.251:18889/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': [{'观点词': [{'text': '热情', 'start': 14, 'end': 16, 'probability': 0.9935871119022295}, {'text': '不错', 'start': 4, 'end': 6, 'probability': 0.9989314085019032}]}, {'观点词': [{'text': '不热情', 'start': 3, 'end': 6, 'probability': 0.5518652359279201}, {'text': '不好', 'start': 11, 'end': 13, 'probability': 0.9952652583815258}]}]}
1
# 启动自定义模型服务
上面我们挂载了目录/opt/model/nlp
,将模型上传到该目录下
修改/opt/PaddleNLP/applications/sentiment_analysis/unified_sentiment_extraction/deploy
目录下的server.py
文件,设置模型目录
senta = Taskflow("sentiment_analysis", schema=schema, model="uie-senta-base")
修改为
senta = Taskflow("sentiment_analysis", model="uie-senta-base", schema=schema, task_path="/opt/model/nlp/model_best")
1
2
3
2
3
启动服务paddlenlp server server:app --workers 1 --host 0.0.0.0 --port 18889
即可
# 创建启动脚本
在用户目录下创建run.sh启动脚本,内容为
cd /opt/PaddleNLP/applications/sentiment_analysis/unified_sentiment_extraction/deploy
nohup paddlenlp server server:app --workers 1 --host 0.0.0.0 --port 18889 > /root/nlp-server.log &
1
2
2
运行sh run.sh
即可启动
上次更新: 2023/12/13, 14:39:09