models_download.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. json_url = 'https://gcore.jsdelivr.net/gh/myhloli/Magic-PDF@dev/mineru.template.json'
  38. config_file_name = 'mineru.json'
  39. home_dir = os.path.expanduser('~')
  40. config_file = os.path.join(home_dir, config_file_name)
  41. json_mods = {
  42. 'models-dir': {
  43. f'{model_type}': model_dir
  44. }
  45. }
  46. download_and_modify_json(json_url, config_file, json_mods)
  47. print(f'The configuration file has been successfully configured, the path is: {config_file}')
  48. @click.command()
  49. @click.option(
  50. '-s',
  51. '--source',
  52. 'model_source',
  53. type=click.Choice(['huggingface', 'modelscope']),
  54. help="""
  55. The source of the model repository.
  56. """,
  57. default=None,
  58. )
  59. @click.option(
  60. '-m',
  61. '--model_type',
  62. 'model_type',
  63. type=click.Choice(['pipeline', 'vlm', 'all']),
  64. help="""
  65. The type of the model to download.
  66. """,
  67. default=None,
  68. )
  69. def download_models(model_source, model_type):
  70. """Download MinerU model files.
  71. Supports downloading pipeline or VLM models from ModelScope or HuggingFace.
  72. """
  73. # 如果未显式指定则交互式输入下载来源
  74. if model_source is None:
  75. model_source = click.prompt(
  76. "Please select the model download source: ",
  77. type=click.Choice(['huggingface', 'modelscope']),
  78. default='huggingface'
  79. )
  80. if os.getenv('MINERU_MODEL_SOURCE', None) is None:
  81. os.environ['MINERU_MODEL_SOURCE'] = model_source
  82. # 如果未显式指定则交互式输入模型类型
  83. if model_type is None:
  84. model_type = click.prompt(
  85. "Please select the model type to download: ",
  86. type=click.Choice(['pipeline', 'vlm', 'all']),
  87. default='all'
  88. )
  89. click.echo(f"Downloading {model_type} model from {os.getenv('MINERU_MODEL_SOURCE', None)}...")
  90. def download_pipeline_models():
  91. """下载Pipeline模型"""
  92. model_paths = [
  93. ModelPath.doclayout_yolo,
  94. ModelPath.yolo_v8_mfd,
  95. ModelPath.unimernet_small,
  96. ModelPath.pytorch_paddle,
  97. ModelPath.layout_reader,
  98. ModelPath.slanet_plus
  99. ]
  100. download_finish_path = ""
  101. for model_path in model_paths:
  102. click.echo(f"Downloading model: {model_path}")
  103. download_finish_path = auto_download_and_get_model_root_path(model_path, repo_mode='pipeline')
  104. click.echo(f"Pipeline models downloaded successfully to: {download_finish_path}")
  105. configure_model(download_finish_path, model_type)
  106. def download_vlm_models():
  107. """下载VLM模型"""
  108. download_finish_path = auto_download_and_get_model_root_path("/", repo_mode='vlm')
  109. click.echo(f"VLM models downloaded successfully to: {download_finish_path}")
  110. configure_model(download_finish_path, model_type)
  111. try:
  112. if model_type == 'pipeline':
  113. download_pipeline_models()
  114. elif model_type == 'vlm':
  115. download_vlm_models()
  116. elif model_type == 'all':
  117. download_pipeline_models()
  118. download_vlm_models()
  119. else:
  120. click.echo(f"Unsupported model type: {model_type}", err=True)
  121. sys.exit(1)
  122. except Exception as e:
  123. click.echo(f"Download failed: {str(e)}", err=True)
  124. sys.exit(1)
  125. if __name__ == '__main__':
  126. download_models()