From 6ae3681d0a3d2baf62e2fdc01b2468fe806a260b Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Mon, 7 Dec 2020 15:00:32 -0800 Subject: [PATCH] update sample so it can run in ci --- .../sample_manage_custom_models_async.py | 16 +++++++++------- .../samples/sample_manage_custom_models.py | 16 +++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_manage_custom_models_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_manage_custom_models_async.py index 1add217a55a3..a9742cf49951 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_manage_custom_models_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_manage_custom_models_async.py @@ -19,6 +19,7 @@ Set the environment variables with your own values before running the sample: 1) AZURE_FORM_RECOGNIZER_ENDPOINT - the endpoint to your Cognitive Services resource. 2) AZURE_FORM_RECOGNIZER_KEY - your Form Recognizer API key + 3) CONTAINER_SAS_URL - The shared access signature (SAS) Url of your Azure Blob Storage container """ import os @@ -28,14 +29,15 @@ class ManageCustomModelsSampleAsync(object): async def manage_custom_models(self): - # [START get_account_properties_async] from azure.core.credentials import AzureKeyCredential from azure.core.exceptions import ResourceNotFoundError from azure.ai.formrecognizer.aio import FormTrainingClient endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] + container_sas_url = os.environ["CONTAINER_SAS_URL"] + # [START get_account_properties_async] async with FormTrainingClient( endpoint=endpoint, credential=AzureKeyCredential(key) ) as form_training_client: @@ -51,17 +53,17 @@ async def manage_custom_models(self): custom_models = form_training_client.list_custom_models() print("We have models with the following IDs:") - - # Let's pull out the first model - first_model = await custom_models.__anext__() - print(first_model.model_id) async for model in custom_models: print(model.model_id) # [END list_custom_models_async] - # Now we'll get information for the first custom model in the paged list + # let's train a model to use for this sample + poller = await form_training_client.begin_training(container_sas_url, use_training_labels=False) + model = await poller.result() + + # Now we'll get information for the model we just trained # [START get_custom_model_async] - custom_model = await form_training_client.get_custom_model(model_id=first_model.model_id) + custom_model = await form_training_client.get_custom_model(model_id=model.model_id) print("\nModel ID: {}".format(custom_model.model_id)) print("Status: {}".format(custom_model.status)) print("Model name: {}".format(custom_model.model_name)) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_manage_custom_models.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_manage_custom_models.py index 30556d196953..45df5fc41838 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_manage_custom_models.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_manage_custom_models.py @@ -19,6 +19,7 @@ Set the environment variables with your own values before running the sample: 1) AZURE_FORM_RECOGNIZER_ENDPOINT - the endpoint to your Cognitive Services resource. 2) AZURE_FORM_RECOGNIZER_KEY - your Form Recognizer API key + 3) CONTAINER_SAS_URL - The shared access signature (SAS) Url of your Azure Blob Storage container """ import os @@ -27,14 +28,15 @@ class ManageCustomModelsSample(object): def manage_custom_models(self): - # [START get_account_properties] from azure.core.credentials import AzureKeyCredential from azure.core.exceptions import ResourceNotFoundError from azure.ai.formrecognizer import FormTrainingClient endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] + container_sas_url = os.environ["CONTAINER_SAS_URL"] + # [START get_account_properties] form_training_client = FormTrainingClient(endpoint=endpoint, credential=AzureKeyCredential(key)) # First, we see how many custom models we have, and what our limit is account_properties = form_training_client.get_account_properties() @@ -48,17 +50,17 @@ def manage_custom_models(self): custom_models = form_training_client.list_custom_models() print("We have models with the following IDs:") - - # Let's pull out the first model - first_model = next(custom_models) - print(first_model.model_id) for model in custom_models: print(model.model_id) # [END list_custom_models] - # Now we'll get information for the first custom model in the paged list + # let's train a model to use for this sample + poller = form_training_client.begin_training(container_sas_url, use_training_labels=False) + model = poller.result() + + # Now we'll get information for the model we just trained # [START get_custom_model] - custom_model = form_training_client.get_custom_model(model_id=first_model.model_id) + custom_model = form_training_client.get_custom_model(model_id=model.model_id) print("\nModel ID: {}".format(custom_model.model_id)) print("Status: {}".format(custom_model.status)) print("Model name: {}".format(custom_model.model_name))