models_download.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import json
  2. import os
  3. import sys
  4. import click
  5. import requests
  6. from mineru.utils.enum_class import ModelPath
  7. from mineru.utils.models_download_utils import auto_download_and_get_model_root_path
  8. def download_json(url):
  9. """下载JSON文件"""
  10. response = requests.get(url)
  11. response.raise_for_status()
  12. return response.json()
  13. def download_and_modify_json(url, local_filename, modifications):
  14. """下载JSON并修改内容"""
  15. if os.path.exists(local_filename):
  16. data = json.load(open(local_filename))
  17. config_version = data.get('config_version', '0.0.0')
  18. if config_version < '1.3.0':
  19. data = download_json(url)
  20. else:
  21. data = download_json(url)
  22. # 修改内容
  23. for key, value in modifications.items():
  24. if key in data:
  25. if isinstance(data[key], dict):
  26. # 如果是字典,合并新值
  27. data[key].update(value)
  28. else:
  29. # 否则直接替换
  30. data[key] = value
  31. # 保存修改后的内容
  32. with open(local_filename, 'w', encoding='utf-8') as f:
  33. json.dump(data, f, ensure_ascii=False, indent=4)
  34. def configure_model(model_dir, model_type):
  35. """配置模型"""
  36. json_url = 'https://gcore.jsdelivr.net/gh/opendatalab/MinerU@master/mineru.template.json'
  37. config_file_name = 'mineru.json'
  38. home_dir = os.path.expanduser('~')
  39. config_file = os.path.join(home_dir, config_file_name)
  40. json_mods = {
  41. 'models-dir': {
  42. f'{model_type}': model_dir
  43. }
  44. }
  45. download_and_modify_json(json_url, config_file, json_mods)
  46. print(f'The configuration file has been successfully configured, the path is: {config_file}')
  47. @click.command()
  48. @click.option(
  49. '-s',
  50. '--source',
  51. 'model_source',
  52. type=click.Choice(['huggingface', 'modelscope']),
  53. help="""
  54. The source of the model repository.
  55. """,
  56. default=None,
  57. )
  58. @click.option(
  59. '-m',
  60. '--model_type',
  61. 'model_type',
  62. type=click.Choice(['pipeline', 'vlm', 'all']),
  63. help="""
  64. The type of the model to download.
  65. """,
  66. default=None,
  67. )
  68. def download_models(model_source, model_type):
  69. """Download MinerU model files.
  70. Supports downloading pipeline or VLM models from ModelScope or HuggingFace.
  71. """
  72. # 如果未显式指定则交互式输入下载来源
  73. if model_source is None:
  74. model_source = click.prompt(
  75. "Please select the model download source: ",
  76. type=click.Choice(['huggingface', 'modelscope']),
  77. default='huggingface'
  78. )
  79. if os.getenv('MINERU_MODEL_SOURCE', None) is None:
  80. os.environ['MINERU_MODEL_SOURCE'] = model_source
  81. # 如果未显式指定则交互式输入模型类型
  82. if model_type is None:
  83. model_type = click.prompt(
  84. "Please select the model type to download: ",
  85. type=click.Choice(['pipeline', 'vlm', 'all']),
  86. default='all'
  87. )
  88. click.echo(f"Downloading {model_type} model from {os.getenv('MINERU_MODEL_SOURCE', None)}...")
  89. def download_pipeline_models():
  90. """下载Pipeline模型"""
  91. model_paths = [
  92. ModelPath.doclayout_yolo,
  93. ModelPath.yolo_v8_mfd,
  94. ModelPath.unimernet_small,
  95. ModelPath.pytorch_paddle,
  96. ModelPath.layout_reader,
  97. ModelPath.slanet_plus
  98. ]
  99. download_finish_path = ""
  100. for model_path in model_paths:
  101. click.echo(f"Downloading model: {model_path}")
  102. download_finish_path = auto_download_and_get_model_root_path(model_path, repo_mode='pipeline')
  103. click.echo(f"Pipeline models downloaded successfully to: {download_finish_path}")
  104. configure_model(download_finish_path, model_type)
  105. def download_vlm_models():
  106. """下载VLM模型"""
  107. download_finish_path = auto_download_and_get_model_root_path("/", repo_mode='vlm')
  108. click.echo(f"VLM models downloaded successfully to: {download_finish_path}")
  109. configure_model(download_finish_path, model_type)
  110. try:
  111. if model_type == 'pipeline':
  112. download_pipeline_models()
  113. elif model_type == 'vlm':
  114. download_vlm_models()
  115. elif model_type == 'all':
  116. download_pipeline_models()
  117. download_vlm_models()
  118. else:
  119. click.echo(f"Unsupported model type: {model_type}", err=True)
  120. sys.exit(1)
  121. except Exception as e:
  122. click.echo(f"Download failed: {str(e)}", err=True)
  123. sys.exit(1)
  124. if __name__ == '__main__':
  125. download_models()