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 test_list_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.list_custom_models()
         for res in result:
             test = res
Example #3
0
    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))
            "Field '{}' has label '{}' with value '{}' and a confidence score of {}"
            .format(name, field.label_data.text if field.label_data else name,
                    field.value, field.confidence))
# </snippet_analyze>

# <snippet_manage_count>
account_properties = form_training_client.get_account_properties()
print(
    "Our account has {} custom models, and we can have at most {} custom models"
    .format(account_properties.custom_model_count,
            account_properties.custom_model_limit))
# </snippet_manage_count>

# <snippet_manage_list>
# 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))
 def test_list_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.list_custom_models()
         for res in result:
             test = res