Step By Step
1、直接使用镜像获取Python环境
2、容器配置
3、镜像构建与上传
4、基于构建镜像创建EAS服务
5、Java SDK调用服务
一、直接使用镜像获取Python环境
- 1.1 如果未安装Docker,请参考链接先安装Docker环境
- 1.2 run命令进入容器
sudo docker run -ti registry.cn-shanghai.aliyuncs.com/eas/eas-python-base-image:py3.6-allspark-0.8
二、容器配置
- 2.1 删除app.py,下载pmml文件
- 2.2 docker中安装:sklearn-pmml-model
ENV/bin/pip install sklearn-pmml-model
三、镜像构建与上传
- 3.1 登陆阿里云镜像服务
sudo docker login --username=gts mubu**.cn-shanghai.cr.aliyuncs.com
- 3.2 获取容器ID
- 3.3 基于容器生成镜像
sudo docker commit d52f5f01607b mubu .cn-shanghai.cr.aliyuncs.com/taro/eas_pmml:v2
- 3.4 提交镜像到阿里云镜像仓库
sudo docker push mubu .cn-shanghai.cr.aliyuncs.com/taro/eas_pmml:v2
- 3.5 镜像仓库查看
- 3.6 设置镜像公开匿名拉取权限
四、基于构建镜像创建EAS服务
- 4.1 app.py代码
# -*- coding: utf-8 -*-
import allspark
import pandas as pd
import numpy as np
from sklearn_pmml_model.ensemble import PMMLForestClassifier
class MyProcessor(allspark.BaseProcessor):
""" MyProcessor is a example
you can send mesage like this to predict
curl -v http://127.0.0.1:8080/api/predict/service_name -d '2.1 105'
"""
def initialize(self):
""" load module, executed once at the start of the service
do service intialization and load models in this function.
"""
self.model = PMMLForestClassifier(pmml="randomForest.pmml")
def pre_proccess(self, data):
""" data format pre process
"""
x, y, z, w = data.split(b' ')
return float(x), float(y), float(z), float(w)
def post_process(self, data):
""" proccess after process
"""
return str(data).encode()
def process(self, data):
""" process the request data
"""
x, y, z, w = self.pre_proccess(data)
df = pd.DataFrame([[x, y, z, w]],columns=['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)'])
result = self.model.predict(df)
print(result)
return self.post_process(result), 200
if __name__ == '__main__':
# paramter worker_threads indicates concurrency of processing
runner = MyProcessor(worker_threads=10)
runner.run()
- 4.2 上传app.py到阿里云OSS存储,并设置权限为公共可读,获取下载地址
- 4.3 app.json部署文件
{
"name": "taro_docker_v2",
"processor_entry": "./app.py",
"processor_type": "python",
"processor_path": "https://taro******.oss-cn-shanghai.aliyuncs.com/eas_python_app/app.py",
"data_image": "mubu******.cn-shanghai.cr.aliyuncs.com/taro/eas_pmml:v1",
"metadata": {
"instance": 1,
"memory": 2000,
"cpu": 1
}
}
- 4.4 EASCMD部署服务
./eascmd64 create demo2.json
五、Java SDK调用服务
- 5.1 控制台查看部署服务
- 5.2 POSTMAN调用测试
- 5.3 pom.xml
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.3</version>
</dependency>
- 5.4 Java Code
import com.squareup.okhttp.*;
import java.io.IOException;
public class EASDEMO {
public static void main(String[] args) throws IOException {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody body = RequestBody.create(mediaType, "5.6 3.0 4.1 1.3");
Request request = new Request.Builder()
.url("http://172*********.cn-shanghai.pai-eas.aliyuncs.com/api/predict/taro_docker_v2")
.post(body)
.addHeader("authorization", "MjE2ZDNkMj*********")
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
}
}
- 5.5 测试结果
更多参考
安装 Docker
阿里云机器学习模型在线服务自定义Processor部署PMML模型(一)
使用Python开发自定义Processor
eascmd客户端工具