perf(kb):1.优化 PDF只对可疑页做 image box 分析避免整本重复扫描/重复 reopen;2.doc/.docx 不再因为“有图片”就强制 OCR;3.只在 intake 路由 明确要求 OCR 时才跑
This commit is contained in:
1 parent
a3123cd602
commit
c58ed37cc1
11 files changed
+717
-122
No files matched your search
@@ -212,12 +212,33 @@ def _derive_output_rel_md(file_path: Path, input_path: Path) -> Path:
|
||||
return rel.with_suffix(".md")
|
||||
|
||||
|
||||
def _strip_in_memory_images(blocks: list) -> list:
|
||||
out = []
|
||||
for b in blocks:
|
||||
meta = dict(getattr(b, "meta", {}) or {})
|
||||
if "image" in meta:
|
||||
meta.pop("image", None)
|
||||
b = type(b)(
|
||||
id=b.id,
|
||||
page=b.page,
|
||||
type=b.type,
|
||||
text=b.text,
|
||||
bbox=b.bbox,
|
||||
meta=meta,
|
||||
)
|
||||
out.append(b)
|
||||
return out
|
||||
|
||||
|
||||
def _normalize_path_for_match(p: Path | str) -> str:
|
||||
return str(p).replace("\\", "/")
|
||||
|
||||
|
||||
def _file_timeout_sec() -> int | None:
|
||||
raw = str(os.environ.get("EDKB_FILE_TIMEOUT_SEC", "0")).strip()
|
||||
_WORD_TIMEOUT_EXTS = {".doc", ".docx"}
|
||||
|
||||
|
||||
def _parse_timeout_env(name: str, default: str = "0") -> int | None:
|
||||
raw = str(os.environ.get(name, default)).strip()
|
||||
try:
|
||||
value = int(raw)
|
||||
except ValueError:
|
||||
@@ -225,6 +246,15 @@ def _file_timeout_sec() -> int | None:
|
||||
return value if value > 0 else None
|
||||
|
||||
|
||||
def _file_timeout_sec(file_path: Path | None = None) -> int | None:
|
||||
suffix = str(getattr(file_path, "suffix", "") or "").lower()
|
||||
if suffix in _WORD_TIMEOUT_EXTS:
|
||||
specific = _parse_timeout_env("EDKB_WORD_FILE_TIMEOUT_SEC")
|
||||
if specific:
|
||||
return specific
|
||||
return _parse_timeout_env("EDKB_FILE_TIMEOUT_SEC")
|
||||
|
||||
|
||||
def _cleanup_input_temp_files(input_path: Path) -> list[Path]:
|
||||
if input_path.is_file():
|
||||
candidates = [input_path]
|
||||
@@ -246,6 +276,28 @@ def _cleanup_input_temp_files(input_path: Path) -> list[Path]:
|
||||
return removed
|
||||
|
||||
|
||||
def _should_run_ocr(profile, cfg, blocks: list, context: Context | None = None) -> bool:
|
||||
if not blocks or not cfg.ocr.enable:
|
||||
return False
|
||||
if profile.file_type in {"ppt", "pptx"}:
|
||||
return False
|
||||
|
||||
pipeline_requests_ocr = "edkb-ocr" in profile.recommended_pipeline
|
||||
has_image_blocks = any(b.type == "image" for b in blocks)
|
||||
|
||||
# Word documents often contain decorative or embedded images. If intake
|
||||
# did not explicitly request OCR, do not force a slow OCR pass just because
|
||||
# image blocks exist.
|
||||
if profile.file_type in {"doc", "docx"} and not pipeline_requests_ocr:
|
||||
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
|
||||
|
||||
|
||||
def _matches_pattern(candidates: list[str], patterns: list[str]) -> str | None:
|
||||
if not patterns:
|
||||
return None
|
||||
@@ -311,7 +363,7 @@ def _run_one_with_timeout(
|
||||
final_md_path: Path | None = None,
|
||||
progress_label: str | None = None,
|
||||
) -> dict:
|
||||
timeout = _file_timeout_sec()
|
||||
timeout = _file_timeout_sec(file_path)
|
||||
if not timeout:
|
||||
return _run_one(
|
||||
file_path,
|
||||
@@ -536,18 +588,14 @@ def _run_one(
|
||||
|
||||
if blocks:
|
||||
ctx.images = sum(1 for b in blocks if b.type == "image")
|
||||
should_ocr = cfg.ocr.enable and (
|
||||
"edkb-ocr" in profile.recommended_pipeline
|
||||
or any(b.type == "image" for b in blocks)
|
||||
)
|
||||
if profile.file_type in {"ppt", "pptx"}:
|
||||
should_ocr = False
|
||||
should_ocr = _should_run_ocr(profile, cfg, blocks, ctx)
|
||||
if should_ocr:
|
||||
_announce("ocr")
|
||||
t0 = time.perf_counter()
|
||||
blocks = ocr.run(blocks, ctx, min_confidence=cfg.ocr.min_confidence)
|
||||
_mark("edkb-ocr", t0)
|
||||
trace_steps.append("edkb-ocr")
|
||||
blocks = _strip_in_memory_images(blocks)
|
||||
|
||||
doc_kind = (ctx.extras.get("intake") or {}).get("doc_kind")
|
||||
policy_text = doc_kind == "policy_text"
|
||||
|
||||
Reference in new issue
Block a user