Accessing Models through API

Please view the OneClick Platform Walkthrough before reading this article.

OneClick.ai supports the easy exporting of any model created through the use of our AI to your external API uses. This paper will go over how to export our models as an API.

After the model training process, on the model review page, there is an option to enable API for each model.

Upon clicking that button you can then select to activate or deactivate the API. Only one API can be active at any given time for trial accounts.

The tutorial project always has an API running. This cannot be deactivated and does not count as part of your active API quota.

Deploying the model may take several minutes. Once complete, the API link will be populated with a URL.

https://www.oneclick.ai/api/pipeline_omcw3ellryj6

This is our model, but attempting to copy and paste it into the browser will cause an error.

{
 "error": "api request failure"
}

The model does not know what to predict because no input is given in the request.

You need to include features corresponding to every column in the CSV file used for training the model, except the label column. Those features are specified as key-value pairs in the query part of the URL, and together they represent a single row in your dataset.

https://oneclick.ai/api/pipeline_ykhhjcnnsqym?sepalLength=1.0&sepalWidth=1.0&petalLength=1.5&petalWidth=2.0

This example is predicting flower types based on their length and width, so our columns are the different lengths and widths of parts of the flower (sepal and petal).

Make sure your key/value pairs are URL encoded.

When this URL is put into a browser it should look like this:

{
 "prediction": "Iris-setosa",
 "probability": "[ 0.5 0.41 0.09]"
}

The JSON response consists of the prediction and the probabilities for each flower type. The probabilities sum up to 100% (1.0). Our example has three probabilities because there were three possible flower types in the training dataset.

Probabilities were given because this was a classification task. If your task is regression or forecasting, then there will be no probability breakdown.

Each active model is assigned a unique API URL, which should be kept private.

This URL is not meant to be shared or stored on the browser side, it should only be used on the server site.

If the URL is accessed in the browser (with javascript) there will be two problems:

  1. The URL will not be secure and if stolen and used by another server, the cost of using the model would fall to you.
  2. Due to same origin policy the URL will not function in the browser with javascript, and it will be unable to access the model.

Python

If you would prefer to access the API using programs rather than manual inputs, OneClick.ai supports that as well.

The code to access the API in Python looks like this:

import requests
response = requests.get("https://oneclick.ai/api/pipeline_ykhhjcnnsqym?sepalLength=1.0&sepalWidth=1.0&petalLength=1.5&petalWidth=2.0")
if response.status_code == 200:
    print response.json()
else:
    print "Error when fetching the URL"

The response will then look just like the manually entered URL:

{ “prediction”: “Iris-virginica”, “probability”: [0.39285714, 0.10714286, 0.5] }

If you have files in training data and would like to access the API by sending a file, you should use the “post” method to do this. Here is code snippet to do it:

import requests
url = “https://www.oneclick.ai/api/pipeline_nyiupdycraql”
img = open(“test_1.jpg”, ‘rb’)
# please modify “path” to match the field name in your training data, 
# “data” part is for other columns in your test data if you do have
response = requests.post(url, data={“val_1”: 3.0, “val_2": 4.0}, files={‘path’: (‘test_1.jpg’, img, ‘image/jpeg’)})
print(response.json())

Request a demo