In this notebook we show how to set up a GraphLab Predictive Service deployment, to enable low-latency REST queries of trained machine learning models from a cluster of EC2 instances. For more information, see the Predictive Services section of the User Guide.
We show how easy it is to:
In [ ]:
import graphlab
To launch a Predictive Service deployment we need to specify an Ec2Config object for this deployment. This configuration includes the region (defaults to us-west-2), instance type (defaults to m3.xlarge), and your aws credentials (if needed).
In [ ]:
env = graphlab.deploy.Ec2Config(region='us-west-2',
instance_type='m3.xlarge',
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY')
To launch a Predictive Service Deployment the API is graphlab.deploy.predictive_service.create. The required parameters are a name for the deployment, the EC2 configuration we defined in the previous step, and an S3 path for the root 'working directory' for this Predictive Service. This path is where the models and predictive objects will be uploaded, and where logs will be written for this Predictive Service.
When this command is executed the EC2 instances will be launched immediately, then a load balancer will be launched, configured, and finally the load balancer will add the instances into the cluster as they pass health checks. The Predictive Service object will also be added to the workbench, so the deployment can be easily loaded in a later Python session.
In [ ]:
deployment = graphlab.deploy.predictive_service.create('sample-service-onefive', env, 's3://gl-rajat-testing/pred-root/sample-service')
deployment.save()
There are other parameters which are optional, like specifying an SSL credential pair for HTTPS, specifying API keys and Admin keys (API keys enable REST queries, Admin keys enable adding/modifying the deployment), and the number of hosts in the cluster.
Print the deployment to see the details about this deployment. This shows the information necessary to connect to the deployment from an application, which predictive objects (models) are deployed, and whether there are any pending changes.
In [ ]:
deployment
Out[ ]:
GraphLab Canvas shows the details of this Predictive Service, and provides the introductory monitoring dashboard for the cluster. See the API documentation here.
In [ ]:
deployment.show()
Now that the cluster is ready, let's quickly train a Model to upload to this deployment. Each GraphLab Model is a Predictive Object. Custom Predictive Objects can also be specified by writing a function in Python, see the User Guide for more information.
In [ ]:
data_url = 'https://static.turi.com/datasets/movie_ratings/sample.small'
data = graphlab.SFrame.read_csv(data_url,delimiter='\t',column_type_hints={'rating':int})
(train_set, test_set) = data.random_split(0.8)
model = graphlab.popularity_recommender.create(train_set, 'user', 'movie', 'rating')
Adding a Predictive Object stages this Predictive Object to be deployed to the cluster. To add a predictive object specify a name (which will be used in the URI to query the model in the REST API), and then specify the model object (or a path to the model). See the API documentation here.
In [ ]:
deployment.add('recs', model, description='Sample Recommender')
Now if you print the deployment there will be a pending predictive object that was just added.
In [ ]:
deployment
Out[ ]:
To update an existing model or predictive object, use the update method. Update will increment the version of an existing predictive object. When published, the updated model will be proactively warmed in the distributed cache, and existing cached entries for this model will be expired in 15 minutes.
To complete the publishing of this Predictive Object (model), call the apply_changes method. By calling this API the pending predictive objects will be uploaded to S3, and the cluster will be notified to download them from S3.
In [ ]:
deployment.apply_changes()
To see the status of the deployment, call the get_status API.
In [ ]:
deployment.get_status()
Out[ ]:
To test this Predictive Object deployed to the Predictive Service, use the query function from the deployment object.
In [ ]:
recs = deployment.query('recs', method='recommend', data={'users':['Jacob Smith']})
recs
Out[ ]:
Now to integrate this Predictive Service deployment into an application, next step is to query the REST API on the cluster. Below are the available client libraries for Predictive Service. One can also use curl, which can be modified for any programming language needed.
Please follow the instructions to install and use these client libraries.
In [ ]:
!curl -X POST -d '{"api key": "806fc046-62da-4e5b-ab9d-0dc6ac2ff549", "data":{"method":"recommend", "data": {"users":["Jacob Smith"]}}}' http://sample-service-onefive-137425129.us-west-2.elb.amazonaws.com/query/recs
To terminate a Predictive Service, call the terminate_service() API. There are options to delete the logs and predictive objects as well.
In [ ]:
deployment.terminate_service(remove_logs=True, remove_state=True)
There is a lot more you can do with a Predictive Service. Spin one up for yourself and see how easy it is to consume a Model, or define your own Custom Predictive Object.