GCP API for AI Documents

34 Views Asked by At

I'm having issues with the API, there is no response whatsoever. I have created  the service account with the corresponding API key with its JSON file, however, I cannot seem to get any response when running the file. Do you know anything that may help?

I'm using this file https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/documentai/snippets/process_document_sample.py

from typing import Optional

from google.api_core.client_options import ClientOptions
from google.cloud import documentai

project_id = "mystuff"
location = "eu"
processor_id = "mystuff"
file_path = "mypdf"
mime_type = "application/pdf"
field_mask = "text,entities,pages.pageNumber"

def process_document_sample(
    project_id: str,
    location: str,
    processor_id: str,
    file_path: str,
    mime_type: str,
    field_mask: Optional[str] = None,
    processor_version_id: Optional[str] = None,
) -> None:
    opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")

    client = documentai.DocumentProcessorServiceClient(client_options=opts)

    if processor_version_id:
        name = client.processor_version_path(
            project_id, location, processor_id, processor_version_id
        )
    else:
        name = client.processor_path(project_id, location, processor_id)

    with open(file_path, "rb") as image:
        image_content = image.read()

    raw_document = documentai.RawDocument(content=image_content, mime_type=mime_type)

    request = documentai.ProcessRequest(
        name=name,
        raw_document=raw_document,
        field_mask=field_mask,
    )

    result = client.process_document(request=request)

    document = result.document

    print("The document contains the following text:")
    print(document.text)


I have tried different portions of the Document AI, the OCR and the custom extractor is what i need. I am simply just trying to get any result right now but nothing works, simply no response:

My terminal when I run the file

PS D:\Careers\LXLibrary\Automation> python googleCloud\documentAI\customExtract.py
PS D:\Careers\LXLibrary\Automation> 
1

There are 1 best solutions below

0
Holt Skinner On

That file only contains the function definition, it doesn't call the function. It's intended to be used within a code base.

You'll need to add a section similar to the following for the function to run.

if __name__ == "__main__":
    process_document_sample(project_id, location, processor_id, file_path, mime_type)