From 55f1bcda2b7c309cbe40711c7f4f0828ca5ae7d0 Mon Sep 17 00:00:00 2001 From: Lucain Date: Fri, 11 Aug 2023 12:07:41 +0200 Subject: [PATCH] Support factory reboot when restarting a Space (#1586) * Implement factory reboot for Spaces * fix --- src/huggingface_hub/hf_api.py | 11 +++++++++-- tests/test_hf_api.py | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/huggingface_hub/hf_api.py b/src/huggingface_hub/hf_api.py index 8424635c90..930f945ba9 100644 --- a/src/huggingface_hub/hf_api.py +++ b/src/huggingface_hub/hf_api.py @@ -5172,7 +5172,9 @@ def pause_space(self, repo_id: str, *, token: Optional[str] = None) -> SpaceRunt return SpaceRuntime(r.json()) @validate_hf_hub_args - def restart_space(self, repo_id: str, *, token: Optional[str] = None) -> SpaceRuntime: + def restart_space( + self, repo_id: str, *, token: Optional[str] = None, factory_reboot: bool = False + ) -> SpaceRuntime: """Restart your Space. This is the only way to programmatically restart a Space if you've put it on Pause (see [`pause_space`]). You @@ -5186,6 +5188,8 @@ def restart_space(self, repo_id: str, *, token: Optional[str] = None) -> SpaceRu ID of the Space to restart. Example: `"Salesforce/BLIP2"`. token (`str`, *optional*): Hugging Face token. Will default to the locally saved token if not provided. + factory_reboot (`bool`, *optional*): + If `True`, the Space will be rebuilt from scratch without caching any requirements. Returns: [`SpaceRuntime`]: Runtime information about your Space. @@ -5201,8 +5205,11 @@ def restart_space(self, repo_id: str, *, token: Optional[str] = None) -> SpaceRu If your Space is a static Space. Static Spaces are always running and never billed. If you want to hide a static Space, you can set it to private. """ + params = {} + if factory_reboot: + params["factory"] = "true" r = get_session().post( - f"{self.endpoint}/api/spaces/{repo_id}/restart", headers=self._build_hf_headers(token=token) + f"{self.endpoint}/api/spaces/{repo_id}/restart", headers=self._build_hf_headers(token=token), params=params ) hf_raise_for_status(r) return SpaceRuntime(r.json()) diff --git a/tests/test_hf_api.py b/tests/test_hf_api.py index 3fe74c7b36..bf93989617 100644 --- a/tests/test_hf_api.py +++ b/tests/test_hf_api.py @@ -2691,6 +2691,14 @@ def test_delete_space_storage(self) -> None: ) assert runtime.storage is None + def test_restart_space_factory_reboot(self) -> None: + self.api.restart_space(self.repo_id, factory_reboot=True) + self.post_mock.assert_called_once_with( + f"{self.api.endpoint}/api/spaces/{self.repo_id}/restart", + headers=self.api._build_hf_headers(), + params={"factory": "true"}, + ) + class ListGitRefsTest(unittest.TestCase): @classmethod