11import logging
2+ import os
23import requests
34import socket
4- from typing import Any
5+ from urllib . parse import urlparse
56from pathlib import Path
67from typing import Optional
78
2324]
2425
2526
27+ def _resolve_hf_endpoint () -> tuple [str , int , bool ]:
28+ endpoint = os .environ .get ("HF_ENDPOINT" , "https://huggingface.co" )
29+ parsed = urlparse (endpoint )
30+ scheme = parsed .scheme or "https"
31+ host = parsed .netloc or parsed .path or "huggingface.co"
32+ if ":" in host :
33+ host_only , port_str = host .split (":" , 1 )
34+ try :
35+ port = int (port_str )
36+ except ValueError :
37+ port = 443 if scheme == "https" else 80
38+ host = host_only
39+ else :
40+ port = 443 if scheme == "https" else 80
41+ use_https = scheme == "https"
42+ return host , port , use_https
43+
44+
45+ def _quick_hf_reachability_check (repo_id : str , timeout_s : float = 3.0 ) -> bool :
46+ host , port , _ = _resolve_hf_endpoint ()
47+ try :
48+ socket .create_connection ((host , port ), timeout = timeout_s ).close ()
49+ return True
50+ except OSError :
51+ # Try an HTTP-level check which may respect proxies
52+ try :
53+ endpoint = os .environ .get ("HF_ENDPOINT" , "https://huggingface.co" )
54+ base = endpoint .rstrip ("/" )
55+ # We don't care about response code, only that we can reach the server fast
56+ url = f"{ base } /api/models/{ repo_id } "
57+ requests .get (url , timeout = timeout_s , allow_redirects = True )
58+ return True
59+ except Exception :
60+ return False
61+
62+
2663def download_metadata_only (
2764 repo_id : str ,
2865 cache_dir : Optional [str ] = None ,
@@ -35,6 +72,20 @@ def download_metadata_only(
3572 return local_path
3673
3774 try :
75+ # Quick pre-check to avoid hanging on unreachable networks
76+ if not local_files_only :
77+ ok = _quick_hf_reachability_check (repo_id )
78+ if not ok :
79+ logger .error (
80+ "Cannot reach Hugging Face endpoint before download (pre-check failed). "
81+ "This node likely has no egress or DNS to the Hub."
82+ )
83+ logger .error (
84+ "Please verify network connectivity, proxy settings, firewall rules, or set "
85+ "`local_files_only=True` / provide a local model path."
86+ )
87+ raise RuntimeError ("Hugging Face Hub not reachable from this node" )
88+
3889 path = snapshot_download (
3990 repo_id = repo_id ,
4091 cache_dir = cache_dir ,
0 commit comments