googleapis/python-documentai

Running Google Cloud DocumentAI sample code on Python returned the error 503

Closed this issue · 1 comments

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

Please run down the following list and make sure you've tried the usual "quick fixes":

If you are still having issues, please be sure to include as much information as possible:

Environment details

  • OS type and version: Ubuntu 20.04 (WSL on MS Windows 10)
  • Python version: 3.8.10
  • pip version: 21.1.3
  • google-cloud-documentai version: 1.0.0

Steps to reproduce

  1. export GOOGLE_APPLICATION_CREDENTIALS="/mnt/c/workspace/document_AI/google_ai.json"
  2. Put the following Py file and document.jpg into the same folder.
  3. Run python test_document_ai.py

Code example

from google.cloud import documentai_v1 as documentai
import os

# TODO(developer): Uncomment these variables before running the sample.

project_id= '123456789'
location = 'us' # Format is 'us' or 'eu'
processor_id = '1a23345gh823892' #  Create processor in Cloud Console
file_path = 'document.jpg'

os.environ['GRPC_DNS_RESOLVER'] = 'native'


def quickstart(project_id: str, location: str, processor_id: str, file_path: str):

    # You must set the api_endpoint if you use a location other than 'us', e.g.:
    opts = {}
    if location == "eu":
        opts = {"api_endpoint": "eu-documentai.googleapis.com"}

    client = documentai.DocumentProcessorServiceClient(client_options=opts)

    # The full resource name of the processor, e.g.:
    # projects/project-id/locations/location/processor/processor-id
    # You must create new processors in the Cloud Console first
    name = f"projects/{project_id}/locations/{location}/processors/{processor_id}:process"

    # Read the file into memory
    with open(file_path, "rb") as image:
        image_content = image.read()

    document = {"content": image_content, "mime_type": "image/jpeg"}

    # Configure the process request
    request = {"name": name, "raw_document": document}

    result = client.process_document(request=request)
    document = result.document

    document_pages = document.pages

    # For a full list of Document object attributes, please reference this page: https://googleapis.dev/python/documentai/latest/_modules/google/cloud/documentai_v1beta3/types/document.html#Document

    # Read the text recognition output from the processor
    print("The document contains the following paragraphs:")
    for page in document_pages:
        paragraphs = page.paragraphs
        for paragraph in paragraphs:
            print(paragraph)
            paragraph_text = get_text(paragraph.layout, document)
            print(f"Paragraph text: {paragraph_text}")


def get_text(doc_element: dict, document: dict):
    """
    Document AI identifies form fields by their offsets
    in document text. This function converts offsets
    to text snippets.
    """
    response = ""
    # If a text segment spans several lines, it will
    # be stored in different text segments.
    for segment in doc_element.text_anchor.text_segments:
        start_index = (
            int(segment.start_index)
            if segment in doc_element.text_anchor.text_segments
            else 0
        )
        end_index = int(segment.end_index)
        response += document.text[start_index:end_index]
    return response

def main ():
    quickstart (project_id = project_id, location = location, processor_id = processor_id, file_path = file_path)

if __name__ == '__main__':
    main ()

Stack trace

metadata=[('x-goog-request-params', 'name=projects/my_proj_id/locations/us/processors/my_processor_id'), ('x-goog-api-client', 'gl-python/3.8.10 grpc/1.38.1 gax/1.30.0 gapic/1.0.0')]), last exception: 503 DNS resolution failed for service: https://us-documentai.googleapis.com/v1/

I can use the DocumentAI service using the web interface, so I assume that there is something wrong with the local Python code?

Hi @vinhqdang,

First off, please do not post your credentials file to GitHub.

Secondly, the 503 error that you report is a service-side issue rather than a Python client library issue. I highly recommend that you register an issue in the support console.