config_reader.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright (c) Opendatalab. All rights reserved.
  2. import json
  3. import os
  4. from loguru import logger
  5. # 定义配置文件名常量
  6. CONFIG_FILE_NAME = os.getenv('MINERU_TOOLS_CONFIG_JSON', 'mineru.json')
  7. def read_config():
  8. if os.path.isabs(CONFIG_FILE_NAME):
  9. config_file = CONFIG_FILE_NAME
  10. else:
  11. home_dir = os.path.expanduser('~')
  12. config_file = os.path.join(home_dir, CONFIG_FILE_NAME)
  13. if not os.path.exists(config_file):
  14. # logger.warning(f'{config_file} not found, using default configuration')
  15. return None
  16. else:
  17. with open(config_file, 'r', encoding='utf-8') as f:
  18. config = json.load(f)
  19. return config
  20. def get_s3_config(bucket_name: str):
  21. """~/magic-pdf.json 读出来."""
  22. config = read_config()
  23. bucket_info = config.get('bucket_info')
  24. if bucket_name not in bucket_info:
  25. access_key, secret_key, storage_endpoint = bucket_info['[default]']
  26. else:
  27. access_key, secret_key, storage_endpoint = bucket_info[bucket_name]
  28. if access_key is None or secret_key is None or storage_endpoint is None:
  29. raise Exception(f'ak, sk or endpoint not found in {CONFIG_FILE_NAME}')
  30. # logger.info(f"get_s3_config: ak={access_key}, sk={secret_key}, endpoint={storage_endpoint}")
  31. return access_key, secret_key, storage_endpoint
  32. def get_s3_config_dict(path: str):
  33. access_key, secret_key, storage_endpoint = get_s3_config(get_bucket_name(path))
  34. return {'ak': access_key, 'sk': secret_key, 'endpoint': storage_endpoint}
  35. def get_bucket_name(path):
  36. bucket, key = parse_bucket_key(path)
  37. return bucket
  38. def parse_bucket_key(s3_full_path: str):
  39. """
  40. 输入 s3://bucket/path/to/my/file.txt
  41. 输出 bucket, path/to/my/file.txt
  42. """
  43. s3_full_path = s3_full_path.strip()
  44. if s3_full_path.startswith("s3://"):
  45. s3_full_path = s3_full_path[5:]
  46. if s3_full_path.startswith("/"):
  47. s3_full_path = s3_full_path[1:]
  48. bucket, key = s3_full_path.split("/", 1)
  49. return bucket, key
  50. def get_device():
  51. device_mode = os.getenv('MINERU_DEVICE_MODE', None)
  52. if device_mode is not None:
  53. return device_mode
  54. else:
  55. import torch
  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