我正在研究使用文档AI拆分文档的问题。在这个问题中,我通过文档AI遵循官方github仓库进行批处理。
批处理函数返回一个长时间运行的操作。然后使用操作元数据
轮询和填充操作。当我尝试使用长时间运行的操作api单独检索操作时,我正在获得操作对象,但元数据不同,因此我无法进一步处理文档。
为了稍后检索操作,我在同一个repo中使用get_operation函数。
提前感谢!!
存储库链接:https://github.com/GoogleCloudPlatform/python-docs-samples/tree/239d42f8dcb564db35c0b9fc79d8c07f6f6fe489/documentai/snippets
batch_process_sample工作得很好,但需要得到相同的结果,即使我单独检索操作对象并尝试类似地处理
从get_operation()
返回的Operation
数据的格式与直接从batch_process_documents()
返回的格式略有不同。这似乎是Google api处理操作的一个怪癖。
代码示例和文档没有包含有关此的信息,但我发现了如何使用内置方法来做到这一点。(我正在向文档AI工具箱SDK添加功能,该SDK从BatchProcessMetadata
或Operation
名称的GCS uri中提取Document
输出,以使其更容易。)
更新:文档AI工具箱代码
from google.cloud import documentai
from google.cloud.documentai_toolbox import document
project_id = "YOUR_PROJECT_ID"
location = "YOUR_PROCESSOR_LOCATION"
operation = client.batch_process_documents(request)
# Format: projects/{project_id}/locations/{location}/operations/15842030886767182557
operation_name = operation.operation.name
# Use this wrapped document to get the extraction information you need.
wrapped_document = document.from_batch_process_operation(location, operation_name)
<<h2>主要api/h2>from google.api_core.client_options import ClientOptions
from google.cloud import documentai
from google.longrunning.operations_pb2 import GetOperationRequest
project_id = "YOUR_PROJECT_ID"
location = "YOUR_PROCESSOR_LOCATION"
operation_name = (
f"projects/{project_id}/locations/{location}/operations/15842030886767182557"
)
client = documentai.DocumentProcessorServiceClient(
client_options=ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
)
while True:
operation = client.get_operation(
request=GetOperationRequest(name=operation_name)
)
if operation.done:
break
# The BatchProcessMetadata information is serialized, must be deserialized to access the values
metadata = documentai.BatchProcessMetadata.deserialize(operation.metadata.value)
# Get the individual_process_statuses
for process in list(metadata.individual_process_statuses):
# Handle the response however you need
print(process.output_gcs_destination)