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
feat(pdf-preview): improve file switching behavior and maintain UI state during PDF processing
myhloli6a3ef95393ae
diff --git a/mineru/kit/gradio/app.py b/mineru/kit/gradio/app.pyindex 5938bd8c..2efd7a6d 100644--- a/mineru/kit/gradio/app.py+++ b/mineru/kit/gradio/app.py@@ -706,7 +706,6 @@ def build_gradio_app()download_reset_outputs = [- status_panel,active_run_id,*download_files.values(),*download_requests.values(),@@ -714,6 +713,8 @@ def build_gradio_app(*download_buttons.values(),download_notice,]+ # 文件切换只重置下载控件;转换点击另加状态卡片,避免重置结果错位清空卡片。+ begin_conversion_outputs = [status_panel, *download_reset_outputs]# 先在前端失效旧请求,再等待上传/清除/转换的 Python 回调,防止迟到的下载被触发。gr.on(@@ -966,7 +967,7 @@ def build_gradio_app(begin_conversion = convert_button.click(fn=reset_download_ui,inputs=[],- outputs=download_reset_outputs,+ outputs=begin_conversion_outputs,js=(f"() => {{ ({pdf_preview_js('begin')})(); "f"return [{json.dumps(_status_html(STATUS_PREPARING_REQUEST))}, ...({download_js('reset')})()]; }}"@@ -984,6 +985,8 @@ def build_gradio_app(inputs=input_file,outputs=preview_outputs,cancels=[convert_event],+ # 示例切换也会触发文件变更,预览准备期间保持状态卡片可见。+ show_progress="hidden",**private_event_kwargs,)# 成功事件接收已经序列化的原生 FileData,避免首次挂载时丢失 URL。diff --git a/mineru/resources/gradio_app.css b/mineru/resources/gradio_app.cssindex 89098ff7..6615a391 100644--- a/mineru/resources/gradio_app.css+++ b/mineru/resources/gradio_app.css@@ -50,6 +50,11 @@ body.dark .gradio-container {.mineru-control-column:has(#mineru-kit-examples) {align-self: stretch !important;}+/* PDF 切换时 iframe 会短暂卸载;保持预览列内边距,避免 flex 重新分配左栏宽度。 */+.mineru-kit-workspace:has(.mineru-page-values[data-range-visible="true"]) .mineru-kit-preview {+ padding: 0 !important;+ gap: 0 !important;+}/* HF Space 不执行 Gradio 的自定义 CSS 前缀改写,以下规则必须独立压过Gradio 自带 scoped 样式(div.svelte-*、.head.svelte-*、.prose 等)。 */.mineru-tier-slider .head,@@ -59,8 +64,9 @@ body.dark .gradio-container {}/* 两个原生 range 共用同一条轨道;仅滑块接收指针,保留原生键盘交互。 */.mineru-kit-page-range {- display: none !important; position: relative; padding: 12px 12px 32px !important; overflow: visible !important;+ display: none !important; position: relative; padding: 8px 12px 22px !important; overflow: visible !important;border: 1px solid var(--mineru-panel-border); border-radius: 8px;+ gap: 8px !important; flex: 0 0 auto !important;}/* 用明确的选页状态控制可见性,保留原生滑块的交互状态。 */.mineru-kit-page-range:has([data-range-visible="true"]) { display: flex !important; }@@ -70,7 +76,7 @@ body.dark .gradio-container {.mineru-page-selection { font-weight: 600; color: var(--mineru-accent); }.mineru-page-axis { position: absolute; bottom: 0; left: 0; right: 0; font-size: 12px; opacity: .7; }.mineru-page-summary { position: static !important; }-.mineru-kit-page-range .mineru-page-axis { bottom: 8px; left: 12px; right: 12px; }+.mineru-kit-page-range .mineru-page-axis { bottom: 5px; left: 12px; right: 12px; }.mineru-page-sliders { position: relative !important; height: 32px; min-height: 32px !important; gap: 0 !important; }.mineru-page-sliders::before {content: ""; position: absolute; left: 8px; right: 8px; top: 14px; height: 4px; border-radius: 4px;diff --git a/mineru/resources/gradio_page_range.js b/mineru/resources/gradio_page_range.jsindex f8bbb143..f4fcb75e 100644--- a/mineru/resources/gradio_page_range.js+++ b/mineru/resources/gradio_page_range.js@@ -61,19 +61,22 @@} else if (state.handle_b < state.handle_a) {state.start_handle = "b";}- const visible = needsRange && count > 0;+ // PDF 页数读取期间保留选页控件的占位,避免切换文件时整列高度跳动。+ const visible = needsRange;const interactive = visible && count > 1;const start = Math.min(state.handle_a, state.handle_b);const end = Math.max(state.handle_a, state.handle_b);const selected = end - start + 1;const limitText = maxPages === null ? text("page_unlimited") : text("page_limit", { count: maxPages });+ const selectionText = count ? `[${start}-${end}] · ${text("page_count", { count: selected })}`+ : text(state.error ? "page_read_failed" : "reading_pages");const summary = `<div class="mineru-page-values" data-range-visible="${visible}" data-start-handle="${state.start_handle}">`- + `<span class="mineru-page-selection">[${start}-${end}] · ${text("page_count", { count: selected })}</span>`+ + `<span class="mineru-page-selection">${selectionText}</span>`+ `</div>`- + `<div class="mineru-page-axis"><span>1</span><span>${limitText}</span><span>${count}</span></div>`;+ + `<div class="mineru-page-axis"><span>1</span><span>${limitText}</span><span>${count || "–"}</span></div>`;const notice = flashUnavailable ? text("flash_unavailable")- : needsRange && !count ? (message(state.error) || text("reading_pages")) : "";- const range = visible ? (start === end ? String(start) : `${start}-${end}`) : "";+ : needsRange && !count ? message(state.error) : "";+ const range = count ? (start === end ? String(start) : `${start}-${end}`) : "";// 新版 Gradio 要求非零跨度;单页时禁用滑块,实际页数和提交范围仍为 1。const slider = (value, label) => update({ minimum: 1, maximum: Math.max(2, count), value, interactive, label });return [diff --git a/tests/unittest/test_kit_gradio.py b/tests/unittest/test_kit_gradio.pyindex 8b771c44..575919f0 100644--- a/tests/unittest/test_kit_gradio.py+++ b/tests/unittest/test_kit_gradio.py@@ -1075,6 +1075,13 @@ def test_gradio_examples_render_only_with_local_files(monkeypatch: pytest.Monkeyupload = 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"]+ status = next(block for block in demo.blocks.values() if "mineru-status-panel" in (block.elem_classes or []))+ preview_change = next(+ event+ for event in demo.config["dependencies"]+ if (upload._id, "change") in event["targets"] and status._id in event["outputs"]+ )+ assert preview_change["show_progress"] == "hidden"empty_cwd = tmp_path / "empty"empty_cwd.mkdir()diff --git a/tests/unittest/test_kit_gradio_download.py b/tests/unittest/test_kit_gradio_download.pyindex 0fd471b7..389fac86 100644--- a/tests/unittest/test_kit_gradio_download.py+++ b/tests/unittest/test_kit_gradio_download.py@@ -120,6 +120,14 @@ def test_download_event_chain_and_pdf_transport(tmp_path: Path) -> None:assert "Preparing request..." in reset_event.fn()[0]reset_dependency = next(dep for dep in app.config["dependencies"] if dep["id"] == reset_event._id)assert "Preparing request..." in reset_dependency["js"]+ upload = next(block for block in app.blocks.values() if "mineru-upload-file" in (block.elem_classes or []))+ reset_on_upload = next(+ dep+ for dep in app.config["dependencies"]+ if (upload._id, "change") in dep["targets"] and dep["outputs"] == [output._id for output in reset_event.outputs[1:]]+ )+ assert reset_event.outputs[0]._id not in reset_on_upload["outputs"]+ assert reset_on_upload["js"] and reset_on_upload["backend_fn"] is Falsehandlers = [fn for fn in app.fns.values() if fn.name == "handler"]assert len(handlers) == 7files = []diff --git a/tests/unittest/test_kit_gradio_page_range.cjs b/tests/unittest/test_kit_gradio_page_range.cjsindex 356d9639..61c6b260 100644--- a/tests/unittest/test_kit_gradio_page_range.cjs+++ b/tests/unittest/test_kit_gradio_page_range.cjs@@ -114,7 +114,10 @@ for (const availableTiers of [["basic", "standard", "advanced"], ["advanced"], [let out = ui.step("file", { file: "/new.pdf" });assert.equal(out[7].value, 3);assert.equal(out[5].interactive, false);- assert.match(out[6].value, /正在读取/);+ assert.match(out[2], /data-range-visible="true"/);+ assert.match(out[2], /正在读取/);+ assert.equal(out[3], "");+ assert.equal(out[6].visible, false);out = ui.step("metadata", { metadata: { path: "/report.pdf", page_count: 100, error: "" } });assert.equal(out[5].interactive, false);out = ui.step("metadata", { metadata: { path: "/new.pdf", page_count: 12, error: "" } });@@ -148,7 +151,10 @@ let ui = createUi();assert.match(ui.step("file")[2], /data-range-visible="false"/);let out = ui.step("file", { file: { path: "/report.pdf" } });assert.equal(out[5].interactive, false);-assert.match(out[6].value, /正在读取/);+assert.match(out[2], /data-range-visible="true"/);+assert.match(out[2], /正在读取/);+assert.equal(out[6].visible, false);+assert.equal(out[0].interactive, false);out = ui.upload(100);assert.equal(out[0].minimum, 1);assert.equal(out[1].maximum, 100);@@ -224,6 +230,7 @@ ui = createUi();ui.step("file", { file: { path: "/bad.pdf" } });out = ui.step("metadata", { metadata: { path: "/bad.pdf", page_count: 0, error: "<bad>" } });assert.equal(out[5].interactive, false);+assert.match(out[2], /无法读取 PDF 页数/);assert.equal(out[6].value, "<bad>");out = ui.step("tier", { position: 0 });assert.equal(out[5].interactive, false);