Skip to content
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

[DI] Add a sample to README.md #39085

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -961,6 +962,46 @@ print(

<!-- END SNIPPET -->

### Parse analyzed result to JSON format
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section should be added to the list of samples earlier in the readme


The result from poller is not JSON parse-able by default, you should call `as_dict()` before parsing to JSON.

<!-- SNIPPET:sample_convert_to_and_from_dict.convert -->

```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("----------------------------------------")
```

<!-- END SNIPPET -->

## Troubleshooting

### General
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading