models_download_utils.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import os
  2. from huggingface_hub import snapshot_download as hf_snapshot_download
  3. from modelscope import snapshot_download as ms_snapshot_download
  4. from mineru.utils.config_reader import get_local_models_dir
  5. from mineru.utils.enum_class import ModelPath
  6. def auto_download_and_get_model_root_path(relative_path: str, repo_mode='pipeline') -> str:
  7. """
  8. 支持文件或目录的可靠下载。
  9. - 如果输入文件: 返回本地文件绝对路径
  10. - 如果输入目录: 返回本地缓存下与 relative_path 同结构的相对路径字符串
  11. :param repo_mode: 指定仓库模式,'pipeline' 或 'vlm'
  12. :param relative_path: 文件或目录相对路径
  13. :return: 本地文件绝对路径或相对路径
  14. """
  15. model_source = os.getenv('MINERU_MODEL_SOURCE', "huggingface")
  16. if model_source == 'local':
  17. local_models_config = get_local_models_dir()
  18. if local_models_config is None:
  19. raise ValueError(
  20. "MINERU_MODEL_SOURCE=local but local models config is not set. "
  21. "Either set MINERU_MODEL_SOURCE to 'modelscope' or 'huggingface', "
  22. "or configure 'models-dir' (with 'pipeline' and 'vlm' keys) in MinerU config."
  23. )
  24. root_path = local_models_config.get(repo_mode, None)
  25. if not root_path:
  26. raise ValueError(f"Local path for repo_mode '{repo_mode}' is not configured.")
  27. return root_path
  28. # 建立仓库模式到路径的映射
  29. repo_mapping = {
  30. 'pipeline': {
  31. 'huggingface': ModelPath.pipeline_root_hf,
  32. 'modelscope': ModelPath.pipeline_root_modelscope,
  33. 'default': ModelPath.pipeline_root_hf
  34. },
  35. 'vlm': {
  36. 'huggingface': ModelPath.vlm_root_hf,
  37. 'modelscope': ModelPath.vlm_root_modelscope,
  38. 'default': ModelPath.vlm_root_hf
  39. }
  40. }
  41. if repo_mode not in repo_mapping:
  42. raise ValueError(f"Unsupported repo_mode: {repo_mode}, must be 'pipeline' or 'vlm'")
  43. # 如果没有指定model_source或值不是'modelscope',则使用默认值
  44. repo = repo_mapping[repo_mode].get(model_source, repo_mapping[repo_mode]['default'])
  45. if model_source == "huggingface":
  46. snapshot_download = hf_snapshot_download
  47. elif model_source == "modelscope":
  48. snapshot_download = ms_snapshot_download
  49. else:
  50. raise ValueError(f"未知的仓库类型: {model_source}")
  51. cache_dir = None
  52. if repo_mode == 'pipeline':
  53. relative_path = relative_path.strip('/')
  54. cache_dir = snapshot_download(repo, allow_patterns=[relative_path, relative_path+"/*"])
  55. elif repo_mode == 'vlm':
  56. # VLM 模式下,根据 relative_path 的不同处理方式
  57. if relative_path == "/":
  58. cache_dir = snapshot_download(repo)
  59. else:
  60. relative_path = relative_path.strip('/')
  61. cache_dir = snapshot_download(repo, allow_patterns=[relative_path, relative_path+"/*"])
  62. if not cache_dir:
  63. raise FileNotFoundError(f"Failed to download model: {relative_path} from {repo}")
  64. return cache_dir
  65. if __name__ == '__main__':
  66. path1 = "models/README.md"
  67. root = auto_download_and_get_model_root_path(path1)
  68. print("本地文件绝对路径:", os.path.join(root, path1))