download_models.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import json
  2. import shutil
  3. import os
  4. import requests
  5. from modelscope 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_model_dir = snapshot_download('ppaanngggg/layoutreader')
  37. model_dir = model_dir + '/models'
  38. print(f'model_dir is: {model_dir}')
  39. print(f'layoutreader_model_dir is: {layoutreader_model_dir}')
  40. # paddleocr_model_dir = model_dir + '/OCR/paddleocr'
  41. # user_paddleocr_dir = os.path.expanduser('~/.paddleocr')
  42. # if os.path.exists(user_paddleocr_dir):
  43. # shutil.rmtree(user_paddleocr_dir)
  44. # shutil.copytree(paddleocr_model_dir, user_paddleocr_dir)
  45. json_url = 'https://gcore.jsdelivr.net/gh/opendatalab/MinerU@master/magic-pdf.template.json'
  46. config_file_name = 'magic-pdf.json'
  47. home_dir = os.path.expanduser('~')
  48. config_file = os.path.join(home_dir, config_file_name)
  49. json_mods = {
  50. 'models-dir': model_dir,
  51. 'layoutreader-model-dir': layoutreader_model_dir,
  52. }
  53. download_and_modify_json(json_url, config_file, json_mods)
  54. print(f'The configuration file has been configured successfully, the path is: {config_file}')