Skip to content

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

feat: enhance model download functionality with temporary source handling

myhloli6cdd35c27afc
diff --git a/docs/en/usage/model_source.md b/docs/en/usage/model_source.md
index a6a840cf..6ba4ddff 100644
--- a/docs/en/usage/model_source.md
+++ b/docs/en/usage/model_source.md
@@ -42,6 +42,7 @@ mineru-models-download
>- 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 the `mineru-models-download` command 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-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.
### 2. Use Local Models for Parsing
diff --git a/docs/zh/usage/model_source.md b/docs/zh/usage/model_source.md
index 5570ada2..520b51b4 100644
--- a/docs/zh/usage/model_source.md
+++ b/docs/zh/usage/model_source.md
@@ -43,6 +43,7 @@ mineru-models-download
>- 模型下载到本地后,您可以自由移动模型文件夹到其他位置,同时需要在 `mineru.json` 中更新模型路径。
>- 如您将模型文件夹部署到其他服务器上,请确保将 `mineru.json`文件一同移动到新设备的用户目录中并正确配置模型路径。
>- 如您需要更新模型文件,可以再次运行 `mineru-models-download` 命令,模型更新暂不支持自定义路径,如您没有移动本地模型文件夹,模型文件会增量更新;如您移动了模型文件夹,模型文件会重新下载到默认位置并更新`mineru.json`。
+>- `mineru-models-download` 必须使用远端模型源执行真实下载;如果当前终端已设置 `MINERU_MODEL_SOURCE=local`,该命令会仅在本次执行中临时忽略该值,并改用您选择的 `huggingface` 或 `modelscope` 下载模型。
### 2. 使用本地模型进行解析
@@ -53,4 +54,4 @@ mineru -p <input_path> -o <output_path> --source local
```bash
export MINERU_MODEL_SOURCE=local
mineru -p <input_path> -o <output_path>
-```
\ No newline at end of file
+```
diff --git a/mineru/cli/models_download.py b/mineru/cli/models_download.py
index ea834962..0c84f3c5 100644
--- a/mineru/cli/models_download.py
+++ b/mineru/cli/models_download.py
@@ -1,3 +1,4 @@
+from contextlib import contextmanager
import json
import os
import sys
@@ -8,6 +9,9 @@ from loguru import logger
from mineru.utils.enum_class import ModelPath
from mineru.utils.models_download_utils import auto_download_and_get_model_root_path
+MODEL_SOURCE_ENV_VAR = 'MINERU_MODEL_SOURCE'
+REMOTE_MODEL_SOURCES = ('huggingface', 'modelscope')
+
def download_json(url):
"""下载JSON文件"""
@@ -85,12 +89,43 @@ def download_vlm_models():
configure_model(download_finish_path, "vlm")
+def get_effective_download_model_source(requested_model_source):
+ """获取本次下载命令实际使用的模型源。"""
+ current_model_source = os.getenv(MODEL_SOURCE_ENV_VAR)
+ if current_model_source == 'local':
+ logger.warning(
+ f"{MODEL_SOURCE_ENV_VAR}=local means using pre-downloaded local models. "
+ f"`mineru-models-download` will temporarily use '{requested_model_source}' "
+ f"to perform a real download."
+ )
+ return requested_model_source
+
+ if current_model_source is None:
+ return requested_model_source
+
+ return current_model_source
+
+
+@contextmanager
+def temporary_model_source(model_source):
+ """在命令执行期间临时设置模型源,并在结束后恢复。"""
+ original_model_source = os.getenv(MODEL_SOURCE_ENV_VAR)
+ os.environ[MODEL_SOURCE_ENV_VAR] = model_source
+ try:
+ yield
+ finally:
+ if original_model_source is None:
+ os.environ.pop(MODEL_SOURCE_ENV_VAR, None)
+ else:
+ os.environ[MODEL_SOURCE_ENV_VAR] = original_model_source
+
+
@click.command()
@click.option(
'-s',
'--source',
'model_source',
- type=click.Choice(['huggingface', 'modelscope']),
+ type=click.Choice(REMOTE_MODEL_SOURCES),
help="""
The source of the model repository.
""",
@@ -115,12 +150,11 @@ def download_models(model_source, model_type):
if model_source is None:
model_source = click.prompt(
"Please select the model download source: ",
- type=click.Choice(['huggingface', 'modelscope']),
+ type=click.Choice(REMOTE_MODEL_SOURCES),
default='huggingface'
)
- if os.getenv('MINERU_MODEL_SOURCE', None) is None:
- os.environ['MINERU_MODEL_SOURCE'] = model_source
+ effective_model_source = get_effective_download_model_source(model_source)
# 如果未显式指定则交互式输入模型类型
if model_type is None:
@@ -130,19 +164,20 @@ def download_models(model_source, model_type):
default='all'
)
- logger.info(f"Downloading {model_type} model from {os.getenv('MINERU_MODEL_SOURCE', None)}...")
+ logger.info(f"Downloading {model_type} model from {effective_model_source}...")
try:
- if model_type == 'pipeline':
- download_pipeline_models()
- elif model_type == 'vlm':
- download_vlm_models()
- elif model_type == 'all':
- download_pipeline_models()
- download_vlm_models()
- else:
- click.echo(f"Unsupported model type: {model_type}", err=True)
- sys.exit(1)
+ with temporary_model_source(effective_model_source):
+ if model_type == 'pipeline':
+ download_pipeline_models()
+ elif model_type == 'vlm':
+ download_vlm_models()
+ elif model_type == 'all':
+ download_pipeline_models()
+ download_vlm_models()
+ else:
+ click.echo(f"Unsupported model type: {model_type}", err=True)
+ sys.exit(1)
except Exception as e:
logger.exception(f"An error occurred while downloading models: {str(e)}")