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
Fix URL encoding for file paths in Gradio API and update comments for clarity
myhloli8de2e84c21d8
diff --git a/mineru/kit/gradio/app.py b/mineru/kit/gradio/app.pyindex 5e3eaa2a..ffde428f 100644--- a/mineru/kit/gradio/app.py+++ b/mineru/kit/gradio/app.py@@ -1076,7 +1076,7 @@ def _build_office_preview_html(file_path: str | Path, request: object | None = Nheaders = getattr(request, "headers", None) or {}host = headers.get("x-forwarded-host") or headers.get("host") or "localhost:7860"protocol = headers.get("x-forwarded-proto") or "http"- public_url = f"{protocol}://{host}/gradio_api/file={quote(str(source_path), safe='/:')}"+ public_url = f"{protocol}://{host}/gradio_api/file={quote(source_path.as_posix(), safe='/:')}"short_name = f"{source_path.stem[-12:]}{source_path.suffix}" if source_path.stem else source_path.nameshort_public_url = f"{protocol}://{host}/....{short_name}"viewer_url = "https://view.officeapps.live.com/op/embed.aspx?src=" + quote(public_url, safe="")diff --git a/mineru/kit/gradio/artifacts.py b/mineru/kit/gradio/artifacts.pyindex 4147fd1b..f2991584 100644--- a/mineru/kit/gradio/artifacts.py+++ b/mineru/kit/gradio/artifacts.py@@ -323,7 +323,7 @@ def _render_html(artifacts: RunArtifacts, *, public_base_url: str) -> str:if urlsplit(public_base_url).scheme not in {"http", "https"} or not urlsplit(public_base_url).netloc:raise ValueError("HTML rendering requires the current Gradio HTTP base URL")result = ParseResult.from_json(artifacts.middle_json_path.read_text(encoding="utf-8"))- asset_base_url = f"{public_base_url.rstrip('/')}/gradio_api/file={quote(str(artifacts.root), safe='/:')}"+ asset_base_url = f"{public_base_url.rstrip('/')}/gradio_api/file={quote(artifacts.root.as_posix(), safe='/:')}"return cast(str,render(diff --git a/mineru/model/_internal/pytorchocr/base_ocr_v20.py b/mineru/model/_internal/pytorchocr/base_ocr_v20.pyindex 1b2b672d..7412ef65 100644--- a/mineru/model/_internal/pytorchocr/base_ocr_v20.py+++ b/mineru/model/_internal/pytorchocr/base_ocr_v20.py@@ -7,7 +7,7 @@ import torchfrom .modeling.architectures.base_model import BaseModel-# OCR 推理精度开关:auto 表示 CPU 使用 fp32,非 CPU 自动使用 fp16。+# OCR rec/cls 推理精度开关:auto 表示 CPU 使用 fp32,非 CPU 自动使用 fp16;det 固定使用 fp32。OCR_INFERENCE_PRECISION = "auto"diff --git a/mineru/model/_internal/pytorchocr/infer/predict_det.py b/mineru/model/_internal/pytorchocr/infer/predict_det.pyindex 4752ea7d..c035e440 100644--- a/mineru/model/_internal/pytorchocr/infer/predict_det.py+++ b/mineru/model/_internal/pytorchocr/infer/predict_det.py@@ -126,9 +126,10 @@ class TextDetector(BaseOCRV20):super(TextDetector, self).__init__(network_config, **kwargs)self.load_pytorch_weights(self.weights_path)self.net.eval()+ # Tiny/seal det 的 BatchNorm 统计值超出 fp16 范围,所有设备均固定 fp32;rec/cls 仍使用各自精度策略。self._apply_inference_precision(self.device,- precision_override=getattr(args, "det_inference_precision", None),+ precision_override="fp32",)for module in self.net.modules():if hasattr(module, 'rep'):diff --git a/mineru/model/ocr/pytorch_paddle.py b/mineru/model/ocr/pytorch_paddle.pyindex 5c7363ce..937764be 100644--- a/mineru/model/ocr/pytorch_paddle.py+++ b/mineru/model/ocr/pytorch_paddle.py@@ -70,8 +70,6 @@ class PytorchPaddleOCR(TextSystem):kwargs['rec_char_dict_path'] = str(PPOCRV6_DICT_PATH)kwargs['rec_batch_num'] = 6if self.is_seal:- # Seal detector 的 BatchNorm 统计量会在 fp16 下溢出,初始化时固定使用 fp32。- kwargs['det_inference_precision'] = 'fp32'kwargs['det_limit_side_len'] = 736kwargs['det_limit_type'] = 'min'kwargs['det_max_side_limit'] = 4000diff --git a/tests/unittest/test_kit_gradio_html.py b/tests/unittest/test_kit_gradio_html.pyindex e681e62e..7801821d 100644--- a/tests/unittest/test_kit_gradio_html.py+++ b/tests/unittest/test_kit_gradio_html.py@@ -8,7 +8,8 @@ import ioimport jsonimport reimport zipfile-from pathlib import Path+from dataclasses import replace+from pathlib import Path, PurePosixPath, PureWindowsPathfrom types import SimpleNamespacefrom urllib.parse import quote, unquote@@ -21,11 +22,12 @@ from PIL import Imagefrom starlette.requests import Requestfrom mineru.backend.analyze import doc_analyze+from mineru.kit.gradio import artifacts as artifacts_modulefrom mineru.kit.gradio.app import _download_handler, _gradio_public_base_url, build_gradio_appfrom mineru.kit.gradio.artifacts import _prepare_preview_links, persist_parse_result, render_download, render_html_previewfrom mineru.kit.gradio.client import V1ServerCapabilitiesfrom mineru.parser.base import ParseResult-from mineru.types import BlockType, EquationBlock, TableBlock, TableBodyBlock+from mineru.types import BlockType, ChartBlock, ChartBodyBlock, EquationBlock, TableBlock, TableBodyBlockfrom test_kit_gradio import _middle_json@@ -36,6 +38,77 @@ def _image_bytes(color: str) -> bytes:return output.getvalue()[email protected]("entrypoint", ["preview", "download"])+ "platform_root",+ [+ PurePosixPath("/tmp/输出 文件/gradio/授权书"),+ PureWindowsPath("C:/Users/测试用户/输出 文件/gradio/授权书"),+ PureWindowsPath("//fileserver/共享目录/输出 文件/gradio/授权书"),+ ],+ ids=["posix", "windows-drive", "windows-unc"],+)+def test_platform_asset_urls_preserve_seal_images_and_details(+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+ platform_root: PurePosixPath | PureWindowsPath,+ entrypoint: str,+) -> None:+ """跨平台资源根保留印章、普通图片、图表和表内图片,预览及下载均不会被路径校验过滤。"""+ middle = _middle_json(file_suffix="docx")+ seal = middle.pages[0].blocks[1]+ seal.sub_type = "seal"+ seal.content[0].content = "上海人工智能创新中心"+ seal.content[0].image_path = "images/印章 1.png"+ photo = seal.model_copy(deep=True, update={"index": 2, "sub_type": None})+ photo.content[0].index = 2+ middle.pages[0].blocks.extend(+ [+ photo,+ ChartBlock(+ type="chart",+ index=3,+ content=[ChartBodyBlock(type="chart_body", index=3, content="", image_path="images/chart.png")],+ ),+ TableBlock(+ type="table",+ index=4,+ content=[+ TableBodyBlock(+ type="table_body", index=4, content='<table><tr><td><img src="images/cell.png"></td></tr></table>'+ )+ ],+ ),+ ]+ )+ artifacts = artifacts_module.create_run_artifacts(tmp_path / "授权书.docx", tmp_path)+ artifacts.middle_json_path.write_text(ParseResult(middle_json=middle).to_json(), encoding="utf-8")+ original_render_html = artifacts_module._render_html++ def render_with_platform_root(artifacts: artifacts_module.RunArtifacts, *, public_base_url: str) -> str:+ """文件读写与边界校验仍在本机执行,仅在实际 URL 构造处注入目标平台路径。"""+ return original_render_html(replace(artifacts, root=platform_root), public_base_url=public_base_url)++ monkeypatch.setattr(artifacts_module, "_render_html", render_with_platform_root)+ public_base = "https://demo.example.test/mineru"+ if entrypoint == "preview":+ frame = BeautifulSoup(render_html_preview(artifacts, public_base_url=public_base), "html.parser").iframe+ document = frame["srcdoc"]+ else:+ path = render_download(artifacts.as_state(), "html", allowed_root=tmp_path, public_base_url=public_base)+ document = Path(path).read_text(encoding="utf-8")+ soup = BeautifulSoup(document, "html.parser")+ images = soup.find_all("img")+ assert len(images) == 4+ expected_base = f"{public_base}/gradio_api/file={quote(platform_root.as_posix(), safe='/:')}/images/"+ assert all(image["src"].startswith(expected_base) for image in images)+ assert all("%5c" not in image["src"].lower() and " " not in image["src"] for image in images)+ details = soup.find("summary", string="seal").parent+ assert details.name == "details" and "open" not in details.attrs+ assert "上海人工智能创新中心" in details.get_text()+ assert details.find_previous_sibling("img") is not None++@pytest.mark.parametrize("image_source", ["inline", "sidecar"])def test_materialized_html_and_independent_archives(tmp_path: Path, image_source: str) -> None:"""正文、表格图片和公式保持语义,HTTP 与 ZIP 各自使用正确的图片地址。"""diff --git a/tests/unittest/test_kit_gradio_office_preview.py b/tests/unittest/test_kit_gradio_office_preview.pyindex 4a0743a6..1362dd86 100644--- a/tests/unittest/test_kit_gradio_office_preview.py+++ b/tests/unittest/test_kit_gradio_office_preview.py@@ -5,7 +5,7 @@ from __future__ import annotationsimport asyncioimport htmlimport re-from pathlib import Path+from pathlib import Path, PureWindowsPathfrom types import SimpleNamespacefrom unittest.mock import AsyncMockfrom urllib.parse import parse_qs, quote, urlsplit@@ -61,7 +61,7 @@ def test_office_short_address_keeps_full_encoded_iframe_url(headers: dict[str, smarkup = app_module._build_office_preview_html(source, SimpleNamespace(headers=headers))iframe_url = html.unescape(re.search(r'<iframe[^>]+src="([^"]+)"', markup)[1])public_url = parse_qs(urlsplit(iframe_url).query)["src"][0]- assert public_url == f"{base}/gradio_api/file={quote(str(source), safe='/:')}"+ assert public_url == f"{base}/gradio_api/file={quote(source.as_posix(), safe='/:')}"displayed = html.unescape(re.search(r'<div class="office-preview-source-link">(.*?)</div>', markup)[1])assert f"{base}/....{source.stem[-12:]}{source.suffix}" in displayedassert "/uploaded/private" not in displayed@@ -72,6 +72,17 @@ def test_office_short_address_keeps_full_encoded_iframe_url(headers: dict[str, sassert markup == app_module._build_office_preview_html(source)+def test_office_windows_file_url_uses_forward_slashes(monkeypatch: pytest.MonkeyPatch) -> None:+ """Windows 上传预览使用正斜杠 URL,保留盘符、中文及空格的正确编码。"""+ source = PureWindowsPath("C:/Users/测试用户/上传 文件/报告.docx")+ monkeypatch.setattr(app_module, "Path", PureWindowsPath)+ markup = app_module._build_office_preview_html(str(source))+ iframe_url = html.unescape(re.search(r'<iframe[^>]+src="([^"]+)"', markup)[1])+ public_url = parse_qs(urlsplit(iframe_url).query)["src"][0]+ assert public_url == "http://localhost:7860/gradio_api/file=" + quote(source.as_posix(), safe="/:")+ assert "%5c" not in public_url.lower()++def test_office_copy_matches_345_release() -> None:"""固定中英文文案属于本次恢复的用户可见契约。"""assert MESSAGES["office_preview_title"] == ("Office online preview", "Office 在线预览")diff --git a/tests/unittest/test_ocr_detector_precision.py b/tests/unittest/test_ocr_detector_precision.pynew file mode 100644index 00000000..6bbd0d7f--- /dev/null+++ b/tests/unittest/test_ocr_detector_precision.py@@ -0,0 +1,95 @@+"""验证 det 的 FP32 约束独立于共享 OCR 精度和设备路由。"""++from argparse import Namespace+from typing import Any, Self+from unittest.mock import Mock++import numpy as np+import pytest+import torch++from mineru.model._internal.pytorchocr import base_ocr_v20+from mineru.model._internal.pytorchocr.infer import pytorchocr_utility+from mineru.model._internal.pytorchocr.infer.predict_det import TextDetector+from mineru.model._internal.pytorchocr.infer.predict_system import TextSystem+++class _PrecisionNet(torch.nn.Module):+ """使用真实参数和大数值 BN 缓冲区,仅模拟设备迁移以覆盖本机不存在的硬件。"""++ def __init__(self, config: dict[str, Any], **kwargs: Any) -> None:+ """构造会在 FP16 转换时溢出的 BN 统计值。"""+ super().__init__()+ self.norm = torch.nn.BatchNorm2d(3)+ self.norm.running_var.fill_(1_000_000)+ self.requested_device: str | None = None+ self.input_dtypes: list[torch.dtype] = []++ def to(self, device: str | None = None, *, dtype: torch.dtype | None = None) -> Self:+ """保留真实 dtype 转换,只记录目标设备而不调用加速卡运行时。"""+ if device is not None:+ self.requested_device = str(device)+ return super().to(dtype=dtype) if dtype is not None else self++ def forward(self, inputs: torch.Tensor) -> dict[str, torch.Tensor]:+ """记录真实批处理输入精度,并返回可供 DB 后处理检测的概率图。"""+ self.input_dtypes.append(inputs.dtype)+ return {"maps": self.norm(inputs).mean(dim=1, keepdim=True).sigmoid() + 0.25}+++def ocr_args(monkeypatch: pytest.MonkeyPatch) -> Namespace:+ """替换模型文件加载,保留 det/rec/cls 的真实初始化和精度设置。"""+ monkeypatch.setattr(base_ocr_v20, "BaseModel", _PrecisionNet)+ monkeypatch.setattr(pytorchocr_utility, "get_arch_config", Mock(return_value={}))+ monkeypatch.setattr(base_ocr_v20.BaseOCRV20, "load_pytorch_weights", Mock())+ monkeypatch.setattr(base_ocr_v20.BaseOCRV20, "read_pytorch_weights", Mock(return_value={}))+ monkeypatch.setattr(base_ocr_v20.BaseOCRV20, "get_out_channels", Mock(return_value=1))+ monkeypatch.setattr(base_ocr_v20.BaseOCRV20, "load_state_dict", Mock())+ args = pytorchocr_utility.init_args().parse_args([])+ args.use_angle_cls = True+ args.det_inference_precision = "fp16"+ return args++[email protected]("device", ["cpu", "mps", "cuda:0", "npu:0", "xpu:0"])[email protected]("lang", ["ch", "seal", "seal_lite"])[email protected]("precision", ["auto", "fp16", "fp32"])+def test_detector_fp32_keeps_rec_cls_precision_and_device(+ monkeypatch: pytest.MonkeyPatch, ocr_args: Namespace, device: str, lang: str, precision: str+) -> None:+ """所有设备的 det 均保留有限 FP32 权重与输入,且不改变共享参数及 rec/cls 策略。"""+ monkeypatch.setattr(base_ocr_v20, "OCR_INFERENCE_PRECISION", precision)+ ocr_args.device, ocr_args.lang = device, lang+ system = TextSystem(ocr_args)+ detector = system.text_detector+ expected_device = "cpu" if device == "mps" and lang == "ch" else device+ assert detector.device == detector.net.requested_device == expected_device+ assert ocr_args.device == device+ assert detector.ocr_inference_dtype == torch.float32+ for tensor in (*detector.net.parameters(), *detector.net.buffers()):+ if tensor.is_floating_point():+ assert tensor.dtype == torch.float32+ assert torch.isfinite(tensor).all()+ assert detector._to_inference_dtype(torch.ones(1, dtype=torch.float16)).dtype == torch.float32+ indices = torch.ones(1, dtype=torch.int64)+ assert detector._to_inference_dtype(indices) is indices+ expected_rec_dtype = torch.float32 if device == "cpu" or precision == "fp32" else torch.float16+ for model in (system.text_recognizer, system.text_classifier):+ assert model.device == model.net.requested_device == device+ assert model.ocr_inference_dtype == expected_rec_dtype+ assert next(model.net.parameters()).dtype == expected_rec_dtype+ assert model._to_inference_dtype(torch.ones(1)).dtype == expected_rec_dtype+++def test_detector_single_and_mixed_size_batches_use_fp32(monkeypatch: pytest.MonkeyPatch, ocr_args: Namespace) -> None:+ """单图及不同形状分桶的真实前后处理均使用 FP32,且批处理框与单图一致。"""+ monkeypatch.setattr(base_ocr_v20, "OCR_INFERENCE_PRECISION", "fp16")+ detector = TextDetector(ocr_args)+ images = [np.full((height, width, 3), 255, dtype=np.uint8) for height, width in [(32, 64), (64, 64), (32, 64)]]+ singles = [detector(image)[0] for image in images]+ batches = detector.batch_predict(images, max_batch_size=2)+ assert all(len(boxes) > 0 for boxes in singles)+ for single, (batch, _elapsed) in zip(singles, batches):+ np.testing.assert_array_equal(single, batch)+ assert detector.net.input_dtypes == [torch.float32] * 5