<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Introduction to ML Deployment
Deploying models created using python in a Turi Predictive Service is very easy. This notebook walks you through the step-by-step process. 
<img src='images/predictive_services_overview.png'></img>
Deployment Steps
The notebook has three sections
Step1:  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.
Step2:  2. Create a Predictive Service (One time) <a id='create'></a>
This section shows you how to deploy a Predictive Service to EC2. The EC2 instances used by the Predictive Service will be launched in your own AWS account, so you will be responsible for the cost. 
<img src="images/middle.png"></img>
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
Step3:  Load an already created service
Step4:  Query the model <a id='query'></a>
You may do a test query before really deploying it to production. This will help detect errors in the function before deploying it the Predictive Service. 
<img src="images/right.png"></img>
Step5:  Query from external applications via REST
Now other applications can interact with our model! In the next section we will illustrate how to consume the model. We can also  use other APIs like ps.update() to update a mode, ps.remove() to remove a model.
The model query is exposed through REST API. The path is
<ASSISTANT_TASK:>
Python Code:
# 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
Explanation: Introduction to ML Deployment
Deploying models created using python in a Turi Predictive Service is very easy. This notebook walks you through the step-by-step process. 
<img src='images/predictive_services_overview.png'></img>
Deployment Steps
The notebook has three sections: 
<a href='#cpo'>Create a model</a>
<a href='#create'>Create a predictive service</a>
<a href='#query'>Query the model</a>
If you are deploying a model in an existing Predictive Service instance you can go to step (2) directly.
1. Create a model <a id='cpo'></a>
Let's train a simple pattern mining model
<img src="images/left.png"></img>
End of explanation
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']
Explanation: 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.
End of explanation
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)
Explanation: 2. Create a Predictive Service (One time) <a id='create'></a>
This section shows you how to deploy a Predictive Service to EC2. The EC2 instances used by the Predictive Service will be launched in your own AWS account, so you will be responsible for the cost. 
<img src="images/middle.png"></img>
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:
* Number of nodes in the service - By default the number of hosts (num_hosts) is 1. To obtain good cache utility and high availability, we recommended setting num_hosts to at least 3.
* State path to persist service state and service logs. This is a s3 location. 
* Port to be used by the server.
* Other settings, such as SSL credentials etc.
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.
End of explanation
import graphlab as gl
ps = gl.deploy.predictive_service.load('s3://gl-demo-usw2/predictive_service/demolab/ps-1.6')
ps
# ps.add('pattern-mining', predict) (When you add this for the first time)
ps.update('pattern-mining', predict)
ps.apply_changes()
Explanation: Load an already created service
End of explanation
# test query to make sure the model works fine
ps.query('pattern-mining', x={'Receipt': [1], 'StoreNum': [2], 'Item': ['CherryTart']})
Explanation: Query the model <a id='query'></a>
You may do a test query before really deploying it to production. This will help detect errors in the function before deploying it the Predictive Service. 
<img src="images/right.png"></img>
End of explanation
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()
restful_query({'Receipt': [1], 'StoreNum': [2], 'Item': ['CherryTart']})
Explanation: Query from external applications via REST
Now other applications can interact with our model! In the next section we will illustrate how to consume the model. We can also  use other APIs like ps.update() to update a mode, ps.remove() to remove a model.
The model query is exposed through REST API. The path is:
http(s)://&lt;your-ps-endpoint&gt;/data/&lt;model-name&gt;
And the payload is a JSON serialized string in the following format:
{"api_key": &lt;api key&gt;,
 "data": &lt;data-passed-to-custom-query&gt;}
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
Query through Python
End of explanation
<END_TASK>