From 940ea66a7ff89e97d906a9ff4f99cbe7ed363b49 Mon Sep 17 00:00:00 2001 From: Yalin Li Date: Wed, 8 Jan 2025 13:41:35 -0800 Subject: [PATCH 1/4] Add a sample to README.md --- .../azure-ai-documentintelligence/README.md | 40 +++++++++++++++++++ ... sample_convert_to_and_from_dict_async.py} | 20 ++++++++-- ....py => sample_convert_to_and_from_dict.py} | 22 ++++++++-- 3 files changed, 74 insertions(+), 8 deletions(-) rename sdk/documentintelligence/azure-ai-documentintelligence/samples/async_samples/{sample_convert_to_dict_async.py => sample_convert_to_and_from_dict_async.py} (80%) rename sdk/documentintelligence/azure-ai-documentintelligence/samples/{sample_convert_to_dict.py => sample_convert_to_and_from_dict.py} (78%) diff --git a/sdk/documentintelligence/azure-ai-documentintelligence/README.md b/sdk/documentintelligence/azure-ai-documentintelligence/README.md index a2afd7644a71..a385014f8828 100644 --- a/sdk/documentintelligence/azure-ai-documentintelligence/README.md +++ b/sdk/documentintelligence/azure-ai-documentintelligence/README.md @@ -961,6 +961,46 @@ print( +### Parse analyzed result to JSON format + +The result from poller is not JSON parse-able by default, you should call `as_dict()` before parsing to JSON. + + + +```python +from azure.core.credentials import AzureKeyCredential +from azure.ai.documentintelligence import DocumentIntelligenceClient +from azure.ai.documentintelligence.models import AnalyzeResult + +endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"] +key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"] + +document_intelligence_client = DocumentIntelligenceClient(endpoint=endpoint, credential=AzureKeyCredential(key)) +with open(path_to_sample_documents, "rb") as f: + poller = document_intelligence_client.begin_analyze_document("prebuilt-layout", body=f) +result: AnalyzeResult = poller.result() + +# convert the received model to a dictionary +analyze_result_dict = result.as_dict() + +# save the dictionary as JSON content in a JSON file +with open("data.json", "w") as output_file: + json.dump(analyze_result_dict, output_file, indent=4) + +# convert the dictionary back to the original model +model = AnalyzeResult(analyze_result_dict) + +# use the model as normal +print("----Converted from dictionary AnalyzeResult----") +print(f"Model ID: '{model.model_id}'") +print(f"Number of pages analyzed {len(model.pages)}") +print(f"API version used: {model.api_version}") + +print("----------------------------------------") +``` + + + ## Troubleshooting ### General diff --git a/sdk/documentintelligence/azure-ai-documentintelligence/samples/async_samples/sample_convert_to_dict_async.py b/sdk/documentintelligence/azure-ai-documentintelligence/samples/async_samples/sample_convert_to_and_from_dict_async.py similarity index 80% rename from sdk/documentintelligence/azure-ai-documentintelligence/samples/async_samples/sample_convert_to_dict_async.py rename to sdk/documentintelligence/azure-ai-documentintelligence/samples/async_samples/sample_convert_to_and_from_dict_async.py index 3a949a71bd75..1120908ae9e2 100644 --- a/sdk/documentintelligence/azure-ai-documentintelligence/samples/async_samples/sample_convert_to_dict_async.py +++ b/sdk/documentintelligence/azure-ai-documentintelligence/samples/async_samples/sample_convert_to_and_from_dict_async.py @@ -7,14 +7,15 @@ # -------------------------------------------------------------------------- """ -FILE: sample_convert_to_dict_async.py +FILE: sample_convert_to_and_from_dict_async.py DESCRIPTION: - This sample demonstrates how to convert a model returned from an analyze operation - to a JSON serializable dictionary. + This sample demonstrates how to convert models returned from an analyze operation + to and from a dictionary. The dictionary in this sample is then converted to a + JSON file, then the same dictionary is converted back to its original model. USAGE: - python sample_convert_to_dict_async.py + python sample_convert_to_and_from_dict_async.py Set the environment variables with your own values before running the sample: 1) DOCUMENTINTELLIGENCE_ENDPOINT - the endpoint to your Document Intelligence resource. @@ -56,6 +57,17 @@ async def convert_to_and_from_dict_async(): with open("data.json", "w") as output_file: json.dump(analyze_result_dict, output_file, indent=4) + # convert the dictionary back to the original model + model = AnalyzeResult(analyze_result_dict) + + # use the model as normal + print("----Converted from dictionary AnalyzeResult----") + print(f"Model ID: '{model.model_id}'") + print(f"Number of pages analyzed {len(model.pages)}") + print(f"API version used: {model.api_version}") + + print("----------------------------------------") + async def main(): await convert_to_and_from_dict_async() diff --git a/sdk/documentintelligence/azure-ai-documentintelligence/samples/sample_convert_to_dict.py b/sdk/documentintelligence/azure-ai-documentintelligence/samples/sample_convert_to_and_from_dict.py similarity index 78% rename from sdk/documentintelligence/azure-ai-documentintelligence/samples/sample_convert_to_dict.py rename to sdk/documentintelligence/azure-ai-documentintelligence/samples/sample_convert_to_and_from_dict.py index 79f7a4c0b899..76084da11cb4 100644 --- a/sdk/documentintelligence/azure-ai-documentintelligence/samples/sample_convert_to_dict.py +++ b/sdk/documentintelligence/azure-ai-documentintelligence/samples/sample_convert_to_and_from_dict.py @@ -7,14 +7,15 @@ # -------------------------------------------------------------------------- """ -FILE: sample_convert_to_dict.py +FILE: sample_convert_to_and_from_dict.py DESCRIPTION: - This sample demonstrates how to convert a model returned from an analyze operation - to a JSON serializable dictionary. + This sample demonstrates how to convert models returned from an analyze operation + to and from a dictionary. The dictionary in this sample is then converted to a + JSON file, then the same dictionary is converted back to its original model. USAGE: - python sample_convert_to_dict.py + python sample_convert_to_and_from_dict.py Set the environment variables with your own values before running the sample: 1) DOCUMENTINTELLIGENCE_ENDPOINT - the endpoint to your Document Intelligence resource. @@ -34,6 +35,7 @@ def convert_to_and_from_dict(): ) ) + # [START convert] from azure.core.credentials import AzureKeyCredential from azure.ai.documentintelligence import DocumentIntelligenceClient from azure.ai.documentintelligence.models import AnalyzeResult @@ -52,6 +54,18 @@ def convert_to_and_from_dict(): # save the dictionary as JSON content in a JSON file with open("data.json", "w") as output_file: json.dump(analyze_result_dict, output_file, indent=4) + + # convert the dictionary back to the original model + model = AnalyzeResult(analyze_result_dict) + + # use the model as normal + print("----Converted from dictionary AnalyzeResult----") + print(f"Model ID: '{model.model_id}'") + print(f"Number of pages analyzed {len(model.pages)}") + print(f"API version used: {model.api_version}") + + print("----------------------------------------") + # [END convert] if __name__ == "__main__": From 81d9991f7f7ebbe1dc95ce6d9960b63319cfe004 Mon Sep 17 00:00:00 2001 From: Yalin Li Date: Wed, 8 Jan 2025 13:53:00 -0800 Subject: [PATCH 2/4] Run failing tests --- .../tests/test_dac_analyze_layout.py | 1 - .../tests/test_dac_analyze_layout_async.py | 1 - 2 files changed, 2 deletions(-) diff --git a/sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_layout.py b/sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_layout.py index 792de798b6b9..91f2d344d00c 100644 --- a/sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_layout.py +++ b/sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_layout.py @@ -226,7 +226,6 @@ def test_layout_url_barcode(self, client): assert layout.pages[0].barcodes[0].polygon assert layout.pages[0].barcodes[0].confidence > 0.8 - @pytest.mark.skip("Failing in playback. Tracking issue: https://github.com/Azure/azure-sdk-for-python/issues/38881") @skip_flaky_test @DocumentIntelligencePreparer() @recorded_by_proxy diff --git a/sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_layout_async.py b/sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_layout_async.py index e17471ae725e..31efdff0929b 100644 --- a/sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_layout_async.py +++ b/sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_layout_async.py @@ -235,7 +235,6 @@ async def test_layout_url_barcodes(self, client): assert layout.pages[0].barcodes[0].polygon assert layout.pages[0].barcodes[0].confidence > 0.8 - @pytest.mark.skip("Failing in playback. Tracking issue: https://github.com/Azure/azure-sdk-for-python/issues/38881") @skip_flaky_test @DocumentIntelligencePreparer() @recorded_by_proxy_async From 15a91efd19bffb7a9004746d8b445f81496b2a80 Mon Sep 17 00:00:00 2001 From: Yalin Li Date: Wed, 8 Jan 2025 14:51:34 -0800 Subject: [PATCH 3/4] Update assets.json --- .../azure-ai-documentintelligence/assets.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/documentintelligence/azure-ai-documentintelligence/assets.json b/sdk/documentintelligence/azure-ai-documentintelligence/assets.json index fd95bec75946..2dda0b54ce74 100644 --- a/sdk/documentintelligence/azure-ai-documentintelligence/assets.json +++ b/sdk/documentintelligence/azure-ai-documentintelligence/assets.json @@ -2,5 +2,5 @@ "AssetsRepo": "Azure/azure-sdk-assets", "AssetsRepoPrefixPath": "python", "TagPrefix": "python/documentintelligence/azure-ai-documentintelligence", - "Tag": "python/documentintelligence/azure-ai-documentintelligence_bfcdb2d242" + "Tag": "python/documentintelligence/azure-ai-documentintelligence_5f676bd131" } From d06ab463328838dd97ebda9d1b30c93ee310a403 Mon Sep 17 00:00:00 2001 From: Yalin Li Date: Wed, 8 Jan 2025 18:26:24 -0800 Subject: [PATCH 4/4] Address --- sdk/documentintelligence/azure-ai-documentintelligence/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/documentintelligence/azure-ai-documentintelligence/README.md b/sdk/documentintelligence/azure-ai-documentintelligence/README.md index a385014f8828..282508e336c2 100644 --- a/sdk/documentintelligence/azure-ai-documentintelligence/README.md +++ b/sdk/documentintelligence/azure-ai-documentintelligence/README.md @@ -205,6 +205,7 @@ The following section provides several code snippets covering some of the most c - [Manage Your Models](#manage-your-models "Manage Your Models") - [Add-on Capabilities](#add-on-capabilities "Add-on Capabilities") - [Get Raw JSON Result](#get-raw-json-result "Get Raw JSON Result") +- [Parse analyzed result to JSON format](#parse-analyzed-result-to-json-format "Parse analyzed result to JSON format") ### Extract Layout