3 Star 0 Fork 0

mirrors_microsoft/azureml-examples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
deploy-arm-templates-rest.sh 24.58 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
set -x
# <create_variables>
SUBSCRIPTION_ID=$(az account show --query id | tr -d '\r"')
LOCATION=$(az ml workspace show --query location | tr -d '\r"')
RESOURCE_GROUP=$(az group show --query name | tr -d '\r"')
WORKSPACE=$(az configure -l | jq -r '.[] | select(.name=="workspace") | .value')
schema='$schema'
#</create_variables>
echo -e "Using:\nSUBSCRIPTION_ID=$SUBSCRIPTION_ID\nLOCATION=$LOCATION\nRESOURCE_GROUP=$RESOURCE_GROUP\nWORKSPACE=$WORKSPACE"
# <read_condafile>
CONDA_FILE=$(< cli/endpoints/online/model-1/environment/conda.yml)
# <read_condafile>
#<get_access_token>
TOKEN=$(az account get-access-token --query accessToken -o tsv)
#</get_access_token>
# <set_endpoint_name>
export ENDPOINT_NAME=endpt-`echo $RANDOM`
# </set_endpoint_name>
#<api_version>
API_VERSION="2022-05-01"
#</api_version>
# define how to wait
wait_for_completion () {
status="unknown"
operation_id=""
while [[ $operation_id == "" || -z $operation_id || $operation_id == "null" ]]
do
sleep 5
response=$($1)
operation_id=$(echo $response | jq -r '.properties' | jq -r '.properties' | jq -r '.AzureAsyncOperationUri')
done
while [[ $status != "Succeeded" && $status != "Failed" ]]
do
operation_result=$(curl --location --request GET $operation_id --header "Authorization: Bearer $TOKEN")
status=$(echo $operation_result | jq -r '.status')
echo "Current operation status: $status"
sleep 5
done
if [[ $status == "Failed" ]]
then
error=$(echo $operation_result | jq -r '.error')
echo "Error: $error"
fi
}
# <get_storage_details>
response=$(curl --location --request GET "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.MachineLearningServices/workspaces/$WORKSPACE/datastores?api-version=$API_VERSION&isDefault=true" \
--header "Authorization: Bearer $TOKEN")
AZUREML_DEFAULT_DATASTORE=$(echo $response | jq -r '.value[0].name')
AZUREML_DEFAULT_CONTAINER=$(echo $response | jq -r '.value[0].properties.containerName')
export AZURE_STORAGE_ACCOUNT=$(echo $response | jq -r '.value[0].properties.accountName')
# </get_storage_details>
# <upload_code>
az storage blob upload-batch -d $AZUREML_DEFAULT_CONTAINER/score -s cli/endpoints/online/model-1/onlinescoring --account-name $AZURE_STORAGE_ACCOUNT
# </upload_code>
# <create_code>
curl --location --request PUT "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Resources/deployments/score-sklearn?api-version=2021-04-01" \
-H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
--data-raw "{
\"properties\": {
\"mode\": \"Incremental\",
\"template\": {
\"$schema\": \"https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#\",
\"contentVersion\": \"1.0.0.0\",
\"parameters\": {
\"workspaceName\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the name of the Azure Machine Learning Workspace which will contain this compute.\"
}
},
\"codeAssetName\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the name of the Azure Machine Learning code asset which will be created or updated.\"
}
},
\"codeAssetVersion\": {
\"defaultValue\": \"1\",
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the version of the Azure Machine Learning code asset which will be created or updated.\"
}
},
\"codeUri\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the location of the Azure Machine Learning code asset in a storage account.\"
}
},
\"codeAssetDescription\": {
\"defaultValue\": \"This is a test description for a code asset created by an ARM template\",
\"type\": \"string\"
},
\"isAnonymous\": {
\"defaultValue\": false,
\"type\": \"bool\",
\"metadata\": {
\"description\": \"If the name version are system generated (anonymous registration).\"
}
}
},
\"resources\":
[
{
\"type\": \"Microsoft.MachineLearningServices/workspaces/codes/versions\",
\"apiVersion\": \"$API_VERSION\",
\"name\": \"[concat(parameters(\'workspaceName\'), \'/\', parameters(\'codeAssetName\'), \'/\', parameters(\'codeAssetVersion\'))]\",
\"properties\": {
\"description\": \"[parameters(\'codeAssetDescription\')]\",
\"codeUri\": \"[parameters(\'codeUri\')]\",
\"isAnonymous\": \"[parameters(\'isAnonymous\')]\",
\"properties\": {},
\"tags\": {}
}
}
]
},
\"parameters\": {
\"workspaceName\": {
\"value\": \"$WORKSPACE\"
},
\"codeAssetName\": {
\"value\": \"score-sklearn\"
},
\"codeAssetVersion\": {
\"value\": \"1\"
},
\"codeUri\": {
\"value\": \"https://$AZURE_STORAGE_ACCOUNT.blob.core.windows.net/$AZUREML_DEFAULT_CONTAINER/score\"
}
}
}
}"
# <\create_code>
# <upload_model>
az storage blob upload-batch -d $AZUREML_DEFAULT_CONTAINER/model -s cli/endpoints/online/model-1/model --account-name $AZURE_STORAGE_ACCOUNT
# <\upload_model>
# <create_model>
curl --location --request PUT "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Resources/deployments/sklearn?api-version=2021-04-01" \
-H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
--data-raw "{
\"properties\": {
\"mode\": \"Incremental\",
\"template\": {
\"$schema\": \"https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#\",
\"contentVersion\": \"1.0.0.0\",
\"parameters\": {
\"workspaceName\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the name of the Azure Machine Learning Workspace which will contain this compute.\"
}
},
\"modelAssetName\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the name of the Azure Machine Learning model asset which will be created or updated.\"
}
},
\"modelAssetVersion\": {
\"defaultValue\": \"1\",
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the version of the Azure Machine Learning model asset which will be created or updated.\"
}
},
\"modelUri\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the location of the Azure Machine Learning model asset in a storage account.\"
}
},
\"modelAssetDescription\": {
\"defaultValue\": \"This is a test description for a model asset created by an ARM template\",
\"type\": \"string\"
},
\"isAnonymous\": {
\"defaultValue\": false,
\"type\": \"bool\",
\"metadata\": {
\"description\": \"If the name version are system generated (anonymous registration).\"
}
}
},
\"resources\":
[
{
\"type\": \"Microsoft.MachineLearningServices/workspaces/models/versions\",
\"apiVersion\": \"2021-10-01\",
\"name\": \"[concat(parameters(\'workspaceName\'), \'/\', parameters(\'modelAssetName\'), \'/\', parameters(\'modelAssetVersion\'))]\",
\"properties\": {
\"description\": \"[parameters(\'modelAssetDescription\')]\",
\"modelUri\": \"[parameters(\'modelUri\')]\",
\"isAnonymous\": \"[parameters(\'isAnonymous\')]\",
\"properties\": {},
\"tags\": {}
}
}
]
},
\"parameters\": {
\"workspaceName\": {
\"value\": \"$WORKSPACE\"
},
\"modelAssetName\": {
\"value\": \"score-sklearn\"
},
\"modelAssetVersion\": {
\"value\": \"1\"
},
\"modelUri\": {
\"value\": \"azureml://subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/workspaces/$WORKSPACE/datastores/$AZUREML_DEFAULT_DATASTORE/paths/model/sklearn_regression_model.pkl\"
}
}
}
}"
# <\create_model>
# <read_condafile>
CONDA_FILE=$(cat cli/endpoints/online/model-1/environment/conda.yml)
# <read_condafile>
# <create_environment>
ENV_VERSION=$RANDOM
curl --location --request PUT "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Resources/deployments/sklearn-env?api-version=2021-04-01" \
-H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
--data-raw "{
\"properties\": {
\"mode\": \"Incremental\",
\"template\": {
\"$schema\": \"https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#\",
\"contentVersion\": \"1.0.0.0\",
\"parameters\": {
\"workspaceName\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the name of the Azure Machine Learning Workspace which will contain this compute.\"
}
},
\"environmentAssetName\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the name of the Azure Machine Learning environment asset which will be created or updated.\"
}
},
\"environmentAssetVersion\": {
\"defaultValue\": \"1\",
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the version of the Azure Machine Learning environment asset which will be created or updated.\"
}
},
\"dockerImage\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Docker image path, for example: 'docker.io/tensorflow/serving:latest'.\"
}
},
\"condaFile\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Standard configuration file used by Conda that lets you install any kind of package, including Python, R, and C/C++ packages.\"
}
},
\"environmentAssetDescription\": {
\"defaultValue\": \"This is a test description for a environment asset created by an ARM template\",
\"type\": \"string\"
},
\"isAnonymous\": {
\"defaultValue\": false,
\"type\": \"bool\",
\"metadata\": {
\"description\": \"If the name version are system generated (anonymous registration).\"
}
}
},
\"resources\":
[
{
\"type\": \"Microsoft.MachineLearningServices/workspaces/environments/versions\",
\"apiVersion\": \"$API_VERSION\",
\"name\": \"[concat(parameters(\'workspaceName\'), \'/\', parameters(\'environmentAssetName\'), \'/\', parameters(\'environmentAssetVersion\'))]\",
\"properties\": {
\"description\": \"[parameters(\'environmentAssetDescription\')]\",
\"image\": \"[parameters(\'dockerImage\')]\",
\"condaFile\": \"[parameters(\'condaFile\')]\",
\"isAnonymous\": \"[parameters(\'isAnonymous\')]\"
}
}
]
},
\"parameters\": {
\"workspaceName\": {
\"value\": \"$WORKSPACE\"
},
\"environmentAssetName\": {
\"value\": \"sklearn-env\"
},
\"environmentAssetVersion\": {
\"value\": \"$ENV_VERSION\"
},
\"dockerImage\": {
\"value\": \"mcr.microsoft.com/azureml/openmpi3.1.2-ubuntu18.04:20210727.v1\"
},
\"condaFile\": {
\"value\": \"$CONDA_FILE\"
}
}
}
}"
# <\create_environment>
# <create_endpoint>
curl --location --request PUT "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Resources/deployments/$ENDPOINT_NAME?api-version=2021-04-01" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
--data-raw "{
\"properties\": {
\"mode\": \"Incremental\",
\"template\": {
\"$schema\": \"https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#\",
\"contentVersion\": \"1.0.0.0\",
\"parameters\": {
\"workspaceName\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the name of the Azure Machine Learning Workspace which will contain this compute.\"
}
},
\"location\": {
\"type\": \"string\"
},
\"identityType\": {
\"allowedValues\": [
\"SystemAssigned\",
\"UserAssigned\",
\"SystemAssignedUserAssigned\",
\"None\"
],
\"type\": \"string\",
\"metadata\": {
\"description\": \"The MSI Identity that is associated with this resource.\"
}
},
\"authMode\": {
\"defaultValue\": \"Key\",
\"allowedValues\": [
\"AMLToken\",
\"Key\",
\"AADToken\"
],
\"type\": \"string\"
},
\"onlineEndpointName\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the name of the Azure Machine Learning endpoint which will be created or updated.\"
}
},
\"onlineEndpointDescription\": {
\"defaultValue\": \"This is an online endpoint created by an ARM template\",
\"type\": \"string\"
},
\"onlineEndpointTags\": {
\"defaultValue\": {
\"tag1\": \"value1\",
\"tag2\": \"value2\",
\"tag3\": \"value3\"
},
\"type\": \"object\"
}
},
\"resources\":
[
{
\"type\": \"Microsoft.MachineLearningServices/workspaces/onlineEndpoints\",
\"apiVersion\": \"$API_VERSION\",
\"name\": \"[concat(parameters(\'workspaceName\'), \'/\', parameters(\'onlineEndpointName\'))]\",
\"location\": \"[parameters(\'location\')]\",
\"tags\": \"[parameters(\'onlineEndpointTags\')]\",
\"identity\": {
\"type\": \"[parameters(\'identityType\')]\"
},
\"properties\": {
\"authMode\": \"[parameters(\'authMode\')]\",
\"description\": \"[parameters(\'onlineEndpointDescription\')]\",
}
}
]
},
\"parameters\": {
\"workspaceName\": {
\"value\": \"$WORKSPACE\"
},
\"location\": {
\"value\": \"$LOCATION\"
},
\"onlineEndpointName\": {
\"value\": \"$ENDPOINT_NAME\"
},
\"identityType\": {
\"value\": \"SystemAssigned\"
},
\"authMode\": {
\"value\": \"AMLToken\"
}
}
}
}"
# <\create_endpoint>
# <get_endpoint>
endpoint_cmd() {
curl --location --request GET "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.MachineLearningServices/workspaces/$WORKSPACE/onlineEndpoints/$ENDPOINT_NAME?api-version=$API_VERSION" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $TOKEN"
}
wait_for_completion endpoint_cmd
# </get_endpoint>
# <create_deployment>
resourceScope="/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.MachineLearningServices"
curl --location --request PUT "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Resources/deployments/blue?api-version=2021-04-01" \
-H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
--data-raw "{
\"properties\": {
\"mode\": \"Incremental\",
\"template\": {
\"$schema\": \"https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#\",
\"contentVersion\": \"1.0.0.0\",
\"parameters\": {
\"workspaceName\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the name of the Azure Machine Learning Workspace which will contain this compute.\"
}
},
\"location\": {
\"type\": \"string\"
},
\"appInsightsEnabled\": {
\"type\": \"bool\",
\"defaultValue\": false
},
\"onlineEndpointName\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the name of the Azure Machine Learning online endpoint which will be deployed.\"
}
},
\"onlineDeploymentName\": {
\"defaultValue\": \"blue\",
\"type\": \"string\",
\"metadata\": {
\"description\": \"Specifies the name of the Azure Machine Learning online endpoint which will be deployed.\"
}
},
\"onlineEndpointDescription\": {
\"defaultValue\": \"This is an online endpoint deployment created by an ARM template\",
\"type\": \"string\"
},
\"onlineDeploymentTags\": {
\"defaultValue\": {
\"tag1\": \"value1\",
\"tag2\": \"value2\",
\"tag3\": \"value3\"
},
\"type\": \"object\"
},
\"codeId\": {
\"type\": \"string\"
},
\"scoringScript\": {
\"type\": \"string\",
\"defaultValue\": \"score.py\",
\"metadata\": {
\"description\": \"The script to execute on startup. eg. 'score.py'\"
}
},
\"environmentId\": {
\"type\": \"string\"
},
\"model\": {
\"type\": \"string\"
},
\"endpointComputeType\": {
\"type\": \"string\",
\"allowedValues\": [
\"Managed\",
\"Kubernetes\",
\"AzureMLCompute\"
]
},
\"skuName\": {
\"type\": \"string\",
\"metadata\": {
\"description\": \"The name of the SKU. Ex - P3. It is typically a letter+number code\"
}
},
\"skuCapacity\": {
\"type\": \"int\",
\"defaultValue\": 1,
\"metadata\": {
\"description\": \"If the SKU supports scale out/in then the capacity integer should be included. If scale out/in is not possible for the resource this may be omitted.\"
}
}
},
\"resources\": [
{
\"type\": \"Microsoft.MachineLearningServices/workspaces/onlineEndpoints/deployments\",
\"apiVersion\": \"$API_VERSION\",
\"name\": \"[concat(parameters(\'workspaceName\'), \'/\', parameters(\'onlineEndpointName\'), \'/\', parameters(\'onlineDeploymentName\'))]\",
\"location\": \"[parameters(\'location\')]\",
\"tags\": \"[parameters(\'onlineDeploymentTags\')]\",
\"sku\": {
\"name\": \"[parameters(\'skuName\')]\",
\"capacity\": \"[parameters(\'skuCapacity\')]\"
},
\"properties\": {
\"description\": \"[parameters(\'onlineEndpointDescription\')]\",
\"codeConfiguration\": {
\"codeId\": \"[parameters(\'codeId\')]\",
\"scoringScript\": \"[parameters(\'scoringScript\')]\"
},
\"environmentId\": \"[parameters(\'environmentId\')]\",
\"appInsightsEnabled\": \"[parameters(\'appInsightsEnabled\')]\",
\"endpointComputeType\": \"[parameters(\'endpointComputeType\')]\",
\"model\": \"[parameters(\'model\')]\"
}
}
]
},
\"parameters\": {
\"workspaceName\": {
\"value\": \"$WORKSPACE\"
},
\"location\": {
\"value\": \"$LOCATION\"
},
\"onlineEndpointName\": {
\"value\": \"$ENDPOINT_NAME\"
},
\"onlineDeploymentName\": {
\"value\": \"blue\"
},
\"codeId\": {
\"value\": \"$resourceScope/workspaces/$WORKSPACE/codes/score-sklearn/versions/1\"
},
\"scoringScript\": {
\"value\": \"score.py\"
},
\"environmentId\": {
\"value\": \"$resourceScope/workspaces/$WORKSPACE/environments/sklearn-env/versions/$ENV_VERSION\"
},
\"model\": {
\"value\": \"$resourceScope/workspaces/$WORKSPACE/models/score-sklearn/versions/1\"
},
\"endpointComputeType\": {
\"value\": \"Managed\"
},
\"skuName\": {
\"value\": \"Standard_F2s_v2\"
},
\"skuCapacity\": {
\"value\": 1
}
}
}
}"
# </create_deployment>
# <get_deployment>
deployment_cmd() {
curl --location --request GET "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.MachineLearningServices/workspaces/$WORKSPACE/onlineEndpoints/$ENDPOINT_NAME/deployments/blue?api-version=$API_VERSION" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $TOKEN"
}
wait_for_completion deployment_cmd
scoringUri=$(echo deployment_cmd | jq -r '.properties' | jq -r '.scoringUri')
# </get_deployment>
# <get_access_token>
response=$(curl -H "Content-Length: 0" --location --request POST "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.MachineLearningServices/workspaces/$WORKSPACE/onlineEndpoints/$ENDPOINT_NAME/token?api-version=$API_VERSION" \
--header "Authorization: Bearer $TOKEN")
accessToken=$(echo $response | jq -r '.accessToken')
# </get_access_token>
# <score_endpoint>
curl --location --request POST $scoringUri \
--header "Authorization: Bearer $accessToken" \
--header "Content-Type: application/json" \
--data @cli/endpoints/online/model-1/sample-request.json
# </score_endpoint>
# <get_deployment_logs>
curl --location --request POST "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.MachineLearningServices/workspaces/$WORKSPACE/onlineEndpoints/$ENDPOINT_NAME/deployments/blue/getLogs?api-version=$API_VERSION" \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data-raw "{ \"tail\": 100 }"
# </get_deployment_logs>
# <delete_endpoint>
curl --location --request DELETE "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.MachineLearningServices/workspaces/$WORKSPACE/onlineEndpoints/$ENDPOINT_NAME?api-version=$API_VERSION" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $TOKEN" || true
# </delete_endpoint>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_microsoft/azureml-examples.git
git@gitee.com:mirrors_microsoft/azureml-examples.git
mirrors_microsoft
azureml-examples
azureml-examples
main

搜索帮助