mirror of
https://github.com/gradio-app/gradio.git
synced 2025-01-30 11:00:11 +08:00
Python Client: Handle invalid space state (#3822)
* Raise error on status check * Add BUILDING * Remove SLEEPING
This commit is contained in:
parent
a04f303c3c
commit
00d7a5ce24
@ -83,6 +83,12 @@ class Client:
|
||||
self.src = _src
|
||||
if self.verbose:
|
||||
print(f"Loaded as API: {self.src} ✔")
|
||||
state = self._get_space_state()
|
||||
if state in utils.INVALID_RUNTIME:
|
||||
raise ValueError(
|
||||
f"The current space is in the invalid state: {state}. "
|
||||
"Please contact the owner to fix this."
|
||||
)
|
||||
|
||||
self.api_url = urllib.parse.urljoin(self.src, utils.API_URL)
|
||||
self.ws_url = urllib.parse.urljoin(
|
||||
@ -104,6 +110,12 @@ class Client:
|
||||
# Disable telemetry by setting the env variable HF_HUB_DISABLE_TELEMETRY=1
|
||||
threading.Thread(target=self._telemetry_thread).start()
|
||||
|
||||
def _get_space_state(self):
|
||||
if not self.space_id:
|
||||
return None
|
||||
api = huggingface_hub.HfApi(token=self.hf_token)
|
||||
return api.get_space_runtime(self.space_id).stage
|
||||
|
||||
def predict(
|
||||
self,
|
||||
*args,
|
||||
|
@ -27,6 +27,14 @@ UPLOAD_URL = "/upload"
|
||||
RESET_URL = "/reset"
|
||||
DUPLICATE_URL = "https://huggingface.co/spaces/{}?duplicate=true"
|
||||
STATE_COMPONENT = "state"
|
||||
INVALID_RUNTIME = [
|
||||
"NO_APP_FILE",
|
||||
"CONFIG_ERROR",
|
||||
"BUILDING",
|
||||
"BUILD_ERROR",
|
||||
"RUNTIME_ERROR",
|
||||
"PAUSED",
|
||||
]
|
||||
|
||||
__version__ = (pkgutil.get_data(__name__, "version.txt") or b"").decode("ascii").strip()
|
||||
|
||||
|
@ -19,6 +19,11 @@ HF_TOKEN = "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" # Intentionally reveali
|
||||
|
||||
|
||||
class TestPredictionsFromSpaces:
|
||||
@pytest.mark.flaky
|
||||
def test_raise_error_invalid_state(self):
|
||||
with pytest.raises(ValueError, match="invalid state"):
|
||||
Client("gradio-tests/paused-space")
|
||||
|
||||
@pytest.mark.flaky
|
||||
def test_numerical_to_label_space(self):
|
||||
client = Client("gradio-tests/titanic-survival")
|
||||
|
Loading…
Reference in New Issue
Block a user