Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: add example file rendering for supported file types in the Gradio app

myhloli9472eb870934
diff --git a/mineru/kit/gradio/app.py b/mineru/kit/gradio/app.py
index eeeeb04f..d96fc60f 100644
--- a/mineru/kit/gradio/app.py
+++ b/mineru/kit/gradio/app.py
@@ -326,6 +326,7 @@ def build_gradio_app(
raise ValueError("V1 API server did not advertise any parsing tier")
preferred_tier = _default_tier(capabilities)
file_types = _supported_file_types()
+ examples = _example_files(file_types) if enable_example else []
app_css = _resource_text("gradio_app.css") + _KIT_MENU_CSS + _download_icon_css()
i18n = gr.I18n(**translations())
app_js = _resource_text("gradio_app.js").replace(
@@ -420,6 +421,13 @@ def build_gradio_app(
)
clear_button = gr.ClearButton(value=i18n("mineru.clear"), scale=1, min_width=1)
status_panel = gr.HTML(_status_html(), elem_classes=["mineru-status-panel"])
+ if examples:
+ gr.Examples(
+ examples=examples,
+ inputs=input_file,
+ label=i18n("mineru.examples"),
+ elem_id="mineru-kit-examples",
+ )
with gr.Column(scale=4, min_width=340, elem_classes=["mineru-kit-preview", "mineru-preview-pane"]):
pdf_preview = gr.File(visible=False, interactive=False, label="PDF", type="filepath")
@@ -446,7 +454,8 @@ def build_gradio_app(
elem_classes=["mineru-kit-office-preview", "mineru-office-preview-html"],
)
ofd_preview = gr.HTML(
- value="", apply_default_css=False,
+ value="",
+ apply_default_css=False,
elem_classes=["mineru-kit-ofd-preview"],
)
ofd_ticket = gr.Textbox(value="", visible=False)
@@ -495,11 +504,6 @@ def build_gradio_app(
)
download_notice = gr.HTML(value="", elem_classes=["mineru-kit-download-notice"])
- if enable_example:
- examples = _example_files(file_types)
- if examples:
- gr.Examples(examples=examples, inputs=input_file, label=i18n("mineru.examples"), elem_id="mineru-kit-examples")
-
artifact_state = gr.State(value=None)
active_run_id = gr.Textbox(value="", visible=False)
download_files = {name: gr.File(visible=False, interactive=False) for name, _label in _DOWNLOAD_FORMATS}
@@ -586,22 +590,34 @@ def build_gradio_app(
ofd_script = _resource_text("gradio_ofd_preview.js")
input_file.change(
- fn=None, inputs=input_file, outputs=[ofd_ticket, ofd_preview],
+ fn=None,
+ inputs=input_file,
+ outputs=[ofd_ticket, ofd_preview],
js=f"(...args) => ({ofd_script})('begin', ...args)",
**private_event_kwargs,
)
# Gradio 6.8 的纯前端事件不可靠地触发 then;通过请求值变化启动后台转换。
render_ofd = ofd_ticket.change(
- fn=prepare_ofd_preview, inputs=[input_file, ofd_ticket], outputs=ofd_receipt,
- concurrency_limit=None, trigger_mode="multiple", **private_event_kwargs,
+ fn=prepare_ofd_preview,
+ inputs=[input_file, ofd_ticket],
+ outputs=ofd_receipt,
+ concurrency_limit=None,
+ trigger_mode="multiple",
+ **private_event_kwargs,
)
render_ofd.then(
- fn=None, inputs=ofd_receipt, outputs=ofd_preview,
- js=f"(...args) => ({ofd_script})('apply', ...args)", **private_event_kwargs,
+ fn=None,
+ inputs=ofd_receipt,
+ outputs=ofd_preview,
+ js=f"(...args) => ({ofd_script})('apply', ...args)",
+ **private_event_kwargs,
)
clear_button.click(
- fn=None, inputs=[], outputs=[ofd_ticket, ofd_preview],
- js=f"(...args) => ({ofd_script})('clear', ...args)", **private_event_kwargs,
+ fn=None,
+ inputs=[],
+ outputs=[ofd_ticket, ofd_preview],
+ js=f"(...args) => ({ofd_script})('clear', ...args)",
+ **private_event_kwargs,
)
download_script = _resource_text("gradio_download.js")
diff --git a/mineru/resources/gradio_app.css b/mineru/resources/gradio_app.css
index 0f57cd98..89098ff7 100644
--- a/mineru/resources/gradio_app.css
+++ b/mineru/resources/gradio_app.css
@@ -46,6 +46,10 @@ body.dark .gradio-container {
top: 12px;
align-self: flex-start;
}
+/* 有示例时控制列拉伸到与预览面板等高,由示例块吸收底部空白。 */
+.mineru-control-column:has(#mineru-kit-examples) {
+ align-self: stretch !important;
+}
/* HF Space 不执行 Gradio 的自定义 CSS 前缀改写,以下规则必须独立压过
Gradio 自带 scoped 样式(div.svelte-*、.head.svelte-*、.prose 等)。 */
.mineru-tier-slider .head,
@@ -105,21 +109,41 @@ body.dark .gradio-container {
.mineru-upload-file label[data-testid="block-label"] > span {
margin-top: 2px;
}
-#mineru-example-files label[data-testid="block-label"],
-#mineru-example-files [data-testid="block-label"],
-#mineru-example-files .label {
- display: none !important;
-}
-.mineru-examples-panel,
-.block.mineru-examples-panel {
+/* 示例块:吸收控制列剩余高度,与状态卡一致的卡片外观;文件多时内部滚动。 */
+#mineru-kit-examples {
width: 100% !important;
max-width: none !important;
margin-left: 0 !important;
margin-right: 0 !important;
+ flex: 1 1 auto !important;
+ min-height: 0 !important;
+ display: flex !important;
+ flex-direction: column !important;
+ overflow: hidden !important;
+ border: 1px solid var(--mineru-panel-border) !important;
+ border-radius: 8px !important;
+ background: var(--mineru-status-panel-bg) !important;
+ padding: 12px !important;
}
-#mineru-example-files {
- width: 100% !important;
- max-width: none !important;
+#mineru-kit-examples > .label {
+ flex: none;
+ margin: 0 0 8px !important;
+ font-size: 13px;
+ font-weight: 700;
+}
+#mineru-kit-examples .gallery {
+ flex: 1 1 auto;
+ min-height: 0;
+ align-content: flex-start;
+ overflow-y: auto;
+}
+#mineru-kit-examples .gallery-item {
+ font-size: 13px;
+ transition: border-color .15s ease, color .15s ease;
+}
+#mineru-kit-examples .gallery-item:hover {
+ border-color: var(--mineru-accent) !important;
+ color: var(--mineru-accent) !important;
}
.mineru-actions {
flex-wrap: nowrap !important;
@@ -143,8 +167,9 @@ body.dark .gradio-container {
min-width: 340px;
}
.mineru-preview-pane > .block {
- height: calc(var(--mineru-preview-content-height) + 48px) !important;
- max-height: calc(var(--mineru-preview-content-height) + 48px) !important;
+ /* +110px 与结果栏 min-height 对齐,扣除本栏 padding+border 共 26px。 */
+ height: calc(var(--mineru-preview-content-height) + 110px - 26px) !important;
+ max-height: calc(var(--mineru-preview-content-height) + 110px - 26px) !important;
overflow: hidden !important;
resize: none !important;
}
@@ -153,8 +178,8 @@ body.dark .gradio-container {
min-height: var(--mineru-preview-content-height);
}
.mineru-preview-pane > .block.mineru-office-preview-html {
- height: calc(var(--mineru-preview-content-height) + 48px) !important;
- max-height: calc(var(--mineru-preview-content-height) + 48px) !important;
+ height: calc(var(--mineru-preview-content-height) + 110px - 26px) !important;
+ max-height: calc(var(--mineru-preview-content-height) + 110px - 26px) !important;
min-height: var(--mineru-preview-content-height) !important;
overflow: hidden !important;
resize: none !important;
@@ -367,4 +392,11 @@ body.dark .gradio-container {
.status-steps-list {
grid-template-columns: 1fr;
}
+ /* 纵向堆叠时保持自然高度,避免示例块拉伸整页。 */
+ #mineru-kit-examples {
+ flex: 0 0 auto !important;
+ }
+ #mineru-kit-examples .gallery {
+ max-height: 240px;
+ }
}
diff --git a/tests/unittest/test_kit_gradio.py b/tests/unittest/test_kit_gradio.py
index 66aee85b..51305d41 100644
--- a/tests/unittest/test_kit_gradio.py
+++ b/tests/unittest/test_kit_gradio.py
@@ -1034,6 +1034,31 @@ def test_gradio_ocr_control_visibility_reset_and_event_binding(tmp_path: Path) -
assert change["queue"] is False and change["trigger_mode"] == "always_last"
+def test_gradio_examples_render_only_with_local_files(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
+ """验证示例块仅在启动目录存在受支持文件时渲染,并把点击绑定到上传组件。"""
+ capabilities = V1ServerCapabilities("http://127.0.0.1:1", ("flash", "standard"), ("zip",), ("file_id",))
+
+ def find_examples(demo: Any) -> list[Any]:
+ return [block for block in demo.blocks.values() if getattr(block, "elem_id", None) == "mineru-kit-examples"]
+
+ example_cwd = tmp_path / "cwd"
+ (example_cwd / "examples").mkdir(parents=True)
+ (example_cwd / "examples" / "demo.pdf").write_bytes(_pdf_bytes())
+ (example_cwd / "note.txt").write_text("unsupported")
+ monkeypatch.chdir(example_cwd)
+ demo = build_gradio_app(Mock(), capabilities, output_root=tmp_path / "out", enable_example=True)
+ (dataset,) = find_examples(demo)
+ assert [sample[0] for sample in dataset.samples] == ["demo.pdf"]
+ upload = next(block for block in demo.blocks.values() if "mineru-upload-file" in (block.elem_classes or []))
+ click = next(event for event in demo.config["dependencies"] if (dataset._id, "click") in event["targets"])
+ assert upload._id in click["outputs"]
+
+ empty_cwd = tmp_path / "empty"
+ empty_cwd.mkdir()
+ monkeypatch.chdir(empty_cwd)
+ assert not find_examples(build_gradio_app(Mock(), capabilities, output_root=tmp_path / "out2", enable_example=True))
+
+
@pytest.mark.parametrize("suffix", [".png", ".docx", ".csv"])
def test_gradio_non_pdf_ignores_hidden_force_ocr(tmp_path: Path, suffix: str) -> None:
"""直接调用事件并传入残留 True,非 PDF 请求仍使用 auto。"""