将本地文件的直接访问权限授予 Document AI

问题描述

我知道有一种方法可以让我们从本地系统的 python 环境中调用 Document AI。在该过程中,需要将本地文件上传到 GCS 存储桶,以便 Document AI 可以从那里访问该文件。有什么方法可以让我们使用 python 直接访问本地文件到 Document AI(即,无需将文件上传到 GCS 存储桶)? [请注意,我必须在本地系统中运行 python 代码,而不是在 GCP 中。]

解决方法

DocumentAI 无法从本地文件系统中自行“打开”文件。

如果您不想/无法将文档上传到存储桶,您可以将它们作为 REST API 的一部分发送。 但是在这种情况下您不能使用 BatchProcessing:我的意思是,您必须一个一个地处理文件并等待响应。

相关的 REST API 文档在这里:https://cloud.google.com/document-ai/docs/reference/rest/v1/projects.locations.processors/process

quickstart documentation for python 中,您有以下示例代码,用于读取文件并将其作为请求的一部分内联发送:

# 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}"

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

document = {"content": image_content,"mime_type": "application/pdf"}

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

result = client.process_document(request=request)