forked from Azure/azureml-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy-torchserve.sh
118 lines (94 loc) · 3.71 KB
/
deploy-torchserve.sh
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
BASE_PATH=endpoints/online/custom-container
AML_MODEL_NAME=torchserve-densenet161
AZUREML_MODEL_DIR=azureml-models/$AML_MODEL_NAME/1
MODEL_BASE_PATH=/var/azureml-app/$AZUREML_MODEL_DIR
ENDPOINT_NAME=torchserve-endpoint
DEPLOYMENT_NAME=torchserve-deployment
# Download model and config file
echo "Downling model and config file..."
mkdir $BASE_PATH/torchserve
wget --progress=dot:mega https://aka.ms/torchserve-densenet161 -O $BASE_PATH/torchserve/densenet161.mar
wget --progress=dot:mega https://aka.ms/torchserve-config -O $BASE_PATH/torchserve/config.properties
# Get name of workspace ACR, build image
WORKSPACE=$(az config get --query "defaults[?name == 'workspace'].value" -o tsv)
ACR_NAME=$(az ml workspace show --name $WORKSPACE --query container_registry -o tsv | cut -d'/' -f9-)
if [[ $ACR_NAME == "" ]]
then
echo "ACR login failed, exiting"
exit 1
fi
az acr login -n $ACR_NAME
IMAGE_TAG=${ACR_NAME}.azurecr.io/torchserve:8080
az acr build $BASE_PATH/ -f $BASE_PATH/torchserve.dockerfile -t $IMAGE_TAG -r $ACR_NAME
# Run image locally for testing
docker run --rm -d -p 8080:8080 --name torchserve-test \
-e MODEL_BASE_PATH=$MODEL_BASE_PATH \
-v $PWD/$BASE_PATH/torchserve:$MODEL_BASE_PATH/torchserve $IMAGE_TAG
sleep 10
cleanTestingFiles(){
rm -r $BASE_PATH/torchserve
rm kitten_small.jpg
}
# Check Torchserve health
echo "Checking Torchserve health..."
curl http://localhost:8080/ping
# Download test image
echo "Downloading test image..."
wget https://aka.ms/torchserve-test-image -O kitten_small.jpg
# Check scoring locally
echo "Uploading testing image, the scoring is..."
curl http://localhost:8080/predictions/densenet161 -T kitten_small.jpg
docker stop torchserve-test
# Deploy model to online endpoint
sed -i 's/{{acr_name}}/'$ACR_NAME'/' $BASE_PATH/$DEPLOYMENT_NAME.yml
EXISTS=$(az ml online-endpoint show -n $ENDPOINT_NAME --query name -o tsv)
# Update endpoint if exists, else create
if [[ $EXISTS == $ENDPOINT_NAME ]]
then
az ml online-endpoint update -f $BASE_PATH/$ENDPOINT_NAME.yml
else
az ml online-endpoint create -f $BASE_PATH/$ENDPOINT_NAME.yml
fi
ENDPOINT_STATUS=$(az ml online-endpoint show --name $ENDPOINT_NAME --query "provisioning_state" -o tsv)
echo "Endpoint status is $ENDPOINT_STATUS"
if [[ $ENDPOINT_STATUS == "Succeeded" ]]
then
echo "Endpoint created successfully"
else
echo "Something went wrong when creating endpoint. Cleaning up..."
az ml online-endpoint delete --name $ENDPOINT_NAME
exit 1
fi
# Create deployment
echo "Creating deployment..."
az ml online-deployment create --name $DEPLOYMENT_NAME --endpoint $ENDPOINT_NAME --file $BASE_PATH/$DEPLOYMENT_NAME.yml --all-traffic
deploy_status=`az ml online-deployment show --name $DEPLOYMENT_NAME --endpoint $ENDPOINT_NAME --query "provisioning_state" -o tsv`
echo $deploy_status
if [[ $deploy_status == "Succeeded" ]]
then
echo "Deployment completed successfully"
else
echo "Deployment failed"
cleanTestingFiles
az ml online-endpoint delete -n $ENDPOINT_NAME --yes
az ml model delete -n $AML_MODEL_NAME --version 1
exit 1
fi
# Get accessToken
echo "Getting access token..."
TOKEN=$(az ml online-endpoint get-credentials -n $ENDPOINT_NAME --query accessToken -o tsv)
# Get scoring url
echo "Getting scoring url..."
SCORING_URL=$(az ml online-endpoint show -n $ENDPOINT_NAME --query scoring_uri -o tsv)
echo "Scoring url is $SCORING_URL"
# Check scoring
echo "Uploading testing image, the scoring is..."
curl -H "Authorization: {Bearer $TOKEN}" -T kitten_small.jpg $SCORING_URL
echo "Tested successfully, cleaning up"
cleanTestingFiles
# Delete endpoint
echo "Deleting endpoint..."
az ml online-endpoint delete -n $ENDPOINT_NAME --yes
# Delete model
echo "Deleting model..."
az ml model archive -n $AML_MODEL_NAME --version 1