Deploying models created using python in a Turi Predictive Service is very easy. This notebook walks you through the step-by-step process.
The notebook has three sections:
If you are deploying a model in an existing Predictive Service instance you can go to step (2) directly.
In [5]:
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
iris = load_iris()
model = RandomForestClassifier(n_estimators=10)
model = model.fit(iris['data'], iris['target'])
model
Out[5]:
We can expose the trained model as a REST endpoint. This will allow other applications to consume the predictions from the model.
In order to do that, we wrap the model object in a Python function and add it to the Predictive Service. In the function you may add your own logic for transform input to the model, ensemble different models or manipulate output before returning. Checkout out user guide for more details.
The result of the function needs to be a JSON serializable object.
In [46]:
def classify(x):
prediction = model.predict(x)
# convert into a json serializable value
return list(prediction)
To create a Predictive Service in Amazon AWS, we first configure the EC2 Config object, which contains the configuration parameters required for launching a Predictive Service cluster in EC2. These fields are optional and include the region, instance type, CIDR rules etc. Predictive Service uses this configuration for service creation.
Having configured our EC2 Config object, we're ready to launch a Predictive Service Deployment, There are a few aspects of the Predictive Service that can be customized:
num_hosts
) is 1. To obtain good cache utility and high availability, we recommended setting num_hosts to at least 3.The following code snippet shows you how to create a Predictive Service. You will have to replace the ps_state_path and credentials for your Predictive Service.
In [1]:
import graphlab as gl
# Replace with your path.
ps_state_path = 's3://<your-bucket-name>/predictive_service/ps'
# Set your AWS credentials.
gl.aws.set_credentials(<key>, <secret>)
# Create an EC2 config
ec2_config = gl.deploy.Ec2Config()
# Launch a predictive service
ps = gl.deploy.predictive_service.create(name = 'sklearn-predictive-service',
ec2_config = ec2_config, state_path = ps_state_path, num_hosts = 1)
In [47]:
import graphlab as gl
ps = gl.deploy.predictive_service.load('s3://gl-demo-usw2/predictive_service/demolab/ps-1.6')
In [62]:
ps
Out[62]:
In [53]:
# ps.add('classify-sklearn', classify) (If done for the first time)
ps.update('classify-sklearn', classify)
In [55]:
ps.apply_changes()
In [56]:
ps.test_query('classify-sklearn', x=[5.1, 3.5, 1.4, 0.2])
Out[56]:
Now, let us query the real service.
In [69]:
# test query to make sure the model works fine
ps.query('classify-sklearn', x=[5.1, 3.5, 1.4, 0.2])
Out[69]:
The model query is exposed through REST API. The path is:
http(s)://<your-ps-endpoint>/data/<model-name>
And the payload is a JSON serialized string in the following format:
{"api_key": <api key>,
"data": <data-passed-to-custom-query>}
Here the 'api key' may be obtained through ps.api_key, and data is the actual data passed to the custom predictive object in the Predictive Service. It will be passed to the query using **kwargs format
Here is a sample curl command to query your model:
curl -X POST -d '{"api_key":"b437e588-0f2b-45e1-81c8-ce3acfa81ade", "data":{"x":[5.1, 3.5, 1.4, 0.2]}}' http://demolab-one-six-2015364754.us-west-2.elb.amazonaws.com/query/classify-sklearn
You can also query though Python using the requests module
In [77]:
import json
import requests
def restful_query(x):
headers = {'content-type': 'application/json'}
payload = {'api_key':'b437e588-0f2b-45e1-81c8-ce3acfa81ade', "data":{"x": x}}
end_point = 'http://demolab-one-six-2015364754.us-west-2.elb.amazonaws.com/query/classify-sklearn'
return requests.post(end_point, json.dumps(payload), headers=headers).json()
In [78]:
restful_query([5.1, 3.5, 1.4, 0.2])
Out[78]:
In [80]:
restful_query([5.1, 3.5, 1.4, 0.3])
Out[80]: