copilot-swe-agent[bot] SpinTillYouWin-WheelPulsePro commited on
Commit
f153b1a
·
unverified ·
1 Parent(s): 764e1ab

Fix 429 rate-limit crash loop: add exponential backoff retry to HuggingFace login

Browse files

Agent-Logs-Url: https://github.com/SpinTillYouWin-WheelPulsePro/WheelPulsePro/sessions/0ef43430-1d3b-43d2-9c9b-d0942aab75c0

Co-authored-by: SpinTillYouWin-WheelPulsePro <213299693+SpinTillYouWin-WheelPulsePro@users.noreply.github.com>

Files changed (2) hide show
  1. README.md +5 -3
  2. app.py +40 -1
README.md CHANGED
@@ -1,7 +1,7 @@
1
  ---
2
  title: WheelPulsePro
3
- emoji: 🌖
4
- colorFrom: gray
5
  colorTo: blue
6
  sdk: gradio
7
  sdk_version: 5.31.0
@@ -9,4 +9,6 @@ app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
1
  ---
2
  title: WheelPulsePro
3
+ emoji: 🎡
4
+ colorFrom: purple
5
  colorTo: blue
6
  sdk: gradio
7
  sdk_version: 5.31.0
 
9
  pinned: false
10
  ---
11
 
12
+ # WheelPulsePro
13
+
14
+ WheelPulsePro — Your interactive wheel experience! This Space handles HuggingFace authentication with automatic retry and exponential backoff to avoid rate-limit crash loops.
app.py CHANGED
@@ -1,8 +1,47 @@
1
  from huggingface_hub import login, hf_hub_download
 
2
  import os
3
  import sys
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- login(token=os.environ.get("HF_TOKEN"))
6
  repo_id = "Spintillyouwinwheelpulsepro/WheelPulsePro"
7
  for file in ["app.py", "roulette_data.py"]:
8
  hf_hub_download(repo_id=repo_id, filename=file, repo_type="model", local_dir=".")
 
1
  from huggingface_hub import login, hf_hub_download
2
+ from huggingface_hub.errors import HfHubHTTPError
3
  import os
4
  import sys
5
+ import time
6
+ import logging
7
+
8
+ logging.basicConfig(level=logging.INFO)
9
+ logger = logging.getLogger(__name__)
10
+
11
+
12
+ def safe_hf_login(max_retries=5):
13
+ """Login to HuggingFace with retry logic and exponential backoff."""
14
+ token = os.environ.get("HF_TOKEN")
15
+ if not token:
16
+ logger.warning("HF_TOKEN not set. Skipping HuggingFace login.")
17
+ return False
18
+
19
+ for attempt in range(max_retries):
20
+ try:
21
+ login(token=token)
22
+ logger.info("Successfully logged in to HuggingFace.")
23
+ return True
24
+ except HfHubHTTPError as e:
25
+ if getattr(getattr(e, "response", None), "status_code", None) == 429:
26
+ wait_time = min(2 ** attempt * 30, 300) # 30s, 60s, 120s, 240s, 300s (capped)
27
+ logger.warning(
28
+ f"Rate limited (attempt {attempt + 1}/{max_retries}). "
29
+ f"Retrying in {wait_time}s..."
30
+ )
31
+ time.sleep(wait_time)
32
+ else:
33
+ logger.error(f"HuggingFace login failed: {e}")
34
+ return False
35
+ except Exception as e:
36
+ logger.error(f"Unexpected error during login: {e}")
37
+ return False
38
+
39
+ logger.error("Failed to login after all retries. Continuing without auth.")
40
+ return False
41
+
42
+
43
+ safe_hf_login()
44
 
 
45
  repo_id = "Spintillyouwinwheelpulsepro/WheelPulsePro"
46
  for file in ["app.py", "roulette_data.py"]:
47
  hf_hub_download(repo_id=repo_id, filename=file, repo_type="model", local_dir=".")