使用 curl 将 post 请求发送到 mlflow api 到多个记录

问题描述

我已经提供了一个 mlflow 模型并且正在以这种格式发送 POST 请求:

self.setCentralWidget(someWidget)

它正在得分。但是,对于我的特定项目,用于评分的 rest api 的输入将始终是 dataframe/csv 格式的多个记录,而不是单个记录。有人可以指出我如何实现这一目标吗?

解决方法

{
    "columns": [
        "alcohol","chlorides","citric acid","density","fixed acidity","free sulfur dioxide","pH","residual sugar","sulphates","total sulfur dioxide","volatile acidity"
    ],"index": [
        0,1
    ],"data": [
        [
            12.8,0.029,0.48,0.98,6.2,29.0,3.33,1.2,0.39,75.0,0.66
        ],[
            12.8,0.66
        ]
    ]
}

使用pandas生成json:

import pandas as pd

columns = ["alcohol","volatile acidity"]
data = [[12.8,29,75,0.66],[12.8,0.66]]

df = pd.DataFrame(data,columns=columns)
json = df.to_json(orient="split")
print(json)
,

这有效:

import requests
host = 'localhost'
port = '8001'
url = f'http://{host}:{port}/invocations'
headers = {'Content-Type': 'application/json',}
http_data = test_df.to_json(orient='split')
r = requests.post(url=url,headers=headers,data=http_data)
print(f'Predictions: {r.text}')