document2md.mineru_server

Manages a persistent mineru-api process for batch conversions.

Manages a persistent mineru-api process for batch conversions.

Left to itself, the mineru CLI spins up (and tears down) a fresh temporary API server — reloading all layout/OCR models — on every single invocation. That’s fine for converting one document, but wasteful when a batch job converts many documents in the same run. MineruServer starts one mineru-api process, waits for it to report healthy, and points convert_to_markdown at it via the MINERU_API_URL environment variable for the duration of the batch.

_find_free_port() → int[source]

Return a currently-unused TCP port on 127.0.0.1, by binding to port 0 and reading back the OS-assigned port.

_wait_until_healthy(base_url: str, timeout: float = 300.0, interval: float = 1.0) → None[source]

Poll base_url’s /health endpoint every interval seconds until it answers 200, raising RuntimeError if it hasn’t within timeout seconds — mineru-api takes a while to come up because it loads its models on startup.

class MineruServer[source]

Context manager: starts a persistent mineru-api server on __enter__, exposes it to convert_to_markdown() via MINERU_API_URL, and stops it on __exit__ (restoring any previous MINERU_API_URL value).

__init__(host: str = '127.0.0.1', port: int | None = None)[source]

Create an unstarted server bound to host/port (a free port is picked when port is None); call start() (or use as a context manager) to actually launch it.

start() → None[source]

Launch mineru-api as a subprocess, block until it reports healthy (stopping it again on failure), and point MINERU_API_URL at it so converter.py picks it up.

stop() → None[source]

Restore MINERU_API_URL to whatever it was before start(), and terminate the mineru-api subprocess (killing it if it doesn’t exit within 15s).