Test the deployment from a Python script

It is convenient to use the deployment API from within a Python script to visualize results, compute additional metrics and so on.

Before you try, remember to check the limitations stated in Deploy to API limitations.

Python script example with deployed mnist model

There are several ways to do this. In this example, we show a method that uses the requests and requests_toolbelt libraries.

This example shows how to classify a single image based on the MNIST tutorial Deploy an operational AI model. In this tutorial we built, trained and deployed a model that now is ready to be tested.

The Deployment view of this deployed model looks like this:

Deployed MNIST example

Download image

You can download this image and save it as Six.png in a folder on your computer.


Python script

import requests
from requests_toolbelt import MultipartEncoder

Define a short function to make a POST request to the API.

def get_prediction(input_dict, token, url):
	payload = MultipartEncoder(input_dict)
	r = requests.post(url,
                  	headers={'Content-Type': payload.content_type},
                  	auth=(token, ''))

Make a dictionary containing the name and file path of the image you want to classify. The name should be the Input feature name you see on the Deployment view.

If you try this code, replace '/PATH/TO/YOUR/FILE/Six.png' with the actual path to the file.

image, path = 'Image', '/PATH/TO/YOUR/FILE/Six.png'
input_dict = {image: (path, open(path, 'rb'), 'image/png')}

Copy the API token and URL in the Deployment view.

token = '908453eb-8aed-4947-8d01-6a4923b366eb'
url = 'https://a.peltarion.com/deployment/886e41a5-8801-4e76-ae0e-19159a5e9023/forward'

Call the function to get a prediction.

pred = get_prediction(input_dict, token, url)


You’ll get this result. ‘6’ gets the highest value, 0.99453683. This means that the model predicts the image to be a ‘6’.

Results of number 6 prediction

More than one input

If you have more than one input, you add those to the input_dict dictionary that is sent to get_prediction. Images should follow the format above. So if you have two images, one PNG and one JPG, that you want to send to the API, the dictionary should look something like this:

img_dict = {feature_name1: (img_path1, open(img_path1, 'rb'), 'image/png'),
            feature_name2: (img_path2, open(img_path2, 'rb'), 'image/jpg')}

String and numerical values as input

If the inputs are not images but strings or numerical values, their entries in the dictionary will look something like this:

num_dict = {feature_name3: 122,
            feature_name4: 'a string'}

You can combine image and numerical inputs according to the above formats in a single dictionary.

The quotes that surround the feature value determines if the Platform will interpret it as a numerical value or a string. When the feature of a dataset is of float- or integer type, you must omit the quotes.
Was this page helpful?
Yes No