download_models_hf.py 2.3 KB

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