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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
FILE: ml_samples_authentication.py
DESCRIPTION:
These samples demonstrate authenticating a client for multiple clouds.
USAGE:
python ml_samples_authentication.py
Set the environment variables with your own values before running the sample:
1) AZURE_SUBSCRIPTION_ID - The subscription id.
2) RESOURCE_GROUP_NAME - Resource group name.
"""
import os
class MLClientSamples(object):
def ml_auth_azure_default_credential(self):
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_group = os.environ["RESOURCE_GROUP_NAME"]
workspace_name = "test-ws1"
# [START create_ml_client_default_credential]
from azure.ai.ml import MLClient
from azure.identity import AzureAuthorityHosts, DefaultAzureCredential
ml_client = MLClient(
subscription_id=subscription_id,
resource_group_name=resource_group,
workspace_name=workspace_name,
credential=DefaultAzureCredential(),
)
# [END create_ml_client_default_credential]
# [START create_ml_client_from_config_default]
from azure.ai.ml import MLClient
client = MLClient.from_config(credential=DefaultAzureCredential(), path="./sdk/ml/azure-ai-ml/samples/src")
# [END create_ml_client_from_config_default]
# [START create_ml_client_from_config_custom_filename]
from azure.ai.ml import MLClient
client = MLClient.from_config(
credential=DefaultAzureCredential(),
file_name="./sdk/ml/azure-ai-ml/samples/team_workspace_configuration.json",
)
# [END create_ml_client_from_config_custom_filename]
# [START ml_client_create_or_update]
from azure.ai.ml import Input, command
from azure.ai.ml.constants import AssetTypes
from azure.ai.ml.entities import ManagedOnlineEndpoint, UserIdentityConfiguration
client = MLClient(
subscription_id=subscription_id,
resource_group_name=resource_group,
workspace_name=workspace_name,
credential=DefaultAzureCredential(),
)
job = command(
code="./sdk/ml/azure-ai-ml/samples/src",
command="echo hello world",
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu:1",
compute="cpu-cluster",
identity=UserIdentityConfiguration(),
)
client.create_or_update(job)
# [END ml_client_create_or_update]
# [START ml_client_begin_create_or_update]
from random import randint
from azure.ai.ml.entities import ManagedOnlineEndpoint
client = MLClient(
subscription_id=subscription_id,
resource_group_name=resource_group,
workspace_name=workspace_name,
credential=DefaultAzureCredential(),
)
endpoint = ManagedOnlineEndpoint(
name=f"online-endpoint-name-{randint(1, 1000)}",
description="this is a sample online endpoint",
auth_mode="key",
)
created_job = client.begin_create_or_update(endpoint)
# [END ml_client_begin_create_or_update]
# [START user_identity_configuration]
from azure.ai.ml import Input, command
from azure.ai.ml.constants import AssetTypes
from azure.ai.ml.entities import UserIdentityConfiguration
job = command(
code="./sdk/ml/azure-ai-ml/samples/src",
command="python read_data.py --input_data ${{inputs.input_data}}",
inputs={"input_data": Input(type=AssetTypes.MLTABLE, path="./sample_data")},
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu:1",
compute="cpu-cluster",
identity=UserIdentityConfiguration(),
)
# [END user_identity_configuration]
# [START aml_token_configuration]
from azure.ai.ml import Input, command
from azure.ai.ml.constants import AssetTypes
from azure.ai.ml.entities._credentials import AmlTokenConfiguration
node = command(
description="description",
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu:33",
code="./tests/test_configs/training/",
command="python read_data.py --input_data ${{inputs.input_data}}",
inputs={"input_data": Input(type=AssetTypes.MLTABLE, path="./sample_data")},
display_name="builder_command_job",
compute="testCompute",
experiment_name="mfe-test1-dataset",
identity=AmlTokenConfiguration(),
)
# [END aml_token_configuration]
# [START create_ml_client_sovereign_cloud]
from azure.ai.ml import MLClient
from azure.identity import AzureAuthorityHosts, DefaultAzureCredential
kwargs = {"cloud": "AzureChinaCloud"}
ml_client = MLClient(
subscription_id=subscription_id,
resource_group_name=resource_group,
credential=DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_CHINA),
**kwargs,
)
# [END create_ml_client_sovereign_cloud]
if __name__ == "__main__":
sample = MLClientSamples()
sample.ml_auth_azure_default_credential()
|