config_reader.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """
  2. 根据bucket的名字返回对应的s3 AK, SK,endpoint三元组
  3. """
  4. import json
  5. import os
  6. from loguru import logger
  7. from magic_pdf.libs.commons import parse_bucket_key
  8. def read_config():
  9. home_dir = os.path.expanduser("~")
  10. config_file = os.path.join(home_dir, "magic-pdf.json")
  11. if not os.path.exists(config_file):
  12. raise Exception(f"{config_file} not found")
  13. with open(config_file, "r") as f:
  14. config = json.load(f)
  15. return config
  16. def get_s3_config(bucket_name: str):
  17. """
  18. ~/magic-pdf.json 读出来
  19. """
  20. config = read_config()
  21. bucket_info = config.get("bucket_info")
  22. if bucket_name not in bucket_info:
  23. access_key, secret_key, storage_endpoint = bucket_info["[default]"]
  24. else:
  25. access_key, secret_key, storage_endpoint = bucket_info[bucket_name]
  26. if access_key is None or secret_key is None or storage_endpoint is None:
  27. raise Exception("ak, sk or endpoint not found in magic-pdf.json")
  28. # logger.info(f"get_s3_config: ak={access_key}, sk={secret_key}, endpoint={storage_endpoint}")
  29. return access_key, secret_key, storage_endpoint
  30. def get_s3_config_dict(path: str):
  31. access_key, secret_key, storage_endpoint = get_s3_config(get_bucket_name(path))
  32. return {"ak": access_key, "sk": secret_key, "endpoint": storage_endpoint}
  33. def get_bucket_name(path):
  34. bucket, key = parse_bucket_key(path)
  35. return bucket
  36. def get_local_dir():
  37. config = read_config()
  38. return config.get("temp-output-dir", "/tmp")
  39. def get_local_models_dir():
  40. config = read_config()
  41. return config.get("models-dir", "/tmp/models")
  42. def get_device():
  43. config = read_config()
  44. return config.get("device-mode", "cpu")
  45. if __name__ == "__main__":
  46. ak, sk, endpoint = get_s3_config("llm-raw")