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 [1]:
# In order to run this code, you need an already trianed model (see the accompanying notebook)
import graphlab as gl
model = gl.load_model('pattern_mining_model.gl')
model
Out[1]:
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 [4]:
def predict(x):
# Construct an SFrame
sf = gl.SFrame(x)
# Add your own business logic here
# Call the predict method on the model.
predictions = model.predict(sf)
return predictions['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 [ ]:
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 [5]:
import graphlab as gl
ps = gl.deploy.predictive_service.load('s3://gl-demo-usw2/predictive_service/demolab/ps-1.6')
In [6]:
ps
Out[6]:
In [7]:
# ps.add('pattern-mining', predict) (When you add this for the first time)
ps.update('pattern-mining', predict)
In [8]:
ps.apply_changes()
In [9]:
# test query to make sure the model works fine
ps.query('pattern-mining', x={'Receipt': [1], 'StoreNum': [2], 'Item': ['CherryTart']})
Out[9]:
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":{"Receipt": [1], "StoreNum": [2], "Item": ["CherryTart"]}}}' http://demolab-one-six-2015364754.us-west-2.elb.amazonaws.com/query/pattern-mining
You can also query though Python using the requests module
In [10]:
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/pattern-mining'
return requests.post(end_point, json.dumps(payload), headers=headers).json()
In [11]:
restful_query({'Receipt': [1], 'StoreNum': [2], 'Item': ['CherryTart']})
Out[11]: