【发布时间】:2020-07-02 14:42:10
【问题描述】:
我正在尝试运行 GCP 文档 AI 的 pdf 解析中给出的演示代码。要运行代码,将 google 凭据导出为命令行可以正常工作。当代码需要在内存中运行并且因此不允许从磁盘访问凭据文件时,就会出现问题。有没有办法在文档ai解析函数中传递凭证?
google的示例代码:
def main(project_id='YOUR_PROJECT_ID',
input_uri='gs://cloud-samples-data/documentai/invoice.pdf'):
"""Process a single document with the Document AI API, including
text extraction and entity extraction."""
client = documentai.DocumentUnderstandingServiceClient()
gcs_source = documentai.types.GcsSource(uri=input_uri)
# mime_type can be application/pdf, image/tiff,
# and image/gif, or application/json
input_config = documentai.types.InputConfig(
gcs_source=gcs_source, mime_type='application/pdf')
# Location can be 'us' or 'eu'
parent = 'projects/{}/locations/us'.format(project_id)
request = documentai.types.ProcessDocumentRequest(
parent=parent,
input_config=input_config)
document = client.process_document(request=request)
# All text extracted from the document
print('Document Text: {}'.format(document.text))
def _get_text(el):
"""Convert text offset indexes into text snippets.
"""
response = ''
# If a text segment spans several lines, it will
# be stored in different text segments.
for segment in el.text_anchor.text_segments:
start_index = segment.start_index
end_index = segment.end_index
response += document.text[start_index:end_index]
return response
for entity in document.entities:
print('Entity type: {}'.format(entity.type))
print('Text: {}'.format(_get_text(entity)))
print('Mention text: {}\n'.format(entity.mention_text))
【问题讨论】:
-
您的代码在哪里运行?在 gcp 上还是在其他地方?
-
@guillaumeblaquiere,它在 GCF 上运行。由于仅在内存中计算,我如何传递凭证 json ?
-
你没有!我正在写答案
标签: google-cloud-platform google-cloud-functions artificial-intelligence pdf-parsing