Skip to content

Commit b493c23

Browse files
author
Yalin Li
authored
[DI] Add a sample to README.md (Azure#39085)
1 parent 8f5c82c commit b493c23

File tree

6 files changed

+76
-11
lines changed

6 files changed

+76
-11
lines changed

sdk/documentintelligence/azure-ai-documentintelligence/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ The following section provides several code snippets covering some of the most c
205205
- [Manage Your Models](#manage-your-models "Manage Your Models")
206206
- [Add-on Capabilities](#add-on-capabilities "Add-on Capabilities")
207207
- [Get Raw JSON Result](#get-raw-json-result "Get Raw JSON Result")
208+
- [Parse analyzed result to JSON format](#parse-analyzed-result-to-json-format "Parse analyzed result to JSON format")
208209

209210
### Extract Layout
210211

@@ -961,6 +962,46 @@ print(
961962

962963
<!-- END SNIPPET -->
963964

965+
### Parse analyzed result to JSON format
966+
967+
The result from poller is not JSON parse-able by default, you should call `as_dict()` before parsing to JSON.
968+
969+
<!-- SNIPPET:sample_convert_to_and_from_dict.convert -->
970+
971+
```python
972+
from azure.core.credentials import AzureKeyCredential
973+
from azure.ai.documentintelligence import DocumentIntelligenceClient
974+
from azure.ai.documentintelligence.models import AnalyzeResult
975+
976+
endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
977+
key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]
978+
979+
document_intelligence_client = DocumentIntelligenceClient(endpoint=endpoint, credential=AzureKeyCredential(key))
980+
with open(path_to_sample_documents, "rb") as f:
981+
poller = document_intelligence_client.begin_analyze_document("prebuilt-layout", body=f)
982+
result: AnalyzeResult = poller.result()
983+
984+
# convert the received model to a dictionary
985+
analyze_result_dict = result.as_dict()
986+
987+
# save the dictionary as JSON content in a JSON file
988+
with open("data.json", "w") as output_file:
989+
json.dump(analyze_result_dict, output_file, indent=4)
990+
991+
# convert the dictionary back to the original model
992+
model = AnalyzeResult(analyze_result_dict)
993+
994+
# use the model as normal
995+
print("----Converted from dictionary AnalyzeResult----")
996+
print(f"Model ID: '{model.model_id}'")
997+
print(f"Number of pages analyzed {len(model.pages)}")
998+
print(f"API version used: {model.api_version}")
999+
1000+
print("----------------------------------------")
1001+
```
1002+
1003+
<!-- END SNIPPET -->
1004+
9641005
## Troubleshooting
9651006

9661007
### General

sdk/documentintelligence/azure-ai-documentintelligence/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/documentintelligence/azure-ai-documentintelligence",
5-
"Tag": "python/documentintelligence/azure-ai-documentintelligence_bfcdb2d242"
5+
"Tag": "python/documentintelligence/azure-ai-documentintelligence_5f676bd131"
66
}
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
# --------------------------------------------------------------------------
88

99
"""
10-
FILE: sample_convert_to_dict_async.py
10+
FILE: sample_convert_to_and_from_dict_async.py
1111
1212
DESCRIPTION:
13-
This sample demonstrates how to convert a model returned from an analyze operation
14-
to a JSON serializable dictionary.
13+
This sample demonstrates how to convert models returned from an analyze operation
14+
to and from a dictionary. The dictionary in this sample is then converted to a
15+
JSON file, then the same dictionary is converted back to its original model.
1516
1617
USAGE:
17-
python sample_convert_to_dict_async.py
18+
python sample_convert_to_and_from_dict_async.py
1819
1920
Set the environment variables with your own values before running the sample:
2021
1) DOCUMENTINTELLIGENCE_ENDPOINT - the endpoint to your Document Intelligence resource.
@@ -56,6 +57,17 @@ async def convert_to_and_from_dict_async():
5657
with open("data.json", "w") as output_file:
5758
json.dump(analyze_result_dict, output_file, indent=4)
5859

60+
# convert the dictionary back to the original model
61+
model = AnalyzeResult(analyze_result_dict)
62+
63+
# use the model as normal
64+
print("----Converted from dictionary AnalyzeResult----")
65+
print(f"Model ID: '{model.model_id}'")
66+
print(f"Number of pages analyzed {len(model.pages)}")
67+
print(f"API version used: {model.api_version}")
68+
69+
print("----------------------------------------")
70+
5971

6072
async def main():
6173
await convert_to_and_from_dict_async()

sdk/documentintelligence/azure-ai-documentintelligence/samples/sample_convert_to_dict.py renamed to sdk/documentintelligence/azure-ai-documentintelligence/samples/sample_convert_to_and_from_dict.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
# --------------------------------------------------------------------------
88

99
"""
10-
FILE: sample_convert_to_dict.py
10+
FILE: sample_convert_to_and_from_dict.py
1111
1212
DESCRIPTION:
13-
This sample demonstrates how to convert a model returned from an analyze operation
14-
to a JSON serializable dictionary.
13+
This sample demonstrates how to convert models returned from an analyze operation
14+
to and from a dictionary. The dictionary in this sample is then converted to a
15+
JSON file, then the same dictionary is converted back to its original model.
1516
1617
USAGE:
17-
python sample_convert_to_dict.py
18+
python sample_convert_to_and_from_dict.py
1819
1920
Set the environment variables with your own values before running the sample:
2021
1) DOCUMENTINTELLIGENCE_ENDPOINT - the endpoint to your Document Intelligence resource.
@@ -34,6 +35,7 @@ def convert_to_and_from_dict():
3435
)
3536
)
3637

38+
# [START convert]
3739
from azure.core.credentials import AzureKeyCredential
3840
from azure.ai.documentintelligence import DocumentIntelligenceClient
3941
from azure.ai.documentintelligence.models import AnalyzeResult
@@ -52,6 +54,18 @@ def convert_to_and_from_dict():
5254
# save the dictionary as JSON content in a JSON file
5355
with open("data.json", "w") as output_file:
5456
json.dump(analyze_result_dict, output_file, indent=4)
57+
58+
# convert the dictionary back to the original model
59+
model = AnalyzeResult(analyze_result_dict)
60+
61+
# use the model as normal
62+
print("----Converted from dictionary AnalyzeResult----")
63+
print(f"Model ID: '{model.model_id}'")
64+
print(f"Number of pages analyzed {len(model.pages)}")
65+
print(f"API version used: {model.api_version}")
66+
67+
print("----------------------------------------")
68+
# [END convert]
5569

5670

5771
if __name__ == "__main__":

sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_layout.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ def test_layout_url_barcode(self, client):
226226
assert layout.pages[0].barcodes[0].polygon
227227
assert layout.pages[0].barcodes[0].confidence > 0.8
228228

229-
@pytest.mark.skip("Failing in playback. Tracking issue: https://github.com/Azure/azure-sdk-for-python/issues/38881")
230229
@skip_flaky_test
231230
@DocumentIntelligencePreparer()
232231
@recorded_by_proxy

sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_layout_async.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ async def test_layout_url_barcodes(self, client):
235235
assert layout.pages[0].barcodes[0].polygon
236236
assert layout.pages[0].barcodes[0].confidence > 0.8
237237

238-
@pytest.mark.skip("Failing in playback. Tracking issue: https://github.com/Azure/azure-sdk-for-python/issues/38881")
239238
@skip_flaky_test
240239
@DocumentIntelligencePreparer()
241240
@recorded_by_proxy_async

0 commit comments

Comments
 (0)