I have the following Python code to extract text from a locally stored PDF file:
# import libraries
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeResult
from azure.ai.documentintelligence.models import AnalyzeDocumentRequest
import base64
def analyze_layout():
document_intelligence_client = DocumentIntelligenceClient(
endpoint="https://<resource-name>.cognitiveservices.azure.com/",
credential=AzureKeyCredential("EHCGTC....")
)
with open("C:/Users/lvg/source/repos/terraform-ai-iac/terraform-ai-iac/data/documents/test.pdf", "rb") as document:
poller = document_intelligence_client.begin_analyze_document("prebuilt-layout", document)
result: AnalyzeResult = poller.result()
analyze_layout()
However, when I run this code I get the error:
azure.core.exceptions.ResourceNotFoundError: (404) Resource not found
Code: 404
Message: Resource not found
I am able to go to the resource in Azure and use document intelligence there to extract the text from the PDF that is stored locally.
What could be the reason my code is not working?
azure.core.exceptions.ResourceNotFoundError: (404) Resource not found Code: 404 Message: Resource not found.
"The error might be due to the location, API version, or SDK version being used. If you used correct endpoint and key.
In my environment, I used SDK with azure-ai-documentintelligence==1.0.0
version and I deployed my Document intelligence studio in east us
with standard
tier.
Portal:
Now, I used the same code with correct endpoint and key from the portal.
Code:
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
def analyze_layout():
document_intelligence_client = DocumentIntelligenceClient(
endpoint="https://venkatdoc45.cognitiveservices.azure.com/",
credential=AzureKeyCredential("xxxxx")
)
with open(r"C:\Downloads\test (1).pdf", "rb") as document:
poller = document_intelligence_client.begin_analyze_document("prebuilt-layout", document)
AnalyzeResult = poller.result()
print(AnalyzeResult.content)
analyze_layout()
Output:
Your Name
Lorem ipsum dolor sit amet, consectetuer adipiscing elit
EXPERIENCE
Company, Location - Job Title MONTH 20XX - PRESENT
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh.
Company, Location - Job Title MONTH 20XX - MONTH 20XX
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh.
Company, Location - Job Title MONTH 20XX - MONTH 20XX
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh.
EDUCATION
School Name, Location - Degree MONTH 20XX - MONTH 20XX
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore.
School Name, Location - Degree MONTH 20XX - MONTH 20XX
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam.
PROJECTS
Reference: Azure AI Document Intelligence client library for Python | Microsoft Learn