[Python] Document AI is extremely slow in ec2 instance and overall is slow

Discussão em 'Python' iniciado por Stack, Maio 1, 2025.

  1. Stack

    Stack Membro Participativo

    In my local environment using the standard process_request api call, the request call takes 8 to 20secs.

    Then in my staging environment (EC2 instance US servers) the same process_request call times out at 300secs. In my EC2 python shell, I was able to run it once but it took 34secs which is not acceptable for my use case.

    I'm only passing in bytes of a small pdf - 2 pages around 60kb.

    class DocumentAIClient:

    logger = get_logger(__name__)

    def __init__(
    self,
    project_id: str,
    location: str,
    processor_id: str,
    ):
    """
    Initialize a Document AI client

    Args:
    project_id: Google Cloud project ID
    location: Location of the processor (e.g., "us")
    processor_id: Document AI processor ID
    """

    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.environ[
    "GOOGLE_APPLICATION_CREDENTIALS_FILE"
    ]

    self.project_id = project_id
    self.location = location
    self.processor_id = processor_id
    opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
    self.client = documentai.DocumentProcessorServiceClient(client_options=opts)

    def fetch_document_from_xxxxx(
    self,
    file_content: bytes,
    mime_type: str = "application/pdf",
    processor_version_id: str | None = None,
    ) -> documentai.Document:
    """
    Process a document using Google Document AI

    Args:
    file_content: Binary content of the file
    mime_type: MIME type of the document
    field_mask: Optional fields to return in the Document object
    processor_version_id: Optional processor version ID
    pages: Optional list of specific pages to process

    Returns:
    Tuple containing the processed document and processing time
    """
    start_time = time.time()

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

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

    process_options = documentai.ProcessOptions(
    individual_page_selector=documentai.ProcessOptions.IndividualPageSelector(
    pages=[1, 2]
    )
    )

    request = documentai.ProcessRequest(
    name=name,
    raw_document=raw_document,
    field_mask="text,entities,pages.pageNumber",
    process_options=process_options,
    )

    try:
    result = self.client.process_document(request=request, timeout=90.0)
    except Exception as e:
    self.logger.exception("Error getting result: %s", e)
    raise

    end_time = time.time()
    processed_timing = end_time - start_time
    document = result.document
    self.logger.info(
    "Document AI processed document in %s seconds", processed_timing
    )

    return document

    def fetch_pdf_from_s3(
    self,
    pdf_s3_url: str,
    ) -> bytes:
    self.logger = self.logger.bind(pdf_s3_url=pdf_s3_url)
    self.logger.info("S3 URL: %s", pdf_s3_url)

    parsed = urlparse(pdf_s3_url)
    key = parsed.path.lstrip("/")

    try:
    with default_storage.open(key, "rb") as file:
    data = file.read()
    self.logger.info("Fetched %s bytes from path %s", len(data), key)
    return data
    except Exception as e:
    self.logger.exception("Error fetching file from storage: %s", e)
    raise


    Tested in local - 8 to 20s Tested in EC2 instance - 34s one time, the rest of the time it times out

    Continue reading...

Compartilhe esta Página