MinerU Gradio Interface#
Overview#
The Gradio web UI (mineru/cli/gradio_app.py) is the primary interactive frontend for MinerU. It accepts PDF, image, and Office files (DOCX, PPTX, XLSX), dispatches document-parsing jobs to a backend API server (local or remote), and streams real-time status updates back to the user while they wait. Output is delivered as a downloadable ZIP archive alongside rendered Markdown and JSON tabs.
Entry point: mineru/cli/gradio_app.py — invoked via the main() Click command .
Architecture: Local API Server + Gradio Frontend#
The UI does not call the document-parsing libraries directly. Instead, it delegates to a FastAPI backend via HTTP:
- By default, Gradio starts a reusable local
mineru-apiprocess managed byReusableLocalAPIServerinapi_client.py. The singleton is held at module level as_gradio_local_api_server. - A remote API URL can be supplied via
--api-url, in which case the local process is never started . - VLM model preloading on startup is gated by
--enable-vlm-preload.
The local server is launched as a subprocess (or a multiprocessing spawn process on Ascend/Linux) and shut down via atexit .
File I/O and Output Layout#
Each conversion creates an isolated run directory under ./output/gradio/<run_id>/:
./output/gradio/<timestamp>_<uuid>_<stem>/
├── result/ ← extracted parse output (images, MD, JSON)
└── <stem>.zip ← downloadable archive of result/
This directory structure is built by create_gradio_run_paths() and run_root.mkdir() .
The ZIP archive is created by compress_directory_to_zip(), which recursively walks the parse output directory. After extraction, the temporary result ZIP downloaded from the API server is deleted .
Allowed Paths and HTTP File Serving#
Gradio only serves files over HTTP from explicitly allowed directories. The build_gradio_allowed_paths() function:
- Reads a comma-separated
GRADIO_ALLOWED_PATHSenvironment variable. - Always appends the resolved
./outputdirectory (the default output root). - Passes the final list to
demo.launch(allowed_paths=...).
This is the prerequisite for image preview. After parsing, embedded image paths in the Markdown are rewritten from local filesystem paths to /gradio_api/file=<abs_path> HTTP URLs by replace_image_with_gradio_file_urls(). This rewriting only affects the in-memory preview string sent to gr.Markdown — the exported .md file retains its original relative paths .
Supported preview image extensions: .jpg, .jpeg, .png, .gif, .webp, .svg .
For Office file preview, the Gradio file is exposed at a public URL via build_gradio_file_public_url(), which respects x-forwarded-host and x-forwarded-proto headers for reverse-proxy environments, and is passed to Microsoft Office Online's embedding endpoint .
Async Streaming Pattern for Real-Time Status#
The conversion is driven by stream_to_markdown(), an async generator that runs the actual job as a background asyncio.Task and streams UI updates via yield .
Key design points:
- Job task:
_run_to_markdown_job()runs as anasyncio.Task; all its status messages are pushed through a thread-safeasyncio.Queuevialoop.call_soon_threadsafe(). - Event loop: The main
while True:loop usesasyncio.wait(..., return_when=FIRST_COMPLETED)across three competing tasks:queue_get_task,timer_task, and the mainjob_task. - Status panel state: A
StatusPanelStatedataclass accumulates log lines and tracks animated "processing" and "queue" states with elapsed timers. Updates are only yielded when state actually changes. - Concurrency limiting: Before dispatching, the request acquires a slot from
GradioRequestConcurrencyLimiter(backed byasyncio.Semaphore). While waiting, it emits a "Queued locally: N ahead" message . - Timer interval: Status ticks fire every
STATUS_TIMER_INTERVAL_SECONDS = 0.1sduring processing and everySTATUS_QUEUE_ANIMATION_INTERVAL_SECONDS = 1.0sduring queue animation .
The full pipeline of status messages emitted during a job:
Preparing request → Checking server → Submitting task → Queued on server / Processing on server → Downloading result → Preparing outputs → Completed .
Key CLI Options#
| Flag | Default | Purpose |
|---|---|---|
--api-url | None | Point to a remote mineru-api; skips local server |
--server-name / --server-port | None | Gradio listen address |
--enable-vlm-preload | False | Warm up VLM on Gradio start |
--max-convert-pages | 1000 | Cap on PDF pages converted |
--client-side-output-generation | False | Regenerate MD/JSON locally from server's middle JSON |
--latex-delimiters-type | all | LaTeX delimiters for Markdown rendering (a/b/all) |
--enable-http-client | False | Enable HTTP-client backend option (remote VLM/Hybrid) |
Gradio 5 / 6 Compatibility#
The file detects the installed Gradio major version at import time and selects the appropriate kwarg names for API visibility, CSS injection, and footer links . The IS_GRADIO_6 flag gates several blocks throughout the file.