download_from_hub.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import os
  2. import json
  3. from omegaconf import OmegaConf
  4. from funasr.download.name_maps_from_hub import name_maps_ms, name_maps_hf
  5. def download_model(**kwargs):
  6. model_hub = kwargs.get("model_hub", "ms")
  7. if model_hub == "ms":
  8. kwargs = download_from_ms(**kwargs)
  9. return kwargs
  10. def download_from_ms(**kwargs):
  11. model_or_path = kwargs.get("model")
  12. if model_or_path in name_maps_ms:
  13. model_or_path = name_maps_ms[model_or_path]
  14. model_revision = kwargs.get("model_revision")
  15. if not os.path.exists(model_or_path):
  16. model_or_path = get_or_download_model_dir(model_or_path, model_revision, is_training=kwargs.get("is_training"), check_latest=kwargs.get("kwargs", True))
  17. kwargs["model_path"] = model_or_path
  18. if os.path.exists(os.path.join(model_or_path, "configuration.json")):
  19. with open(os.path.join(model_or_path, "configuration.json"), 'r', encoding='utf-8') as f:
  20. conf_json = json.load(f)
  21. cfg = {}
  22. add_file_root_path(model_or_path, conf_json["file_path_metas"], cfg)
  23. cfg.update(kwargs)
  24. config = OmegaConf.load(cfg["config"])
  25. kwargs = OmegaConf.merge(config, cfg)
  26. kwargs["model"] = config["model"]
  27. elif os.path.exists(os.path.join(model_or_path, "config.yaml")) and os.path.exists(os.path.join(model_or_path, "model.pt")):
  28. config = OmegaConf.load(os.path.join(model_or_path, "config.yaml"))
  29. kwargs = OmegaConf.merge(config, kwargs)
  30. init_param = os.path.join(model_or_path, "model.pb")
  31. kwargs["init_param"] = init_param
  32. if os.path.exists(os.path.join(model_or_path, "tokens.txt")):
  33. kwargs["tokenizer_conf"]["token_list"] = os.path.join(model_or_path, "tokens.txt")
  34. if os.path.exists(os.path.join(model_or_path, "tokens.json")):
  35. kwargs["tokenizer_conf"]["token_list"] = os.path.join(model_or_path, "tokens.json")
  36. if os.path.exists(os.path.join(model_or_path, "seg_dict")):
  37. kwargs["tokenizer_conf"]["seg_dict"] = os.path.join(model_or_path, "seg_dict")
  38. if os.path.exists(os.path.join(model_or_path, "bpe.model")):
  39. kwargs["tokenizer_conf"]["bpemodel"] = os.path.join(model_or_path, "bpe.model")
  40. kwargs["model"] = config["model"]
  41. if os.path.exists(os.path.join(model_or_path, "am.mvn")):
  42. kwargs["frontend_conf"]["cmvn_file"] = os.path.join(model_or_path, "am.mvn")
  43. if os.path.exists(os.path.join(model_or_path, "jieba_usr_dict")):
  44. kwargs["jieba_usr_dict"] = os.path.join(model_or_path, "jieba_usr_dict")
  45. return OmegaConf.to_container(kwargs, resolve=True)
  46. def add_file_root_path(model_or_path: str, file_path_metas: dict, cfg = {}):
  47. if isinstance(file_path_metas, dict):
  48. for k, v in file_path_metas.items():
  49. if isinstance(v, str):
  50. p = os.path.join(model_or_path, v)
  51. if os.path.exists(p):
  52. cfg[k] = p
  53. elif isinstance(v, dict):
  54. if k not in cfg:
  55. cfg[k] = {}
  56. add_file_root_path(model_or_path, v, cfg[k])
  57. return cfg
  58. def get_or_download_model_dir(
  59. model,
  60. model_revision=None,
  61. is_training=False,
  62. check_latest=True,
  63. ):
  64. """ Get local model directory or download model if necessary.
  65. Args:
  66. model (str): model id or path to local model directory.
  67. model_revision (str, optional): model version number.
  68. :param is_training:
  69. """
  70. from modelscope.hub.check_model import check_local_model_is_latest
  71. from modelscope.hub.snapshot_download import snapshot_download
  72. from modelscope.utils.constant import Invoke, ThirdParty
  73. key = Invoke.LOCAL_TRAINER if is_training else Invoke.PIPELINE
  74. if os.path.exists(model) and check_latest:
  75. model_cache_dir = model if os.path.isdir(
  76. model) else os.path.dirname(model)
  77. try:
  78. check_local_model_is_latest(
  79. model_cache_dir,
  80. user_agent={
  81. Invoke.KEY: key,
  82. ThirdParty.KEY: "funasr"
  83. })
  84. except:
  85. print("could not check the latest version")
  86. else:
  87. model_cache_dir = snapshot_download(
  88. model,
  89. revision=model_revision,
  90. user_agent={
  91. Invoke.KEY: key,
  92. ThirdParty.KEY: "funasr"
  93. })
  94. return model_cache_dir