Ejemplo n.º 1
0
class BuildModelRequestPreparation(PerfStressTest):   

    def __init__(self, arguments):
        super().__init__(arguments)

        # read test related env vars
        self.formrecognizer_storage_container_sas_url = os.environ["FORMRECOGNIZER_TRAINING_DATA_CONTAINER_SAS_URL"]
        formrecognizer_test_endpoint = os.environ["FORMRECOGNIZER_TEST_ENDPOINT"]
        form_recognizer_account_key = os.environ["FORMRECOGNIZER_TEST_API_KEY"]

        # assign the clients that will be used in the perf tests
        self.admin_client = DocumentModelAdministrationClient(formrecognizer_test_endpoint, AzureKeyCredential(form_recognizer_account_key))
        self.async_admin_client = AsyncDocumentModelAdministrationClient(formrecognizer_test_endpoint, AzureKeyCredential(form_recognizer_account_key))

    async def close(self):
        """This is run after cleanup."""
        await self.async_admin_client.close()
        self.admin_client.close()
        await super().close()

    def run_sync(self):
        """The synchronous perf test."""
        poller = self.admin_client.begin_build_model(self.formrecognizer_storage_container_sas_url)
        assert poller

    async def run_async(self):
        """The asynchronous perf test."""
        poller = await self.async_admin_client.begin_build_model(self.formrecognizer_storage_container_sas_url)
        assert poller
 def test_build_model_auth_bad_key(self, formrecognizer_test_endpoint,
                                   formrecognizer_test_api_key, **kwargs):
     set_bodiless_matcher()
     client = DocumentModelAdministrationClient(
         formrecognizer_test_endpoint, AzureKeyCredential("xxxx"))
     with pytest.raises(ClientAuthenticationError):
         poller = client.begin_build_model("xx")
def sample_build_model():
    # [START build_model]
    from azure.ai.formrecognizer import DocumentModelAdministrationClient, DocumentBuildMode
    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"]

    document_model_admin_client = DocumentModelAdministrationClient(endpoint, AzureKeyCredential(key))
    poller = document_model_admin_client.begin_build_model(
        container_sas_url, DocumentBuildMode.TEMPLATE, description="my model description"
    )
    model = poller.result()

    print("Model ID: {}".format(model.model_id))
    print("Description: {}".format(model.description))
    print("Model created on: {}\n".format(model.created_on))
    print("Doc types the model can recognize:")
    for name, doc_type in model.doc_types.items():
        print("\nDoc Type: '{}' built with '{}' mode which has the following fields:".format(name, doc_type.build_mode))
        for field_name, field in doc_type.field_schema.items():
            print("Field: '{}' has type '{}' and confidence score {}".format(
                field_name, field["type"], doc_type.field_confidence[field_name]
            ))
    async def test_get_document_analysis_client(self,
                                                formrecognizer_test_endpoint,
                                                formrecognizer_test_api_key,
                                                **kwargs):
        # this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers
        set_custom_default_matcher(
            compare_bodies=False,
            excluded_headers=
            "Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id"
        )
        transport = AioHttpTransport()
        dtc = DocumentModelAdministrationClient(
            endpoint=formrecognizer_test_endpoint,
            credential=AzureKeyCredential(formrecognizer_test_api_key),
            transport=transport)

        async with dtc:
            await dtc.get_account_info()
            assert transport.session is not None
            async with dtc.get_document_analysis_client() as dac:
                assert transport.session is not None
                await (await dac.begin_analyze_document_from_url(
                    "prebuilt-receipt", self.receipt_url_jpg)).wait()
                assert dac._api_version == DocumentAnalysisApiVersion.V2022_01_30_PREVIEW
            await dtc.get_account_info()
            assert transport.session is not None
Ejemplo n.º 5
0
 def test_build_model_auth_bad_key(self, formrecognizer_test_endpoint, formrecognizer_test_api_key, **kwargs):
     # this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers
     set_custom_default_matcher(
         compare_bodies=False, excluded_headers="Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id"
     )
     client = DocumentModelAdministrationClient(formrecognizer_test_endpoint, AzureKeyCredential("xxxx"))
     with pytest.raises(ClientAuthenticationError):
         poller = client.begin_build_model("xx", build_mode="template")
