File: sample_manage_custom_models.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (85 lines) | stat: -rw-r--r-- 3,691 bytes parent folder | download | duplicates (2)
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
79
80
81
82
83
84
85
# 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_custom_models.py

DESCRIPTION:
    This sample demonstrates how to manage the custom models on your account. To learn
    how to create and train a custom model, look at sample_train_unlabeled_model.py and
    sample_train_labeled_model.py.
USAGE:
    python sample_manage_custom_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_V2 - The shared access signature (SAS) Url of your Azure Blob Storage container
"""

import os


class ManageCustomModelsSample(object):

    def manage_custom_models(self):
        from azure.core.credentials import AzureKeyCredential
        from azure.core.exceptions import ResourceNotFoundError
        from azure.ai.formrecognizer import FormTrainingClient

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

        # [START get_account_properties]
        form_training_client = FormTrainingClient(endpoint=endpoint, credential=AzureKeyCredential(key))
        # First, we see how many custom models we have, and what our limit is
        account_properties = 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]

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

        print("We have models with the following IDs:")
        for model_info in custom_models:
            print(model_info.model_id)
        # [END list_custom_models]

        # let's train a model to use for this sample
        poller = form_training_client.begin_training(container_sas_url, use_training_labels=False)
        model = poller.result()

        # Now we'll get information for the model we just trained
        # [START get_custom_model]
        custom_model = form_training_client.get_custom_model(model_id=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]

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

        try:
            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))
        # [END delete_model]


if __name__ == '__main__':
    sample = ManageCustomModelsSample()
    sample.manage_custom_models()