text: #!/usr/bin/env python
  # coding: utf-8
  
  # # Sagemaker and Seldon Core Scikit-learn Example
  # 
  #  * Train a model on AWS Sagemaker
  #  * Deploy locally on Seldon Core
  #  
  #  This tutorial assumes you are already familiar with Sagemaker and have an AWS account you can use.
  #  
  #  Dependencies
  #  
  #   * AWS CLI
  #   * Docker
  #   * Git
  #   * Minikube
  
  # ## Train Scikit-learn Iris Model
  # 
  # We will use the Sagemaker example notebook [Iris Training and Prediction with Sagemaker Scikit-learn](https://github.com/aws/amazon-sagemaker-examples/blob/master/sagemaker-python-sdk/scikit_learn_iris/scikit_learn_estimator_example_with_batch_transform.ipynb)
  # 
  #   1. Run this notebook inside Sagemaker but change the training step to include an ```output_path``` parameter with value an S3 bucket you have created. For example ```output_path="s3://<my-bucket>/<my-path>``` as shown below:
  #      ![sagemaker-image](sagemaker-scikit-iris-train.png)
  #   1. Once trained you should find the model saved to the location you specified. You will need to use this S3 location later when deploying locally.
  
  # ## Create Sagemaker SKLearn Image
  # 
  # To run your model locally you will need to create the sagemaker sklearn image
  
  # In[2]:
  
  
  get_ipython().system('git clone https://github.com/aws/sagemaker-scikit-learn-container.git')
  
  
  # Build the base container.
  
  # In[3]:
  
  
  get_ipython().system('cd sagemaker-scikit-learn-container &&     docker build -t sklearn-base:0.20.0-cpu-py3 -f docker/0.20.0/base/Dockerfile.cpu --build-arg py_version=3 .')
  
  
  # In[4]:
  
  
  get_ipython().system('cd sagemaker-scikit-learn-container && python setup.py bdist_wheel')
  
  
  # In[5]:
  
  
  get_ipython().system('cd sagemaker-scikit-learn-container && docker build -t sklearn-final:0.20.0-cpu-py3 -f docker/0.20.0/final/Dockerfile.cpu --build-arg py_version=3 .')
  
  
  # **Specify the location of the model.tar.gz that AWS Sagemaker created on S3** An example is shown below.
  
  # In[1]:
  
  
  get_ipython().run_line_magic('env', 'SAGEMAKER_MODEL_DIRECTORY=s3://seldon-sagemaker-testing/scikit_learn_iris/sagemaker-scikit-learn-2019-01-04-19-26-40-470/output/model.tar.gz')
  
  
  # ## Prepare Prediction Code
  # 
  # We will create the methods required to load and predict against our trained model as a module that can be loaded by the Sagemaker scikit-learn-container image we created.
  
  # In[2]:
  
  
  get_ipython().system('pygmentize scikit_learn_iris/scikit_learn_iris.py')
  
  
  # Package the code as a tar.gz file
  
  # In[8]:
  
  
  get_ipython().system('cd scikit_learn_iris && tar -cvf ../scikit_learn_iris_code.tar .')
  get_ipython().system('gzip -f scikit_learn_iris_code.tar')
  
  
  # **Set the path to store the code**
  
  # In[3]:
  
  
  get_ipython().run_line_magic('env', 'SAGEMAKER_SUBMIT_DIRECTORY=s3://seldon-sagemaker-testing/scikit_learn_iris_code.tar.gz')
  
  
  # In[10]:
  
  
  get_ipython().system('aws s3 cp scikit_learn_iris_code.tar.gz ${SAGEMAKER_SUBMIT_DIRECTORY}')
  
  
  # We can now do a local test with Docker. To make this work you will need to have your aws credentials in ~/.aws/config.
  
  # In[4]:
  
  
  get_ipython().system('docker run -d --rm -p 8080:8080         -e SAGEMAKER_MODEL_DIRECTORY=${SAGEMAKER_MODEL_DIRECTORY}         -e SAGEMAKER_SUBMIT_DIRECTORY=${SAGEMAKER_SUBMIT_DIRECTORY}         -e SAGEMAKER_PROGRAM="scikit_learn_iris.py"         -v ~/.aws:/root/.aws         --name "scikit_predictor"         sklearn-final:0.20.0-cpu-py3 serve')
  
  
  # Test the running image with a prediction call
  
  # In[5]:
  
  
  get_ipython().system('curl 0.0.0.0:8080/invocations -d \'1.0,2.0,3.0,4.0\' -H "Content-Type: text/csv"')
  
  
  # In[6]:
  
  
  get_ipython().system('docker rm -f scikit_predictor')
  
  
  # ## Test with Seldon-Core in Minikube
  
  # In[14]:
  
  
  get_ipython().system('minikube start --memory 4096')
  
  
  # ## Setup Seldon Core
  # 
  # Use the setup notebook to [Setup Cluster](https://docs.seldon.io/projects/seldon-core/en/latest/examples/seldon_core_setup.html#Setup-Cluster) with [Ambassador Ingress](https://docs.seldon.io/projects/seldon-core/en/latest/examples/seldon_core_setup.html#Ambassador) and [Install Seldon Core](https://docs.seldon.io/projects/seldon-core/en/latest/examples/seldon_core_setup.html#Ambassador). Instructions [also online](https://docs.seldon.io/projects/seldon-core/en/latest/examples/seldon_core_setup.html).
  
  # ## Wrap Model and Test
  
  # Recreate the Sagemaker scikit-learn base and final containers inside Minikube. In a production scenario you would build these images and push to your DockerHub or private repo.
  
  # In[12]:
  
  
  get_ipython().system('eval $(minikube docker-env) &&     cd sagemaker-scikit-learn-container &&     docker build -t sklearn-base:0.20.0-cpu-py3 -f docker/0.20.0/base/Dockerfile.cpu --build-arg py_version=3 .')
  
  
  # In[13]:
  
  
  get_ipython().system('eval $(minikube docker-env) &&     cd sagemaker-scikit-learn-container &&     docker build -t sklearn-final:0.20.0-cpu-py3 -f docker/0.20.0/final/Dockerfile.cpu --build-arg py_version=3 .')
  
  
  # ## Create AWS Credential Secret from Template
  # 
  
  # In[ ]:
  
  
  get_ipython().system('cp aws-config-secret.yaml.tmpl aws-config-secret.yaml')
  
  
  # Edit aws-config-secret.yaml and fill in the base64 encoded values, for example you can run on linux:
  # 
  #  ``` echo -n <my_aws_access_id> | base64```
  # 
  # Take the value and place into the file.
  # 
  # Once completed you can create the secret via ```kubectl```
  
  # In[14]:
  
  
  get_ipython().system('kubectl create -f aws-config-secret.yaml')
  
  
  # ## Create Seldon Deployment from Template
  # 
  # We will use the environment variables you set above to fill in the details for the Seldon Deployment so the Sagemake image container knows where to download your code.
  
  # In[17]:
  
  
  get_ipython().system('cat scikit_learn_iris_deployment.json.tmpl | sed s#{SAGEMAKER_MODEL_DIRECTORY}#${SAGEMAKER_MODEL_DIRECTORY}# | sed s#{SAGEMAKER_SUBMIT_DIRECTORY}#${SAGEMAKER_SUBMIT_DIRECTORY}# > scikit_learn_iris_deployment.json')
  
  
  # In[18]:
  
  
  get_ipython().system('kubectl create -f scikit_learn_iris_deployment.json')
  
  
  # ## Test Predictive Endpoint
  
  # In[19]:
  
  
  get_ipython().system("seldon-core-api-tester contract.json `minikube ip` `kubectl get svc ambassador -o jsonpath='{.spec.ports[0].nodePort}'`     sklearn-sagemaker --namespace default -p")
  
  
  # ## Teardown
  
  # In[ ]:
  
  
  get_ipython().system('minikube delete')
  
  