Skip to content

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

feat: update model source handling to support auto detection and improve configuration management

myhlolif1c87edaa2a0
diff --git a/docs/en/usage/model_source.md b/docs/en/usage/model_source.md
index d36c83e4..41b11250 100644
--- a/docs/en/usage/model_source.md
+++ b/docs/en/usage/model_source.md
@@ -2,13 +2,14 @@
MinerU uses `HuggingFace` and `ModelScope` as model repositories. Users can switch model sources or use local models as needed.
-- `HuggingFace` is the default model source, providing excellent loading speed and high stability globally.
+- `auto` is the default model source policy. It first requests `https://huggingface.co/models` to check whether Hugging Face is reachable. If reachable, MinerU uses `HuggingFace`; otherwise, it falls back to `ModelScope`.
+- `HuggingFace` provides excellent loading speed and high stability globally.
- `ModelScope` is the best choice for users in mainland China, providing seamlessly compatible `hf` SDK modules, suitable for users who cannot access HuggingFace.
## Methods to Switch Model Sources
### Configure via Environment Variables
-MinerU configures model sources through the `MINERU_MODEL_SOURCE` environment variable. This applies to all command line tools and API calls.
+MinerU configures model sources through the `MINERU_MODEL_SOURCE` environment variable. This applies to all command line tools and API calls. Supported values are `huggingface`, `modelscope`, and `local`. The environment variable has higher priority than `model-source` in `mineru.json`. Do not set this environment variable to `auto`; unset it if you want MinerU to choose a source automatically.
```bash
export MINERU_MODEL_SOURCE=modelscope
mineru -p <input_path> -o <output_path>
@@ -21,6 +22,14 @@ os.environ["MINERU_MODEL_SOURCE"] = "modelscope"
>[!TIP]
> MinerU no longer provides a CLI flag for model source selection. Model sources set through environment variables take effect in the current terminal session until the terminal is closed or the environment variable is modified.
+### Configure via Configuration File
+If `MINERU_MODEL_SOURCE` is not set, MinerU reads the `model-source` field from `mineru.json` in the user directory. `model-source` supports fixed values `huggingface` and `modelscope`, and also supports the template's first-run placeholder value `auto`. When the value is `auto` or the field is missing, MinerU probes the actual source first. After the first auto probe resolves an actual source, MinerU writes `model-source` back as `huggingface` or `modelscope` to avoid switching sources on later startups due to network fluctuations.
+```json
+{
+ "model-source": "auto"
+}
+```
+
## Using Local Models
### 1. Download Models to Local Storage
@@ -32,12 +41,12 @@ or download all built-in model bundles:
mineru-kit models download all
```
> [!NOTE]
->- After download completion, the model path will be output in the current terminal window and automatically written to `mineru.json` in the user directory.
->- You can also create it by copying the [configuration template file](https://github.com/opendatalab/MinerU/blob/master/mineru.template.json) to your user directory and renaming it to `mineru.json`.
+>- After download completion, the model path will be output in the current terminal window and automatically written to `mineru.json` in the user directory. The `model-source` field records the actual remote source used for this download, either `huggingface` or `modelscope`.
+>- You can also create it by copying the [configuration template file](https://github.com/opendatalab/MinerU/blob/master/mineru.template.json) to your user directory and renaming it to `mineru.json`. The template sets `model-source` to `auto`, so MinerU auto-detects once and writes back the resolved source on first use.
>- After downloading models locally, you can freely move the model folder to other locations while updating the model path in `mineru.json`.
>- If you deploy the model folder to another server, please ensure you move the `mineru.json` file to the user directory of the new device and configure the model path correctly.
>- If you need to update model files, you can run `mineru-kit models download all` again. Model updates do not support custom paths currently - if you haven't moved the local model folder, model files will be incrementally updated; if you have moved the model folder, model files will be re-downloaded to the default location and `mineru.json` will be updated.
->- `mineru-kit models download` must use a remote model source to perform a real download. If your current shell already sets `MINERU_MODEL_SOURCE=local`, this command will temporarily ignore that value for this invocation and use your selected `huggingface` or `modelscope` source instead.
+>- `mineru-kit models download` must use a remote model source to perform a real download. If your current shell already sets `MINERU_MODEL_SOURCE=local`, this command will temporarily ignore that value for this invocation and use your selected `auto`, `huggingface`, or `modelscope` source instead.
### 2. Use Local Models for Parsing
diff --git a/docs/next/cli/mineru-kit-models.md b/docs/next/cli/mineru-kit-models.md
index 3da69bdc..cf5c3721 100644
--- a/docs/next/cli/mineru-kit-models.md
+++ b/docs/next/cli/mineru-kit-models.md
@@ -46,12 +46,13 @@ mineru-kit models download <pipeline|vlm|all> [flags]
| Flag | 简写 | 类型 | 默认 | 说明 |
|------|------|------|------|------|
-| `--source` | `-s` | `huggingface \| modelscope` | `huggingface` | 下载源 |
+| `--source` | `-s` | `auto \| huggingface \| modelscope` | `auto` | 下载源 |
| `--verbose` | `-v` | bool | false | 输出详细日志 |
规则:
- `bundle` 使用位置参数,必须显式给出
+- `--source auto` 会先探测 Hugging Face 是否可访问,并在本次下载中使用解析出的真实远端源
- 不支持交互式 prompt
- 下载完成后默认更新配置文件
- 不支持 `--no-config`
@@ -70,6 +71,7 @@ mineru-kit models download <pipeline|vlm|all> [flags]
```bash
mineru-kit models download pipeline
mineru-kit models download vlm --source modelscope
+mineru-kit models download all --source auto
mineru-kit models download all
```
@@ -84,6 +86,7 @@ mineru-kit models show
建议输出内容:
- 当前实际使用的配置文件路径
+- `model-source`
- `models-dir.pipeline`
- `models-dir.vlm`
- 当前 `MINERU_MODEL_SOURCE` 环境值
diff --git a/docs/next/decisions/0019-mineru-kit-models-command.md b/docs/next/decisions/0019-mineru-kit-models-command.md
index 094ac9e3..85d28edf 100644
--- a/docs/next/decisions/0019-mineru-kit-models-command.md
+++ b/docs/next/decisions/0019-mineru-kit-models-command.md
@@ -59,13 +59,14 @@ mineru-kit models download <pipeline|vlm|all> [flags]
参数:
- `bundle` 使用位置参数表达,取值为 `pipeline`、`vlm`、`all`
-- `--source/-s`: `huggingface | modelscope`
+- `--source/-s`: `auto | huggingface | modelscope`
- `--verbose/-v`
规则:
- 不再支持交互式 prompt,必须显式给出 `bundle`
-- `--source` 默认值为 `huggingface`
+- `--source` 默认值为 `auto`
+- `--source auto` 会解析为本次下载实际使用的 `huggingface` 或 `modelscope`,并把实际来源写回 `mineru.json`
- `bundle` 没有默认值,必须显式给出
- 下载完成后默认更新 `mineru.json`
- 不支持 `--no-config`
@@ -81,6 +82,7 @@ mineru-kit models download <pipeline|vlm|all> [flags]
建议输出:
- 当前实际使用的配置文件路径
+- `model-source`
- `models-dir.pipeline`
- `models-dir.vlm`
- 当前 `MINERU_MODEL_SOURCE` 环境值
diff --git a/docs/zh/usage/model_source.md b/docs/zh/usage/model_source.md
index 9d472b60..02564285 100644
--- a/docs/zh/usage/model_source.md
+++ b/docs/zh/usage/model_source.md
@@ -2,13 +2,14 @@
MinerU使用 `HuggingFace` 和 `ModelScope` 作为模型仓库,用户可以根据需要切换模型源或使用本地模型。
-- `HuggingFace` 是默认的模型源,在全球范围内提供了优异的加载速度和极高稳定性。
+- `auto` 是默认的模型源策略,会先请求 `https://huggingface.co/models` 探测 HuggingFace 是否可访问;可访问时使用 `HuggingFace`,不可访问时自动回退到 `ModelScope`。
+- `HuggingFace` 在全球范围内提供了优异的加载速度和极高稳定性。
- `ModelScope` 是中国大陆地区用户的最佳选择,提供了无缝兼容的SDK模块,适用于无法访问`HuggingFace`的用户。
## 模型源的切换方法
### 通过环境变量切换
-MinerU 通过 `MINERU_MODEL_SOURCE` 环境变量配置模型源,这适用于所有命令行工具和 API 调用。
+MinerU 通过 `MINERU_MODEL_SOURCE` 环境变量配置模型源,这适用于所有命令行工具和 API 调用。支持的取值为 `huggingface`、`modelscope` 和 `local`,环境变量优先级高于 `mineru.json` 中的 `model-source`。请不要将环境变量设置为 `auto`;如需自动选择来源,请删除该环境变量。
```bash
export MINERU_MODEL_SOURCE=modelscope
mineru -p <input_path> -o <output_path>
@@ -21,6 +22,14 @@ os.environ["MINERU_MODEL_SOURCE"] = "modelscope"
>[!TIP]
> MinerU 已不再提供用于切换模型源的命令行参数。通过环境变量设置的模型源会在当前终端会话中生效,直到终端关闭或环境变量被修改。
+### 通过配置文件切换
+如果未设置 `MINERU_MODEL_SOURCE`,MinerU 会读取用户目录下 `mineru.json` 中的 `model-source` 字段。`model-source` 支持固定值 `huggingface`、`modelscope`,也支持模板中的首次解析占位值 `auto`。当值为 `auto` 或字段缺失时,会先自动探测实际来源;首次自动探测完成后,会将 `model-source` 写回为 `huggingface` 或 `modelscope`,避免后续启动时因网络波动反复切换来源。
+```json
+{
+ "model-source": "auto"
+}
+```
+
## 使用本地模型
@@ -33,12 +42,12 @@ mineru-kit models download --help
mineru-kit models download all
```
> [!NOTE]
->- 下载完成后,模型路径会在当前终端窗口输出,并自动写入用户目录下的 `mineru.json`。
->- 您也可以通过将[配置模板文件](https://github.com/opendatalab/MinerU/blob/master/mineru.template.json)复制到用户目录下并重命名为 `mineru.json` 来创建配置文件。
+>- 下载完成后,模型路径会在当前终端窗口输出,并自动写入用户目录下的 `mineru.json`。配置文件中的 `model-source` 会记录本次实际使用的远端来源,即 `huggingface` 或 `modelscope`。
+>- 您也可以通过将[配置模板文件](https://github.com/opendatalab/MinerU/blob/master/mineru.template.json)复制到用户目录下并重命名为 `mineru.json` 来创建配置文件;模板中的 `model-source` 默认为 `auto`,首次使用时会自动探测并写回实际来源。
>- 模型下载到本地后,您可以自由移动模型文件夹到其他位置,同时需要在 `mineru.json` 中更新模型路径。
>- 如您将模型文件夹部署到其他服务器上,请确保将 `mineru.json`文件一同移动到新设备的用户目录中并正确配置模型路径。
>- 如您需要更新模型文件,可以再次运行 `mineru-kit models download all` 命令,模型更新暂不支持自定义路径,如您没有移动本地模型文件夹,模型文件会增量更新;如您移动了模型文件夹,模型文件会重新下载到默认位置并更新`mineru.json`。
->- `mineru-kit models download` 必须使用远端模型源执行真实下载;如果当前终端已设置 `MINERU_MODEL_SOURCE=local`,该命令会仅在本次执行中临时忽略该值,并改用您选择的 `huggingface` 或 `modelscope` 下载模型。
+>- `mineru-kit models download` 必须使用远端模型源执行真实下载;如果当前终端已设置 `MINERU_MODEL_SOURCE=local`,该命令会仅在本次执行中临时忽略该值,并改用您选择的 `auto`、`huggingface` 或 `modelscope` 下载模型。
### 2. 使用本地模型进行解析
diff --git a/mineru.template.json b/mineru.template.json
index 7d979ec8..a933e799 100644
--- a/mineru.template.json
+++ b/mineru.template.json
@@ -26,5 +26,6 @@
"pipeline": "",
"vlm": ""
},
- "config_version": "1.3.1"
-}
\ No newline at end of file
+ "model-source": "auto",
+ "config_version": "1.3.2"
+}
diff --git a/mineru/utils/config_reader.py b/mineru/utils/config_reader.py
index 803c9d4f..9ff63eec 100644
--- a/mineru/utils/config_reader.py
+++ b/mineru/utils/config_reader.py
@@ -5,16 +5,20 @@ from typing import Any
from loguru import logger
-# 定义配置文件名常量
+# 定义配置文件名常量,保留给旧日志文案使用;实际读取路径通过函数动态解析环境变量。
CONFIG_FILE_NAME = os.getenv("MINERU_TOOLS_CONFIG_JSON", "mineru.json")
+def get_tools_config_file_path() -> str:
+ """获取 MinerU 工具配置文件路径,支持环境变量指定绝对或相对路径。"""
+ config_file_name = os.getenv("MINERU_TOOLS_CONFIG_JSON", "mineru.json")
+ if os.path.isabs(config_file_name):
+ return config_file_name
+ return os.path.join(os.path.expanduser("~"), config_file_name)
+
+
def read_config() -> dict[str, Any] | None:
- if os.path.isabs(CONFIG_FILE_NAME):
- config_file = CONFIG_FILE_NAME
- else:
- home_dir = os.path.expanduser("~")
- config_file = os.path.join(home_dir, CONFIG_FILE_NAME)
+ config_file = get_tools_config_file_path()
if not os.path.exists(config_file):
# logger.warning(f'{config_file} not found, using default configuration')
@@ -25,6 +29,34 @@ def read_config() -> dict[str, Any] | None:
return config
+def get_configured_model_source(default: str | None = None) -> str | None:
+ """读取配置文件中的固定模型来源配置,auto 或缺失时返回默认值。"""
+ supported_sources = {"huggingface", "modelscope"}
+ config = read_config()
+ if config is None:
+ return default
+
+ model_source = config.get("model-source")
+ if model_source is None:
+ return default
+ if not isinstance(model_source, str):
+ logger.warning(f"'model-source' in {get_tools_config_file_path()} must be a string, use {default} as default")
+ return default
+
+ normalized_model_source = model_source.strip().lower()
+ if not normalized_model_source:
+ return default
+ if normalized_model_source == "auto":
+ return default
+ if normalized_model_source in supported_sources:
+ return normalized_model_source
+
+ logger.warning(
+ f"Unsupported 'model-source' in {get_tools_config_file_path()}: {model_source}, use {default} as default"
+ )
+ return default
+
+
def get_s3_config(bucket_name: str) -> tuple[str, str, str]:
"""~/magic-pdf.json 读出来."""
config = read_config()
diff --git a/mineru/utils/models_download_utils.py b/mineru/utils/models_download_utils.py
index 417808db..a5bca9cf 100644
--- a/mineru/utils/models_download_utils.py
+++ b/mineru/utils/models_download_utils.py
@@ -1,12 +1,181 @@
# Copyright (c) Opendatalab. All rights reserved.
+import json
import os
+from functools import lru_cache
from huggingface_hub import snapshot_download as hf_snapshot_download
+from loguru import logger
from modelscope import snapshot_download as ms_snapshot_download
+import requests
-from .config_reader import get_local_models_dir
+from .config_reader import get_configured_model_source, get_local_models_dir, get_tools_config_file_path
from .enum_class import ModelPath
+MODEL_SOURCE_ENV_VAR = "MINERU_MODEL_SOURCE"
+CONFIG_TEMPLATE_URL = "https://gcore.jsdelivr.net/gh/opendatalab/MinerU@master/mineru.template.json"
+MINERU_CONFIG_VERSION = "1.3.2"
+HUGGINGFACE_MODELS_PAGE_URL = "https://huggingface.co/models"
+HUGGINGFACE_MODELS_PAGE_TIMEOUT = 3
+HUGGINGFACE_MODELS_PAGE_MAX_ATTEMPTS = 2
+REMOTE_MODEL_SOURCES = ("huggingface", "modelscope")
+
+
+def download_json(url: str) -> dict:
+ """下载 JSON 文件并返回解析后的内容。"""
+ response = requests.get(url)
+ response.raise_for_status()
+ return response.json()
+
+
+def is_config_version_outdated(config_version: object) -> bool:
+ """判断本地配置版本是否低于当前模板版本。"""
+
+ def version_tuple(version: object) -> tuple[int, ...]:
+ """将版本号字符串转换为可比较的整数元组。"""
+ parts = []
+ for part in str(version).split("."):
+ parts.append(int(part) if part.isdigit() else 0)
+ return tuple(parts)
+
+ current_version = version_tuple(config_version)
+ target_version = version_tuple(MINERU_CONFIG_VERSION)
+ max_len = max(len(current_version), len(target_version))
+ current_version += (0,) * (max_len - len(current_version))
+ target_version += (0,) * (max_len - len(target_version))
+ return current_version < target_version
+
+
+def merge_config_dict(base_config: dict, override_config: dict, skip_keys: set[str] | None = None) -> dict:
+ """递归合并配置字典,用 override_config 覆盖 base_config 并保留新模板字段。"""
+ skip_keys = skip_keys or set()
+ merged_config = dict(base_config)
+ for key, value in override_config.items():
+ if key in skip_keys:
+ continue
+ base_value = merged_config.get(key)
+ if isinstance(base_value, dict) and isinstance(value, dict):
+ merged_config[key] = merge_config_dict(base_value, value, skip_keys=skip_keys)
+ else:
+ merged_config[key] = value
+ return merged_config
+
+
+def download_and_modify_json(url: str, local_filename: str, modifications: dict) -> None:
+ """下载或读取 JSON 配置,并按 modifications 合并更新后写回。"""
+ if os.path.exists(local_filename):
+ with open(local_filename, encoding="utf-8") as f:
+ data = json.load(f)
+ config_version = data.get("config_version", "0.0.0")
+ if is_config_version_outdated(config_version):
+ template_data = download_json(url)
+ data = merge_config_dict(template_data, data, skip_keys={"config_version"})
+ else:
+ data = download_json(url)
+
+ data = merge_config_dict(data, modifications)
+
+ os.makedirs(os.path.dirname(local_filename) or ".", exist_ok=True)
+ with open(local_filename, "w", encoding="utf-8") as f:
+ json.dump(data, f, ensure_ascii=False, indent=4)
+
+
+def persist_resolved_model_source(model_source: str) -> None:
+ """将 auto 解析出的实际模型来源写入配置文件,避免后续启动受网络波动影响。"""
+ if model_source not in REMOTE_MODEL_SOURCES:
+ return
+ try:
+ download_and_modify_json(
+ CONFIG_TEMPLATE_URL,
+ get_tools_config_file_path(),
+ {"model-source": model_source},
+ )
+ except Exception as exc:
+ logger.warning(f"Failed to persist resolved model source '{model_source}': {exc}")
+
+
+@lru_cache(maxsize=1)
+def resolve_auto_model_source() -> str:
+ """通过 Hugging Face 模型列表页探测 auto 应该使用的实际模型来源。"""
+ last_error = None
+ for _ in range(HUGGINGFACE_MODELS_PAGE_MAX_ATTEMPTS):
+ try:
+ response = requests.get(
+ HUGGINGFACE_MODELS_PAGE_URL,
+ timeout=HUGGINGFACE_MODELS_PAGE_TIMEOUT,
+ )
+ if 200 <= response.status_code < 400:
+ return "huggingface"
+ last_error = f"status_code={response.status_code}"
+ except Exception as exc:
+ last_error = str(exc)
+
+ logger.warning(f"Failed to access {HUGGINGFACE_MODELS_PAGE_URL}: {last_error}, fallback to modelscope.")
+ return "modelscope"
+
+
+def resolve_model_source(model_source: str | None = None, allow_auto: bool = False) -> str:
+ """将环境变量或配置文件中的模型来源解析为实际可下载的来源。"""
+ if model_source is None:
+ model_source = os.getenv(MODEL_SOURCE_ENV_VAR)
+ if isinstance(model_source, str) and model_source.strip().lower() == "auto":
+ raise ValueError(
+ f"{MODEL_SOURCE_ENV_VAR}=auto is not supported. "
+ f"Unset {MODEL_SOURCE_ENV_VAR} to use auto detection once, "
+ "or set it to huggingface/modelscope/local."
+ )
+ if model_source is None:
+ model_source = get_configured_model_source()
+ if model_source is None:
+ model_source = "auto"
+ allow_auto = True
+
+ if not isinstance(model_source, str):
+ logger.warning(f"Unsupported model source type: {type(model_source)}, fallback to auto.")
+ model_source = "auto"
+ allow_auto = True
+
+ normalized_model_source = model_source.strip().lower()
+ if normalized_model_source == "local":
+ return "local"
+ if normalized_model_source == "auto":
+ if not allow_auto:
+ raise ValueError(
+ "model source auto is only supported for internal default detection "
+ "or explicit download command selection."
+ )
+ resolved_model_source = resolve_auto_model_source()
+ persist_resolved_model_source(resolved_model_source)
+ return resolved_model_source
+ if normalized_model_source in REMOTE_MODEL_SOURCES:
+ return normalized_model_source
+
+ logger.warning(f"Unsupported model source: {model_source}, fallback to auto.")
+ resolved_model_source = resolve_auto_model_source()
+ persist_resolved_model_source(resolved_model_source)
+ return resolved_model_source
+
+
+@lru_cache(maxsize=None)
+def _snapshot_download_cached(model_source: str, repo_mode: str, repo: str, relative_path: str) -> str:
+ """按进程缓存远端 snapshot_download 结果,减少重复缓存检查和 Fetching 日志。"""
+ if model_source == "huggingface":
+ snapshot_download = hf_snapshot_download
+ elif model_source == "modelscope":
+ snapshot_download = ms_snapshot_download
+ else:
+ raise ValueError(f"未知的仓库类型: {model_source}")
+
+ if repo_mode == "pipeline":
+ return snapshot_download(repo, allow_patterns=[relative_path, relative_path + "/*"])
+
+ if repo_mode == "vlm":
+ # VLM 整仓下载和局部路径下载都参与缓存,但保持原有 allow_patterns 行为。
+ if relative_path == "/":
+ return snapshot_download(repo)
+ return snapshot_download(repo, allow_patterns=[relative_path, relative_path + "/*"])
+
+ raise ValueError(f"Unsupported repo_mode: {repo_mode}, must be 'pipeline' or 'vlm'")
+
def auto_download_and_get_model_root_path(relative_path: str, repo_mode: str = "pipeline") -> str:
"""
@@ -17,10 +186,12 @@ def auto_download_and_get_model_root_path(relative_path: str, repo_mode: str = "
:param relative_path: 文件或目录相对路径
:return: 本地文件绝对路径或相对路径
"""
- model_source = os.getenv("MINERU_MODEL_SOURCE", "huggingface")
+ model_source = resolve_model_source()
if model_source == "local":
local_models_config = get_local_models_dir()
+ if local_models_config is None:
+ raise ValueError("Local model paths are not configured.")
root_path = local_models_config.get(repo_mode, None)
if not root_path:
raise ValueError(f"Local path for repo_mode '{repo_mode}' is not configured.")
@@ -31,40 +202,27 @@ def auto_download_and_get_model_root_path(relative_path: str, repo_mode: str = "
"pipeline": {
"huggingface": ModelPath.pipeline_root_hf,
"modelscope": ModelPath.pipeline_root_modelscope,
- "default": ModelPath.pipeline_root_hf,
},
"vlm": {
"huggingface": ModelPath.vlm_root_hf,
"modelscope": ModelPath.vlm_root_modelscope,
- "default": ModelPath.vlm_root_hf,
},
}
if repo_mode not in repo_mapping:
raise ValueError(f"Unsupported repo_mode: {repo_mode}, must be 'pipeline' or 'vlm'")
- # 如果没有指定model_source或值不是'modelscope',则使用默认值
- repo = repo_mapping[repo_mode].get(model_source, repo_mapping[repo_mode]["default"])
-
- if model_source == "huggingface":
- snapshot_download = hf_snapshot_download
- elif model_source == "modelscope":
- snapshot_download = ms_snapshot_download
- else:
- raise ValueError(f"未知的仓库类型: {model_source}")
-
- cache_dir = None
+ # model_source 已解析为实际远端来源后,再选择对应仓库。
+ repo = repo_mapping[repo_mode][model_source]
if repo_mode == "pipeline":
relative_path = relative_path.strip("/")
- cache_dir = snapshot_download(repo, allow_patterns=[relative_path, relative_path + "/*"])
elif repo_mode == "vlm":
# VLM 模式下,根据 relative_path 的不同处理方式
- if relative_path == "/":
- cache_dir = snapshot_download(repo)
- else:
+ if relative_path != "/":
relative_path = relative_path.strip("/")
- cache_dir = snapshot_download(repo, allow_patterns=[relative_path, relative_path + "/*"])
+
+ cache_dir = _snapshot_download_cached(model_source, repo_mode, repo, relative_path)
if not cache_dir:
raise FileNotFoundError(f"Failed to download model: {relative_path} from {repo}")
diff --git a/tests/unittest/test_kit_commands.py b/tests/unittest/test_kit_commands.py
index 50a1e4c3..f36f01b3 100644
--- a/tests/unittest/test_kit_commands.py
+++ b/tests/unittest/test_kit_commands.py
@@ -171,6 +171,36 @@ def test_models_download_pipeline(monkeypatch: Any) -> None:
assert "Downloaded pipeline models" in result.output
+def test_models_download_auto_source_resolves_before_download(monkeypatch: Any) -> None:
+ captured: dict[str, str] = {}
+
+ def fake_resolve_model_source(model_source: str | None = None, allow_auto: bool = False) -> str:
+ assert model_source == "auto"
+ assert allow_auto is True
+ return "modelscope"
+
+ def fake_update_models_dir(bundle: str, model_dir: str) -> Path:
+ captured["bundle"] = bundle
+ captured["model_dir"] = model_dir
+ captured["effective_source"] = models.os.getenv(models.MODEL_SOURCE_ENV_VAR, "")
+ return Path(f"/tmp/{bundle}.json")
+
+ monkeypatch.delenv(models.MODEL_SOURCE_ENV_VAR, raising=False)
+ monkeypatch.setattr(models, "resolve_model_source", fake_resolve_model_source, raising=False)
+ monkeypatch.setattr(models, "_download_pipeline_models", lambda: "/tmp/pipeline")
+ monkeypatch.setattr(models, "_update_models_dir", fake_update_models_dir)
+
+ result = runner.invoke(app, ["models", "download", "pipeline", "--source", "auto"])
+
+ assert result.exit_code == 0
+ assert captured == {
+ "bundle": "pipeline",
+ "model_dir": "/tmp/pipeline",
+ "effective_source": "modelscope",
+ }
+ assert "Downloaded pipeline models from modelscope" in result.output
+
+
def test_models_show_and_verify(tmp_path: Path, monkeypatch: Any) -> None:
config = tmp_path / "mineru.json"
pipeline_root = tmp_path / "pipeline"