Fix bug duplicating spaces with default cpu hardware (#4583)

* Fix bug

* Add CHANGELOG

* Fix test

---------

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
Freddy Boulton 2023-06-21 15:57:24 -04:00 committed by GitHub
parent 458b2d9bbd
commit 2e351ec6f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 8 deletions

View File

@ -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:

View File

@ -220,9 +220,6 @@ class Client:
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 @@ class Client:
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(

View File

@ -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)} "

View File

@ -11,6 +11,7 @@ from pathlib import Path
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 @@ class TestEndpoints:
]
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 @@ class TestDuplication:
)
@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)