-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathsample_analyze_contracts.py
More file actions
98 lines (80 loc) · 3.55 KB
/
sample_analyze_contracts.py
File metadata and controls
98 lines (80 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
This code sample shows Prebuilt Contracts operations with the Azure AI Document Intelligence client library.
The async versions of the samples require Python 3.8 or later.
To learn more, please visit the documentation - Quickstart: Document Intelligence (formerly Form Recognizer) SDKs
https://learn.microsoft.com/azure/ai-services/document-intelligence/quickstarts/get-started-sdks-rest-api?pivots=programming-language-python
"""
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from dotenv import find_dotenv, load_dotenv
"""
Remember to remove the key from your code when you're done, and never post it publicly. For production, use
secure methods to store and access your credentials. For more information, see
https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-security?tabs=command-line%2Ccsharp#environment-variables-and-application-configuration
"""
load_dotenv(find_dotenv())
endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]
filepath = "YOUR_FILE_PATH"
document_intelligence_client = DocumentIntelligenceClient(
endpoint=endpoint, credential=AzureKeyCredential(key)
)
with open(filepath, "rb") as f:
poller = document_intelligence_client.begin_analyze_document(
"prebuilt-contract", body=f
)
contract = poller.result()
for idx, document in enumerate(contract.documents):
print("--------Recognizing document #{}--------".format(idx + 1))
doc_type = document.doc_type
if doc_type:
print("Document Type: {}".format(doc_type))
title = document.fields.get("Title")
if title:
print(
"Title: {} has confidence: {}".format(
title.value_string, title.confidence
)
)
effective_date = document.fields.get("EffectiveDate")
if effective_date:
print(
"Effective Date: {} has confidence: {}".format(
effective_date.value_date, effective_date.confidence
)
)
parties = document.fields.get("Parties")
if parties:
for party_idx, party in enumerate(parties.value_array):
print(
"Party #{}: {} has confidence: {}".format(
party_idx + 1, party.value_string, party.confidence
)
)
jurisdictions = document.fields.get("Jurisdictions")
if jurisdictions:
for jurisdiction_idx, jurisdiction in enumerate(jurisdictions.value_array):
print("Jurisdiction #{}:".format(jurisdiction_idx + 1))
# Extracting Region and Clause
region = jurisdiction.value_object.get("Region")
if region:
print(
"Region: {} has confidence: {}".format(
region.value_string, region.confidence
)
)
clause = jurisdiction.value_object.get("Clause")
if clause:
print(
"Clause: {} has confidence: {}".format(
clause.value_string, clause.confidence
)
)
print("--------------------------------------")