Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 162 additions & 90 deletions tutorials/get-started-notebooks/deploy-model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,12 @@
"import uuid\n",
"\n",
"# Create a unique name for the endpoint\n",
"online_endpoint_name = \"credit-endpoint-\" + str(uuid.uuid4())[:8]"
"online_endpoint_name = \"credit-endpoint-\" + str(uuid.uuid4())[:8]\n",
"\n",
"# Track deployment readiness\n",
"endpoint_ready = False\n",
"blue_deployment_ready = False\n",
"green_deployment_ready = False"
]
},
{
Expand Down Expand Up @@ -293,6 +298,7 @@
"outputs": [],
"source": [
"from azure.ai.ml.entities import ManagedOnlineEndpoint\n",
"from azure.core.exceptions import HttpResponseError\n",
"\n",
"# define an online endpoint\n",
"endpoint = ManagedOnlineEndpoint(\n",
Expand All @@ -302,7 +308,18 @@
" tags={\n",
" \"training_dataset\": \"credit_defaults\",\n",
" },\n",
")"
")\n",
"\n",
"try:\n",
" # create the online endpoint\n",
" endpoint = ml_client.online_endpoints.begin_create_or_update(endpoint).result()\n",
" endpoint_ready = True\n",
"except HttpResponseError as ex:\n",
" endpoint_ready = False\n",
" print(f\"Endpoint creation failed: {ex}\")\n",
" print(\n",
" \"Continuing notebook execution; deployment and related steps will be skipped.\"\n",
" )"
]
},
{
Expand Down Expand Up @@ -474,15 +491,26 @@
},
"outputs": [],
"source": [
"# create the online deployment\n",
"blue_deployment = ml_client.online_deployments.begin_create_or_update(\n",
" blue_deployment\n",
").result()\n",
"\n",
"# blue deployment takes 100% traffic\n",
"# expect the deployment to take approximately 8 to 10 minutes.\n",
"endpoint.traffic = {\"blue\": 100}\n",
"ml_client.online_endpoints.begin_create_or_update(endpoint).result()"
"if endpoint_ready:\n",
" try:\n",
" # create the online deployment\n",
" blue_deployment = ml_client.online_deployments.begin_create_or_update(\n",
" blue_deployment\n",
" ).result()\n",
" blue_deployment_ready = True\n",
"\n",
" # blue deployment takes 100% traffic\n",
" # expect the deployment to take approximately 8 to 10 minutes.\n",
" endpoint.traffic = {\"blue\": 100}\n",
" ml_client.online_endpoints.begin_create_or_update(endpoint).result()\n",
" except HttpResponseError as ex:\n",
" blue_deployment_ready = False\n",
" print(f\"Blue deployment creation failed: {ex}\")\n",
" print(\n",
" \"Continuing notebook execution; blue deployment dependent steps will be skipped.\"\n",
" )\n",
"else:\n",
" print(\"Skipping blue deployment because endpoint creation did not complete.\")"
]
},
{
Expand All @@ -499,13 +527,16 @@
"metadata": {},
"outputs": [],
"source": [
"# return an object that contains metadata for the endpoint\n",
"endpoint = ml_client.online_endpoints.get(name=online_endpoint_name)\n",
"if endpoint_ready:\n",
" # return an object that contains metadata for the endpoint\n",
" endpoint = ml_client.online_endpoints.get(name=online_endpoint_name)\n",
"\n",
"# print a selection of the endpoint's metadata\n",
"print(\n",
" f\"Name: {endpoint.name}\\nStatus: {endpoint.provisioning_state}\\nDescription: {endpoint.description}\"\n",
")"
" # print a selection of the endpoint's metadata\n",
" print(\n",
" f\"Name: {endpoint.name}\\nStatus: {endpoint.provisioning_state}\\nDescription: {endpoint.description}\"\n",
" )\n",
"else:\n",
" print(\"Skipping endpoint status check because endpoint was not created.\")"
]
},
{
Expand All @@ -514,11 +545,14 @@
"metadata": {},
"outputs": [],
"source": [
"# existing traffic details\n",
"print(endpoint.traffic)\n",
"if endpoint_ready:\n",
" # existing traffic details\n",
" print(endpoint.traffic)\n",
"\n",
"# Get the scoring URI\n",
"print(endpoint.scoring_uri)"
" # Get the scoring URI\n",
" print(endpoint.scoring_uri)\n",
"else:\n",
" print(\"Skipping traffic and scoring URI display because endpoint was not created.\")"
]
},
{
Expand All @@ -536,11 +570,15 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"# Create a directory to store the sample request file.\n",
"deploy_dir = \"./deploy\"\n",
"os.makedirs(deploy_dir, exist_ok=True)"
"if blue_deployment_ready:\n",
" # test the blue deployment with the sample data\n",
" ml_client.online_endpoints.invoke(\n",
" endpoint_name=online_endpoint_name,\n",
" deployment_name=\"blue\",\n",
" request_file=\"./deploy/sample-request.json\",\n",
" )\n",
"else:\n",
" print(\"Skipping endpoint invocation because blue deployment was not created.\")"
]
},
{
Expand All @@ -556,17 +594,13 @@
"metadata": {},
"outputs": [],
"source": [
"%%writefile {deploy_dir}/sample-request.json\n",
"{\n",
" \"input_data\": {\n",
" \"columns\": [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22],\n",
" \"index\": [0, 1],\n",
" \"data\": [\n",
" [20000,2,2,1,24,2,2,-1,-1,-2,-2,3913,3102,689,0,0,0,0,689,0,0,0,0],\n",
" [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 9, 8]\n",
" ]\n",
" }\n",
"}"
"if blue_deployment_ready:\n",
" logs = ml_client.online_deployments.get_logs(\n",
" name=\"blue\", endpoint_name=online_endpoint_name, lines=50\n",
" )\n",
" print(logs)\n",
"else:\n",
" print(\"Skipping log retrieval because blue deployment was not created.\")"
]
},
{
Expand All @@ -588,12 +622,38 @@
"metadata": {},
"outputs": [],
"source": [
"# test the blue deployment with the sample data\n",
"ml_client.online_endpoints.invoke(\n",
" endpoint_name=online_endpoint_name,\n",
" deployment_name=\"blue\",\n",
" request_file=\"./deploy/sample-request.json\",\n",
")"
"if endpoint_ready:\n",
" # pick the model to deploy. Here you use the latest version of the registered model\n",
" model = ml_client.models.get(\n",
" name=registered_model_name, version=latest_model_version\n",
" )\n",
"\n",
" # define an online deployment using a more powerful instance type\n",
" # if you run into an out of quota error, change the instance_type to a comparable VM that is available.\n",
" # Learn more on https://azure.microsoft.com/en-us/pricing/details/machine-learning/.\n",
" green_deployment = ManagedOnlineDeployment(\n",
" name=\"green\",\n",
" endpoint_name=online_endpoint_name,\n",
" model=model,\n",
" instance_type=\"Standard_F4s_v2\",\n",
" instance_count=1,\n",
" )\n",
"\n",
" try:\n",
" # create the online deployment\n",
" # expect the deployment to take approximately 8 to 10 minutes\n",
" green_deployment = ml_client.online_deployments.begin_create_or_update(\n",
" green_deployment\n",
" ).result()\n",
" green_deployment_ready = True\n",
" except HttpResponseError as ex:\n",
" green_deployment_ready = False\n",
" print(f\"Green deployment creation failed: {ex}\")\n",
" print(\n",
" \"Continuing notebook execution; green deployment dependent steps will be skipped.\"\n",
" )\n",
"else:\n",
" print(\"Skipping green deployment because endpoint was not created.\")"
]
},
{
Expand All @@ -612,10 +672,15 @@
"metadata": {},
"outputs": [],
"source": [
"logs = ml_client.online_deployments.get_logs(\n",
" name=\"blue\", endpoint_name=online_endpoint_name, lines=50\n",
")\n",
"print(logs)"
"if green_deployment_ready:\n",
" # update definition of the deployment\n",
" green_deployment.instance_count = 2\n",
"\n",
" # update the deployment\n",
" # expect the deployment to take approximately 8 to 10 minutes\n",
" ml_client.online_deployments.begin_create_or_update(green_deployment).result()\n",
"else:\n",
" print(\"Skipping green deployment scaling because green deployment was not created.\")"
]
},
{
Expand All @@ -634,25 +699,11 @@
"metadata": {},
"outputs": [],
"source": [
"# pick the model to deploy. Here you use the latest version of the registered model\n",
"model = ml_client.models.get(name=registered_model_name, version=latest_model_version)\n",
"\n",
"# define an online deployment using a more powerful instance type\n",
"# if you run into an out of quota error, change the instance_type to a comparable VM that is available.\n",
"# Learn more on https://azure.microsoft.com/en-us/pricing/details/machine-learning/.\n",
"green_deployment = ManagedOnlineDeployment(\n",
" name=\"green\",\n",
" endpoint_name=online_endpoint_name,\n",
" model=model,\n",
" instance_type=\"Standard_F4s_v2\",\n",
" instance_count=1,\n",
")\n",
"\n",
"# create the online deployment\n",
"# expect the deployment to take approximately 8 to 10 minutes\n",
"green_deployment = ml_client.online_deployments.begin_create_or_update(\n",
" green_deployment\n",
").result()"
"if endpoint_ready and blue_deployment_ready and green_deployment_ready:\n",
" endpoint.traffic = {\"blue\": 80, \"green\": 20}\n",
" ml_client.online_endpoints.begin_create_or_update(endpoint).result()\n",
"else:\n",
" print(\"Skipping traffic update because deployments are not ready.\")"
]
},
{
Expand All @@ -672,12 +723,15 @@
"metadata": {},
"outputs": [],
"source": [
"# update definition of the deployment\n",
"green_deployment.instance_count = 2\n",
"\n",
"# update the deployment\n",
"# expect the deployment to take approximately 8 to 10 minutes\n",
"ml_client.online_deployments.begin_create_or_update(green_deployment).result()"
"if endpoint_ready and blue_deployment_ready and green_deployment_ready:\n",
" # You can invoke the endpoint several times\n",
" for i in range(30):\n",
" ml_client.online_endpoints.invoke(\n",
" endpoint_name=online_endpoint_name,\n",
" request_file=\"./deploy/sample-request.json\",\n",
" )\n",
"else:\n",
" print(\"Skipping traffic testing because deployments are not ready.\")"
]
},
{
Expand All @@ -694,8 +748,13 @@
"metadata": {},
"outputs": [],
"source": [
"endpoint.traffic = {\"blue\": 80, \"green\": 20}\n",
"ml_client.online_endpoints.begin_create_or_update(endpoint).result()"
"if green_deployment_ready:\n",
" logs = ml_client.online_deployments.get_logs(\n",
" name=\"green\", endpoint_name=online_endpoint_name, lines=50\n",
" )\n",
" print(logs)\n",
"else:\n",
" print(\"Skipping green deployment logs because green deployment was not created.\")"
]
},
{
Expand All @@ -711,12 +770,11 @@
"metadata": {},
"outputs": [],
"source": [
"# You can invoke the endpoint several times\n",
"for i in range(30):\n",
" ml_client.online_endpoints.invoke(\n",
" endpoint_name=online_endpoint_name,\n",
" request_file=\"./deploy/sample-request.json\",\n",
" )"
"if endpoint_ready and blue_deployment_ready and green_deployment_ready:\n",
" endpoint.traffic = {\"blue\": 0, \"green\": 100}\n",
" ml_client.begin_create_or_update(endpoint).result()\n",
"else:\n",
" print(\"Skipping traffic switch because deployments are not ready.\")"
]
},
{
Expand All @@ -732,10 +790,12 @@
"metadata": {},
"outputs": [],
"source": [
"logs = ml_client.online_deployments.get_logs(\n",
" name=\"green\", endpoint_name=online_endpoint_name, lines=50\n",
")\n",
"print(logs)"
"if blue_deployment_ready:\n",
" ml_client.online_deployments.begin_delete(\n",
" name=\"blue\", endpoint_name=online_endpoint_name\n",
" ).result()\n",
"else:\n",
" print(\"Skipping blue deployment deletion because it was not created.\")"
]
},
{
Expand Down Expand Up @@ -769,8 +829,10 @@
"metadata": {},
"outputs": [],
"source": [
"endpoint.traffic = {\"blue\": 0, \"green\": 100}\n",
"ml_client.begin_create_or_update(endpoint).result()"
"if endpoint_ready:\n",
" ml_client.online_endpoints.begin_delete(name=online_endpoint_name).result()\n",
"else:\n",
" print(\"No endpoint cleanup needed because endpoint was not created.\")"
]
},
{
Expand All @@ -787,9 +849,14 @@
"metadata": {},
"outputs": [],
"source": [
"ml_client.online_deployments.begin_delete(\n",
" name=\"blue\", endpoint_name=online_endpoint_name\n",
").result()"
"from azure.core.exceptions import ResourceNotFoundError\n",
"\n",
"try:\n",
" ml_client.online_deployments.begin_delete(\n",
" name=\"blue\", endpoint_name=online_endpoint_name\n",
" ).result()\n",
"except ResourceNotFoundError:\n",
" print(\"Blue deployment was already deleted or not found.\")"
]
},
{
Expand All @@ -810,7 +877,12 @@
"metadata": {},
"outputs": [],
"source": [
"ml_client.online_endpoints.begin_delete(name=online_endpoint_name).result()"
"from azure.core.exceptions import ResourceNotFoundError\n",
"\n",
"try:\n",
" ml_client.online_endpoints.begin_delete(name=online_endpoint_name).result()\n",
"except ResourceNotFoundError:\n",
" print(\"Endpoint was already deleted or not found.\")"
]
}
],
Expand Down
Loading
Loading