Java调用Python脚本获取预测模型服务的预测结果

最近项目需要通过Java程序获取到模型的预测结果后进行业务逻辑的决策,因此因此训练了一个模型并部署到了服务器,然后编写了Java程序通过调用Python脚本获取预测结果。

模型部署流程:TensorFlow模型部署

1、安装Anaconda

Linux安装Anaconda

2、编写py程序

调用预测模型接口进行结果预测

【注意这里py环境的问题,存在工具包没有导入的情况】

import requests
import json
import numpy as np
import sys

class NpEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        else:
            return super(NpEncoder, self).default(obj)

def getPredict(list):
    data = json.dumps({"instances":list},cls=NpEncoder)
    # headers = {"content-type":"applicaiton/json"}
    response = requests.post("http://192.168.152.111:8501/v1/models/model:predict",data=data)
    # print(response.text)
    embeddings = json.loads(response.text)['predictions']
    return embeddings[0][0]

if __name__ == '__main__':
    list = []
    for i in range(1,len(sys.argv)):
        list.append((float(sys.argv[i])))
        
    list = np.array(list).reshape(1,6)
    
    print(getPredict(list))

3、编写Java程序

通过Java调用python脚本,获取预测返回值

public static String getPredictInfo(NodeIndicatorsVo nodeIndicatorsVo){
        String cpuIdle = nodeIndicatorsVo.getCpuIdle();
        String diskFree = nodeIndicatorsVo.getDiskFree();
        String memFree = nodeIndicatorsVo.getMemFree();
        String AvgWaitTime = String.valueOf(nodeIndicatorsVo.getAvgWaitTime());
        String AvgDealTime = String.valueOf(nodeIndicatorsVo.getAvgDealTime());
        String Cap = String.valueOf(nodeIndicatorsVo.getCapacity());
        String result = null;
        try {
            String[] arg1 = new String[] { "/usr/local/anaconda3/envs/common/bin/python3.8", "/home/hl/model.py", cpuIdle, diskFree, memFree, AvgWaitTime, AvgDealTime, Cap };
            Process proc = Runtime.getRuntime().exec(arg1);// 执行py文件

            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            result = in.readLine();

            in.close();
            proc.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        if (result == null){
            return "NULL";
        }

        if (Float.parseFloat(result) > 0.5){
            return "1";
        }else {
            return "0";
        }
    }

你可能感兴趣的:(大数据,linux,spring,boot,python,深度学习)