Download scripts/hf_commit.py from MitakaKuma/SUMI-OpenCT: direct link, hf CLI and curl.
- Browser
- Download file 1.81 kB
-
https://huggingface.co/datasets/MitakaKuma/SUMI-OpenCT/resolve/main/scripts/hf_commit.py
- Command line
-
hf download hf://datasets/MitakaKuma/SUMI-OpenCT/scripts/hf_commit.py
-
curl -L -o hf_commit.py https://huggingface.co/datasets/MitakaKuma/SUMI-OpenCT/resolve/main/scripts/hf_commit.py
1.81 kB
| """Secret-safe Hugging Face commit helpers shared by CT collectors.""" | |
| from __future__ import annotations | |
| import time | |
| from huggingface_hub.errors import HfHubHTTPError | |
| def create_commit_with_rate_limit_retry( | |
| api, | |
| *, | |
| max_wait_seconds: float, | |
| sleep_interval_seconds: float = 30.0, | |
| sleep_fn=None, | |
| **commit_kwargs, | |
| ): | |
| """Retry only HTTP 429 commit failures, without printing credentials.""" | |
| if max_wait_seconds < 0: | |
| raise ValueError("max_wait_seconds must be non-negative") | |
| if sleep_interval_seconds <= 0: | |
| raise ValueError("sleep_interval_seconds must be positive") | |
| if sleep_fn is None: | |
| sleep_fn = time.sleep | |
| waited = 0.0 | |
| while True: | |
| try: | |
| return api.create_commit(**commit_kwargs) | |
| except HfHubHTTPError as exc: | |
| response = getattr(exc, "response", None) | |
| if getattr(response, "status_code", None) != 429 or waited >= max_wait_seconds: | |
| raise | |
| retry_after_value = getattr(response, "headers", {}).get("retry-after", "60") | |
| try: | |
| retry_after = max(1.0, float(retry_after_value)) | |
| except (TypeError, ValueError): | |
| retry_after = 60.0 | |
| delay = min(retry_after, max_wait_seconds - waited) | |
| if delay <= 0: | |
| raise | |
| print( | |
| "Hugging Face commit rate limit reached; " | |
| f"retrying in {delay:.0f}s (waited {waited:.0f}s, " | |
| f"limit {max_wait_seconds:.0f}s).", | |
| flush=True, | |
| ) | |
| remaining = delay | |
| while remaining > 0: | |
| chunk = min(sleep_interval_seconds, remaining) | |
| sleep_fn(chunk) | |
| waited += chunk | |
| remaining -= chunk | |