@@ -10501,6 +10501,27 @@ def sanitize_string(input_string):
1050110501 sanitized_string = re.sub( r'[^\w\d\.\-_]', '', input_string)
1050210502 return sanitized_string
1050310503
10504+ def resolve_huggingface_xet_url(input_url):
10505+ if "https://huggingface.co/" not in input_url or "/resolve/" not in input_url:
10506+ return input_url
10507+ try:
10508+ req = urllib.request.Request(input_url, headers={'User-Agent': 'Mozilla/5.0'}, method="HEAD")
10509+ with urllib.request.urlopen(req, timeout=10) as response:
10510+ resolved_url = response.geturl()
10511+ except Exception:
10512+ try:
10513+ req = urllib.request.Request(input_url, headers={'User-Agent': 'Mozilla/5.0'})
10514+ with urllib.request.urlopen(req, timeout=10) as response:
10515+ resolved_url = response.geturl()
10516+ except Exception as e:
10517+ print(f"Could not pre-resolve Hugging Face URL, using original URL: {e}")
10518+ return input_url
10519+ # resolved_host = urllib.parse.urlparse(resolved_url).netloc.lower()
10520+ if ("xet-bridge" in resolved_url) and resolved_url != input_url:
10521+ print(f"Resolved Hugging Face xet URL to {resolved_url}", flush=True)
10522+ return resolved_url
10523+ return input_url
10524+
1050410525def downloader_internal(input_url, output_filename, capture_output, min_file_size=64): # 64 bytes required by default
1050510526 download_dir_path = args.downloaddir
1050610527 if "https://huggingface.co/" in input_url and "/blob/main/" in input_url:
@@ -10535,6 +10556,7 @@ def downloader_internal(input_url, output_filename, capture_output, min_file_siz
1053510556 dl_success = False
1053610557 out_dir = os.path.dirname(os.path.abspath(output_filename)) or os.getcwd()
1053710558 out_name = os.path.basename(output_filename)
10559+ download_url = resolve_huggingface_xet_url(input_url)
1053810560 aria2_candidates = []
1053910561 if os.name == 'nt':
1054010562 basepath = os.path.abspath(os.path.dirname(__file__))
@@ -10548,7 +10570,7 @@ def downloader_internal(input_url, output_filename, capture_output, min_file_siz
1054810570 "--summary-interval=10", "--console-log-level=error", "--log-level=error",
1054910571 "--download-result=default", "--continue=true", "--allow-overwrite=true",
1055010572 "--file-allocation=none", "--max-tries=3", "--retry-wait=5",
10551- "-d", out_dir, "-o", out_name, input_url
10573+ "-d", out_dir, "-o", out_name, download_url
1055210574 ]
1055310575 for aria2_exe, aria2_name in aria2_candidates:
1055410576 if dl_success:
@@ -10562,15 +10584,15 @@ def downloader_internal(input_url, output_filename, capture_output, min_file_siz
1056210584
1056310585 try:
1056410586 if not dl_success and shutil.which("curl") is not None:
10565- rc = subprocess.run(["curl", "-fLo", output_filename, input_url ],
10587+ rc = subprocess.run(["curl", "-fLo", output_filename, download_url ],
1056610588 capture_output=capture_output, text=True, check=True, encoding="utf-8")
1056710589 dl_success = (rc.returncode == 0 and os.path.exists(output_filename) and os.path.getsize(output_filename) > min_file_size)
1056810590 except subprocess.CalledProcessError as e:
1056910591 print(f"curl failed: {e}")
1057010592
1057110593 try:
1057210594 if not dl_success and shutil.which("wget") is not None:
10573- rc = subprocess.run(["wget", "-O", output_filename, input_url ],
10595+ rc = subprocess.run(["wget", "-O", output_filename, download_url ],
1057410596 capture_output=capture_output, text=True, check=True, encoding="utf-8")
1057510597 dl_success = (rc.returncode == 0 and os.path.exists(output_filename) and os.path.getsize(output_filename) > min_file_size)
1057610598 except subprocess.CalledProcessError as e:
0 commit comments