Ejemplo n.º 6
0
    def test_dmac_auth_bad_key(self, formrecognizer_test_endpoint,
                               formrecognizer_test_api_key, **kwargs):
        client = DocumentModelAdministrationClient(
            formrecognizer_test_endpoint, AzureKeyCredential("xxxx"))
        with pytest.raises(ClientAuthenticationError):
            result = client.get_account_info()

        return {}
Ejemplo n.º 7
0
    def __init__(self, arguments):
        super().__init__(arguments)

        # read test related env vars
        self.formrecognizer_storage_container_sas_url = os.environ["FORMRECOGNIZER_TRAINING_DATA_CONTAINER_SAS_URL"]
        formrecognizer_test_endpoint = os.environ["FORMRECOGNIZER_TEST_ENDPOINT"]
        form_recognizer_account_key = os.environ["FORMRECOGNIZER_TEST_API_KEY"]

        # assign the clients that will be used in the perf tests
        self.admin_client = DocumentModelAdministrationClient(formrecognizer_test_endpoint, AzureKeyCredential(form_recognizer_account_key))
        self.async_admin_client = AsyncDocumentModelAdministrationClient(formrecognizer_test_endpoint, AzureKeyCredential(form_recognizer_account_key))
Ejemplo n.º 8
0
def authentication_with_api_key_credential_document_model_admin_client():
    # [START create_dt_client_with_key]
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.formrecognizer import DocumentModelAdministrationClient

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

    document_model_admin_client = DocumentModelAdministrationClient(endpoint, AzureKeyCredential(key))
    # [END create_dt_client_with_key]
    info = document_model_admin_client.get_account_info()
Ejemplo n.º 9
0
def sample_manage_models():
    from azure.core.credentials import AzureKeyCredential
    from azure.core.exceptions import ResourceNotFoundError
    from azure.ai.formrecognizer import DocumentModelAdministrationClient, DocumentBuildMode

    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_info]
    document_model_admin_client = DocumentModelAdministrationClient(
        endpoint=endpoint, credential=AzureKeyCredential(key))

    account_info = document_model_admin_client.get_account_info()
    print(
        "Our account has {} custom models, and we can have at most {} custom models\n"
        .format(account_info.document_model_count,
                account_info.document_model_limit))
    # [END get_account_info]

    # Next, we get a paged list of all of our custom models
    # [START list_models]
    models = document_model_admin_client.list_models()

    print("We have the following 'ready' models with IDs and descriptions:")
    for model in models:
        print("{} | {}".format(model.model_id, model.description))
    # [END list_models]

    # let's build a model to use for this sample
    poller = document_model_admin_client.begin_build_model(
        container_sas_url,
        DocumentBuildMode.TEMPLATE,
        description="model for sample")
    model = poller.result()

    # [START get_model]
    my_model = document_model_admin_client.get_model(model_id=model.model_id)
    print("\nModel ID: {}".format(my_model.model_id))
    print("Description: {}".format(my_model.description))
    print("Model created on: {}".format(my_model.created_on))
    # [END get_model]

    # Finally, we will delete this model by ID
    # [START delete_model]
    document_model_admin_client.delete_model(model_id=my_model.model_id)

    try:
        document_model_admin_client.get_model(model_id=my_model.model_id)
    except ResourceNotFoundError:
        print("Successfully deleted model with ID {}".format(
            my_model.model_id))
Ejemplo n.º 10
0
def authentication_with_azure_active_directory_document_model_admin_client():
    # [START create_dt_client_with_aad]
    """DefaultAzureCredential will use the values from these environment
    variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
    """
    from azure.ai.formrecognizer import DocumentModelAdministrationClient
    from azure.identity import DefaultAzureCredential

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

    document_model_admin_client = DocumentModelAdministrationClient(endpoint, credential)
    # [END create_dt_client_with_aad]
    info = document_model_admin_client.get_account_info()
 async def test_active_directory_auth_async(self):
     token = self.generate_oauth_token()
     endpoint = self.get_oauth_endpoint()
     client = DocumentModelAdministrationClient(endpoint, token)
     async with client:
         info = await client.get_account_info()
     assert info
 def test_form_api_version_document_model_admin_client(self):
     with pytest.raises(ValueError) as excinfo:
         client = DocumentModelAdministrationClient(
             "url", "key", api_version=FormRecognizerApiVersion.V2_1)
     assert "Unsupported API version '2.1'. Please select from: {}\nAPI version '2.1' is " \
            "only available for FormRecognizerClient and FormTrainingClient.".format(
         ", ".join(v.value for v in DocumentAnalysisApiVersion)) == str(excinfo.value)
 def test_bad_api_version_document_model_admin_client(self):
     with pytest.raises(ValueError) as excinfo:
         client = DocumentModelAdministrationClient("url",
                                                    "key",
                                                    api_version="9")
     assert "Unsupported API version '9'. Please select from: {}".format(
         ", ".join(v.value for v in DocumentAnalysisApiVersion)) == str(
             excinfo.value)
