fix(knowdge-base): 手册/设计类 PDF 不抑制图片,并对保留图片跑 OCR,把 图中文写进md再同步

This commit is contained in:
sophie committed 2026-07-10 10:18:08 +08:00
1 parent 64205b72bd
commit 0939aeb0d8
5 files changed
+626 -17

No files matched your search

+47 -4
View File
@@ -1,3 +1,11 @@
"""
edkb_system/controller.py
用途:edkb 单文件清洗编排(intake → loader → OCR → clean → export)。
依赖:edkb_system.skills.*、SystemConfig、common.is_figure_rich_handbook。
逻辑:由 knowledge_cleaning.edkb_adapter / CLI 调用;手册类跳过丢图并强制 OCR。
"""
from __future__ import annotations
import json
@@ -30,7 +38,7 @@ from edkb_system.skills import (
quality_gate,
table_linearize,
)
from edkb_system.skills.common import blocks_to_plain_markdown
from edkb_system.skills.common import blocks_to_plain_markdown, is_figure_rich_handbook
from edkb_system.skills.word_bridge import _kill_winword_pids, _list_winword_pids
@@ -281,6 +289,21 @@ def _cleanup_input_temp_files(input_path: Path) -> list[Path]:
def _should_run_ocr(profile, cfg, blocks: list, context: Context | None = None) -> bool:
"""
判断当前文档是否需要跑 OCR。
核心逻辑:PPT 跳过;Word 仅在 pipeline 显式请求时跑;PDF 在扫描/整页图模式
或图文手册(有 image block)时强制跑,以便写入「图中文字」供问答。
Args:
profile: intake 文档画像。
cfg: SystemConfig。
blocks: loader 产出的块。
context: 可选上下文(含 scan_likely / figure_rich_handbook 等 extras)。
Returns:
True 表示应执行 edkb-ocr。
"""
if not blocks or not cfg.ocr.enable:
return False
if profile.file_type in {"ppt", "pptx"}:
@@ -288,6 +311,11 @@ def _should_run_ocr(profile, cfg, blocks: list, context: Context | None = None)
pipeline_requests_ocr = "edkb-ocr" in profile.recommended_pipeline
has_image_blocks = any(b.type == "image" for b in blocks)
extras = getattr(context, "extras", {}) or {} if context is not None else {}
# [STEP] 机械设计手册等:只要有图就 OCR,避免只剩正文导致问答丢图信息
if has_image_blocks and extras.get("figure_rich_handbook"):
return True
# Word documents often contain decorative or embedded images. If intake
# did not explicitly request OCR, do not force a slow OCR pass just because
@@ -296,7 +324,6 @@ def _should_run_ocr(profile, cfg, blocks: list, context: Context | None = None)
return False
if profile.file_type == "pdf" and not pipeline_requests_ocr:
extras = getattr(context, "extras", {}) or {}
return bool(extras.get("scan_likely") or extras.get("pdf_page_image_mode_pages"))
return pipeline_requests_ocr or has_image_blocks
@@ -592,6 +619,13 @@ def _run_one(
if blocks:
ctx.images = sum(1 for b in blocks if b.type == "image")
figure_rich_handbook = bool(
ctx.extras.get("figure_rich_handbook")
or is_figure_rich_handbook(file_path.name)
)
if figure_rich_handbook:
ctx.extras["figure_rich_handbook"] = True
should_ocr = _should_run_ocr(profile, cfg, blocks, ctx)
if should_ocr:
_announce("ocr")
@@ -614,8 +648,13 @@ def _run_one(
for b in blocks
if b.type in {"heading", "paragraph"}
)
# [STEP] policy_text/flow 默认丢图;手册类必须保留 image+ocr_text 供问答
suppress_images = False
if profile.file_type not in {"ppt", "pptx"} and not has_figure_anchor:
if (
profile.file_type not in {"ppt", "pptx"}
and not has_figure_anchor
and not figure_rich_handbook
):
if policy_text and not has_non_flow_info and not has_page_image_mode:
suppress_images = True
elif (
@@ -627,6 +666,8 @@ def _run_one(
if suppress_images:
blocks = [b for b in blocks if b.type not in {"image", "ocr_text"}]
trace_steps.append("edkb-image-suppress(no_figure_anchor)")
elif figure_rich_handbook:
trace_steps.append("edkb-image-keep(figure_rich_handbook)")
flow_info = {}
if "edkb-flow-reconstruct" in profile.recommended_pipeline:
@@ -642,7 +683,9 @@ def _run_one(
encoding="utf-8",
)
text = blocks_to_plain_markdown(blocks)
text = blocks_to_plain_markdown(
blocks, keep_figure_ocr=figure_rich_handbook
)
text = _inject_doc_header_from_flow(text, flow_mds)
elif tables is not None:
_announce("table linearize")