Skip to content

Commit 608d1dc

Browse files
author
Yalin Li
authored
[DI] Add samples and update README (Azure#34107)
1 parent 6f79b4d commit 608d1dc

File tree

5 files changed

+195
-9
lines changed

5 files changed

+195
-9
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ Azure AI Document Intelligence ([previously known as Form Recognizer][service-re
1212

1313
[Source code][python-di-src]
1414
| [Package (PyPI)][python-di-pypi]
15+
| [API reference documentation][python-di-ref-docs]
16+
| [Product documentation][python-di-product-docs]
1517
| [Samples][python-di-samples]
1618

19+
## _Disclaimer_
20+
21+
_The API version 2024-02-29-preview is currently only available in some Azure regions, the available regions can be found from [here][python-di-available-regions]._
22+
1723
## Getting started
1824

1925
### Installating the package
@@ -679,7 +685,12 @@ additional questions or comments.
679685
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
680686
[default_azure_credential]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#defaultazurecredential
681687
[azure_sub]: https://azure.microsoft.com/free/
682-
[python-di-product-docs]: https://learn.microsoft.com/azure/applied-ai-services/form-recognizer/overview?view=form-recog-3.0.0
688+
[python-di-src]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/documentintelligence/azure-ai-documentintelligence/azure/ai/documentintelligence
689+
[python-di-pypi]: https://pypi.org/project/azure-ai-documentintelligence/
690+
[python-di-product-docs]: https://learn.microsoft.com/azure/ai-services/document-intelligence/overview?view=doc-intel-4.0.0&viewFallbackFrom=form-recog-3.0.0
691+
[python-di-ref-docs]: https://aka.ms/azsdk/python/documentintelligence/docs
692+
[python-di-samples]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/documentintelligence/azure-ai-documentintelligence/samples
693+
[python-di-available-regions]: https://aka.ms/azsdk/documentintelligence/available-regions
683694
[azure_portal]: https://ms.portal.azure.com/
684695
[regional_endpoints]: https://azure.microsoft.com/global-infrastructure/services/?products=form-recognizer
685696
[cognitive_resource_portal]: https://ms.portal.azure.com/#create/Microsoft.CognitiveServicesFormRecognizer
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
FILE: sample_convert_to_dict_async.py
11+
12+
DESCRIPTION:
13+
This sample demonstrates how to convert a model returned from an analyze operation
14+
to a JSON serializable dictionary.
15+
16+
USAGE:
17+
python sample_convert_to_dict_async.py
18+
19+
Set the environment variables with your own values before running the sample:
20+
1) DOCUMENTINTELLIGENCE_ENDPOINT - the endpoint to your Document Intelligence resource.
21+
2) DOCUMENTINTELLIGENCE_API_KEY - your Document Intelligence API key
22+
"""
23+
24+
import os
25+
import json
26+
import asyncio
27+
28+
29+
async def convert_to_and_from_dict_async():
30+
path_to_sample_documents = os.path.abspath(
31+
os.path.join(
32+
os.path.abspath(__file__),
33+
"..",
34+
"..",
35+
"./sample_forms/forms/Form_1.jpg",
36+
)
37+
)
38+
39+
from azure.core.credentials import AzureKeyCredential
40+
from azure.ai.documentintelligence.aio import DocumentIntelligenceClient
41+
from azure.ai.documentintelligence.models import AnalyzeResult
42+
43+
endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
44+
key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]
45+
46+
document_intelligence_client = DocumentIntelligenceClient(endpoint=endpoint, credential=AzureKeyCredential(key))
47+
async with document_intelligence_client:
48+
with open(path_to_sample_documents, "rb") as f:
49+
poller = await document_intelligence_client.begin_analyze_document(
50+
"prebuilt-layout", analyze_request=f, content_type="application/octet-stream"
51+
)
52+
result: AnalyzeResult = await poller.result()
53+
54+
# convert the received model to a dictionary
55+
analyze_result_dict = result.as_dict()
56+
57+
# save the dictionary as JSON content in a JSON file.
58+
with open("data.json", "w") as output_file:
59+
json.dump(analyze_result_dict, output_file, indent=4)
60+
61+
62+
async def main():
63+
await convert_to_and_from_dict_async()
64+
65+
66+
if __name__ == "__main__":
67+
from azure.core.exceptions import HttpResponseError
68+
from dotenv import find_dotenv, load_dotenv
69+
70+
try:
71+
load_dotenv(find_dotenv())
72+
asyncio.run(main())
73+
except HttpResponseError as error:
74+
print(
75+
"For more information about troubleshooting errors, see the following guide: "
76+
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting"
77+
)
78+
# Examples of how to check an HttpResponseError
79+
# Check by error code:
80+
if error.error is not None:
81+
if error.error.code == "InvalidImage":
82+
print(f"Received an invalid image error: {error.error}")
83+
if error.error.code == "InvalidRequest":
84+
print(f"Received an invalid request error: {error.error}")
85+
# Raise the error again after printing it
86+
raise
87+
# If the inner error is None and then it is possible to check the message to get more information:
88+
if "Invalid request".casefold() in error.message.casefold():
89+
print(f"Uh-oh! Seems there was an invalid request: {error}")
90+
# Raise the error again
91+
raise

sdk/documentintelligence/azure-ai-documentintelligence/samples/aio/sample_copy_model_to_async.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
FILE: sample_copy_model_to_async.py
1111
1212
DESCRIPTION:
13-
This sample demonstrates how to copy a custom model from a source Form Recognizer resource
14-
to a target Form Recognizer resource.
13+
This sample demonstrates how to copy a custom model from a source Document Intelligence resource
14+
to a target Document Intelligence resource.
1515
1616
USAGE:
1717
python sample_copy_model_to_async.py
1818
1919
Set the environment variables with your own values before running the sample:
2020
1) DOCUMENTINTELLIGENCE_ENDPOINT - the endpoint to your Document Intelligence resource.
2121
2) DOCUMENTINTELLIGENCE_API_KEY - your Document Intelligence API key.
22-
3) DOCUMENTINTELLIGENCE_TARGET_ENDPOINT - the endpoint to your target Form Recognizer resource.
23-
4) DOCUMENTINTELLIGENCE_TARGET_API_KEY - your target Form Recognizer API key
22+
3) DOCUMENTINTELLIGENCE_TARGET_ENDPOINT - the endpoint to your target Document Intelligence resource.
23+
4) DOCUMENTINTELLIGENCE_TARGET_API_KEY - your target Document Intelligence API key
2424
5) AZURE_SOURCE_MODEL_ID - the model ID from the source resource to be copied over to the target resource.
2525
- OR -
2626
DOCUMENTINTELLIGENCE_STORAGE_CONTAINER_SAS_URL - The shared access signature (SAS) Url of your Azure Blob Storage container with your training files.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
FILE: sample_convert_to_dict.py
11+
12+
DESCRIPTION:
13+
This sample demonstrates how to convert a model returned from an analyze operation
14+
to a JSON serializable dictionary.
15+
16+
USAGE:
17+
python sample_convert_to_dict.py
18+
19+
Set the environment variables with your own values before running the sample:
20+
1) DOCUMENTINTELLIGENCE_ENDPOINT - the endpoint to your Document Intelligence resource.
21+
2) DOCUMENTINTELLIGENCE_API_KEY - your Document Intelligence API key
22+
"""
23+
24+
import os
25+
import json
26+
27+
28+
def convert_to_and_from_dict():
29+
path_to_sample_documents = os.path.abspath(
30+
os.path.join(
31+
os.path.abspath(__file__),
32+
"..",
33+
"./sample_forms/forms/Form_1.jpg",
34+
)
35+
)
36+
37+
from azure.core.credentials import AzureKeyCredential
38+
from azure.ai.documentintelligence import DocumentIntelligenceClient
39+
from azure.ai.documentintelligence.models import AnalyzeResult
40+
41+
endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
42+
key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]
43+
44+
document_intelligence_client = DocumentIntelligenceClient(endpoint=endpoint, credential=AzureKeyCredential(key))
45+
with open(path_to_sample_documents, "rb") as f:
46+
poller = document_intelligence_client.begin_analyze_document(
47+
"prebuilt-layout", analyze_request=f, content_type="application/octet-stream"
48+
)
49+
result: AnalyzeResult = poller.result()
50+
51+
# convert the received model to a dictionary
52+
analyze_result_dict = result.as_dict()
53+
54+
# save the dictionary as JSON content in a JSON file
55+
with open("data.json", "w") as output_file:
56+
json.dump(analyze_result_dict, output_file, indent=4)
57+
58+
59+
if __name__ == "__main__":
60+
from azure.core.exceptions import HttpResponseError
61+
from dotenv import find_dotenv, load_dotenv
62+
63+
try:
64+
load_dotenv(find_dotenv())
65+
convert_to_and_from_dict()
66+
except HttpResponseError as error:
67+
print(
68+
"For more information about troubleshooting errors, see the following guide: "
69+
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting"
70+
)
71+
# Examples of how to check an HttpResponseError
72+
# Check by error code:
73+
if error.error is not None:
74+
if error.error.code == "InvalidImage":
75+
print(f"Received an invalid image error: {error.error}")
76+
if error.error.code == "InvalidRequest":
77+
print(f"Received an invalid request error: {error.error}")
78+
# Raise the error again after printing it
79+
raise
80+
# If the inner error is None and then it is possible to check the message to get more information:
81+
if "Invalid request".casefold() in error.message.casefold():
82+
print(f"Uh-oh! Seems there was an invalid request: {error}")
83+
# Raise the error again
84+
raise

