async 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()
         async for res in result:
             test = res
Beispiel #2
0
 async 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):
         async with client:
             result = client.list_custom_models()
             async for res in result:
                 test = res
Beispiel #3
0
    async def test_get_form_recognizer_client(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
        transport = AioHttpTransport()
        ftc = FormTrainingClient(endpoint=form_recognizer_account, credential=AzureKeyCredential(form_recognizer_account_key), transport=transport)

        async with ftc:
            await ftc.get_account_properties()
            assert transport.session is not None
            async with ftc.get_form_recognizer_client() as frc:
                assert transport.session is not None
                await frc.recognize_receipts_from_url(self.receipt_url_jpg)
            await ftc.get_account_properties()
            assert transport.session is not None
Beispiel #4
0
async def main():
    sample = DifferentiateOutputModelsTrainedWithAndWithoutLabelsSampleAsync()
    labeled_model_id = None
    unlabeled_model_id = None
    if os.getenv("CONTAINER_SAS_URL_WITH_LABELS_V2") or os.getenv(
            "CONTAINER_SAS_URL_WITHOUT_LABELS_V2"):

        from azure.core.credentials import AzureKeyCredential
        from azure.ai.formrecognizer.aio import FormTrainingClient

        endpoint = os.getenv("AZURE_FORM_RECOGNIZER_ENDPOINT")
        key = os.getenv("AZURE_FORM_RECOGNIZER_KEY")
        labeled = os.getenv("CONTAINER_SAS_URL_WITH_LABELS_V2")
        unlabeled = os.getenv("CONTAINER_SAS_URL_WITHOUT_LABELS_V2")

        if not endpoint or not key:
            raise ValueError(
                "Please provide endpoint and API key to run the samples.")

        form_training_client = FormTrainingClient(
            endpoint=endpoint, credential=AzureKeyCredential(key))

        async with form_training_client:
            if labeled:
                model = await (await form_training_client.begin_training(
                    labeled, use_training_labels=True)).result()
                labeled_model_id = model.model_id
            if unlabeled:
                model = await (await form_training_client.begin_training(
                    unlabeled, use_training_labels=False)).result()
                unlabeled_model_id = model.model_id

    await sample.recognize_custom_forms(labeled_model_id, unlabeled_model_id)
Beispiel #5
0
    async def train_model_with_labels(self):
        from azure.ai.formrecognizer.aio import FormTrainingClient
        from azure.core.credentials import AzureKeyCredential

        form_training_client = FormTrainingClient(
            endpoint=self.endpoint, credential=AzureKeyCredential(self.key))

        async with form_training_client:
            model = await form_training_client.train_model(
                self.container_sas_url, use_training_labels=True)

            # Custom model information
            print("Model ID: {}".format(model.model_id))
            print("Status: {}".format(model.status))
            print("Requested on: {}".format(model.requested_on))
            print("Completed on: {}".format(model.completed_on))

            print("Recognized fields:")
            # looping through the submodels, which contains the fields they were trained on
            # The labels are based on the ones you gave the training document.
            for submodel in model.models:
                print("...The submodel with form type {} has accuracy '{}'".
                      format(submodel.form_type, submodel.accuracy))
                for name, field in submodel.fields.items():
                    print(
                        "...The model found field '{}' to have name '{}' with an accuracy of {}"
                        .format(name, field.name, field.accuracy))

            # Training result information
            for doc in model.training_documents:
                print("Document name: {}".format(doc.document_name))
                print("Document status: {}".format(doc.status))
                print("Document page count: {}".format(doc.page_count))
                print("Document errors: {}".format(doc.errors))
Beispiel #6
0
 async def test_sample_copy_model_async(self, resource_group, location,
                                        form_recognizer_account,
                                        form_recognizer_account_key):
     os.environ['CONTAINER_SAS_URL'] = self.get_settings_value(
         "FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL")
     ftc = FormTrainingClient(
         form_recognizer_account,
         AzureKeyCredential(form_recognizer_account_key))
     container_sas_url = os.environ['CONTAINER_SAS_URL']
     async with ftc:
         poller = await ftc.begin_training(container_sas_url,
                                           use_training_labels=False)
         model = await poller.result()
     os.environ['AZURE_SOURCE_MODEL_ID'] = model.model_id
     os.environ[
         "AZURE_FORM_RECOGNIZER_TARGET_ENDPOINT"] = form_recognizer_account
     os.environ[
         "AZURE_FORM_RECOGNIZER_TARGET_KEY"] = form_recognizer_account_key
     os.environ["AZURE_FORM_RECOGNIZER_TARGET_REGION"] = location
     os.environ["AZURE_FORM_RECOGNIZER_TARGET_RESOURCE_ID"] = \
         "/subscriptions/" + self.get_settings_value("SUBSCRIPTION_ID") + "/resourceGroups/" + \
         resource_group.name + "/providers/Microsoft.CognitiveServices/accounts/" + \
         FormRecognizerTest._FORM_RECOGNIZER_NAME
     _test_file('sample_copy_model_async.py', form_recognizer_account,
                form_recognizer_account_key)
Beispiel #7
0
    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"]

        async with FormTrainingClient(
                endpoint=endpoint,
                credential=AzureKeyCredential(key)) as form_training_client:
            # First, we see how many custom models we have, and what our limit is
            account_properties = await 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_async]

            # Next, we get a paged list of all of our custom models
            # [START list_custom_models_async]
            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
            # [START get_custom_model_async]
            custom_model = await 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("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_async]

            # Finally, we will delete this model by ID
            # [START delete_model_async]
            await form_training_client.delete_model(
                model_id=custom_model.model_id)

            try:
                await 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))
