async def sample_copy_model_to_async(custom_model_id): # [START begin_copy_model_to_async] from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer.aio 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)) async with target_client: target = await target_client.get_copy_authorization( description="model copied from other resource" ) source_client = DocumentModelAdministrationClient(endpoint=source_endpoint, credential=AzureKeyCredential(source_key)) async with source_client: poller = await source_client.begin_copy_model_to( 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("Description: {}".format(copied_over_model.description)) print("Model created on: {}\n".format(copied_over_model.created_on)) print("Doc types the model can recognize:") for name, doc_type in copied_over_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] ))
async def sample_manage_models_async(): from azure.core.credentials import AzureKeyCredential from azure.core.exceptions import ResourceNotFoundError from azure.ai.formrecognizer.aio import DocumentModelAdministrationClient from azure.ai.formrecognizer import 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_async] document_model_admin_client = DocumentModelAdministrationClient( endpoint=endpoint, credential=AzureKeyCredential(key)) async with document_model_admin_client: account_info = await 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_async] # Next, we get a paged list of all of our custom models # [START list_models_async] models = document_model_admin_client.list_models() print( "We have the following 'ready' models with IDs and descriptions:") async for model in models: print("{} | {}".format(model.model_id, model.description)) # [END list_models_async] # let's build a model to use for this sample poller = await document_model_admin_client.begin_build_model( container_sas_url, DocumentBuildMode.TEMPLATE, description="model for sample") model = await poller.result() # [START get_model_async] my_model = await 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_async] # Finally, we will delete this model by ID # [START delete_model_async] await document_model_admin_client.delete_model( model_id=my_model.model_id) try: await 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))
async def sample_get_operations_async(): # [START list_operations_async] from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer.aio 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)) async with document_model_admin_client: operations = document_model_admin_client.list_operations() print( "The following document model operations exist under my resource:") async 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_async] # [START get_operation_async] # Get an operation by ID try: first_operation = await operations.__anext__() print("\nGetting operation info by ID: {}".format( first_operation.operation_id)) operation_info = await document_model_admin_client.get_operation( first_operation.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)) except StopAsyncIteration: print("No operations found.")
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)
async def sample_build_model_async(): # [START build_model_async] from azure.ai.formrecognizer.aio import DocumentModelAdministrationClient from azure.ai.formrecognizer import 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)) async with document_model_admin_client: poller = await document_model_admin_client.begin_build_model( container_sas_url, DocumentBuildMode.TEMPLATE, description="my model description" ) model = await 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 main(): model_id = None if os.getenv("CONTAINER_SAS_URL"): from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer.aio import DocumentModelAdministrationClient from azure.ai.formrecognizer import 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) ) async with document_model_admin_client: poller = await document_model_admin_client.begin_build_model( os.getenv("CONTAINER_SAS_URL"), DocumentBuildMode.TEMPLATE ) model = await poller.result() model_id = model.model_id await analyze_custom_documents_async(model_id)
async def sample_create_composed_model_async(): # [START composed_model_async] from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer.aio import DocumentModelAdministrationClient from azure.ai.formrecognizer import 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)) async with document_model_admin_client: supplies_poller = await document_model_admin_client.begin_build_model( po_supplies, DocumentBuildMode.TEMPLATE, description="Purchase order-Office supplies") equipment_poller = await document_model_admin_client.begin_build_model( po_equipment, DocumentBuildMode.TEMPLATE, description="Purchase order-Office Equipment") furniture_poller = await document_model_admin_client.begin_build_model( po_furniture, DocumentBuildMode.TEMPLATE, description="Purchase order-Furniture") cleaning_supplies_poller = await document_model_admin_client.begin_build_model( po_cleaning_supplies, DocumentBuildMode.TEMPLATE, description="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() purchase_order_models = [ supplies_model.model_id, equipment_model.model_id, furniture_model.model_id, cleaning_supplies_model.model_id ] poller = await document_model_admin_client.begin_create_composed_model( purchase_order_models, description="Office Supplies Composed Model") model = await 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 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)
async def test_build_model_auth_bad_key(self, formrecognizer_test_endpoint, formrecognizer_test_api_key): client = DocumentModelAdministrationClient( formrecognizer_test_endpoint, AzureKeyCredential("xxxx")) with pytest.raises(ClientAuthenticationError): async with client: poller = await client.begin_build_model("xx") result = await poller.result()
async 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): async with client: poller = await client.begin_build_model("xx", "template") result = await poller.result() return {}
async def authentication_with_api_key_credential_document_model_admin_client_async(): # [START create_dt_client_with_key_async] from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer.aio 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_async] async with document_model_admin_client: info = await document_model_admin_client.get_account_info()
async 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): async with client: poller = await client.begin_build_model("xx", "template") result = await poller.result()
async def authentication_with_azure_active_directory_document_model_admin_client_async(): # [START create_dt_client_with_aad_async] """DefaultAzureCredential will use the values from these environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET """ from azure.ai.formrecognizer.aio import DocumentModelAdministrationClient from azure.identity.aio import DefaultAzureCredential endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] credential = DefaultAzureCredential() document_model_admin_client = DocumentModelAdministrationClient(endpoint, credential) # [END create_dt_client_with_aad_async] async with document_model_admin_client: info = await document_model_admin_client.get_account_info()
async 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) async with client: result = await 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