<jupyter_start><jupyter_text>Overview: Deploying the Dress Recommender==================Let's say you want to make an app which can recommend dresses to you based on a photo you took. You need a way to deploy the model previously built. Turi Predictive Services helps do this in an easy and scalable way. In this notebook, we demonstrate how do that for the dress recommender model.----------------------- Deployment Steps=========The notebook has three sections: 1. Create a model2. Create a predictive service3. Query the model 1. Create a model Let us try and deploy the dress recommender. First, we define a function ```similar_dress``` that takes a query and returns similar dresses. We start by loading the already trained models and datasets.<jupyter_code>reference_sf = graphlab.SFrame('data/sf_processed.sframe/')
pretrained_model = graphlab.load_model('data/imagenet_model')
nn_model = graphlab.load_model('data/nearest_dress_model')
reference_sf
def dress_similar(url):
    img = graphlab.Image(url)
    image_sf = graphlab.SFrame()
    image_sf['image'] = [img]
    image_sf['features'] = pretrained_model.extract_features(image_sf)
    ans = nn_model.query(image_sf, k=5)
    return ans 
QUERY_URL = 'http://static.ddmcdn.com/gif/blue-dress.jpg'
Image(QUERY_URL)
def retrieve_image(nearest_neighbors_output, input_sframe):
    joined = input_sframe.join(nearest_neighbors_output, on={'_id':'reference_label'})
    sorted_sf = joined.sort('rank')
    return sorted_sf['image']
images = retrieve_image(dress_similar(QUERY_URL), reference_sf)
images.show()<jupyter_output><empty_output><jupyter_text>2. Create a Predictive Service (One time) 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. We add our ```similar_dress``` function to the predictive service, so that it can be queried by the outside world. Note: In order to follow along here, you need AWS credentials. You can then uncomment the code below. Be sure that the deployment state path is in an s3 bucket you have access to.<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>Load an already created service<jupyter_code>import graphlab as gl
ps = gl.deploy.predictive_service.load('s3://gl-demo-usw2/predictive_service/demolab/ps-1.6')
ps
#ps.add('dress_similar', dress_similar)
ps.update('dress_similar', dress_similar)
ps.apply_changes()<jupyter_output>[INFO] Endpoint 'dress_similar' is added. Use apply_changes() to deploy all pending changes, or continue with other modification.
[INFO] Uploading local path /var/folders/9h/1m96s7vn5z72cxt_q7g9p86h0000gn/T/predictive_object_l3R0fm to s3 path: s3://gl-demo-usw2/predictive_service/demolab/ps-1.6/predictive_objects/dress_similar/1<jupyter_text>Query the model The Predictive Service is now up! Let's query it and explore the response.<jupyter_code>ps.query('dress_similar', url=QUERY_URL)<jupyter_output><empty_output><jupyter_text>Query via REST======== Query from anywhere. Here, we issue a request via the ```requests``` library, and convert the returning JSON  back into an SFrame. This could easily be done from outside of Python, though.<jupyter_code>import json
import requests

def restful_query(url):
    # Query the service.
    headers = {'content-type': 'application/json' }
    payload = {'api_key':'b437e588-0f2b-45e1-81c8-ce3acfa81ade', 'data':{'url':url}}
    end_point = 'http://demolab-one-six-2015364754.us-west-2.elb.amazonaws.com/query/dress_similar'
    resp = requests.post(end_point,json.dumps(payload), headers=headers)

    # Join with existing data.
    ans = gl.SArray(json.loads(resp.content)['response']).unpack('')
    return retrieve_image(ans, reference_sf)
restful_query('http://static.ddmcdn.com/gif/blue-dress.jpg').show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11180846118084611, 'fraction_numerical': 0.016736401673640166, 'mean_word_length': 5.355982274741507, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '242267', 'license': 'Apache-2.0', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1350, 'n_words': 397, 'path': 'strata-nyc-2015/deployment/predictive_services.ipynb', 'repo_name': 'voltek62/tutorials', 'size': '4302'}