<jupyter_start><jupyter_text>Train a model locally or remote<jupyter_code>from sklearn import datasets, svm
digits = datasets.load_digits()
clf = svm.SVC(gamma=0.001, C=100.,probability=True)
clf.fit(digits.data[:-1], digits.target[:-1])
clf.predict(digits.data[-1:])
!pip show scikit-learn<jupyter_output><empty_output><jupyter_text>Save model file<jupyter_code>!pip install joblib
from joblib import dump
dump(clf, 'model.joblib')<jupyter_output><empty_output><jupyter_text>Step 1 : Write a model transform script Make sure you have a ...- "load_model" function    - input args are model path    - returns loaded model object    - model name is the same as what you saved the model file as (see above step)- "predict" function    - input args are the loaded model object and a payload    - returns the result of model.predict    - make sure you format it as a single (or multiple) string return inside a list for real time (for mini batch)    - from a client, a list  or string or np.array that is sent for prediction is interpreted as bytes. Do what you have to for converting back to list or string or np.array    - return the error for debugging<jupyter_code>%%writefile modelscript_sklearn.py
import sklearn
from joblib import load
import numpy as np
import os

#Return loaded model
def load_model(modelpath):
    print(modelpath)
    clf = load(os.path.join(modelpath,'model.joblib'))
    print("loaded")
    return clf

# return prediction based on loaded model (from the step above) and an input payload
def predict(model, payload):
    try:
        # locally, payload may come in as an np.ndarray
        if type(payload)==np.ndarray:
            out = [str(model.predict(np.frombuffer(payload).reshape((1,64))))]
        # in remote / container based deployment, payload comes in as a stream of bytes
        else:
            out = [str(model.predict(np.frombuffer(payload[0]['body']).reshape((1,64))))]
    except Exception as e:
        out = [type(payload),str(e)] #useful for debugging!
    
    return out<jupyter_output><empty_output><jupyter_text>Does this work locally? (not "_in a container locally_", but _actually_ in local)<jupyter_code>from modelscript_sklearn import *
model = load_model('.')
predict(model,digits.data[-1:])[0]<jupyter_output><empty_output><jupyter_text>ok great! Now let's install ezsmdeployIn some cases, installs fail due to an existing package installed called greenlet.This is not a direct dependency of ezsmdeploy but interferes with the installation. To fix this, either install in a virtualenv as seen above, or do:pip install ezsmdeploy[locust] --ignore-installed greenlet<jupyter_code>!pip install ezsmdeploy
import ezsmdeploy<jupyter_output><empty_output><jupyter_text>If you have been running other inference containers in local mode, stop existing containers to avoid conflict<jupyter_code>!docker container stop $(docker container ls -aq) >/dev/null<jupyter_output><empty_output><jupyter_text>Deploy locally<jupyter_code>ez = ezsmdeploy.Deploy(model = ['model.joblib'], # if you intend to add models later, pass model as list, otherwise str
                  script = 'modelscript_sklearn.py',
                  requirements = ['pyarrow','scikit-learn==0.22.1','numpy','joblib'], #or pass in the path to requirements.txt
                  instance_type = 'local',
                  autoscale = True,
                  #framework = 'sklearn', # not required if you provide requirements
                  wait = True)<jupyter_output><empty_output><jupyter_text>Test containerized version locally<jupyter_code>out = ez.predictor.predict(digits.data[-1:].tobytes()).decode()
out
!docker container stop $(docker container ls -aq) >/dev/null<jupyter_output><empty_output><jupyter_text>Deploy on SageMaker<jupyter_code>ezonsm = ezsmdeploy.Deploy(model = ['model.joblib','model.joblib'], # example of multimodel endpoint. 
                  script = 'modelscript_sklearn.py',
                  requirements = ['pyarrow','scikit-learn==0.22.1','numpy','joblib'])
#!./src/build-docker.sh test
out = ezonsm.predictor.predict(digits.data[-1:].tobytes(),target_model='model1.tar.gz').decode() 
out<jupyter_output><empty_output><jupyter_text>Install the additional locust testing functionality to enable automated testing<jupyter_code>!pip install ezsmdeploy[locust]
ezonsm.test(input_data=digits.data[-1:].tobytes(), target_model='model1.tar.gz',usercount=20,hatchrate=10,timeoutsecs=10)
import pandas as pd

pd.read_csv('src/locuststats_stats.csv')
ezonsm.predictor.delete_endpoint()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1066257130320316, 'fraction_numerical': 0.008556384379113647, 'mean_word_length': 4.5733496332518335, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 43, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '626807', 'license': 'Apache-2.0', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1397, 'n_words': 405, 'path': 'notebooks/.ipynb_checkpoints/Using ezsmdeploy for sklearn deployments-checkpoint.ipynb', 'repo_name': 'aws-samples/easy-amazon-sagemaker-deployments', 'size': '4558'}