From dd7e1933ff523974882e6a967aca45c90db930a6 Mon Sep 17 00:00:00 2001 From: sora <2075279110@qq.com> Date: Thu, 10 Sep 2026 10:57:16 +0000 Subject: [PATCH] HF endpoint auto-fallback: when huggingface.co is unreachable, probe mirrors (hf-mirror.com) and switch automatically with a one-line notice; error only if all fail Co-Authored-By: Claude --- evalharness/data/loader.py | 40 +++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/evalharness/data/loader.py b/evalharness/data/loader.py index 98dfa5d..dde919f 100644 --- a/evalharness/data/loader.py +++ b/evalharness/data/loader.py @@ -462,22 +462,36 @@ def _load_from_hub(spec: DatasetSpec) -> List[Dict[str, Any]]: return [dict(r) for r in ds] +_HF_MIRROR_FALLBACKS = ['https://hf-mirror.com'] + + def _probe_hub_reachable(spec: DatasetSpec) -> None: """Fail fast when the HF endpoint is unreachable -- otherwise the hub - client retries silently for minutes and looks like a hang.""" + client retries silently for minutes and looks like a hang. + + When the DEFAULT endpoint is down but a mirror answers, switch to it + automatically (one-line notice, same behavior as --hf-endpoint).""" base = _hf_base() - try: - req = urllib.request.Request(base, method='HEAD', - headers={'User-Agent': 'evalharness/0.1'}) - urllib.request.urlopen(req, timeout=8) - return - except Exception as e: - raise RuntimeError( - f'HuggingFace endpoint unreachable: {base}\n' - f' reason: {type(e).__name__}: {str(e)[:80]}\n' - f' dataset {spec.source!r} cannot download. Fix:\n' - f' evalharness eval run ... --hf-endpoint https://hf-mirror.com\n' - f' (or export HF_ENDPOINT=https://hf-mirror.com)') from e + err = None + for candidate in [base] + (_HF_MIRROR_FALLBACKS if base == 'https://huggingface.co' else []): + try: + req = urllib.request.Request(candidate, method='HEAD', + headers={'User-Agent': 'evalharness/0.1'}) + urllib.request.urlopen(req, timeout=8) + if candidate != base: + os.environ['HF_ENDPOINT'] = candidate + print(f'ยท {base} unreachable -- falling back to {candidate}', + flush=True) + return + except Exception as e: + err = e + raise RuntimeError( + f'HuggingFace endpoint unreachable: {base}' + + (f' (mirrors tried: {", ".join(_HF_MIRROR_FALLBACKS)})' + if base == 'https://huggingface.co' else '') + + f'\n reason: {type(err).__name__}: {str(err)[:80]}\n' + f' dataset {spec.source!r} cannot download. Fix:\n' + f' evalharness eval run ... --hf-endpoint ') from err def field_spec_to_record_fn(fields: FieldSpec) -> Callable[[Dict[str, Any]], Sample]: