config_reader.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_table_recog_config():
  69. table_enable = os.getenv('MINERU_TABLE_ENABLE', None)
  70. if table_enable is not None:
  71. return json.loads(f'{{"enable": {table_enable}}}')
  72. else:
  73. # logger.warning(f"not found 'MINERU_TABLE_ENABLE' in environment variable, use 'true' as default.")
  74. return json.loads(f'{{"enable": true}}')
  75. def get_formula_config():
  76. formula_enable = os.getenv('MINERU_FORMULA_ENABLE', None)
  77. if formula_enable is not None:
  78. return json.loads(f'{{"enable": {formula_enable}}}')
  79. else:
  80. # logger.warning(f"not found 'MINERU_FORMULA_ENABLE' in environment variable, use 'true' as default.")
  81. return json.loads(f'{{"enable": true}}')
  82. def get_latex_delimiter_config():
  83. config = read_config()
  84. if config is None:
  85. return None
  86. latex_delimiter_config = config.get('latex-delimiter-config', None)
  87. if latex_delimiter_config is None:
  88. # logger.warning(f"'latex-delimiter-config' not found in {CONFIG_FILE_NAME}, use 'None' as default")
  89. return None
  90. else:
  91. return latex_delimiter_config
  92. def get_llm_aided_config():
  93. config = read_config()
  94. if config is None:
  95. return None
  96. llm_aided_config = config.get('llm-aided-config', None)
  97. if llm_aided_config is None:
  98. # logger.warning(f"'llm-aided-config' not found in {CONFIG_FILE_NAME}, use 'None' as default")
  99. return None
  100. else:
  101. return llm_aided_config
  102. def get_local_models_dir():
  103. config = read_config()
  104. if config is None:
  105. return None
  106. models_dir = config.get('models-dir')
  107. if models_dir is None:
  108. logger.warning(f"'models-dir' not found in {CONFIG_FILE_NAME}, use None as default")
  109. return models_dir