1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
FILE: sample_manage_models.py
DESCRIPTION:
This sample demonstrates how to manage the models on your account. To learn
how to build a model, look at sample_build_model.py.
USAGE:
python sample_manage_models.py
Set the environment variables with your own values before running the sample:
1) AZURE_FORM_RECOGNIZER_ENDPOINT - the endpoint to your Form Recognizer resource.
2) AZURE_FORM_RECOGNIZER_KEY - your Form Recognizer API key
3) CONTAINER_SAS_URL - The shared access signature (SAS) Url of your Azure Blob Storage container
"""
import os
def sample_manage_models():
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import ResourceNotFoundError
from azure.ai.formrecognizer import DocumentModelAdministrationClient, ModelBuildMode
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
container_sas_url = os.environ["CONTAINER_SAS_URL"]
# [START get_resource_details]
document_model_admin_client = DocumentModelAdministrationClient(endpoint=endpoint, credential=AzureKeyCredential(key))
account_details = document_model_admin_client.get_resource_details()
print("Our resource has {} custom models, and we can have at most {} custom models\n".format(
account_details.custom_document_models.count, account_details.custom_document_models.limit
))
# [END get_resource_details]
# Next, we get a paged list of all of our custom models
# [START list_document_models]
models = document_model_admin_client.list_document_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_document_models]
# let's build a model to use for this sample
poller = document_model_admin_client.begin_build_document_model(ModelBuildMode.TEMPLATE, blob_container_url=container_sas_url, description="model for sample")
model = poller.result()
# [START get_document_model]
my_model = document_model_admin_client.get_document_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_document_model]
# Finally, we will delete this model by ID
# [START delete_document_model]
document_model_admin_client.delete_document_model(model_id=my_model.model_id)
try:
document_model_admin_client.get_document_model(model_id=my_model.model_id)
except ResourceNotFoundError:
print("Successfully deleted model with ID {}".format(my_model.model_id))
# [END delete_document_model]
if __name__ == '__main__':
sample_manage_models()
|