download_models_hf.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import json
  2. import os
  3. import requests
  4. from huggingface_hub import snapshot_download
  5. def download_json(url):
  6. # 下载JSON文件
  7. response = requests.get(url)
  8. response.raise_for_status() # 检查请求是否成功
  9. return response.json()
  10. def download_and_modify_json(url, local_filename, modifications):
  11. if os.path.exists(local_filename):
  12. data = json.load(open(local_filename))
  13. config_version = data.get('config_version', '0.0.0')
  14. if config_version < '1.0.0':
  15. data = download_json(url)
  16. else:
  17. data = download_json(url)
  18. # 修改内容
  19. for key, value in modifications.items():
  20. data[key] = value
  21. # 保存修改后的内容
  22. with open(local_filename, 'w', encoding='utf-8') as f:
  23. json.dump(data, f, ensure_ascii=False, indent=4)
  24. if __name__ == '__main__':
  25. mineru_patterns = [
  26. "models/Layout/LayoutLMv3/*",
  27. "models/Layout/YOLO/*",
  28. "models/MFD/YOLO/*",
  29. "models/MFR/unimernet_small/*",
  30. "models/TabRec/TableMaster/*",
  31. "models/TabRec/StructEqTable/*",
  32. ]
  33. model_dir = snapshot_download('opendatalab/PDF-Extract-Kit-1.0', allow_patterns=mineru_patterns)
  34. layoutreader_pattern = [
  35. "*.json",
  36. "*.safetensors",
  37. ]
  38. layoutreader_model_dir = snapshot_download('hantian/layoutreader', allow_patterns=layoutreader_pattern)
  39. model_dir = model_dir + '/models'
  40. print(f'model_dir is: {model_dir}')
  41. print(f'layoutreader_model_dir is: {layoutreader_model_dir}')
  42. json_url = 'https://github.com/opendatalab/MinerU/raw/dev/magic-pdf.template.json'
  43. config_file_name = 'magic-pdf.json'
  44. home_dir = os.path.expanduser('~')
  45. config_file = os.path.join(home_dir, config_file_name)
  46. json_mods = {
  47. 'models-dir': model_dir,
  48. 'layoutreader-model-dir': layoutreader_model_dir,
  49. }
  50. download_and_modify_json(json_url, config_file, json_mods)
  51. print(f'The configuration file has been configured successfully, the path is: {config_file}')