我的 API 遇到问题,没有任何响应。我已经使用相应的 API 密钥及其 JSON 文件创建了服务帐户,但是,在运行该文件时我似乎无法获得任何响应。你知道有什么可以帮忙的吗?
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)
我尝试了文档 AI 的不同部分,OCR 和自定义提取器正是我所需要的。我现在只是想得到任何结果,但没有任何效果,根本没有回应:
运行文件时的终端
PS D:\Careers\LXLibrary\Automation> python googleCloud\documentAI\customExtract.py
PS D:\Careers\LXLibrary\Automation>
该文件只包含函数定义,不调用函数。它旨在在代码库中使用。
您需要添加类似于以下内容的部分才能运行该函数。
if __name__ == "__main__":
process_document_sample(project_id, location, processor_id, file_path, mime_type)