hub_env.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. This module is used to store environmental variables for ultra_infer model hub.
  16. ULTRAINFER_HUB_HOME --> the root directory for storing ultra_infer model hub related data. Default to ~/.ultra_infer. Users can change the
  17. ├ default value through the ULTRAINFER_HUB_HOME environment variable.
  18. ├── MODEL_HOME --> Store the downloaded ultra_infer models.
  19. ├── CONF_HOME --> Store the default configuration files.
  20. """
  21. import os
  22. def _get_user_home():
  23. return os.path.expanduser("~")
  24. def _get_hub_home():
  25. if "ULTRAINFER_HUB_HOME" in os.environ:
  26. home_path = os.environ["ULTRAINFER_HUB_HOME"]
  27. if os.path.exists(home_path):
  28. if os.path.isdir(home_path):
  29. return home_path
  30. else:
  31. raise RuntimeError(
  32. "The environment variable ULTRAINFER_HUB_HOME {} is not a directory.".format(
  33. home_path
  34. )
  35. )
  36. else:
  37. return home_path
  38. return os.path.join(_get_user_home(), ".ultra_infer")
  39. def _get_sub_home(directory):
  40. home = os.path.join(_get_hub_home(), directory)
  41. os.makedirs(home, exist_ok=True)
  42. return home
  43. USER_HOME = _get_user_home()
  44. HUB_HOME = _get_hub_home()
  45. MODEL_HOME = _get_sub_home("models")
  46. CONF_HOME = _get_sub_home("conf")
  47. RESOURCE_HOME = _get_sub_home("resources")