Download tools/oxt_hf_network.py from Alan0928/IDEALFT_OXT: direct link, hf CLI and curl.
- Browser
- Download file 3.68 kB
-
https://huggingface.co/datasets/Alan0928/IDEALFT_OXT/resolve/main/tools/oxt_hf_network.py
- Command line
-
hf download hf://datasets/Alan0928/IDEALFT_OXT/tools/oxt_hf_network.py
-
curl -L -o oxt_hf_network.py https://huggingface.co/datasets/Alan0928/IDEALFT_OXT/resolve/main/tools/oxt_hf_network.py
3.68 kB
| """Process-local HTTP settings and bounded, credential-safe Hub retries. | |
| Importing this module does not import Hugging Face or HTTPX. The caller must | |
| isolate its Hub environment before calling ``configure_http``. No credentials, | |
| request bodies, exception messages, or client settings are written to disk. | |
| """ | |
| import time | |
| HTTP_SETTINGS = { | |
| "connect_timeout_seconds": 20.0, | |
| "read_timeout_seconds": 60.0, | |
| "write_timeout_seconds": 120.0, | |
| "pool_timeout_seconds": 60.0, | |
| } | |
| RETRY_HTTP_STATUSES = frozenset((408, 429, 500, 502, 503, 504)) | |
| def configure_http(): | |
| """Keep the SDK's normal hooks and change only this process's client.""" | |
| import httpx | |
| from huggingface_hub import set_client_factory | |
| from huggingface_hub.utils._http import default_client_factory | |
| def factory(): | |
| client = default_client_factory() | |
| client.timeout = httpx.Timeout( | |
| connect=HTTP_SETTINGS["connect_timeout_seconds"], | |
| read=HTTP_SETTINGS["read_timeout_seconds"], | |
| write=HTTP_SETTINGS["write_timeout_seconds"], | |
| pool=HTTP_SETTINGS["pool_timeout_seconds"], | |
| ) | |
| return client | |
| set_client_factory(factory) | |
| return dict(HTTP_SETTINGS) | |
| def is_retryable(error): | |
| """Classify temporary network failures without exposing exception text.""" | |
| import httpx | |
| if isinstance(error, (httpx.TimeoutException, httpx.NetworkError, | |
| httpx.RemoteProtocolError)): | |
| return True | |
| response = getattr(error, "response", None) | |
| return getattr(response, "status_code", None) in RETRY_HTTP_STATUSES | |
| class NetworkRunner: | |
| """Retry an explicitly retry-safe operation while keeping verified data. | |
| Mutation retries require an idempotent API or caller-side reconciliation. | |
| In particular, do not pass a raw ``create_commit`` call to this runner. | |
| Labels must be fixed descriptions, never URLs, tokens, or exception text. | |
| """ | |
| def __init__(self, attempts=4, sleep=time.sleep): | |
| if isinstance(attempts, bool) or not isinstance(attempts, int) or attempts < 1: | |
| raise ValueError("attempts must be a positive integer") | |
| self.attempts = attempts | |
| self.sleep = sleep | |
| self.last_operation = None | |
| self.retries = [] | |
| def call(self, label, function): | |
| self.last_operation = label | |
| print("Network: %s." % label, flush=True) | |
| for attempt in range(1, self.attempts + 1): | |
| self.last_operation = label | |
| try: | |
| result = function() | |
| self.last_operation = label | |
| return result | |
| except Exception as error: | |
| self.last_operation = label | |
| if not is_retryable(error) or attempt == self.attempts: | |
| raise | |
| delay = min(8, 2 ** attempt) | |
| retry = { | |
| "operation": label, | |
| "failed_attempt": attempt, | |
| "next_attempt": attempt + 1, | |
| "wait_seconds": delay, | |
| "error_type": type(error).__name__, | |
| } | |
| status = getattr(getattr(error, "response", None), "status_code", None) | |
| if isinstance(status, int): | |
| retry["http_status"] = status | |
| self.retries.append(retry) | |
| status_label = " (HTTP %d)" % status if isinstance(status, int) else "" | |
| print("Network: %s: %s%s; retry %d/%d in %ds." % | |
| (label, type(error).__name__, status_label, | |
| attempt + 1, self.attempts, delay), flush=True) | |
| self.sleep(delay) | |