File: sample_assign_deployment_resources.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (96 lines) | stat: -rw-r--r-- 3,811 bytes parent folder | download
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
# coding=utf-8
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

"""
FILE: sample_assign_deployment_resources.py

DESCRIPTION:
    This sample demonstrates how to assign deployment resources to a project and deploy a project
    to specific deployment resources. Assigning resources allow you to train your model in one
    resource and deploy them to other assigned resources.

    Assigning deployment resources requires you to authenticate with AAD, you cannot assign
    deployment resources with key based authentication. You must have the role Cognitive Services
    Language Owner assigned to the authoring resource and the target resources.

USAGE:
    python sample_assign_deployment_resources.py

    Set the environment variables with your own values before running the sample:
    1) AZURE_CONVERSATIONS_ENDPOINT             - endpoint for your CLU resource.
    2) AZURE_CLIENT_ID                          - the client ID of your active directory application.
    3) AZURE_TENANT_ID                          - the tenant ID of your active directory application.
    4) AZURE_CLIENT_SECRET                      - the secret of your active directory application.
"""


def sample_assign_resources():
    import os
    from azure.identity import DefaultAzureCredential
    from azure.ai.language.conversations.authoring import ConversationAuthoringClient

    clu_endpoint = os.environ["AZURE_CONVERSATIONS_ENDPOINT"]
    credential = DefaultAzureCredential()

    client = ConversationAuthoringClient(clu_endpoint, credential=credential)

    project_name = "test_project"

    poller = client.begin_assign_deployment_resources(
        project_name=project_name,
        body={
            "resourcesMetadata": [
                {
                    "azureResourceId": "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.CognitiveServices/accounts/<resource-name>",
                    "customDomain": "<resource-name>.cognitiveservices.azure.com",
                    "region": "<region>",
                },
                {
                    "azureResourceId": "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.CognitiveServices/accounts/<resource-name>",
                    "customDomain": "<resource-name>.cognitiveservices.azure.com",
                    "region": "<region>",
                },
            ],
        },
    )

    response = poller.result()
    print(response)


def sample_deploy_model():
    import os
    from azure.identity import DefaultAzureCredential
    from azure.ai.language.conversations.authoring import ConversationAuthoringClient

    clu_endpoint = os.environ["AZURE_CONVERSATIONS_ENDPOINT"]
    credential = DefaultAzureCredential()

    project_name = "test_project"
    deployment_name = "production"

    client = ConversationAuthoringClient(clu_endpoint, credential=credential)

    ## If assigned resource Ids are not provided, project is deployed to all assigned resources

    poller = client.begin_deploy_project(
        project_name=project_name,
        deployment_name=deployment_name,
        deployment={
            "trainedModelLabel": "sample",
            "assignedResourceIds": [
                "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.CognitiveServices/accounts/<resource-name>",
                "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.CognitiveServices/accounts/<resource-name>",
            ],
        },
    )
    response = poller.result()
    print(response)


if __name__ == "__main__":
    sample_assign_resources()
    sample_deploy_model()