Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Oct 24, 2022 10:46:36 AM
Oct 21, 2022 2:14:45 PM
In a previous article, I outlined the steps to POST an API call to realtime-predictions with Postman. In this article, we will look at how to do this in Python and send a file with records to predict against.
As before, we use iris.csv dataset, a common data science example. Variety is the target variable.
In our example, we print response.text, but this could also have been saved as a file. This is a powerful option for generating prediction results locally or in a location python has access to.
Additionally, with pandas, we can send a larger amount of records (from CSV to dataframe) in one POST call rather than having manual JSON entries with Postman.
Here is the code used above:
api_url = 'https://earlyaccess.us.qlik-stage.com/api/v1/automl-deployments/xxx/realtime-predictions'
api_key = 'xxxx'
df = pd.read_csv('C:\\tmp\\iris_pred.csv')
rows = df.to_dict('tight')['data']
headers = {
'Authorization': 'Bearer ' + api_key,
'Content-type': 'application/json',
'Accept': 'text/csv',
}
json_data = {
"rows": rows,
"schema": [
{"name": "sepal.length"},
{"name": "sepal.width"},
{"name": "petal.length"},
{"name": "petal.width"}
],
}
params = {
'includeNotPredictedReason': 'false',
'includeShap': 'true',
'includeSource': 'false'
}
response = requests.post(api_url, headers=headers, json=json_data, params=params)
print(response.text)
The information in this article is provided as-is and to be used at own discretion. Depending on tool(s) used, customization(s), and/or other factors ongoing support on the solution below may not be provided by Qlik Support.