Skip to content

Commit 6ebcf69

Browse files
authored
Add python script to upload sample model to S3 (#42)
Download and upload a xgboost-churn model to S3 for use in the model crd.
1 parent 928c6bf commit 6ebcf69

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

samples/model/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ This sample demonstrates how to start models/create a model using your own model
66

77
This sample assumes that you have completed the [common prerequisites](/samples/README.md).
88

9+
### Upload S3 Data
10+
11+
You will need a model uploaded to an S3 bucket. Make sure you have AWS credentials and and have the bucket in the same region where you plan to create SageMaker resources. Run the following python script to upload sample data to your S3 bucket.
12+
```
13+
python3 s3_sample_model.py $S3_BUCKET_NAME
14+
```
15+
916
### Get an Image
1017

1118
All SageMaker models are run from within a container with all necessary dependencies and modules pre-installed and with the model scripts referencing the acceptable input and output directories. Sample container images are [available](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-algo-docker-registry-paths.html).

samples/model/my-model.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ spec:
77
primaryContainer:
88
containerHostname: xgboost
99
# The source of the model data
10-
modelDataURL: s3://<YOUR BUCKET/PATH>
10+
modelDataURL: s3://<YOUR BUCKET>/sagemaker/xgboost/model/xgb-churn-prediction-model.tar.gz
1111
# The URL and tag of your ECR container
1212
# If you are not on us-west-2 you can find an imageURI here https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-algo-docker-registry-paths.html
13-
image: 433757028032.dkr.ecr.us-west-2.amazonaws.com/xgboost:1
13+
image: 246618743249.dkr.ecr.us-west-2.amazonaws.com/sagemaker-xgboost:0.90-2-cpu-py3
1414
environment:
1515
my_var: my_value
1616
my_var2: my_value2

samples/model/s3_sample_model.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import urllib.request
2+
from urllib.parse import urlparse
3+
import sys
4+
5+
# Gets your bucket name from command line
6+
try:
7+
bucket = str(sys.argv[1])
8+
except Exception as error:
9+
print("Please pass your bucket name as a commandline argument")
10+
sys.exit(1)
11+
12+
# Download model from pinned commit
13+
url = "https://github.com/aws/amazon-sagemaker-examples/raw/af6667bd0be3c9cdec23fecda7f0be6d0e3fa3ea/sagemaker_model_monitor/introduction/model/xgb-churn-prediction-model.tar.gz"
14+
urllib.request.urlretrieve(url, "xgb-churn-prediction-model.tar.gz")
15+
16+
import os
17+
import boto3
18+
19+
# Upload model to S3
20+
prefix = "sagemaker/xgboost/model"
21+
model_file = open("xgb-churn-prediction-model.tar.gz", "rb")
22+
s3_key = os.path.join(prefix, "xgb-churn-prediction-model.tar.gz")
23+
boto3.Session().resource("s3").Bucket(bucket).Object(s3_key).upload_fileobj(model_file)
24+
s3url = f"s3://{bucket}/{s3_key}"
25+
print(f"Writing to {s3url}")
26+
27+
# Remove downloaded file
28+
os.remove("xgb-churn-prediction-model.tar.gz")

0 commit comments

Comments
 (0)