forked from Azure/azure-sdk-for-python
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the reference documentation for onlinedeployment and segregate… (
Azure#27854) * Update the reference documentation for onlinedeployment and segregate the code and samples for MLClient. * Update the path for samples * Remove the subscription Id * Removing keyword from MLClient docstring * fix pylint issue
- Loading branch information
1 parent
f5644b3
commit 635cd97
Showing
4 changed files
with
159 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
page_type: sample | ||
languages: | ||
- python | ||
products: | ||
- azure | ||
- azure-ai-ml | ||
urlFragment: ml-samples | ||
--- | ||
|
||
# Azure Machine Learning Client Library for Python Samples | ||
|
||
These are code samples that show common scenario operations with the Azure Machine Learning Client Library for Python. | ||
|
||
These samples provide example code for additional scenarios commonly encountered while working with Machine Learning Library: | ||
|
||
* [ml_samples_authentication_sovereign_cloud.py](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/ml/azure-ai-ml/samples/ml_samples_authentication_sovereign_cloud.py) - Examples for creating MLClient for non public cloud: | ||
* Set up a MLClient | ||
* List workspaces in the subscription. | ||
|
||
## Prerequisites | ||
|
||
* Python 3.7 or later is required to use this package | ||
* You must have an [Azure subscription](https://azure.microsoft.com/free/) to run these samples. | ||
|
||
## Setup | ||
|
||
1. Install the Azure Machine Learning Client Library for Python with [pip](https://pypi.org/project/pip/): | ||
|
||
```bash | ||
pip install azure-ai-ml | ||
``` | ||
|
||
2. Clone or download this sample repository | ||
3. Open the sample folder in Visual Studio Code or your IDE of choice. | ||
|
||
## Running the samples | ||
|
||
1. Open a terminal window and `cd` to the directory that the samples are saved in. | ||
2. Set the environment variables specified in the sample file you wish to run. | ||
3. Follow the usage described in the file, e.g. `python ml_samples_authentication_sovereign_cloud.py` | ||
|
||
## Next steps | ||
|
||
Check out the [API reference documentation](https://learn.microsoft.com/python/api/overview/azure/ai-ml-readme?view=azure-python) to learn more about what you can do with the Azure Machine Learning Client Library. |
59 changes: 59 additions & 0 deletions
59
sdk/ml/azure-ai-ml/samples/ml_samples_authentication_sovereign_cloud.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# 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_sovereign_cloud.py | ||
DESCRIPTION: | ||
These samples demonstrate authenticating a client for multiple clouds. | ||
USAGE: | ||
python ml_samples_authentication_sovereign_cloud.py | ||
""" | ||
|
||
import os | ||
|
||
class MLClientSamples(object): | ||
|
||
def ml_auth_azure_default_credential(self): | ||
# [START create_ml_client_default_credential] | ||
# Get a credential for authentication | ||
# Default Azure Credentials attempt a chained set of authentication methods, per documentation here: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity | ||
# Alternately, one can specify the AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET to use the EnvironmentCredentialClass. | ||
# The docs above specify all mechanisms which the defaultCredential internally support. | ||
# Enter details of your subscription | ||
subscription_id = "AZURE_SUBSCRIPTION_ID" | ||
resource_group = "RESOURCE_GROUP_NAME" | ||
|
||
# Instantiate a MLClient | ||
from azure.identity import DefaultAzureCredential, AzureAuthorityHosts | ||
from azure.ai.ml import MLClient | ||
|
||
# When using sovereign domains (that is, any cloud other than AZURE_PUBLIC_CLOUD), | ||
# you must use an authority with DefaultAzureCredential. | ||
# Default authority value : AzureAuthorityHosts.AZURE_PUBLIC_CLOUD | ||
# Expected values for authority for sovereign clouds: | ||
# AzureAuthorityHosts.AZURE_CHINA or AzureAuthorityHosts.AZURE_GOVERNMENT | ||
# credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_CHINA) | ||
credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_PUBLIC_CLOUD) | ||
|
||
# When using sovereign domains (that is, any cloud other than AZURE_PUBLIC_CLOUD), | ||
# you must pass in the cloud name in kwargs. Default cloud is AzureCloud | ||
kwargs = {"cloud": "AzureCloud"} | ||
# get a handle to the subscription | ||
ml_client = MLClient(credential, subscription_id, resource_group, **kwargs) | ||
# [END create_ml_client_default_credential] | ||
|
||
from azure.ai.ml.entities import Workspace | ||
# Get a list of workspaces in a resource group | ||
for ws in ml_client.workspaces.list(): | ||
print(ws.name, ":", ws.location, ":", ws.description) | ||
|
||
|
||
if __name__ == '__main__': | ||
sample = MLClientSamples() | ||
sample.ml_auth_azure_default_credential() |