def manage_custom_models(self): 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_V2"] # [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() print( "Our account has {} custom models, and we can have at most {} custom models\n" .format(account_properties.custom_model_count, account_properties.custom_model_limit)) # [END get_account_properties] # Next, we get a paged list of all of our custom models # [START list_custom_models] custom_models = form_training_client.list_custom_models() print("We have models with the following IDs:") for model in custom_models: print(model.model_id) # [END list_custom_models] # 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=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)) print("Is this a composed model?: {}".format( custom_model.properties.is_composed_model)) print("Training started on: {}".format( custom_model.training_started_on)) print("Training completed on: {}".format( custom_model.training_completed_on)) # [END get_custom_model] # Finally, we will delete this model by ID # [START delete_model] form_training_client.delete_model(model_id=custom_model.model_id) try: form_training_client.get_custom_model( model_id=custom_model.model_id) except ResourceNotFoundError: print("Successfully deleted model with id {}".format( custom_model.model_id))
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"] 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() print( "Our account has {} custom models, and we can have at most {} custom models\n" .format(account_properties.custom_model_count, account_properties.custom_model_limit)) # [END get_account_properties] # Next, we get a paged list of all of our custom models # [START list_custom_models] 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 # [START get_custom_model] custom_model = form_training_client.get_custom_model( model_id=first_model.model_id) print("\nModel ID: {}".format(custom_model.model_id)) print("Status: {}".format(custom_model.status)) print("Training started on: {}".format( custom_model.training_started_on)) print("Training completed on: {}".format( custom_model.training_completed_on)) # [END get_custom_model] # Finally, we will delete this model by ID # [START delete_model] form_training_client.delete_model(model_id=custom_model.model_id) try: form_training_client.get_custom_model( model_id=custom_model.model_id) except ResourceNotFoundError: print("Successfully deleted model with id {}".format( custom_model.model_id))
def test_get_model_auth_bad_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormTrainingClient(form_recognizer_account, AzureKeyCredential("xxxx")) with self.assertRaises(ClientAuthenticationError): result = client.get_custom_model("xx")
def test_get_model_none_model_id(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormTrainingClient( form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) with self.assertRaises(ValueError): result = client.get_custom_model(None)
def copy_model(self): from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer import FormTrainingClient source_endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] source_key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] target_endpoint = os.environ["AZURE_FORM_RECOGNIZER_TARGET_ENDPOINT"] target_key = os.environ["AZURE_FORM_RECOGNIZER_TARGET_KEY"] source_model_id = os.environ["AZURE_SOURCE_MODEL_ID"] target_region = os.environ["AZURE_FORM_RECOGNIZER_TARGET_REGION"] target_resource_id = os.environ[ "AZURE_FORM_RECOGNIZER_TARGET_RESOURCE_ID"] # [START get_copy_authorization] target_client = FormTrainingClient( endpoint=target_endpoint, credential=AzureKeyCredential(target_key)) target = target_client.get_copy_authorization( resource_region=target_region, resource_id=target_resource_id) # [END get_copy_authorization] # [START begin_copy_model] source_client = FormTrainingClient( endpoint=source_endpoint, credential=AzureKeyCredential(source_key)) target_client = FormTrainingClient( endpoint=target_endpoint, credential=AzureKeyCredential(target_key)) poller = source_client.begin_copy_model(model_id=source_model_id, target=target) copy = poller.result() copied_over_model = target_client.get_custom_model(copy.model_id) print("Model ID: {}".format(copied_over_model.model_id)) print("Status: {}".format(copied_over_model.status))
# Next, we get a paged list of all of our custom models 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) # </snippet_manage_list> # <snippet_manage_getmodel> model_id = "<model_id from the Train a Model sample>" custom_model = form_training_client.get_custom_model(model_id=model_id) print("Model ID: {}".format(custom_model.model_id)) print("Status: {}".format(custom_model.status)) print("Training started on: {}".format(custom_model.training_started_on)) print("Training completed on: {}".format(custom_model.training_completed_on)) # </snippet_manage_getmodel> # <snippet_manage_delete> form_training_client.delete_model(model_id=custom_model.model_id) try: form_training_client.get_custom_model(model_id=custom_model.model_id) except ResourceNotFoundError: print("Successfully deleted model with id {}".format( custom_model.model_id)) # </snippet_manage_delete>
def test_get_model_auth_bad_key(self, formrecognizer_test_endpoint, formrecognizer_test_api_key): client = FormTrainingClient(formrecognizer_test_endpoint, AzureKeyCredential("xxxx")) with self.assertRaises(ClientAuthenticationError): result = client.get_custom_model("xx")