async def main():
    sample = CopyModelSampleAsync()
    model_id = None
    if os.getenv("CONTAINER_SAS_URL"):

        from azure.core.credentials import AzureKeyCredential
        from azure.ai.formrecognizer.aio import FormTrainingClient

        endpoint = os.getenv("AZURE_FORM_RECOGNIZER_ENDPOINT")
        key = os.getenv("AZURE_FORM_RECOGNIZER_KEY")

        if not endpoint or not key:
            raise ValueError(
                "Please provide endpoint and API key to run the samples.")

        form_training_client = FormTrainingClient(
            endpoint=endpoint, credential=AzureKeyCredential(key))
        async with form_training_client:
            model = await (await
                           form_training_client.begin_training(
                               os.getenv("CONTAINER_SAS_URL"),
                               use_training_labels=True)).result()
            model_id = model.model_id

    await sample.copy_model_async(model_id)
Beispiel #9
0
async def main():
    sample = TestDifferentiateOutputLabeledTablesAsync()
    fixed_model_id = None
    dynamic_model_id = None
    if os.getenv("CONTAINER_SAS_URL_FIXED_V2") or os.getenv(
            "CONTAINER_SAS_URL_DYNAMIC_V2"):

        from azure.core.credentials import AzureKeyCredential
        from azure.ai.formrecognizer.aio import FormTrainingClient

        endpoint = os.getenv("AZURE_FORM_RECOGNIZER_ENDPOINT")
        key = os.getenv("AZURE_FORM_RECOGNIZER_KEY")
        fixed = os.getenv("CONTAINER_SAS_URL_FIXED_V2")
        dynamic = os.getenv("CONTAINER_SAS_URL_DYNAMIC_V2")

        if not endpoint or not key:
            raise ValueError(
                "Please provide endpoint and API key to run the samples.")

        form_training_client = FormTrainingClient(
            endpoint=endpoint, credential=AzureKeyCredential(key))

        async with form_training_client:
            if fixed:
                model = await (await form_training_client.begin_training(
                    fixed, use_training_labels=True)).result()
                fixed_model_id = model.model_id
            if dynamic:
                model = await (await form_training_client.begin_training(
                    dynamic, use_training_labels=True)).result()
                dynamic_model_id = model.model_id

    await sample.test_recognize_tables_fixed_rows_async(fixed_model_id)
    await sample.test_recognize_tables_dynamic_rows_async(dynamic_model_id)
 async def test_training_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 = await client.train_model("xx")