def sample_get_operations():
    # [START list_operations]
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.formrecognizer import DocumentModelAdministrationClient

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

    document_model_admin_client = DocumentModelAdministrationClient(
        endpoint=endpoint, credential=AzureKeyCredential(key))

    operations = list(document_model_admin_client.list_operations())

    print("The following document model operations exist under my resource:")
    for operation in operations:
        print("\nOperation ID: {}".format(operation.operation_id))
        print("Operation kind: {}".format(operation.kind))
        print("Operation status: {}".format(operation.status))
        print("Operation percent completed: {}".format(
            operation.percent_completed))
        print("Operation created on: {}".format(operation.created_on))
        print("Operation last updated on: {}".format(
            operation.last_updated_on))
        print("Resource location of successful operation: {}".format(
            operation.resource_location))
    # [END list_operations]

    # [START get_operation]
    # Get an operation by ID
    if operations:
        print("\nGetting operation info by ID: {}".format(
            operations[0].operation_id))
        operation_info = document_model_admin_client.get_operation(
            operations[0].operation_id)
        if operation_info.status == "succeeded":
            print("My {} operation is completed.".format(operation_info.kind))
            result = operation_info.result
            print("Model ID: {}".format(result.model_id))
        elif operation_info.status == "failed":
            print("My {} operation failed.".format(operation_info.kind))
            error = operation_info.error
            print("{}: {}".format(error.code, error.message))
        else:
            print("My operation status is {}".format(operation_info.status))
    else:
        print("No operations found.")
    async def test_get_document_analysis_client(self,
                                                formrecognizer_test_endpoint,
                                                formrecognizer_test_api_key):
        transport = AioHttpTransport()
        dtc = DocumentModelAdministrationClient(
            endpoint=formrecognizer_test_endpoint,
            credential=AzureKeyCredential(formrecognizer_test_api_key),
            transport=transport)

        async with dtc:
            await dtc.get_account_info()
            assert transport.session is not None
            async with dtc.get_document_analysis_client() as dac:
                assert transport.session is not None
                await (await dac.begin_analyze_document_from_url(
                    "prebuilt-receipt", self.receipt_url_jpg)).wait()
                assert dac._api_version == DocumentAnalysisApiVersion.V2021_09_30_PREVIEW
            await dtc.get_account_info()
            assert transport.session is not None
Ejemplo n.º 16
0
    def test_logging_info_dmac_client(self, formrecognizer_test_endpoint,
                                      formrecognizer_test_api_key):
        client = DocumentModelAdministrationClient(
            formrecognizer_test_endpoint,
            AzureKeyCredential(formrecognizer_test_api_key))
        mock_handler = MockHandler()

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

        result = client.get_account_info()

        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
