这是针对文档问答任务精调的多模态 LayoutLM 模型版本。该模型基于 SQuAD2.0 和 DocVQA 数据集进行了精细调优。
运行以下示例前,请确保已安装 PIL、pytesseract 和 PyTorch,以及 transformers 库。
from transformers import pipeline
nlp = pipeline(
"document-question-answering",
model="impira/layoutlm-document-qa",
)
nlp(
"https://templates.invoicehome.com/invoice-template-us-neat-750px.png",
"What is the invoice number?"
)
# {'score': 0.9943977, 'answer': 'us-001', 'start': 15, 'end': 15}
nlp(
"https://miro.medium.com/max/787/1*iECQRIiOGTmEFLdWkVIH2g.jpeg",
"What is the purchase amount?"
)
# {'score': 0.9912159, 'answer': '$1,000,000,000', 'start': 97, 'end': 97}
nlp(
"https://www.accountingcoach.com/wp-content/uploads/2013/10/income-statement-example@2x.png",
"What are the 2020 net sales?"
)
# {'score': 0.59147286, 'answer': '$ 3,750', 'start': 19, 'end': 20}注意:该模型及流程最近通过 PR #18407 和 PR #18414 合并至 transformers 库,因此您需要使用最新版本的 transformers,例如:
pip install git+https://github.com/huggingface/transformers.git@2ef774211733f0acf8d3415f9284c49ef219e991该模型由 Impira 团队创建。