-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[formrecognizer] update samples to train a model where a model_id is needed #18789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
e56052a
9fa88db
0e80a75
2ed1308
ebd0736
b223c45
d76da15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,9 @@ | |
| to a target Form Recognizer resource. The resource id and the resource region can be found | ||
| in the azure portal. | ||
|
|
||
| The model used in this sample can be created in the sample_train_model_with_labels_async.py using the | ||
| training files in https://aka.ms/azsdk/formrecognizer/sampletrainingfiles | ||
|
|
||
| USAGE: | ||
| python sample_copy_model_async.py | ||
|
|
||
|
|
@@ -23,6 +26,9 @@ | |
| 3) AZURE_FORM_RECOGNIZER_TARGET_ENDPOINT - the endpoint to your target Form Recognizer resource. | ||
| 4) AZURE_FORM_RECOGNIZER_TARGET_KEY - your target Form Recognizer API key | ||
| 5) AZURE_SOURCE_MODEL_ID - the model ID from the source resource to be copied over to the target resource. | ||
| - OR - | ||
| CONTAINER_SAS_URL - The shared access signature (SAS) Url of your Azure Blob Storage container with your forms. | ||
| A model will be trained and used to to run the sample. | ||
| 6) AZURE_FORM_RECOGNIZER_TARGET_REGION - the region the target resource was created in | ||
| 7) AZURE_FORM_RECOGNIZER_TARGET_RESOURCE_ID - the entire resource ID to the target resource | ||
| """ | ||
|
|
@@ -33,15 +39,15 @@ | |
|
|
||
| class CopyModelSampleAsync(object): | ||
|
|
||
| async def copy_model_async(self): | ||
| async def copy_model_async(self, custom_model_id): | ||
| from azure.core.credentials import AzureKeyCredential | ||
| from azure.ai.formrecognizer.aio import FormTrainingClient | ||
|
|
||
| source_endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] | ||
| source_key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] | ||
| target_endpoint = os.environ["AZURE_FORM_RECOGNIZER_TARGET_ENDPOINT"] | ||
| target_key = os.environ["AZURE_FORM_RECOGNIZER_TARGET_KEY"] | ||
| source_model_id = os.environ["AZURE_SOURCE_MODEL_ID"] | ||
| source_model_id = os.getenv("AZURE_SOURCE_MODEL_ID") or custom_model_id | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: i think you can do os.getenv("AZURE_SOURCE_MODEL_ID", custom_model_id)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch! I was using os.environ before and then missed this when I switched to getenv :) |
||
| target_region = os.environ["AZURE_FORM_RECOGNIZER_TARGET_REGION"] | ||
| target_resource_id = os.environ["AZURE_FORM_RECOGNIZER_TARGET_RESOURCE_ID"] | ||
|
|
||
|
|
@@ -74,7 +80,27 @@ async def copy_model_async(self): | |
|
|
||
| async def main(): | ||
| sample = CopyModelSampleAsync() | ||
| await sample.copy_model_async() | ||
| model_id = None | ||
| if os.getenv("CONTAINER_SAS_URL"): | ||
|
|
||
| from azure.core.credentials import AzureKeyCredential | ||
| from azure.ai.formrecognizer.aio import FormTrainingClient | ||
|
|
||
| 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.") | ||
|
|
||
| form_training_client = FormTrainingClient( | ||
| endpoint=endpoint, credential=AzureKeyCredential(key) | ||
| ) | ||
| async with form_training_client: | ||
| model = await (await form_training_client.begin_training( | ||
| os.getenv("CONTAINER_SAS_URL"), use_training_labels=True)).result() | ||
| model_id = model.model_id | ||
|
|
||
| await sample.copy_model_async(model_id) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| This sample demonstrates the differences in output that arise when begin_recognize_custom_forms | ||
| is called with custom models trained with labels and without labels. The models used in this | ||
| sample can be created in sample_train_model_with_labels_async.py and sample_train_model_without_labels_async.py | ||
| using the training files found here: https://aka.ms/azsdk/formrecognizer/sampletrainingfiles | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. smart! |
||
|
|
||
| For a more general example of recognizing custom forms, see sample_recognize_custom_forms_async.py | ||
|
|
||
|
|
@@ -26,7 +27,13 @@ | |
| 1) AZURE_FORM_RECOGNIZER_ENDPOINT - the endpoint to your Cognitive Services resource. | ||
| 2) AZURE_FORM_RECOGNIZER_KEY - your Form Recognizer API key | ||
| 3) ID_OF_MODEL_TRAINED_WITH_LABELS - the ID of your custom model trained with labels | ||
| -OR- | ||
| CONTAINER_SAS_URL_WITH_LABELS - The shared access signature (SAS) Url of your Azure Blob Storage container with | ||
| your labeled data. A model will be trained and used to to run the sample. | ||
| 4) ID_OF_MODEL_TRAINED_WITHOUT_LABELS - the ID of your custom model trained without labels | ||
| -OR- | ||
| CONTAINER_SAS_URL_WITHOUT_LABELS - The shared access signature (SAS) Url of your Azure Blob Storage container with | ||
| your forms. A model will be trained and used to to run the sample. | ||
| """ | ||
|
|
||
| import os | ||
|
|
@@ -41,14 +48,14 @@ def format_bounding_box(bounding_box): | |
|
|
||
| class DifferentiateOutputModelsTrainedWithAndWithoutLabelsSampleAsync(object): | ||
|
|
||
| async def recognize_custom_forms(self): | ||
| async def recognize_custom_forms(self, labeled_model_id, unlabeled_model_id): | ||
| from azure.core.credentials import AzureKeyCredential | ||
| from azure.ai.formrecognizer.aio import FormRecognizerClient | ||
|
|
||
| endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] | ||
| key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] | ||
| model_trained_with_labels_id = os.environ["ID_OF_MODEL_TRAINED_WITH_LABELS"] | ||
| model_trained_without_labels_id = os.environ["ID_OF_MODEL_TRAINED_WITHOUT_LABELS"] | ||
| model_trained_with_labels_id = os.getenv("ID_OF_MODEL_TRAINED_WITH_LABELS") or labeled_model_id | ||
| model_trained_without_labels_id = os.getenv("ID_OF_MODEL_TRAINED_WITHOUT_LABELS") or unlabeled_model_id | ||
|
|
||
| path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "./sample_forms/forms/Form_1.jpg")) | ||
| async with FormRecognizerClient( | ||
|
|
@@ -120,7 +127,34 @@ async def recognize_custom_forms(self): | |
|
|
||
| async def main(): | ||
| sample = DifferentiateOutputModelsTrainedWithAndWithoutLabelsSampleAsync() | ||
| await sample.recognize_custom_forms() | ||
| labeled_model_id = None | ||
| unlabeled_model_id = None | ||
| if os.getenv("CONTAINER_SAS_URL_WITH_LABELS") or os.getenv("CONTAINER_SAS_URL_WITHOUT_LABELS"): | ||
|
|
||
| from azure.core.credentials import AzureKeyCredential | ||
| from azure.ai.formrecognizer.aio import FormTrainingClient | ||
|
|
||
| endpoint = os.getenv("AZURE_FORM_RECOGNIZER_ENDPOINT") | ||
| key = os.getenv("AZURE_FORM_RECOGNIZER_KEY") | ||
| labeled = os.getenv("CONTAINER_SAS_URL_WITH_LABELS") | ||
| unlabeled = os.getenv("CONTAINER_SAS_URL_WITHOUT_LABELS") | ||
|
|
||
| if not endpoint or not key: | ||
| raise ValueError("Please provide endpoint and API key to run the samples.") | ||
|
|
||
| form_training_client = FormTrainingClient( | ||
| endpoint=endpoint, credential=AzureKeyCredential(key) | ||
| ) | ||
|
|
||
| async with form_training_client: | ||
| if labeled: | ||
| model = await (await form_training_client.begin_training(labeled, use_training_labels=True)).result() | ||
| labeled_model_id = model.model_id | ||
| if unlabeled: | ||
| model = await (await form_training_client.begin_training(unlabeled, use_training_labels=False)).result() | ||
| unlabeled_model_id = model.model_id | ||
|
|
||
| await sample.recognize_custom_forms(labeled_model_id, unlabeled_model_id) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicate
to toit is in other places tooThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And not related, but in my head, when I read it in Spanish it sounded like: https://www.youtube.com/watch?v=5AkDqm-cEgg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for catching this! love the song :)