Python 2.7.5
该文档以Python2为例展示如何在Docker中运行Paddle Serving,您也可以通过将python
更换成python3
来用Python3运行相关命令。
docker pull hub.baidubce.com/paddlepaddle/serving:latest
docker run -p 9292:9292 --name test -dit hub.baidubce.com/paddlepaddle/serving:latest
docker exec -it test bash
-p
选项是为了将容器的9292
端口映射到宿主机的9292
端口。
为了减小镜像的体积,镜像中没有安装Serving包,要执行下面命令进行安装。
/usr/bin/python -m pip install --upgrade pip
pip install paddle-serving-server
您可能需要使用国内镜像源(例如清华源)来加速下载。
pip install paddle-serving-server -i https://pypi.tuna.tsinghua.edu.cn/simple
手动安装sentencepiece
pip install sentencepiece==0.1.82 (直接pip install paddle-serving-server遇到问题,手动执行命令安装sentencepiece)
在rpc方式调用的时候,需要引入“ from paddle_serving_client import Client”
在此情况下安装paddle-serving-client。
pip install paddle-serving-client==0.3.2
通过下面命令获取训练好的Boston房价预估模型:
wget --no-check-certificate https://paddle-serving.bj.bcebos.com/uci_housing.tar.gz
tar -xzf uci_housing.tar.gz
测试HTTP服务
在Server端(容器内)运行:
python -m paddle_serving_server.serve --model uci_housing_model --thread 10 --port 9292 --name uci >std.log 2>err.log &
在Client端(容器内或容器外)运行:
curl -H "Content-Type:application/json" -X POST -d '{"feed":{"x": [0.0137, -0.1136, 0.2553, -0.0692, 0.0582, -0.0727, -0.1583, -0.0584, 0.6283, 0.4919, 0.1856, 0.0795, -0.0332]}, "fetch":["price"]}' http://127.0.0.1:9292/uci/prediction
测试RPC服务
在Server端(容器内)运行:
python -m paddle_serving_server.serve --model uci_housing_model --thread 10 --port 9292 >std.log 2>err.log &
在Client端(容器内或容器外,需要安装paddle-serving-client
包)运行下面Python代码:
from paddle_serving_client import Client
client = Client()
client.load_client_config("uci_housing_client/serving_client_conf.prototxt")
client.connect(["127.0.0.1:9292"])
data = [0.0137, -0.1136, 0.2553, -0.0692, 0.0582, -0.0727,
-0.1583, -0.0584, 0.6283, 0.4919, 0.1856, 0.0795, -0.0332]
fetch_map = client.predict(feed={
"x": data}, fetch=["price"])
print(fetch_map)
https://github.com/PaddlePaddle/Serving/blob/develop/doc/RUN_IN_DOCKER_CN.md