Beispiel #11
0
    async def train_model_without_labels(self):
        # [START training_async]
        from azure.ai.formrecognizer.aio import FormTrainingClient
        from azure.core.credentials import AzureKeyCredential

        async with FormTrainingClient(self.endpoint,
                                      AzureKeyCredential(
                                          self.key)) as form_training_client:

            # Default for train_model is `use_labels=False`
            model = await form_training_client.train_model(
                self.container_sas_url)

            # Custom model information
            print("Model ID: {}".format(model.model_id))
            print("Status: {}".format(model.status))
            print("Created on: {}".format(model.created_on))
            print("Last modified: {}".format(model.last_modified))

            print("Recognized fields:")
            # Looping through the submodels, which contains the fields they were trained on
            for submodel in model.models:
                print("...The submodel has form type '{}'".format(
                    submodel.form_type))
                for name, field in submodel.fields.items():
                    print("...The model found field '{}' to have label '{}'".
                          format(name, field.label))
        # [END training_async]
        # Training result information
            for doc in model.training_documents:
                print("Document name: {}".format(doc.document_name))
                print("Document status: {}".format(doc.status))
                print("Document page count: {}".format(doc.page_count))
                print("Document errors: {}".format(doc.errors))
Beispiel #12
0
 async def test_active_directory_auth_async(self):
     token = self.generate_oauth_token()
     endpoint = self.get_oauth_endpoint()
     client = FormTrainingClient(endpoint, token)
     async with client:
         props = await client.get_account_properties()
     self.assertIsNotNone(props)
Beispiel #13
0
 async 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):
         async with client:
             result = await client.get_account_properties()
 async def test_delete_model_empty_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 = await client.delete_model("")
Beispiel #15
0
 async def test_sample_recognize_custom_forms_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
     os.environ['CONTAINER_SAS_URL'] = self.get_settings_value("FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL")
     ftc = FormTrainingClient(form_recognizer_account,  AzureKeyCredential(form_recognizer_account_key))
     container_sas_url = os.environ['CONTAINER_SAS_URL']
     poller = await ftc.begin_training(container_sas_url, use_training_labels=False)
     model = await poller.result()
     os.environ['CUSTOM_TRAINED_MODEL_ID'] = model.model_id
     _test_file('sample_recognize_custom_forms_async.py', form_recognizer_account, form_recognizer_account_key)
 def test_document_api_version_form_training_client(self):
     with pytest.raises(ValueError) as excinfo:
         client = FormTrainingClient(
             "url",
             "key",
             api_version=DocumentAnalysisApiVersion.V2021_09_30_PREVIEW)
     assert "Unsupported API version '2021-09-30-preview'. Please select from: {}\nAPI version '2021-09-30-preview' is " \
            "only available for DocumentAnalysisClient and DocumentModelAdministrationClient.".format(
         ", ".join(v.value for v in FormRecognizerApiVersion)) == str(excinfo.value)
Beispiel #17
0
 async def test_training_auth_bad_key(self, formrecognizer_test_endpoint,
                                      formrecognizer_test_api_key):
     client = FormTrainingClient(formrecognizer_test_endpoint,
                                 AzureKeyCredential("xxxx"))
     with self.assertRaises(ClientAuthenticationError):
         async with client:
             poller = await client.begin_training("xx",
                                                  use_training_labels=False)
             result = await poller.result()
Beispiel #18
0
 async def test_training_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):
         poller = await client.begin_training("xx",
                                              use_training_labels=False)
         result = await poller.result()
    async 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 = await client.get_account_properties()

        self.assertIsNotNone(properties.custom_model_limit)
        self.assertIsNotNone(properties.custom_model_count)
