hub_config.py 2.0 KB

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