文档问答

使用 Hugging Face

Jacob H. Marquez

Lead Data Engineer

什么是文档问答?

$$

  • 基于文档内容回答问题
  • 需要文档与问题
  • 输出直接答案或释义

$$

问题:"Q3 的总收入是多少?"

一份文档

使用 Hugging Face

文档问答的用例

法务、财务与支持的用例

$$

  • 📑 法务: 识别合同条款

$$

  • 💰 财务: 提取关键数据

$$

  • 🤓 支持: 从手册检索答案
使用 Hugging Face

用文档问答自动化 HR 咨询

$$

  • 📄 信息存于 US-Employee_Policy.pdf

$$

  • 🤖 构建系统以提取答案

$$

  • 🕑 节省人力资源时间与精力

$$

人力资源团队负荷过重

使用 Hugging Face

使用 pypdf 提取文本

from pypdf import PdfReader


# Load the PDF file reader = PdfReader("US-Employee_Policy.pdf")
# Extract text from all pages document_text = "" for page in reader.pages:
document_text += page.extract_text()
Welcome to the US Employee Policy document...
使用 Hugging Face

创建问答流水线

# Load the question-answering pipeline
qa_pipeline = pipeline(
    task="question-answering",
    model="distilbert-base-cased-distilled-squad")


question = "How many volunteer days are offered annually?"
# Get the answer from the QA pipeline result = qa_pipeline(question=question, context=document_text)
print(f"Answer: {result['answer']}")
Answer: 1
使用 Hugging Face

整合以上步骤

$$

  • 📄 使用 pypdfPdfReader 加载并读取 PDF
  • 🔎 通过 .pages.extract_text() 提取为 document_text
  • 🤔 设置 question-answering 流水线
  • ❓ 向流水线传入 questioncontext
  • ⏰ 封装为函数以自动化查询

文档问答

人力资源团队建设公司文化

使用 Hugging Face

Passons à la pratique !

使用 Hugging Face

Preparing Video For Download...