Hello @Hadi Abou-Ghaida
This is an altered sample code for Batch analysis
The original code is https://github.com/Azure-Samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Prebuilt_model/sample_analyze_invoices.py
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeResult, AnalyzeDocumentRequest
def analyze_invoices(invoice_urls):
endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]
document_intelligence_client = DocumentIntelligenceClient(endpoint=endpoint, credential=AzureKeyCredential(key))
# Create a batch of AnalyzeDocumentRequests
requests = [AnalyzeDocumentRequest(url_source=url) for url in invoice_urls]
# Begin analyzing the batch of invoices
poller = document_intelligence_client.begin_analyze_document(
"prebuilt-invoice",
requests
)
invoices: AnalyzeResult = poller.result()
if invoices.documents:
for idx, invoice in enumerate(invoices.documents):
print(f"--------Analyzing invoice #{idx + 1}--------")
if invoice.fields:
vendor_name = invoice.fields.get("VendorName")
if vendor_name:
print(f"Vendor Name: {vendor_name.get('content')} has confidence: {vendor_name.get('confidence')}")
# Repeat similar blocks for other fields as in your original code...
# (Omitted here for brevity)
if __name__ == "__main__":
from azure.core.exceptions import HttpResponseError
from dotenv import find_dotenv, load_dotenv
try:
load_dotenv(find_dotenv())
# List of URLs for the invoices you want to process in a batch
invoice_urls = [
"https://github.com/Azure-Samples/cognitive-services-REST-api-samples/raw/master/curl/form-recognizer/rest-api/invoice.pdf",
"https://another-url-for-invoice.com/invoice2.pdf",
# Add more URLs as needed
]
analyze_invoices(invoice_urls)
except HttpResponseError as error:
if error.error is not None:
if error.error.code == "InvalidImage":
print(f"Received an invalid image error: {error.error}")
if error.error.code == "InvalidRequest":
print(f"Received an invalid request error: {error.error}")
raise
if "Invalid request".casefold() in error.message.casefold():
print(f"Uh-oh! Seems there was an invalid request: {error}")
raise
My suggestion is to try . !
--
I hope this helps!
Kindly mark the answer as Accepted and Upvote in case it helped!
Regards