Beispiel #20
0
    async def authentication_with_api_key_credential_form_training_client_async(self):
        # [START create_ft_client_with_key_async]
        from azure.core.credentials import AzureKeyCredential
        from azure.ai.formrecognizer.aio 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_async]
        async with form_training_client:
            properties = await form_training_client.get_account_properties()
    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

        async with FormTrainingClient(endpoint=self.endpoint,
                                      credential=AzureKeyCredential(
                                          self.key)) as form_training_client:
            # First, we see how many custom models we have, and what our limit is
            account_properties = await 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))
            # [END get_account_properties_async]

            # Next, we get a paged list of all of our custom models
            # [START list_model_infos_async]
            custom_models = form_training_client.list_model_infos()

            print("We have models with the following ids:")

            # Let's pull out the first model
            first_model = None
            async for model in custom_models:
                print(model.model_id)
                if not first_model:
                    first_model = model
            # [END list_model_infos_async]

            # Now we'll get the first custom model in the paged list
            # [START get_custom_model_async]
            custom_model = await form_training_client.get_custom_model(
                model_id=first_model.model_id)
            print("Model ID: {}".format(custom_model.model_id))
            print("Status: {}".format(custom_model.status))
            print("Created on: {}".format(custom_model.created_on))
            print("Last modified: {}".format(custom_model.last_modified))
            # [END get_custom_model_async]

            # Finally, we will delete this model by ID
            # [START delete_model_async]
            await form_training_client.delete_model(
                model_id=custom_model.model_id)

            try:
                await 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))
Beispiel #22
0
    async def copy_model_async(self):
        from azure.core.credentials import AzureKeyCredential
        from azure.ai.formrecognizer.aio 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_async]
        target_client = FormTrainingClient(
            endpoint=target_endpoint,
            credential=AzureKeyCredential(target_key))

        async with target_client:
            target = await target_client.get_copy_authorization(
                resource_region=target_region, resource_id=target_resource_id)
        # [END get_copy_authorization_async]

        # [START copy_model_async]
        source_client = FormTrainingClient(
            endpoint=source_endpoint,
            credential=AzureKeyCredential(source_key))
        target_client = FormTrainingClient(
            endpoint=target_endpoint,
            credential=AzureKeyCredential(target_key))

        async with source_client:
            copy = await source_client.copy_model(model_id=source_model_id,
                                                  target=target)

        async with target_client:
            copied_over_model = await target_client.get_custom_model(
                copy.model_id)
            print("Model ID: {}".format(copied_over_model.model_id))
            print("Status: {}".format(copied_over_model.status))
    async def copy_model_async(self, custom_model_id):
        from azure.core.credentials import AzureKeyCredential
        from azure.ai.formrecognizer.aio 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.getenv("AZURE_SOURCE_MODEL_ID", custom_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_async]
        target_client = FormTrainingClient(
            endpoint=target_endpoint,
            credential=AzureKeyCredential(target_key))

        async with target_client:
            target = await target_client.get_copy_authorization(
                resource_region=target_region, resource_id=target_resource_id)
        # model ID that target client will use to access the model once copy is complete
        print("Model ID: {}".format(target["modelId"]))
        # [END get_copy_authorization_async]

        # [START copy_model_async]
        source_client = FormTrainingClient(
            endpoint=source_endpoint,
            credential=AzureKeyCredential(source_key))

        async with source_client:
            poller = await source_client.begin_copy_model(
                model_id=source_model_id,
                target=
                target  # output from target client's call to get_copy_authorization()
            )
            copied_over_model = await poller.result()

            print("Model ID: {}".format(copied_over_model.model_id))
            print("Status: {}".format(copied_over_model.status))
    async def authentication_with_azure_active_directory_form_training_client_async(
            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_async]
        from azure.ai.formrecognizer.aio import FormTrainingClient
        from azure.identity.aio import DefaultAzureCredential

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

        form_training_client = FormTrainingClient(endpoint, credential)
        # [END create_ft_client_with_aad_async]
        properties = await form_training_client.get_account_properties()
