_space_api.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # coding=utf-8
  2. # Copyright 2019-present, the HuggingFace Inc. team.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from dataclasses import dataclass
  16. from datetime import datetime
  17. from enum import Enum
  18. from typing import Dict, Optional
  19. from huggingface_hub.utils import parse_datetime
  20. class SpaceStage(str, Enum):
  21. """
  22. Enumeration of possible stage of a Space on the Hub.
  23. Value can be compared to a string:
  24. ```py
  25. assert SpaceStage.BUILDING == "BUILDING"
  26. ```
  27. Taken from https://github.com/huggingface/moon-landing/blob/main/server/repo_types/SpaceInfo.ts#L61 (private url).
  28. """
  29. # Copied from moon-landing > server > repo_types > SpaceInfo.ts (private repo)
  30. NO_APP_FILE = "NO_APP_FILE"
  31. CONFIG_ERROR = "CONFIG_ERROR"
  32. BUILDING = "BUILDING"
  33. BUILD_ERROR = "BUILD_ERROR"
  34. RUNNING = "RUNNING"
  35. RUNNING_BUILDING = "RUNNING_BUILDING"
  36. RUNTIME_ERROR = "RUNTIME_ERROR"
  37. DELETING = "DELETING"
  38. STOPPED = "STOPPED"
  39. PAUSED = "PAUSED"
  40. class SpaceHardware(str, Enum):
  41. """
  42. Enumeration of hardwares available to run your Space on the Hub.
  43. Value can be compared to a string:
  44. ```py
  45. assert SpaceHardware.CPU_BASIC == "cpu-basic"
  46. ```
  47. Taken from https://github.com/huggingface-internal/moon-landing/blob/main/server/repo_types/SpaceHardwareFlavor.ts (private url).
  48. """
  49. # CPU
  50. CPU_BASIC = "cpu-basic"
  51. CPU_UPGRADE = "cpu-upgrade"
  52. CPU_XL = "cpu-xl"
  53. # ZeroGPU
  54. ZERO_A10G = "zero-a10g"
  55. # GPU
  56. T4_SMALL = "t4-small"
  57. T4_MEDIUM = "t4-medium"
  58. L4X1 = "l4x1"
  59. L4X4 = "l4x4"
  60. L40SX1 = "l40sx1"
  61. L40SX4 = "l40sx4"
  62. L40SX8 = "l40sx8"
  63. A10G_SMALL = "a10g-small"
  64. A10G_LARGE = "a10g-large"
  65. A10G_LARGEX2 = "a10g-largex2"
  66. A10G_LARGEX4 = "a10g-largex4"
  67. A100_LARGE = "a100-large"
  68. H100 = "h100"
  69. H100X8 = "h100x8"
  70. class SpaceStorage(str, Enum):
  71. """
  72. Enumeration of persistent storage available for your Space on the Hub.
  73. Value can be compared to a string:
  74. ```py
  75. assert SpaceStorage.SMALL == "small"
  76. ```
  77. Taken from https://github.com/huggingface/moon-landing/blob/main/server/repo_types/SpaceHardwareFlavor.ts#L24 (private url).
  78. """
  79. SMALL = "small"
  80. MEDIUM = "medium"
  81. LARGE = "large"
  82. @dataclass
  83. class SpaceRuntime:
  84. """
  85. Contains information about the current runtime of a Space.
  86. Args:
  87. stage (`str`):
  88. Current stage of the space. Example: RUNNING.
  89. hardware (`str` or `None`):
  90. Current hardware of the space. Example: "cpu-basic". Can be `None` if Space
  91. is `BUILDING` for the first time.
  92. requested_hardware (`str` or `None`):
  93. Requested hardware. Can be different than `hardware` especially if the request
  94. has just been made. Example: "t4-medium". Can be `None` if no hardware has
  95. been requested yet.
  96. sleep_time (`int` or `None`):
  97. Number of seconds the Space will be kept alive after the last request. By default (if value is `None`), the
  98. Space will never go to sleep if it's running on an upgraded hardware, while it will go to sleep after 48
  99. hours on a free 'cpu-basic' hardware. For more details, see https://huggingface.co/docs/hub/spaces-gpus#sleep-time.
  100. raw (`dict`):
  101. Raw response from the server. Contains more information about the Space
  102. runtime like number of replicas, number of cpu, memory size,...
  103. """
  104. stage: SpaceStage
  105. hardware: Optional[SpaceHardware]
  106. requested_hardware: Optional[SpaceHardware]
  107. sleep_time: Optional[int]
  108. storage: Optional[SpaceStorage]
  109. raw: Dict
  110. def __init__(self, data: Dict) -> None:
  111. self.stage = data["stage"]
  112. self.hardware = data.get("hardware", {}).get("current")
  113. self.requested_hardware = data.get("hardware", {}).get("requested")
  114. self.sleep_time = data.get("gcTimeout")
  115. self.storage = data.get("storage")
  116. self.raw = data
  117. @dataclass
  118. class SpaceVariable:
  119. """
  120. Contains information about the current variables of a Space.
  121. Args:
  122. key (`str`):
  123. Variable key. Example: `"MODEL_REPO_ID"`
  124. value (`str`):
  125. Variable value. Example: `"the_model_repo_id"`.
  126. description (`str` or None):
  127. Description of the variable. Example: `"Model Repo ID of the implemented model"`.
  128. updatedAt (`datetime` or None):
  129. datetime of the last update of the variable (if the variable has been updated at least once).
  130. """
  131. key: str
  132. value: str
  133. description: Optional[str]
  134. updated_at: Optional[datetime]
  135. def __init__(self, key: str, values: Dict) -> None:
  136. self.key = key
  137. self.value = values["value"]
  138. self.description = values.get("description")
  139. updated_at = values.get("updatedAt")
  140. self.updated_at = parse_datetime(updated_at) if updated_at is not None else None