send post request using curl to mlflow api to multiple records

1.2k Views Asked by At

I have served an mlflow model and am sending POST requests in this format:

curl -X POST -H "Content-Type:application/json; format=pandas-split" 
--data '{"columns":["alcohol", "chlorides", "citric acid", "density", 
"fixed acidity", "free sulfur dioxide", "pH", "residual sugar", "sulphates", 
"total sulfur dioxide", "volatile acidity"],"data":[[12.8, 0.029, 0.48, 0.98, 
6.2, 29, 3.33, 1.2, 0.39, 75, 0.66]]}' 
http://127.0.0.1:1234/invocations

It is getting scored. However for my particular project, the input to rest api for scoring will always be multiple records in dataframe/csv format instead of a single record. Can someone point me to how to achieve this ?

2

There are 2 best solutions below

0
On
{
    "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.029,
            0.48,
            0.98,
            6.2,
            29.0,
            3.33,
            1.2,
            0.39,
            75.0,
            0.66
        ]
    ]
}

To generate json use pandas:

import pandas as pd

columns = ["alcohol", "chlorides", "citric acid", "density", 
"fixed acidity", "free sulfur dioxide", "pH", "residual sugar", "sulphates", 
"total sulfur dioxide", "volatile acidity"]
data = [[12.8, 0.029, 0.48, 0.98, 
6.2, 29, 3.33, 1.2, 0.39, 75, 0.66], [12.8, 0.029, 0.48, 0.98, 
6.2, 29, 3.33, 1.2, 0.39, 75, 0.66]]

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

This worked:

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}')