Ahmed commited on
Commit
ba1573f
·
1 Parent(s): 36f4302

fix: improve HF resume/restart

Browse files
Files changed (1) hide show
  1. set-hf-gemini-secret.ps1 +13 -3
set-hf-gemini-secret.ps1 CHANGED
@@ -113,6 +113,7 @@ $env:HF_RESUME_SPACE = $(if ($ResumeSpace) { "1" } else { "" })
113
  @'
114
  import os
115
  from huggingface_hub import HfApi
 
116
 
117
  api = HfApi(token=os.environ["HF_TOKEN"])
118
  api.add_space_secret(
@@ -121,9 +122,18 @@ api.add_space_secret(
121
  value=os.environ["HF_SECRET_VALUE"],
122
  )
123
  if os.environ.get("HF_RESUME_SPACE") == "1":
124
- api.pause_space(repo_id=os.environ["HF_SPACE_REPO_ID"], paused=False)
125
- api.restart_space(repo_id=os.environ["HF_SPACE_REPO_ID"])
126
- print("OK: Secret set and Space resumed/restarted.")
 
 
 
 
 
 
 
 
 
127
  else:
128
  print("OK: Secret set. HF will restart the Space automatically.")
129
  '@ | python -
 
113
  @'
114
  import os
115
  from huggingface_hub import HfApi
116
+ from huggingface_hub.utils import HfHubHTTPError
117
 
118
  api = HfApi(token=os.environ["HF_TOKEN"])
119
  api.add_space_secret(
 
122
  value=os.environ["HF_SECRET_VALUE"],
123
  )
124
  if os.environ.get("HF_RESUME_SPACE") == "1":
125
+ try:
126
+ api.restart_space(repo_id=os.environ["HF_SPACE_REPO_ID"])
127
+ print("OK: Secret set and Space resumed/restarted.")
128
+ except HfHubHTTPError as e:
129
+ # HF can return 503 while a restart is already in progress.
130
+ if getattr(getattr(e, "response", None), "status_code", None) == 503:
131
+ rt = api.get_space_runtime(repo_id=os.environ["HF_SPACE_REPO_ID"])
132
+ if getattr(rt, "stage", None) == "PAUSED":
133
+ raise
134
+ print(f"OK: Secret set; Space stage is {rt.stage} (restart in progress).")
135
+ else:
136
+ raise
137
  else:
138
  print("OK: Secret set. HF will restart the Space automatically.")
139
  '@ | python -