Files
2026-11-09 21:49:28 +08:00

183 lines
7.3 KiB
Python

"""LiteParse engine adapter implementing the ``Parser`` protocol.
LiteParse (run-llama/liteparse) is a Rust-backed PDF/Office/image parser: no
model weights, no CUDA, in-process. Like the PyMuPDF4LLM engine it can extract
embedded images, so it follows the same workdir contract — one
``<stem>.md`` plus an ``images/`` dir with portable ``images/<name>`` links
(the MinerU/Docling asset convention the cache loader expects).
Two of LiteParse's own defaults are overridden here rather than exposed:
``output_format`` is pinned to Markdown (the workdir contract is a ``.md``
file, so JSON or plain text would produce a mislabelled document), and
``image_output_dir`` is pinned to the workdir's ``images/`` (anywhere else is
invisible to the cache loader).
"""
from __future__ import annotations
import importlib.util
import json
from pathlib import Path
import re
from typing import Any, Callable, Optional
from ...base import ReadinessReport
from ...signature import ParserSignature
from ...types import ParserError
from .._versions import package_version
from .config import LiteParseConfig, resolve_liteparse_config
from .formats import (
LITEPARSE_2_14_2_FORMATS,
MIN_LITEPARSE_VERSION,
installed_liteparse_version,
liteparse_version_is_current,
)
# LiteParse references extracted images by bare file name — ``![](img_p1_1.png)``
# — so links are rewritten to ``images/<name>`` to match the asset convention.
_IMAGE_LINK_RE = re.compile(r"!\[([^\]]*)\]\(([^)]+)\)")
class LiteParseParser:
"""PDF/Office/image → Markdown via liteparse (no models, optional images)."""
name = "liteparse"
needs_local_models = False
@classmethod
def is_available(cls) -> bool:
return importlib.util.find_spec("liteparse") is not None
def resolve_config(self) -> LiteParseConfig:
return resolve_liteparse_config()
def supported_formats(self) -> frozenset[str]:
return LITEPARSE_2_14_2_FORMATS
def signature(self, config: LiteParseConfig) -> ParserSignature:
return ParserSignature.build(
"liteparse",
package_version("liteparse"),
{
"image_mode": config.image_mode,
"extract_links": config.extract_links,
"extract_images": config.extract_images,
"max_pages": config.max_pages,
# v1 preserves LiteParse's physical-page boundaries in the
# canonical content-list IR. Keep this in the signature so
# caches produced before page metadata was retained are not
# reused by page-grounded retrieval.
"page_structure": 1,
},
)
def is_ready(self, config: LiteParseConfig) -> ReadinessReport:
if not self.is_available():
return ReadinessReport(
ready=False,
reason="not_configured",
message="liteparse isn't installed (pip install deeptutor[parse-liteparse]).",
)
version = installed_liteparse_version()
if not liteparse_version_is_current(version):
return ReadinessReport(
ready=False,
reason="update_required",
message=(
f"Installed LiteParse {version or 'unknown'} is too old. DeepTutor needs "
f"LiteParse >= {MIN_LITEPARSE_VERSION} for the current multi-format input "
"set. Update it under Settings → Document Parsing."
),
)
return ReadinessReport(ready=True)
def parse(
self,
source_path: Path,
workdir: Path,
*,
config: LiteParseConfig,
on_output: Optional[Callable[[str], None]] = None,
) -> None:
from liteparse import LiteParse
source_path = Path(source_path)
workdir = Path(workdir)
if on_output:
on_output(f"Converting {source_path.name} via LiteParse…")
images_dir = workdir / "images"
kwargs: dict[str, Any] = {
"output_format": "markdown",
"image_mode": config.image_mode,
"extract_links": config.extract_links,
"quiet": True,
# A systemic OCR failure aborts the whole parse by default. Prefer
# the natively recovered text over losing the document outright —
# the ingestion pipeline treats a ParserError as "no content".
"ocr_failure_fatal": False,
}
if config.extract_images:
images_dir.mkdir(parents=True, exist_ok=True)
kwargs["extract_images"] = True
kwargs["image_output_dir"] = str(images_dir)
if config.max_pages > 0:
kwargs["max_pages"] = config.max_pages
try:
result = LiteParse(**kwargs).parse(str(source_path))
except Exception as exc: # noqa: BLE001 - surface as a parser error
raise ParserError(f"LiteParse failed to convert {source_path.name}: {exc}") from exc
markdown = str(getattr(result, "text", "") or "")
if config.extract_images:
markdown = self._portable_image_links(markdown, getattr(result, "images", None))
# Drop the images dir if nothing was actually extracted, so the
# cache loader doesn't report an empty asset_dir.
if images_dir.is_dir() and not any(images_dir.iterdir()):
images_dir.rmdir()
(workdir / f"{source_path.stem}.md").write_text(markdown, encoding="utf-8")
# LiteParse already exposes per-page text, but saving only result.text
# discards those locators. Scanned PDFs then remain searchable through
# one unlocated Markdown document while source-grounded lessons reject
# every hit because it has no page_index. Emit the optional canonical
# content list so all downstream consumers retain the physical page.
page_blocks = []
for page in getattr(result, "pages", None) or []:
page_num = getattr(page, "page_num", None)
if type(page_num) is not int or page_num < 1:
continue
text = str(getattr(page, "markdown", "") or getattr(page, "text", "") or "")
if config.extract_images:
text = self._portable_image_links(text, getattr(result, "images", None))
if text.strip():
page_blocks.append({"type": "text", "text": text, "page_idx": page_num - 1})
if page_blocks:
(workdir / f"{source_path.stem}_content_list.json").write_text(
json.dumps(page_blocks, ensure_ascii=False), encoding="utf-8"
)
@staticmethod
def _portable_image_links(markdown: str, images: Any) -> str:
"""Prefix links naming an extracted image with the ``images/`` dir.
Only names LiteParse reports as extracted are rewritten, so a link the
document itself carried to an unrelated URL is left alone.
"""
names = {
name for image in (images or []) if (name := str(getattr(image, "name", "") or ""))
}
if not names:
return markdown
def _repl(match: re.Match[str]) -> str:
alt, target = match.group(1), match.group(2)
return f"![{alt}](images/{target})" if target in names else match.group(0)
return _IMAGE_LINK_RE.sub(_repl, markdown)
__all__ = ["LiteParseParser"]