config_reader.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (c) Opendatalab. All rights reserved.
  2. import json
  3. import os
  4. import torch
  5. from loguru import logger
  6. # 定义配置文件名常量
  7. CONFIG_FILE_NAME = os.getenv('MINERU_TOOLS_CONFIG_JSON', 'mineru.json')
  8. def read_config():
  9. if os.path.isabs(CONFIG_FILE_NAME):
  10. config_file = CONFIG_FILE_NAME
  11. else:
  12. home_dir = os.path.expanduser('~')
  13. config_file = os.path.join(home_dir, CONFIG_FILE_NAME)
  14. if not os.path.exists(config_file):
  15. # logger.warning(f'{config_file} not found, using default configuration')
  16. return None
  17. else:
  18. with open(config_file, 'r', encoding='utf-8') as f:
  19. config = json.load(f)
  20. return config
  21. def get_s3_config(bucket_name: str):
  22. """~/magic-pdf.json 读出来."""
  23. config = read_config()
  24. bucket_info = config.get('bucket_info')
  25. if bucket_name not in bucket_info:
  26. access_key, secret_key, storage_endpoint = bucket_info['[default]']
  27. else:
  28. access_key, secret_key, storage_endpoint = bucket_info[bucket_name]
  29. if access_key is None or secret_key is None or storage_endpoint is None:
  30. raise Exception(f'ak, sk or endpoint not found in {CONFIG_FILE_NAME}')
  31. # logger.info(f"get_s3_config: ak={access_key}, sk={secret_key}, endpoint={storage_endpoint}")
  32. return access_key, secret_key, storage_endpoint
  33. def get_s3_config_dict(path: str):
  34. access_key, secret_key, storage_endpoint = get_s3_config(get_bucket_name(path))
  35. return {'ak': access_key, 'sk': secret_key, 'endpoint': storage_endpoint}
  36. def get_bucket_name(path):
  37. bucket, key = parse_bucket_key(path)
  38. return bucket
  39. def parse_bucket_key(s3_full_path: str):
  40. """
  41. 输入 s3://bucket/path/to/my/file.txt
  42. 输出 bucket, path/to/my/file.txt
  43. """
  44. s3_full_path = s3_full_path.strip()
  45. if s3_full_path.startswith("s3://"):
  46. s3_full_path = s3_full_path[5:]
  47. if s3_full_path.startswith("/"):
  48. s3_full_path = s3_full_path[1:]
  49. bucket, key = s3_full_path.split("/", 1)
  50. return bucket, key
  51. def get_device():
  52. device_mode = os.getenv('MINERU_DEVICE_MODE', None)
  53. if device_mode is not None:
  54. return device_mode
  55. else:
  56. if torch.cuda.is_available():
  57. return "cuda"
  58. elif torch.backends.mps.is_available():
  59. return "mps"
  60. else:
  61. try:
  62. import torch_npu
  63. if torch_npu.npu.is_available():
  64. return "npu"
  65. except Exception as e:
  66. pass
  67. return "cpu"
  68. def get_formula_enable(formula_enable):
  69. formula_enable_env = os.getenv('MINERU_FORMULA_ENABLE')
  70. formula_enable = formula_enable if formula_enable_env is None else formula_enable_env.lower() == 'true'
  71. return formula_enable
  72. def get_table_enable(table_enable):
  73. table_enable_env = os.getenv('MINERU_TABLE_ENABLE')
  74. table_enable = table_enable if table_enable_env is None else table_enable_env.lower() == 'true'
  75. return table_enable
  76. def get_latex_delimiter_config():
  77. config = read_config()
  78. if config is None:
  79. return None
  80. latex_delimiter_config = config.get('latex-delimiter-config', None)
  81. if latex_delimiter_config is None:
  82. # logger.warning(f"'latex-delimiter-config' not found in {CONFIG_FILE_NAME}, use 'None' as default")
  83. return None
  84. else:
  85. return latex_delimiter_config
  86. def get_llm_aided_config():
  87. config = read_config()
  88. if config is None:
  89. return None
  90. llm_aided_config = config.get('llm-aided-config', None)
  91. if llm_aided_config is None:
  92. # logger.warning(f"'llm-aided-config' not found in {CONFIG_FILE_NAME}, use 'None' as default")
  93. return None
  94. else:
  95. return llm_aided_config
  96. def get_local_models_dir():
  97. config = read_config()
  98. if config is None:
  99. return None
  100. models_dir = config.get('models-dir')
  101. if models_dir is None:
  102. logger.warning(f"'models-dir' not found in {CONFIG_FILE_NAME}, use None as default")
  103. return models_dir