def sample_create_composed_model():
    # [START composed_model]
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.formrecognizer import DocumentModelAdministrationClient, DocumentBuildMode

    endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
    key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
    po_supplies = os.environ['PURCHASE_ORDER_OFFICE_SUPPLIES_SAS_URL']
    po_equipment = os.environ['PURCHASE_ORDER_OFFICE_EQUIPMENT_SAS_URL']
    po_furniture = os.environ['PURCHASE_ORDER_OFFICE_FURNITURE_SAS_URL']
    po_cleaning_supplies = os.environ[
        'PURCHASE_ORDER_OFFICE_CLEANING_SUPPLIES_SAS_URL']

    document_model_admin_client = DocumentModelAdministrationClient(
        endpoint=endpoint, credential=AzureKeyCredential(key))
    supplies_poller = document_model_admin_client.begin_build_model(
        po_supplies,
        DocumentBuildMode.TEMPLATE,
        description="Purchase order-Office supplies")
    equipment_poller = document_model_admin_client.begin_build_model(
        po_equipment,
        DocumentBuildMode.TEMPLATE,
        description="Purchase order-Office Equipment")
    furniture_poller = document_model_admin_client.begin_build_model(
        po_furniture,
        DocumentBuildMode.TEMPLATE,
        description="Purchase order-Furniture")
    cleaning_supplies_poller = document_model_admin_client.begin_build_model(
        po_cleaning_supplies,
        DocumentBuildMode.TEMPLATE,
        description="Purchase order-Cleaning Supplies")
    supplies_model = supplies_poller.result()
    equipment_model = equipment_poller.result()
    furniture_model = furniture_poller.result()
    cleaning_supplies_model = cleaning_supplies_poller.result()

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

    poller = document_model_admin_client.begin_create_composed_model(
        purchase_order_models, description="Office Supplies Composed Model")
    model = poller.result()

    print("Office Supplies Composed Model Info:")
    print("Model ID: {}".format(model.model_id))
    print("Description: {}".format(model.description))
    print("Model created on: {}\n".format(model.created_on))
    print("Doc types the model can recognize:")
    for name, doc_type in model.doc_types.items():
        print("\nDoc Type: '{}' which has the following fields:".format(name))
        for field_name, field in doc_type.field_schema.items():
            print("Field: '{}' has type '{}' and confidence score {}".format(
                field_name, field["type"],
                doc_type.field_confidence[field_name]))
def sample_copy_model(custom_model_id):
    # [START begin_copy_model]
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.formrecognizer import DocumentModelAdministrationClient

    source_endpoint = os.environ["AZURE_FORM_RECOGNIZER_SOURCE_ENDPOINT"]
    source_key = os.environ["AZURE_FORM_RECOGNIZER_SOURCE_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_client = DocumentModelAdministrationClient(endpoint=target_endpoint, credential=AzureKeyCredential(target_key))

    target = target_client.get_copy_authorization(
        description="model copied from other resource"
    )

    source_client = DocumentModelAdministrationClient(endpoint=source_endpoint, credential=AzureKeyCredential(source_key))
    poller = 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 = poller.result()

    print("Model ID: {}".format(model.model_id))
    print("Description: {}".format(model.description))
    print("Model created on: {}\n".format(model.created_on))
    print("Doc types the model can recognize:")
    for name, doc_type in model.doc_types.items():
        print("\nDoc Type: '{}' which has the following fields:".format(name))
        for field_name, field in doc_type.field_schema.items():
            print("Field: '{}' has type '{}' and confidence score {}".format(
                field_name, field["type"], doc_type.field_confidence[field_name]
            ))
    def test_get_document_analysis_client(self, formrecognizer_test_endpoint,
                                          formrecognizer_test_api_key,
                                          **kwargs):
        set_bodiless_matcher()
        transport = RequestsTransport()
        dtc = DocumentModelAdministrationClient(
            endpoint=formrecognizer_test_endpoint,
            credential=AzureKeyCredential(formrecognizer_test_api_key),
            transport=transport)

        with dtc:
            dtc.get_account_info()
            assert transport.session is not None
            with dtc.get_document_analysis_client() as dac:
                assert transport.session is not None
                dac.begin_analyze_document_from_url(
                    "prebuilt-receipt", self.receipt_url_jpg).wait()
                assert dac._api_version == DocumentAnalysisApiVersion.V2021_09_30_PREVIEW
            dtc.get_account_info()
            assert transport.session is not None
Ejemplo n.º 20
0
            print("...{}".format(i + 1, region.page_number))
        for cell in table.cells:
            print("...Cell[{}][{}] has content '{}'".format(
                cell.row_index, cell.column_index, cell.content))
    print("-----------------------------------")
    # [END analyze_custom_documents]


if __name__ == "__main__":
    model_id = None
    if os.getenv("CONTAINER_SAS_URL"):

        from azure.core.credentials import AzureKeyCredential
        from azure.ai.formrecognizer import DocumentModelAdministrationClient, DocumentBuildMode

        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.")

        document_model_admin_client = DocumentModelAdministrationClient(
            endpoint=endpoint, credential=AzureKeyCredential(key))
        model = document_model_admin_client.begin_build_model(
            os.getenv("CONTAINER_SAS_URL"),
            DocumentBuildMode.TEMPLATE).result()
        model_id = model.model_id

    analyze_custom_documents(model_id)