sdk/documentintelligence/azure-ai-documentintelligence/samples/sample_copy_model_to.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
FILE: sample_copy_model_to.py
1111
1212
DESCRIPTION:
13-
This sample demonstrates how to copy a custom model from a source Form Recognizer resource
14-
to a target Form Recognizer resource.
13+
This sample demonstrates how to copy a custom model from a source Document Intelligence resource
14+
to a target Document Intelligence resource.
1515
1616
USAGE:
1717
python sample_copy_model_to.py
1818
1919
Set the environment variables with your own values before running the sample:
2020
1) DOCUMENTINTELLIGENCE_ENDPOINT - the endpoint to your Document Intelligence resource.
2121
2) DOCUMENTINTELLIGENCE_API_KEY - your Document Intelligence API key.
22-
3) DOCUMENTINTELLIGENCE_TARGET_ENDPOINT - the endpoint to your target Form Recognizer resource.
23-
4) DOCUMENTINTELLIGENCE_TARGET_API_KEY - your target Form Recognizer API key
22+
3) DOCUMENTINTELLIGENCE_TARGET_ENDPOINT - the endpoint to your target Document Intelligence resource.
23+
4) DOCUMENTINTELLIGENCE_TARGET_API_KEY - your target Document Intelligence API key
2424
5) AZURE_SOURCE_MODEL_ID - the model ID from the source resource to be copied over to the target resource.
2525
- OR -
2626
DOCUMENTINTELLIGENCE_STORAGE_CONTAINER_SAS_URL - The shared access signature (SAS) Url of your Azure Blob Storage container with your training files.

0 commit comments

Comments
 (0)