diff --git a/client/python/CHANGELOG.md b/client/python/CHANGELOG.md index 5c95073eb90c2..4cbd962289656 100644 --- a/client/python/CHANGELOG.md +++ b/client/python/CHANGELOG.md @@ -6,7 +6,8 @@ No changes to highlight. ## Bug Fixes: -No changes to highlight. +- Fix bug where space duplication would error if the demo has cpu-basic hardware by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 4583](https://github.com/gradio-app/gradio/pull/4583) + ## Breaking Changes: diff --git a/client/python/gradio_client/client.py b/client/python/gradio_client/client.py index b49c61b6ef049..4ca9efab3e0bb 100644 --- a/client/python/gradio_client/client.py +++ b/client/python/gradio_client/client.py @@ -220,9 +220,6 @@ def duplicate( huggingface_hub.add_space_secret( space_id, key, value, token=hf_token ) - utils.set_space_timeout( - space_id, hf_token=hf_token, timeout_in_seconds=sleep_timeout * 60 - ) if verbose: print(f"Created new Space: {utils.SPACE_URL.format(space_id)}") current_info = huggingface_hub.get_space_runtime(space_id, token=hf_token) @@ -235,6 +232,12 @@ def duplicate( print( f"-------\nNOTE: this Space uses upgraded hardware: {hardware}... see billing info at https://huggingface.co/settings/billing\n-------" ) + # Setting a timeout only works if the hardware is not basic + # so set it here after the hardware has been requested + if hardware != huggingface_hub.SpaceHardware.CPU_BASIC: + utils.set_space_timeout( + space_id, hf_token=hf_token, timeout_in_seconds=sleep_timeout * 60 + ) if verbose: print("") client = cls( diff --git a/client/python/gradio_client/utils.py b/client/python/gradio_client/utils.py index b4e476f3e83fa..e61372df889c7 100644 --- a/client/python/gradio_client/utils.py +++ b/client/python/gradio_client/utils.py @@ -444,13 +444,13 @@ def set_space_timeout( library_name="gradio_client", library_version=__version__, ) - r = requests.post( + req = requests.post( f"https://huggingface.co/api/spaces/{space_id}/sleeptime", json={"seconds": timeout_in_seconds}, headers=headers, ) try: - huggingface_hub.utils.hf_raise_for_status(r) + huggingface_hub.utils.hf_raise_for_status(req) except huggingface_hub.utils.HfHubHTTPError as err: raise SpaceDuplicationError( f"Could not set sleep timeout on duplicated Space. Please visit {SPACE_URL.format(space_id)} " diff --git a/client/python/test/test_client.py b/client/python/test/test_client.py index 328110a589877..6018ba95e8c9f 100644 --- a/client/python/test/test_client.py +++ b/client/python/test/test_client.py @@ -11,6 +11,7 @@ from unittest.mock import MagicMock, patch import gradio as gr +import huggingface_hub import pytest import uvicorn from fastapi import FastAPI @@ -860,9 +861,12 @@ def test_upload(self): ] +cpu = huggingface_hub.SpaceHardware.CPU_BASIC + + class TestDuplication: @pytest.mark.flaky - @patch("huggingface_hub.get_space_runtime", return_value=MagicMock(hardware="cpu")) + @patch("huggingface_hub.get_space_runtime", return_value=MagicMock(hardware=cpu)) @patch("gradio_client.client.Client.__init__", return_value=None) def test_new_space_id(self, mock_init, mock_runtime): Client.duplicate("gradio/calculator", "test", hf_token=HF_TOKEN) @@ -879,7 +883,39 @@ def test_new_space_id(self, mock_init, mock_runtime): ) @pytest.mark.flaky - @patch("huggingface_hub.get_space_runtime", return_value=MagicMock(hardware="cpu")) + @patch("gradio_client.utils.set_space_timeout") + @patch("huggingface_hub.get_space_runtime", return_value=MagicMock(hardware=cpu)) + @patch("gradio_client.client.Client.__init__", return_value=None) + def test_dont_set_timeout_if_default_hardware( + self, mock_init, mock_runtime, mock_set_timeout + ): + Client.duplicate("gradio/calculator", "test", hf_token=HF_TOKEN) + mock_set_timeout.assert_not_called() + + @pytest.mark.flaky + @patch("huggingface_hub.request_space_hardware") + @patch("gradio_client.utils.set_space_timeout") + @patch( + "huggingface_hub.get_space_runtime", + return_value=MagicMock(hardware=huggingface_hub.SpaceHardware.CPU_UPGRADE), + ) + @patch("gradio_client.client.Client.__init__", return_value=None) + def test_set_timeout_if_not_default_hardware( + self, mock_init, mock_runtime, mock_set_timeout, mock_request_hardware + ): + Client.duplicate( + "gradio/calculator", + "test", + hf_token=HF_TOKEN, + hardware="cpu-upgrade", + sleep_timeout=15, + ) + mock_set_timeout.assert_called_once_with( + "gradio-tests/test", hf_token=HF_TOKEN, timeout_in_seconds=15 * 60 + ) + + @pytest.mark.flaky + @patch("huggingface_hub.get_space_runtime", return_value=MagicMock(hardware=cpu)) @patch("gradio_client.client.Client.__init__", return_value=None) def test_default_space_id(self, mock_init, mock_runtime): Client.duplicate("gradio/calculator", hf_token=HF_TOKEN)