hub_config.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. import os
  15. import json
  16. import yaml
  17. from . import hub_env as hubenv
  18. class HubConfig:
  19. """
  20. UltraInfer model management configuration class.
  21. """
  22. def __init__(self):
  23. self._initialize()
  24. self.file = os.path.join(hubenv.CONF_HOME, "config.yaml")
  25. if not os.path.exists(self.file):
  26. self.flush()
  27. return
  28. with open(self.file, "r") as file:
  29. try:
  30. cfg = yaml.load(file, Loader=yaml.FullLoader)
  31. self.data.update(cfg)
  32. except:
  33. ...
  34. def _initialize(self):
  35. # Set default configuration values.
  36. self.data = {}
  37. self.data["server"] = "http://paddlepaddle.org.cn/paddlehub"
  38. def reset(self):
  39. """Reset configuration to default."""
  40. self._initialize()
  41. self.flush()
  42. @property
  43. def server(self):
  44. """Model server url."""
  45. return self.data["server"]
  46. @server.setter
  47. def server(self, url: str):
  48. self.data["server"] = url
  49. self.flush()
  50. def flush(self):
  51. """Flush the current configuration into the configuration file."""
  52. with open(self.file, "w") as file:
  53. cfg = json.loads(json.dumps(self.data))
  54. yaml.dump(cfg, file)
  55. def __str__(self):
  56. cfg = json.loads(json.dumps(self.data))
  57. return yaml.dump(cfg)
  58. config = HubConfig()