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.
# Licensed under the MIT License.
# ------------------------------------
"""
FILE: sample_manage_projects.py
DESCRIPTION:
This sample demonstrates some common authoring operation snippets with the ConversationAuthoringClient.
USAGE:
python sample_manage_projects.py
Set the environment variables with your own values before running the sample:
1) AZURE_CONVERSATIONS_ENDPOINT - endpoint for your CLU resource.
2) AZURE_CONVERSATIONS_KEY - API key for your CLU resource.
3) AZURE_CONVERSATIONS_PROJECT_NAME - project name for your existing CLU conversations project
"""
import uuid
def sample_export_project():
import os
from azure.core.rest import HttpRequest
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.conversations.authoring import ConversationAuthoringClient
clu_endpoint = os.environ["AZURE_CONVERSATIONS_ENDPOINT"]
clu_key = os.environ["AZURE_CONVERSATIONS_KEY"]
existing_project_name = os.environ["AZURE_CONVERSATIONS_PROJECT_NAME"]
client = ConversationAuthoringClient(
clu_endpoint, AzureKeyCredential(clu_key)
)
poller = client.begin_export_project(
project_name=existing_project_name,
string_index_type="Utf16CodeUnit",
exported_project_format="Conversation"
)
job_state = poller.result()
print(f"Export project status: {job_state['status']}")
request = HttpRequest("GET", job_state["resultUrl"])
response = client.send_request(request)
exported_project = response.json()
return exported_project
def sample_import_project(exported_project, project_name):
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.conversations.authoring import ConversationAuthoringClient
clu_endpoint = os.environ["AZURE_CONVERSATIONS_ENDPOINT"]
clu_key = os.environ["AZURE_CONVERSATIONS_KEY"]
print(f"Importing project as '{project_name}'")
client = ConversationAuthoringClient(
clu_endpoint, AzureKeyCredential(clu_key)
)
poller = client.begin_import_project(
project_name=project_name,
project=exported_project
)
response = poller.result()
print(f"Import project status: {response['status']}")
return project_name
def sample_train_model(project_name):
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.conversations.authoring import ConversationAuthoringClient
clu_endpoint = os.environ["AZURE_CONVERSATIONS_ENDPOINT"]
clu_key = os.environ["AZURE_CONVERSATIONS_KEY"]
client = ConversationAuthoringClient(
clu_endpoint, AzureKeyCredential(clu_key)
)
print(f"Training model under label 'sample'.")
poller = client.begin_train(
project_name=project_name,
configuration={"modelLabel": "sample", "trainingMode": "standard"},
)
response = poller.result()
print(f"Train model status: {response['status']}")
def sample_deploy_model(project_name):
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.conversations.authoring import ConversationAuthoringClient
clu_endpoint = os.environ["AZURE_CONVERSATIONS_ENDPOINT"]
clu_key = os.environ["AZURE_CONVERSATIONS_KEY"]
deployment_name = "production"
client = ConversationAuthoringClient(
clu_endpoint, AzureKeyCredential(clu_key)
)
print(f"Deploying 'sample' model to 'production'.")
poller = client.begin_deploy_project(
project_name=project_name,
deployment_name=deployment_name,
deployment={"trainedModelLabel": "sample"},
)
response = poller.result()
print(f"Model '{response['modelId']}' deployed to '{response['deploymentName']}'")
def sample_delete_project(project_name):
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.conversations.authoring import ConversationAuthoringClient
clu_endpoint = os.environ["AZURE_CONVERSATIONS_ENDPOINT"]
clu_key = os.environ["AZURE_CONVERSATIONS_KEY"]
client = ConversationAuthoringClient(
clu_endpoint, AzureKeyCredential(clu_key)
)
poller = client.begin_delete_project(
project_name=project_name
)
poller.result()
print(f"Deleted project {project_name}")
if __name__ == '__main__':
project_name = "test_project" + str(uuid.uuid4())
try:
print("Exporting project...")
project = sample_export_project()
print("Importing project...")
project_name = sample_import_project(project, project_name)
print("Training model...")
sample_train_model(project_name)
print("Deploying model...")
sample_deploy_model(project_name)
finally:
print("Deleting project...")
sample_delete_project(project_name)
|