Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
perf(webui): remove redundant layout PDF generation
myhloli2dfd42b8e568
diff --git a/docs/next/cli/mineru-kit-webui.md b/docs/next/cli/mineru-kit-webui.mdindex 90c8c88e..64b79c52 100644--- a/docs/next/cli/mineru-kit-webui.md+++ b/docs/next/cli/mineru-kit-webui.md@@ -140,7 +140,7 @@ Gradio 转换事件在原有输出末尾追加 Structured Content JSON 文本,每次选择格式后,按钮会显示“准备中”,文件就绪后自动开始下载,无需再次点击。再次下载时,HTML 根据当前访问地址重新生成,其他格式复用已有文件;生成失败时显示错误并允许重试。清除、更换文件或重新解析会使旧下载请求失效。-每次解析的文件保存在独立的 `output-dir/gradio/<run-id>/` 目录,包含源文件、Middle JSON、基础文本产物以及可选的 `origin.pdf`、`layout.pdf` 和图片资源。路径只向 Gradio 暴露在配置的 output root 内。+每次解析的文件保存在独立的 `output-dir/gradio/<run-id>/` 目录,包含源文件、Middle JSON、基础文本产物以及可选的 `origin.pdf` 和图片资源。路径只向 Gradio 暴露在配置的 output root 内。## 界面语言@@ -156,7 +156,7 @@ Office 转换开始、失败及成功后都保留已挂载的源预览。“忽转换开始、排队、解析和整理输出期间保留当前预览及浏览位置;PDF 和图片成功生成结果后刷新预览。转换失败也保留当前预览;点击清除会恢复空预览,更换文件会显示新文件的源预览或对应格式提示。-PDF 和图片会生成与解析范围一致的 `origin.pdf`;布局预览使用 schema 2.0 顶层 block/bbox 生成语义 overlay。无法生成 overlay 时仍保留 origin PDF 预览。+PDF 和图片会生成与解析范围一致的 `origin.pdf`,由 PDF.js 展示。浏览器根据布局区域和来源映射,用 JavaScript 绘制标框、标签与双向溯源交互浮层。`mineru-webui` 提供为 `mineru-kit webui` 的独立命令名,参数与行为完全相同;旧 HTTP 协议和旧 Gradio 专属参数不再提供。diff --git a/mineru/kit/gradio/artifacts.py b/mineru/kit/gradio/artifacts.pyindex b94b0ba9..865557c6 100644--- a/mineru/kit/gradio/artifacts.py+++ b/mineru/kit/gradio/artifacts.py@@ -69,7 +69,6 @@ class RunArtifacts:downloads_dir: Pathfile_suffix: strorigin_pdf_path: Path | None = None- layout_pdf_path: Path | None = Nonepage_indices: tuple[int, ...] = ()generated_downloads: dict[str, Path] = field(default_factory=dict)@@ -85,7 +84,6 @@ class RunArtifacts:"downloads_dir": str(self.downloads_dir),"file_suffix": self.file_suffix,"origin_pdf_path": str(self.origin_pdf_path) if self.origin_pdf_path else "",- "layout_pdf_path": str(self.layout_pdf_path) if self.layout_pdf_path else "","page_indices": list(self.page_indices),"generated_downloads": {key: str(value) for key, value in self.generated_downloads.items()},}@@ -135,10 +133,9 @@ class RunArtifacts:]for path in core_paths:_ensure_path_inside(root, path)- optional_paths = [_optional_path(state.get(key)) for key in ("origin_pdf_path", "layout_pdf_path")]- for path in optional_paths:- if path is not None:- _ensure_path_inside(root, path)+ origin_pdf_path = _optional_path(state.get("origin_pdf_path"))+ if origin_pdf_path is not None:+ _ensure_path_inside(root, origin_pdf_path)return cls(root=root,stem=stem,@@ -148,8 +145,7 @@ class RunArtifacts:structured_content_path=Path(cast(str, state["structured_content_path"])).resolve(),downloads_dir=Path(cast(str, state["downloads_dir"])).resolve(),file_suffix=cast(str, state["file_suffix"]),- origin_pdf_path=optional_paths[0],- layout_pdf_path=optional_paths[1],+ origin_pdf_path=origin_pdf_path,page_indices=page_indices,generated_downloads=generated_downloads,)@@ -267,23 +263,6 @@ def persist_parse_result(finally:_close_image_context(image_context)- if origin_pdf is not None:- from .preview import draw_layout_overlay-- layout_path = artifacts.root / "layout.pdf"- try:- with _output_stage("layout_pdf", artifacts):- draw_layout_overlay(- result.middle_json,- origin_pdf,- layout_path,- page_indices=artifacts.page_indices,- )- except Exception as exc:- logger.warning("Skipping Gradio layout overlay for {}: {}", artifacts.stem, exc)- else:- artifacts.layout_pdf_path = layout_path-return artifactsdiff --git a/mineru/kit/gradio/conversion_service.py b/mineru/kit/gradio/conversion_service.pyindex 079da576..3e4b2ecb 100644--- a/mineru/kit/gradio/conversion_service.py+++ b/mineru/kit/gradio/conversion_service.py@@ -219,7 +219,7 @@ def _prepare_preview(suffix: str, artifacts: RunArtifacts) -> PreviewPlan:"""仅描述结果预览数据,保留已有 Office、OFD、EPUB 和网页源预览。"""if suffix in OFFICE_EXTENSIONS | HTML_EXTENSIONS | MHTML_EXTENSIONS | {"ofd", "epub"}:return PreviewPlan(preserve_source=True)- preview_path = artifacts.origin_pdf_path or artifacts.layout_pdf_path+ preview_path = artifacts.origin_pdf_pathreturn PreviewPlan(preserve_source=False,pdf_path=preview_path,diff --git a/mineru/kit/gradio/document_io.py b/mineru/kit/gradio/document_io.pyindex a8badeac..4b7d96c9 100644--- a/mineru/kit/gradio/document_io.py+++ b/mineru/kit/gradio/document_io.py@@ -36,7 +36,7 @@ class GradioDocumentIO(Protocol):output_root: Path,page_range: str,) -> RunArtifacts:- """物化解析结果及布局预览,只返回路径和可序列化的产物描述。"""+ """物化解析结果及原文预览,只返回路径和可序列化的产物描述。"""...diff --git a/mineru/kit/gradio/preview.py b/mineru/kit/gradio/preview.pydeleted file mode 100644index 05e67c5a..00000000--- a/mineru/kit/gradio/preview.py+++ /dev/null@@ -1,30 +0,0 @@-"""Gradio 布局预览的文件适配,绘制能力由 DocVortex 统一维护。"""--from __future__ import annotations--from pathlib import Path--from docvortex.visualization import render_layout_pdf--from ...types import MiddleJson---def draw_layout_overlay(- middle_json: MiddleJson,- origin_pdf_path: Path,- output_path: Path,- *,- page_indices: tuple[int, ...] = (),-) -> None:- """读取原始 PDF 并写出共享绘制结果,保留 Gradio 的调用和文件契约。"""- if not isinstance(middle_json, MiddleJson):- raise TypeError("middle_json must be a MiddleJson instance")- if not origin_pdf_path.is_file():- raise FileNotFoundError(origin_pdf_path)-- pdf_bytes = render_layout_pdf(origin_pdf_path.read_bytes(), middle_json.pages, page_indices=page_indices or None)- output_path.parent.mkdir(parents=True, exist_ok=True)- output_path.write_bytes(pdf_bytes)---__all__ = ["draw_layout_overlay"]diff --git a/tests/unittest/test_kit_gradio.py b/tests/unittest/test_kit_gradio.pyindex 25bdff91..6a1366fd 100644--- a/tests/unittest/test_kit_gradio.py+++ b/tests/unittest/test_kit_gradio.py@@ -903,7 +903,7 @@ def test_managed_local_api_server_cleans_process_control_and_temp_dir(def test_persist_and_render_all_gradio_download_formats(tmp_path: Path) -> None:- """验证严格 Middle JSON、布局 PDF 和七种下载产物。"""+ """验证严格 Middle JSON、原文预览 PDF 和七种下载产物。"""source = tmp_path / "报告.pdf"source.write_bytes(_pdf_bytes())result = ParseResult(middle_json=_middle_json())@@ -911,9 +911,9 @@ def test_persist_and_render_all_gradio_download_formats(tmp_path: Path) -> None:assert artifacts.middle_json_path.read_text(encoding="utf-8").find('"schema_version": "2.0"') >= 0assert artifacts.origin_pdf_path == artifacts.root / "origin.pdf"- assert artifacts.layout_pdf_path == artifacts.root / "layout.pdf"assert artifacts.origin_pdf_path is not None and artifacts.origin_pdf_path.is_file()- assert artifacts.layout_pdf_path is not None and artifacts.layout_pdf_path.is_file()+ assert artifacts.origin_pdf_path.read_bytes() == source.read_bytes()+ assert not (artifacts.root / "layout.pdf").exists()assert not (artifacts.root / "model_output.json").exists()assert not list(artifacts.root.glob("*.zip"))assert list((artifacts.root / "images").glob("*.jpg"))@@ -939,16 +939,10 @@ def test_persist_and_render_all_gradio_download_formats(tmp_path: Path) -> None:with zipfile.ZipFile(latex_path) as archive:assert f"{artifacts.stem}.tex" in archive.namelist()assert any(name.startswith("images/") for name in archive.namelist())- layout_page = PdfReader(str(artifacts.layout_pdf_path)).pages[0]- layout_stream = layout_page.get_contents()- assert layout_stream is not None- layout_commands = layout_stream.get_data()- assert b"20 240 140 30 re" in layout_commands- assert b"20 60 140 150 re" in layout_commands-def test_persist_image_input_creates_real_origin_and_layout_pdfs(tmp_path: Path) -> None:- """验证图片输入会生成可读取的 origin/layout PDF,而不是伪装扩展名。"""+def test_persist_image_input_creates_real_origin_pdf(tmp_path: Path) -> None:+ """验证图片输入生成可读取的原文预览 PDF。"""source = tmp_path / "scan.png"Image.new("RGB", (64, 48), "white").save(source)artifacts = persist_parse_result(@@ -958,11 +952,9 @@ def test_persist_image_input_creates_real_origin_and_layout_pdfs(tmp_path: Path)page_range="",)assert artifacts.origin_pdf_path is not None- assert artifacts.layout_pdf_path is not Noneassert artifacts.origin_pdf_path.read_bytes().startswith(b"%PDF")- assert artifacts.layout_pdf_path.read_bytes().startswith(b"%PDF")assert len(PdfReader(str(artifacts.origin_pdf_path)).pages) == 1- assert len(PdfReader(str(artifacts.layout_pdf_path)).pages) == 1+ assert not (artifacts.root / "layout.pdf").exists()def test_page_range_image_crops_follow_original_page_indices(tmp_path: Path) -> None:@@ -986,7 +978,7 @@ def test_page_range_image_crops_follow_original_page_indices(tmp_path: Path) ->def test_persist_page_range_keeps_cropped_origin_page_order(tmp_path: Path) -> None:- """验证任意 V1 page_range 会同步应用到 origin 和 layout PDF。"""+ """验证 V1 page_range 会应用到原文预览 PDF 并保留原始页号映射。"""source = tmp_path / "three-pages.pdf"source.write_bytes(_pdf_bytes(page_count=3))artifacts = persist_parse_result(@@ -998,8 +990,7 @@ def test_persist_page_range_keeps_cropped_origin_page_order(tmp_path: Path) -> Nassert artifacts.page_indices == (1, 2)assert artifacts.origin_pdf_path is not Noneassert len(PdfReader(str(artifacts.origin_pdf_path)).pages) == 2- assert artifacts.layout_pdf_path is not None- assert len(PdfReader(str(artifacts.layout_pdf_path)).pages) == 2+ assert not (artifacts.root / "layout.pdf").exists()def test_run_artifact_paths_are_absolute_and_inside_output_root(tmp_path: Path) -> None:@@ -1585,82 +1576,36 @@ def test_gradio_output_failure_stops_timer_and_allows_next_conversion(tmp_path:asyncio.run(scenario())[email protected]("page_indices", [(), (2, 0, 1)])-def test_layout_preview_matches_shared_renderer_and_pdf_document(tmp_path: Path, page_indices: tuple[int, ...]) -> None:- """三个公开入口使用相同页映射、边框和页面属性,缺失页不回退到其他结果。"""- from docvortex.document.pdf import PDFDocument- from docvortex.visualization import render_layout_pdf-- from mineru.kit.gradio.preview import draw_layout_overlay-- source = tmp_path / "source.pdf"- source.write_bytes(_pdf_bytes(page_count=3))- middle = _middle_json(page_indices=(1, 2))- before = middle.model_dump(mode="json")- expected = render_layout_pdf(source.read_bytes(), middle.pages, page_indices=page_indices or None)- preview = tmp_path / "preview" / "layout.pdf"- draw_layout_overlay(middle, source, preview, page_indices=page_indices)- document_output = tmp_path / "document.pdf"- with PDFDocument(source.read_bytes()) as document:- document.draw_layout_bbox(middle.pages, str(document_output), page_indices=page_indices or None)- expected_pages = PdfReader(io.BytesIO(expected)).pages- for output in (preview, document_output):- actual_pages = PdfReader(output).pages- assert len(actual_pages) == 3- for actual, wanted in zip(actual_pages, expected_pages):- assert actual.mediabox == wanted.mediabox- assert actual.cropbox == wanted.cropbox- assert actual.rotation == wanted.rotation- wanted_content = wanted.get_contents()- if wanted_content is None:- assert actual.get_contents() is None- else:- assert actual.get_contents().get_data() == wanted_content.get_data()- assert middle.model_dump(mode="json") == before- missing_page = 1 if page_indices else 0- assert expected_pages[missing_page].get_contents() is None---def test_layout_preview_rejects_invalid_input_without_writing(tmp_path: Path) -> None:- """薄封装保留输入校验,并在映射非法时不写入布局产物。"""- from mineru.kit.gradio.preview import draw_layout_overlay[email protected]("suffix", ["pdf", "png"])+def test_persist_skips_layout_pdf_rendering(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, suffix: str) -> None:+ """PDF 和图片持久化跳过布局 PDF 绘制,并能恢复含旧布局路径的会话状态。"""+ from docvortex import visualization- source = tmp_path / "source.pdf"- output = tmp_path / "layout.pdf"- with pytest.raises(TypeError, match="MiddleJson"):- draw_layout_overlay({}, source, output)- with pytest.raises(FileNotFoundError):- draw_layout_overlay(_middle_json(), source, output)- source.write_bytes(_pdf_bytes())- with pytest.raises(ValueError, match="page_indices length"):- draw_layout_overlay(_middle_json(), source, output, page_indices=(0, 1))- assert not output.exists()---def test_shared_layout_failure_keeps_other_gradio_artifacts(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:- """共享绘制失败仍由 Gradio 记录告警,其他解析文件和下载包继续生成。"""from mineru.kit.gradio import artifacts as artifact_module- from mineru.kit.gradio import preview-- def fail_layout(*_args: Any, **_kwargs: Any) -> bytes:- """模拟共享绘制引擎失败以验证产品层降级路径。"""- raise ValueError("layout fixture failure")+ from mineru.kit.gradio.conversion_service import _prepare_preview- warnings = Mock()- monkeypatch.setattr(preview, "render_layout_pdf", fail_layout)- monkeypatch.setattr(artifact_module.logger, "warning", warnings)- source = tmp_path / "source.pdf"- source.write_bytes(_pdf_bytes())+ render_layout = Mock(side_effect=AssertionError("WebUI must skip layout PDF rendering"))+ monkeypatch.setattr(visualization, "render_layout_pdf", render_layout)+ source = tmp_path / f"source.{suffix}"+ if suffix == "pdf":+ source.write_bytes(_pdf_bytes())+ else:+ Image.new("RGB", (64, 48), "white").save(source)artifacts = persist_parse_result(ParseResult(middle_json=_middle_json()), source, output_root=tmp_path / "output", page_range="")- assert artifacts.layout_pdf_path is None+ render_layout.assert_not_called()assert artifacts.origin_pdf_path.is_file()assert artifacts.middle_json_path.is_file()assert artifacts.markdown_path.is_file()assert not (artifacts.root / "layout.pdf").exists()- assert any("Skipping Gradio layout overlay" in call.args[0] for call in warnings.call_args_list)- with zipfile.ZipFile(render_download(artifacts.as_state(), "markdown")) as archive:+ state = artifacts.as_state()+ assert "layout_pdf_path" not in state+ legacy_state = {**state, "layout_pdf_path": str(artifacts.root / "layout.pdf")}+ restored = artifact_module.RunArtifacts.from_state(legacy_state)+ assert restored.as_state() == state+ assert _prepare_preview(suffix, restored).pdf_path == artifacts.origin_pdf_path+ with zipfile.ZipFile(render_download(legacy_state, "markdown")) as archive:assert f"{artifacts.stem}.md" in archive.namelist()assert "layout.pdf" not in archive.namelist()diff --git a/tests/unittest/test_kit_gradio_pdf_preview.cjs b/tests/unittest/test_kit_gradio_pdf_preview.cjsindex 9944293b..8683fff7 100644--- a/tests/unittest/test_kit_gradio_pdf_preview.cjs+++ b/tests/unittest/test_kit_gradio_pdf_preview.cjs@@ -36,22 +36,22 @@ const relativeParams = new URLSearchParams(iframe(invoke("source", relative, firassert.equal(new URL(relativeParams.get("file")).pathname, pdfUrl.pathname);invoke("begin");-const layout = file("run/layout.pdf");-assert.equal(invoke("result", layout, first, viewer, "run-1").visible, true);+const origin = file("run/origin.pdf");+assert.equal(invoke("result", origin, first, viewer, "run-1").visible, true);assert.equal(invoke("source", first, first, viewer).value, undefined);const beforeReset = disposed;invoke("reset", second);assert.equal(disposed, beforeReset + 1);-assert.equal(invoke("result", layout, first, viewer, "run-1").value, undefined);+assert.equal(invoke("result", origin, first, viewer, "run-1").value, undefined);assert.equal(invoke("source", first, second, viewer).value, undefined);assert.equal(disposed, beforeReset + 1);assert.equal(invoke("source", second, second, viewer).visible, true);assert.equal(disposed, beforeReset + 2);invoke("begin");-assert.equal(invoke("result", layout, second, viewer, "").value, undefined);+assert.equal(invoke("result", origin, second, viewer, "").value, undefined);assert.equal(invoke("clear").value, "");assert.equal(disposed, beforeReset + 3);-assert.equal(invoke("result", layout, second, viewer, "run-2").value, undefined);+assert.equal(invoke("result", origin, second, viewer, "run-2").value, undefined);assert.equal(invoke("source", second, second, viewer).value, undefined);invoke("reset", first);assert.notEqual(iframe(invoke("source", first, first, viewer)).search, initialUrl.search);@@ -60,7 +60,7 @@ const image = file("photo.png");invoke("reset", image);assert.equal(invoke("source", null, image, viewer).value, "");invoke("begin");-assert.equal(invoke("result", layout, image, viewer, "image-run").visible, true);+assert.equal(invoke("result", origin, image, viewer, "image-run").visible, true);const win = { path: "C:\\cache\\中文 #1.pdf", url: "/prefix/gradio_api/file=C:/cache/中文 #1.pdf" };invoke("reset", win);const windowsUrl = new URL(new URLSearchParams(iframe(invoke("source", win, win, viewer)).hash.slice(1)).get("file"));diff --git a/tests/unittest/test_kit_gradio_trace.py b/tests/unittest/test_kit_gradio_trace.pyindex a122cee4..64137030 100644--- a/tests/unittest/test_kit_gradio_trace.py+++ b/tests/unittest/test_kit_gradio_trace.py@@ -20,6 +20,7 @@ def test_pdf_trace_preview_and_public_render(tmp_path: Path) -> None:middle = _middle_json(with_image=False)artifacts = persist_parse_result(ParseResult(middle_json=middle), source, output_root=tmp_path, page_range="")assert _prepare_preview("pdf", artifacts).pdf_path == artifacts.origin_pdf_path+ assert not (artifacts.root / "layout.pdf").exists()preview = render_html_preview(artifacts, public_base_url="http://testserver")frame = BeautifulSoup(preview, "html.parser").iframeassert "allow-same-origin" not in frame["sandbox"]diff --git a/tests/unittest/test_pdf_page_id.py b/tests/unittest/test_pdf_page_id.pyindex 442fdf6d..ebccb755 100644--- a/tests/unittest/test_pdf_page_id.py+++ b/tests/unittest/test_pdf_page_id.py@@ -237,7 +237,7 @@ def _numbered_pdf(path: Path, page_count: int = 5) -> None:def test_real_pdf_selection_preserves_original_indices_and_artifact_order(tmp_path: Path) -> None:- """实测非连续选页与末页组合,同时核对解析页号、原始 PDF 和 layout PDF。"""+ """实测非连续选页与末页组合,同时核对解析页号和原文预览 PDF。"""source = tmp_path / "numbered.pdf"_numbered_pdf(source)parser = MinerUParser(tier="flash")@@ -248,10 +248,10 @@ def test_real_pdf_selection_preserves_original_indices_and_artifact_order(tmp_paassert "Source page 2" not in result.markdown()artifacts = persist_parse_result(result, source, output_root=tmp_path / "review", page_range="r1,3,1,3")assert artifacts.page_indices == (0, 2, 4)- assert artifacts.origin_pdf_path is not None and artifacts.layout_pdf_path is not None+ assert artifacts.origin_pdf_path is not Nonereader = PdfReader(str(artifacts.origin_pdf_path))assert [page.extract_text().strip() for page in reader.pages] == ["Source page 1", "Source page 3", "Source page 5"]- assert len(PdfReader(str(artifacts.layout_pdf_path)).pages) == 3+ assert not (artifacts.root / "layout.pdf").exists()prepared = parser._prepare_input(source, "r1")assert prepared.retained_page_indices == [4]assert PdfReader(io.BytesIO(prepared.file_bytes)).pages[0].extract_text().strip() == "Source page 5"