Beispiel #25
0
    async def train_model_with_labels(self):
        from azure.ai.formrecognizer.aio import FormTrainingClient
        from azure.core.credentials import AzureKeyCredential

        endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
        key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
        container_sas_url = os.environ["CONTAINER_SAS_URL"]

        form_training_client = FormTrainingClient(
            endpoint=endpoint, credential=AzureKeyCredential(key))

        async with form_training_client:
            poller = await form_training_client.begin_training(
                container_sas_url,
                use_training_labels=True,
                model_name="mymodel")
            model = await poller.result()

            # Custom model information
            print("Model ID: {}".format(model.model_id))
            print("Status: {}".format(model.status))
            print("Model name: {}".format(model.model_name))
            print("Is this a composed model?: {}".format(
                model.properties.is_composed_model))
            print("Training started on: {}".format(model.training_started_on))
            print("Training completed on: {}".format(
                model.training_completed_on))

            print("Recognized fields:")
            # looping through the submodels, which contains the fields they were trained on
            # The labels are based on the ones you gave the training document.
            for submodel in model.submodels:
                print("...The submodel has model ID: {}".format(
                    submodel.model_id))
                print(
                    "...The submodel with form type {} has an average accuracy '{}'"
                    .format(submodel.form_type, submodel.accuracy))
                for name, field in submodel.fields.items():
                    print(
                        "...The model found the field '{}' with an accuracy of {}"
                        .format(name, field.accuracy))

            # Training result information
            for doc in model.training_documents:
                print("Document name: {}".format(doc.name))
                print("Document status: {}".format(doc.status))
                print("Document page count: {}".format(doc.page_count))
                print("Document errors: {}".format(doc.errors))
Beispiel #26
0
 async def test_sample_differentiate_output_models_trained_with_and_without_labels_async(
         self, resource_group, location, form_recognizer_account, form_recognizer_account_key
 ):
     os.environ['CONTAINER_SAS_URL'] = self.get_settings_value("FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL")
     ftc = FormTrainingClient(form_recognizer_account,  AzureKeyCredential(form_recognizer_account_key))
     container_sas_url = os.environ['CONTAINER_SAS_URL']
     async with ftc:
         poller = await ftc.begin_training(container_sas_url, use_training_labels=False)
         unlabeled_model = await poller.result()
         poller = await ftc.begin_training(container_sas_url, use_training_labels=True)
         labeled_model = await poller.result()
     os.environ["ID_OF_MODEL_TRAINED_WITH_LABELS"] = labeled_model.model_id
     os.environ["ID_OF_MODEL_TRAINED_WITHOUT_LABELS"] = unlabeled_model.model_id
     _test_file('sample_differentiate_output_models_trained_with_and_without_labels_async.py',
                form_recognizer_account,
                form_recognizer_account_key
                )
    async def test_logging_info_ft_client(self, formrecognizer_test_endpoint,
                                          formrecognizer_test_api_key):
        client = FormTrainingClient(
            formrecognizer_test_endpoint,
            AzureKeyCredential(formrecognizer_test_api_key))
        mock_handler = MockHandler()

        logger = logging.getLogger("azure")
        logger.addHandler(mock_handler)
        logger.setLevel(logging.INFO)
        async with client:
            result = await 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
