Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug duplicating spaces with default cpu hardware #4583

Merged
merged 7 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion client/python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
9 changes: 6 additions & 3 deletions client/python/gradio_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions client/python/gradio_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)} "
Expand Down
40 changes: 38 additions & 2 deletions client/python/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down