Ejemplo n.º 1
0
 def test_account_properties_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_account_properties()
    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))
Ejemplo n.º 3
0
    def test_get_form_recognizer_client(self, resource_group, location,
                                        form_recognizer_account,
                                        form_recognizer_account_key):
        transport = RequestsTransport()
        ftc = FormTrainingClient(
            endpoint=form_recognizer_account,
            credential=AzureKeyCredential(form_recognizer_account_key),
            transport=transport)

        with ftc:
            ftc.get_account_properties()
            assert transport.session is not None
            with ftc.get_form_recognizer_client() as frc:
                assert transport.session is not None
                frc.begin_recognize_receipts_from_url(
                    self.receipt_url_jpg).wait()
            ftc.get_account_properties()
            assert transport.session is not None
Ejemplo n.º 4
0
    def authentication_with_api_key_credential_form_training_client(self):
        # [START create_ft_client_with_key]
        from azure.core.credentials import AzureKeyCredential
        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, AzureKeyCredential(key))
        # [END create_ft_client_with_key]
        properties = form_training_client.get_account_properties()
Ejemplo n.º 5
0
    def test_account_properties(self, resource_group, location,
                                form_recognizer_account,
                                form_recognizer_account_key):
        client = FormTrainingClient(
            form_recognizer_account,
            AzureKeyCredential(form_recognizer_account_key))
        properties = client.get_account_properties()

        self.assertIsNotNone(properties.custom_model_limit)
        self.assertIsNotNone(properties.custom_model_count)
Ejemplo n.º 6
0
    def test_get_form_recognizer_client_v2(self, formrecognizer_test_endpoint,
                                           formrecognizer_test_api_key):
        transport = RequestsTransport()
        ftc = FormTrainingClient(
            endpoint=formrecognizer_test_endpoint,
            credential=AzureKeyCredential(formrecognizer_test_api_key),
            transport=transport,
            api_version="2.1")

        with ftc:
            ftc.get_account_properties()
            assert transport.session is not None
            with ftc.get_form_recognizer_client() as frc:
                assert transport.session is not None
                frc.begin_recognize_receipts_from_url(
                    self.receipt_url_jpg).wait()
                assert frc._api_version == FormRecognizerApiVersion.V2_1
            ftc.get_account_properties()
            assert transport.session is not None
Ejemplo n.º 7
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))
Ejemplo n.º 8
0
    def authentication_with_azure_active_directory_form_training_client(self):
        """DefaultAzureCredential will use the values from these environment
        variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
        """
        # [START create_ft_client_with_aad]
        from azure.ai.formrecognizer import FormTrainingClient
        from azure.identity import DefaultAzureCredential

        endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
        credential = DefaultAzureCredential()

        form_training_client = FormTrainingClient(endpoint, credential)
        # [END create_ft_client_with_aad]
        properties = form_training_client.get_account_properties()
    def test_logging_info_ft_client(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
        client = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key))
        mock_handler = MockHandler()

        logger = logging.getLogger("azure")
        logger.addHandler(mock_handler)
        logger.setLevel(logging.INFO)

        result = client.get_account_properties()

        for message in mock_handler.messages:
            if message.levelname == "INFO":
                # not able to use json.loads here. At INFO level only API key should be REDACTED
                if message.message.find("Ocp-Apim-Subscription-Key") != -1:
                    assert message.message.find("REDACTED") != -1
                else:
                    assert message.message.find("REDACTED") == -1
poller = form_recognizer_client.begin_recognize_custom_forms_from_url(
    model_id=model_id, form_url=formUrl)
result = poller.result()

for recognized_form in result:
    print("Form type: {}".format(recognized_form.form_type))
    for name, field in recognized_form.fields.items():
        print(
            "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)
Ejemplo n.º 11
0
 def test_account_properties_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_account_properties()