Beispiel #28
0
    async def train_model_without_labels(self):
        # [START training_async]
        from azure.ai.formrecognizer.aio import FormTrainingClient
        from azure.core.credentials import AzureKeyCredential

        endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
        key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
        container_sas_url = os.environ["CONTAINER_SAS_URL"]

        async with FormTrainingClient(
                endpoint, AzureKeyCredential(key)) as form_training_client:

            poller = await form_training_client.begin_training(
                container_sas_url, use_training_labels=False)
            model = await poller.result()

            # Custom model information
            print("Model ID: {}".format(model.model_id))
            print("Status: {}".format(model.status))
            print("Model name: {}".format(model.model_name))
            print("Training started on: {}".format(model.training_started_on))
            print("Training completed on: {}".format(
                model.training_completed_on))

            print("Recognized fields:")
            # Looping through the submodels, which contains the fields they were trained on
            for submodel in model.submodels:
                print("...The submodel has form type '{}'".format(
                    submodel.form_type))
                for name, field in submodel.fields.items():
                    print("...The model found field '{}' to have label '{}'".
                          format(name, field.label))
        # [END training_async]
        # Training result information
            for doc in model.training_documents:
                print("Document name: {}".format(doc.name))
                print("Document status: {}".format(doc.status))
                print("Document page count: {}".format(doc.page_count))
                print("Document errors: {}".format(doc.errors))
 def test_bad_api_version_form_training_client(self):
     with pytest.raises(ValueError) as excinfo:
         client = FormTrainingClient("url", "key", api_version="9")
     assert "Unsupported API version '9'. Please select from: {}".format(
         ", ".join(v.value
                   for v in FormRecognizerApiVersion)) == str(excinfo.value)
    async def create_composed_model_async(self):
        # [START begin_create_composed_model_async]
        from azure.core.credentials import AzureKeyCredential
        from azure.ai.formrecognizer.aio import FormTrainingClient

        endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
        key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
        po_supplies = os.environ['PURCHASE_ORDER_OFFICE_SUPPLIES_SAS_URL_V2']
        po_equipment = os.environ['PURCHASE_ORDER_OFFICE_EQUIPMENT_SAS_URL_V2']
        po_furniture = os.environ['PURCHASE_ORDER_OFFICE_FURNITURE_SAS_URL_V2']
        po_cleaning_supplies = os.environ[
            'PURCHASE_ORDER_OFFICE_CLEANING_SUPPLIES_SAS_URL_V2']

        form_training_client = FormTrainingClient(
            endpoint=endpoint, credential=AzureKeyCredential(key))
        async with form_training_client:
            supplies_poller = await form_training_client.begin_training(
                po_supplies,
                use_training_labels=True,
                model_name="Purchase order - Office supplies")
            equipment_poller = await form_training_client.begin_training(
                po_equipment,
                use_training_labels=True,
                model_name="Purchase order - Office Equipment")
            furniture_poller = await form_training_client.begin_training(
                po_furniture,
                use_training_labels=True,
                model_name="Purchase order - Furniture")
            cleaning_supplies_poller = await form_training_client.begin_training(
                po_cleaning_supplies,
                use_training_labels=True,
                model_name="Purchase order - Cleaning Supplies")
            supplies_model = await supplies_poller.result()
            equipment_model = await equipment_poller.result()
            furniture_model = await furniture_poller.result()
            cleaning_supplies_model = await cleaning_supplies_poller.result()

            models_trained_with_labels = [
                supplies_model.model_id, equipment_model.model_id,
                furniture_model.model_id, cleaning_supplies_model.model_id
            ]

            poller = await form_training_client.begin_create_composed_model(
                models_trained_with_labels,
                model_name="Office Supplies Composed Model")
            model = await poller.result()

        print("Office Supplies Composed Model Info:")
        print("Model ID: {}".format(model.model_id))
        print("Model name: {}".format(model.model_name))
        print("Is this a composed model?: {}".format(
            model.properties.is_composed_model))
        print("Status: {}".format(model.status))
        print("Composed model creation started on: {}".format(
            model.training_started_on))
        print("Creation completed on: {}".format(model.training_completed_on))

        # [END begin_create_composed_model_async]

        print("Recognized fields:")
        for submodel in model.submodels:
            print("The submodel has model ID: {}".format(submodel.model_id))
            print(
                "...The submodel with form type {} has an average accuracy '{}'"
                .format(submodel.form_type, submodel.accuracy))
            for name, field in submodel.fields.items():
                print(
                    "...The model found the field '{}' with an accuracy of {}".
                    format(name, field.accuracy))

        # Training result information
        for doc in model.training_documents:
            print("Document was used to train model with ID: {}".format(
                doc.model_id))
            print("Document name: {}".format(doc.name))
            print("Document status: {}".format(doc.status))
            print("Document page count: {}".format(doc.page_count))
            print("Document errors: {}".format(doc.errors))