Xplainer / hf_compat.py
ChantalPellegrini's picture
-updates
e51fd50
Raw
History Blame Contribute Delete
858 Bytes
from urllib.parse import urljoin
def patch_transformers_relative_redirects():
"""Patch old transformers download code to handle relative HF redirects."""
try:
from transformers import file_utils as transformers_file_utils
except Exception:
return
original_head = transformers_file_utils.requests.head
if getattr(original_head, "_xplainer_relative_redirect_patch", False):
return
def patched_head(url, *args, **kwargs):
response = original_head(url, *args, **kwargs)
location = response.headers.get("Location")
if location and not location.startswith(("http://", "https://")):
response.headers["Location"] = urljoin(url, location)
return response
patched_head._xplainer_relative_redirect_patch = True
transformers_file_utils.requests.head = patched_head