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]))
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 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] ))
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 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")
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))
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)
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])) # [END begin_copy_model] if __name__ == '__main__': model_id = None if os.getenv("CONTAINER_SAS_URL"): from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer import DocumentModelAdministrationClient endpoint = os.getenv("AZURE_FORM_RECOGNIZER_SOURCE_ENDPOINT") key = os.getenv("AZURE_FORM_RECOGNIZER_SOURCE_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")).result() model_id = model.model_id sample_copy_model(model_id)