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(vlm-server): honor offline mode before resolving the default model
myhlolief2ab0fbf13b
diff --git a/mineru/kit/vlm_server/llama_cpp_server.py b/mineru/kit/vlm_server/llama_cpp_server.pyindex 060a5ded..88e4ed76 100644--- a/mineru/kit/vlm_server/llama_cpp_server.py+++ b/mineru/kit/vlm_server/llama_cpp_server.py@@ -231,7 +231,16 @@ def main() -> None:)elif not user_model_specified:repo = vlm_model_repo("llama-cpp")- model_dir = repo.ensure()+ # --offline(或 LLAMA_ARG_OFFLINE)明确禁止网络访问:默认模型只做本地+ # 校验——已缓存则复用,未缓存则报错退出,不在 wrapper 里先碰远端。+ offline = _has_arg(args, "--offline") or _has_env("LLAMA_ARG_OFFLINE")+ try:+ model_dir = repo.ensure(source="local") if offline else repo.ensure()+ except FileNotFoundError as exc:+ raise SystemExit(+ f"--offline 模式禁止网络访问,且默认模型未完整缓存:{exc}\n"+ "请先联网完成一次默认模型下载,或显式指定本地模型(-m <路径>)。"+ ) from excmodel_path = model_dir / repo.paths["main"]args.extend(["-m", str(model_path)])if not _has_arg(args, "--no-mmproj"):diff --git a/tests/unittest/test_llama_cpp_server.py b/tests/unittest/test_llama_cpp_server.pyindex ebc594e5..6d09244a 100644--- a/tests/unittest/test_llama_cpp_server.py+++ b/tests/unittest/test_llama_cpp_server.py@@ -80,7 +80,12 @@ def test_gguf_context_length_returns_zero_without_matching_architecture(tmp_pathdef _run_main(- monkeypatch: pytest.MonkeyPatch, tmp_path: Path, capsys: pytest.CaptureFixture[str], user_args: list[str]+ monkeypatch: pytest.MonkeyPatch,+ tmp_path: Path,+ capsys: pytest.CaptureFixture[str],+ user_args: list[str],+ *,+ repo: SimpleNamespace | None = None,) -> tuple[list[str], str, Path]:"""执行 main() 并捕获转交 llama-server 的 argv 与横幅输出,不真正启动进程。"""model_dir = tmp_path / "repo"@@ -94,13 +99,16 @@ def _run_main(binary.write_bytes(b"")executed: list[list[str]] = []monkeypatch.setattr(llama_cpp_server, "llama_server_binary", lambda: binary)- monkeypatch.setattr(- llama_cpp_server,- "vlm_model_repo",- lambda name: SimpleNamespace(- name="mineru-test-model", ensure=lambda: model_dir, paths={"main": gguf.name, "mmproj": "mmproj.gguf"}- ),+ fake_repo = (+ repo+ if repo is not None+ else SimpleNamespace(+ name="mineru-test-model",+ ensure=lambda source=None: model_dir,+ paths={"main": gguf.name, "mmproj": "mmproj.gguf"},+ ))+ monkeypatch.setattr(llama_cpp_server, "vlm_model_repo", lambda name: fake_repo)monkeypatch.setattr("os.execv", lambda path, argv: executed.append(argv))monkeypatch.setattr(sys, "argv", ["llama_cpp_server", *user_args])@@ -362,6 +370,51 @@ def test_main_env_model_source_skips_partitioned_parallel(assert "--ctx-size" not in argv+ ("offline_args", "offline_env"),+ [(["--offline"], ""), ([], "1")],+ ids=["flag", "env"],+)+def test_main_offline_uses_local_verification_only(+ monkeypatch: pytest.MonkeyPatch,+ tmp_path: Path,+ capsys: pytest.CaptureFixture[str],+ offline_args: list[str],+ offline_env: str,+) -> None:+ """--offline 禁止网络访问:默认模型只做本地校验(ensure(source="local")),不走远端下载。"""+ ensure_calls: list[dict[str, object]] = []++ def _ensure(**kwargs: object) -> Path:+ ensure_calls.append(kwargs)+ return tmp_path / "repo"++ repo = SimpleNamespace(name="mineru-test-model", ensure=_ensure, paths={"main": "main.gguf", "mmproj": "mmproj.gguf"})+ if offline_env:+ monkeypatch.setenv("LLAMA_ARG_OFFLINE", offline_env)++ argv, _, _ = _run_main(monkeypatch, tmp_path, capsys, offline_args, repo=repo)++ assert ensure_calls == [{"source": "local"}]+ assert argv[argv.index("-m") + 1] == str(tmp_path / "repo" / "main.gguf")+++def test_main_offline_without_cached_model_fails_fast(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path, capsys: pytest.CaptureFixture[str]+) -> None:+ """--offline 且默认模型未缓存:直接报错并给出修法,不碰网络。"""++ def _ensure(**kwargs: object) -> Path:+ if kwargs.get("source") == "local":+ raise FileNotFoundError("Model repo mineru-test-model is not ready; missing: main.gguf")+ return tmp_path / "repo"++ repo = SimpleNamespace(name="mineru-test-model", ensure=_ensure, paths={"main": "main.gguf", "mmproj": "mmproj.gguf"})++ with pytest.raises(SystemExit, match="--offline"):+ _run_main(monkeypatch, tmp_path, capsys, ["--offline"], repo=repo)++def test_main_env_parallel_suppresses_default_and_ctx_conversion(monkeypatch: pytest.MonkeyPatch, tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: