config_reader.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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', 'magic-pdf.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_local_models_dir():
  51. config = read_config()
  52. models_dir = config.get('models-dir')
  53. if models_dir is None:
  54. logger.warning(f"'models-dir' not found in {CONFIG_FILE_NAME}, use '/tmp/models' as default")
  55. return '/tmp/models'
  56. else:
  57. return models_dir
  58. def get_local_layoutreader_model_dir():
  59. config = read_config()
  60. layoutreader_model_dir = config.get('layoutreader-model-dir')
  61. if layoutreader_model_dir is None or not os.path.exists(layoutreader_model_dir):
  62. home_dir = os.path.expanduser('~')
  63. layoutreader_at_modelscope_dir_path = os.path.join(home_dir, '.cache/modelscope/hub/ppaanngggg/layoutreader')
  64. logger.warning(f"'layoutreader-model-dir' not exists, use {layoutreader_at_modelscope_dir_path} as default")
  65. return layoutreader_at_modelscope_dir_path
  66. else:
  67. return layoutreader_model_dir
  68. def get_device():
  69. device_mode = os.getenv('MINERU_DEVICE_MODE', None)
  70. if device_mode is not None:
  71. return device_mode
  72. else:
  73. logger.warning(f"not found 'MINERU_DEVICE_MODE' in environment variable, use 'cpu' as default.")
  74. return 'cpu'
  75. def get_table_recog_config():
  76. table_enable = os.getenv('MINERU_TABLE_ENABLE', None)
  77. if table_enable is not None:
  78. return json.loads(f'{{"enable": {table_enable}}}')
  79. else:
  80. logger.warning(f"not found 'MINERU_TABLE_ENABLE' in environment variable, use 'true' as default.")
  81. return json.loads(f'{{"enable": true}}')
  82. def get_formula_config():
  83. formula_enable = os.getenv('MINERU_FORMULA_ENABLE', None)
  84. if formula_enable is not None:
  85. return json.loads(f'{{"enable": {formula_enable}}}')
  86. else:
  87. logger.warning(f"not found 'MINERU_FORMULA_ENABLE' in environment variable, use 'true' as default.")
  88. return json.loads(f'{{"enable": true}}')
  89. def get_latex_delimiter_config():
  90. config = read_config()
  91. latex_delimiter_config = config.get('latex-delimiter-config')
  92. if latex_delimiter_config is None:
  93. logger.warning(f"'latex-delimiter-config' not found in {CONFIG_FILE_NAME}, use 'None' as default")
  94. return None
  95. else:
  96. return latex_delimiter_config
  97. def get_llm_aided_config():
  98. config = read_config()
  99. llm_aided_config = config.get('llm-aided-config')
  100. if llm_aided_config is None:
  101. logger.warning(f"'llm-aided-config' not found in {CONFIG_FILE_NAME}, use 'None' as default")
  102. return None
  103. else:
  104. return llm_aided_config