-
Notifications
You must be signed in to change notification settings - Fork 59
/
scikit_learn_bring_your_own_model_local_serving.py
66 lines (51 loc) · 2.29 KB
/
scikit_learn_bring_your_own_model_local_serving.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# This is a sample Python program that serve a scikit-learn model pre-trained on the California Housing dataset.
# This implementation will work on your *local computer* or in the *AWS Cloud*.
#
# Prerequisites:
# 1. Install required Python packages:
# `pip install -r requirements.txt`
# 2. Docker Desktop installed and running on your computer:
# `docker ps`
# 3. You should have AWS credentials configured on your local machine
# in order to be able to pull the docker image from ECR.
###############################################################################################
import boto3
import pandas as pd
import tarfile
from sagemaker.sklearn import SKLearnModel
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
DUMMY_IAM_ROLE = 'arn:aws:iam::111111111111:role/service-role/AmazonSageMaker-ExecutionRole-20200101T000001'
s3 = boto3.client('s3')
def main():
# Prepare data for model inference - we use the Boston housing dataset
print('Preparing data for model inference')
data = fetch_california_housing()
X_train, X_test, y_train, y_test = train_test_split(
data.data, data.target, test_size=0.25, random_state=42
)
# we don't train a model, so we will need only the testing data
testX = pd.DataFrame(X_test, columns=data.feature_names)
# Download a pre-trained model file
print('Downloading a pre-trained model file')
s3.download_file('aws-ml-blog', 'artifacts/scikit_learn_bring_your_own_model/model.joblib', 'model.joblib')
# Creating a model.tar.gz file
tar = tarfile.open('model.tar.gz', 'w:gz')
tar.add('model.joblib')
tar.close()
model = SKLearnModel(
role=DUMMY_IAM_ROLE,
model_data='file://./model.tar.gz',
framework_version='0.23-1',
py_version='py3',
source_dir='code',
entry_point='inference.py'
)
print('Deploying endpoint in local mode')
predictor = model.deploy(initial_instance_count=1, instance_type='local')
predictions = predictor.predict(testX[data.feature_names].head(5))
print(f"Predictions: {predictions}")
print('About to delete the endpoint to stop paying (if in cloud mode).')
predictor.delete_endpoint(predictor.endpoint_name)
if __name__ == "__main__":
main()