file_download.py 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936
  1. import copy
  2. import errno
  3. import os
  4. import re
  5. import shutil
  6. import stat
  7. import time
  8. import uuid
  9. import warnings
  10. from dataclasses import dataclass
  11. from pathlib import Path
  12. from typing import Any, BinaryIO, Literal, NoReturn, overload
  13. from urllib.parse import quote, urlparse
  14. import httpx
  15. from tqdm.auto import tqdm as base_tqdm
  16. from . import constants
  17. from ._local_folder import (
  18. _create_cachedir_tag,
  19. get_local_download_paths,
  20. read_download_metadata,
  21. write_download_metadata,
  22. )
  23. from .errors import (
  24. FileMetadataError,
  25. GatedRepoError,
  26. HfHubHTTPError,
  27. LocalEntryNotFoundError,
  28. RemoteEntryNotFoundError,
  29. RepositoryNotFoundError,
  30. RevisionNotFoundError,
  31. )
  32. from .utils import (
  33. OfflineModeIsEnabled,
  34. SoftTemporaryDirectory,
  35. WeakFileLock,
  36. XetFileData,
  37. build_hf_headers,
  38. hf_raise_for_status,
  39. logging,
  40. parse_xet_file_data_from_response,
  41. refresh_xet_connection_info,
  42. tqdm,
  43. validate_hf_hub_args,
  44. )
  45. from .utils._http import (
  46. _DEFAULT_RETRY_ON_EXCEPTIONS,
  47. _DEFAULT_RETRY_ON_STATUS_CODES,
  48. _adjust_range_header,
  49. _httpx_follow_relative_redirects_with_backoff,
  50. http_stream_backoff,
  51. )
  52. from .utils._runtime import is_xet_available
  53. from .utils.sha import sha_fileobj
  54. from .utils.tqdm import _get_progress_bar_context
  55. logger = logging.get_logger(__name__)
  56. # Return value when trying to load a file from cache but the file does not exist in the distant repo.
  57. _CACHED_NO_EXIST = object()
  58. _CACHED_NO_EXIST_T = Any
  59. # Regex to get filename from a "Content-Disposition" header for CDN-served files
  60. HEADER_FILENAME_PATTERN = re.compile(r'filename="(?P<filename>.*?)";')
  61. # Regex to check if the revision IS directly a commit_hash
  62. REGEX_COMMIT_HASH = re.compile(r"^[0-9a-f]{40}$")
  63. # Regex to check if the file etag IS a valid sha256
  64. REGEX_SHA256 = re.compile(r"^[0-9a-f]{64}$")
  65. _are_symlinks_supported_in_dir: dict[str, bool] = {}
  66. # Internal retry timeout for metadata fetch when no local file exists
  67. _ETAG_RETRY_TIMEOUT = 60
  68. def are_symlinks_supported(cache_dir: str | Path | None = None) -> bool:
  69. """Return whether the symlinks are supported on the machine.
  70. Since symlinks support can change depending on the mounted disk, we need to check
  71. on the precise cache folder. By default, the default HF cache directory is checked.
  72. Args:
  73. cache_dir (`str`, `Path`, *optional*):
  74. Path to the folder where cached files are stored.
  75. Returns: [bool] Whether symlinks are supported in the directory.
  76. """
  77. # Defaults to HF cache
  78. if cache_dir is None:
  79. cache_dir = constants.HF_HUB_CACHE
  80. cache_dir = str(Path(cache_dir).expanduser().resolve()) # make it unique
  81. # If symlinks are explicitly disabled by the user, always return False
  82. if constants.HF_HUB_DISABLE_SYMLINKS:
  83. return False
  84. # Check symlink compatibility only once (per cache directory) at first time use
  85. if cache_dir not in _are_symlinks_supported_in_dir:
  86. _are_symlinks_supported_in_dir[cache_dir] = True
  87. os.makedirs(cache_dir, exist_ok=True)
  88. with SoftTemporaryDirectory(dir=cache_dir) as tmpdir:
  89. src_path = Path(tmpdir) / "dummy_file_src"
  90. src_path.touch()
  91. dst_path = Path(tmpdir) / "dummy_file_dst"
  92. # Relative source path as in `_create_symlink``
  93. relative_src = os.path.relpath(src_path, start=os.path.dirname(dst_path))
  94. try:
  95. os.symlink(relative_src, dst_path)
  96. except OSError:
  97. # Likely running on Windows
  98. _are_symlinks_supported_in_dir[cache_dir] = False
  99. if not constants.HF_HUB_DISABLE_SYMLINKS_WARNING:
  100. message = (
  101. "`huggingface_hub` cache-system uses symlinks by default to"
  102. " efficiently store duplicated files but your machine does not"
  103. f" support them in {cache_dir}. Caching files will still work"
  104. " but in a degraded version that might require more space on"
  105. " your disk. This warning can be disabled by setting the"
  106. " `HF_HUB_DISABLE_SYMLINKS_WARNING` environment variable. For"
  107. " more details, see"
  108. " https://huggingface.co/docs/huggingface_hub/how-to-cache#limitations."
  109. )
  110. if os.name == "nt":
  111. message += (
  112. "\nTo support symlinks on Windows, you either need to"
  113. " activate Developer Mode or to run Python as an"
  114. " administrator. In order to activate developer mode,"
  115. " see this article:"
  116. " https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development"
  117. )
  118. warnings.warn(message)
  119. return _are_symlinks_supported_in_dir[cache_dir]
  120. @dataclass(frozen=True)
  121. class HfFileMetadata:
  122. """Data structure containing information about a file versioned on the Hub.
  123. Returned by [`get_hf_file_metadata`] based on a URL.
  124. Args:
  125. commit_hash (`str`, *optional*):
  126. The commit_hash related to the file.
  127. etag (`str`, *optional*):
  128. Etag of the file on the server.
  129. location (`str`):
  130. Location where to download the file. Can be a Hub url or not (CDN).
  131. size (`size`):
  132. Size of the file. In case of an LFS file, contains the size of the actual
  133. LFS file, not the pointer.
  134. xet_file_data (`XetFileData`, *optional*):
  135. Xet information for the file. This is only set if the file is stored using Xet storage.
  136. """
  137. commit_hash: str | None
  138. etag: str | None
  139. location: str
  140. size: int | None
  141. xet_file_data: XetFileData | None
  142. @dataclass
  143. class DryRunFileInfo:
  144. """Information returned when performing a dry run of a file download.
  145. Returned by [`hf_hub_download`] when `dry_run=True`.
  146. Args:
  147. commit_hash (`str`):
  148. The commit_hash related to the file.
  149. file_size (`int`):
  150. Size of the file. In case of an LFS file, contains the size of the actual LFS file, not the pointer.
  151. filename (`str`):
  152. Name of the file in the repo.
  153. is_cached (`bool`):
  154. Whether the file is already cached locally.
  155. will_download (`bool`):
  156. Whether the file will be downloaded if `hf_hub_download` is called with `dry_run=False`.
  157. In practice, will_download is `True` if the file is not cached or if `force_download=True`.
  158. """
  159. commit_hash: str
  160. file_size: int
  161. filename: str
  162. local_path: str
  163. is_cached: bool
  164. will_download: bool
  165. @validate_hf_hub_args
  166. def hf_hub_url(
  167. repo_id: str,
  168. filename: str,
  169. *,
  170. subfolder: str | None = None,
  171. repo_type: str | None = None,
  172. revision: str | None = None,
  173. endpoint: str | None = None,
  174. ) -> str:
  175. """Construct the URL of a file from the given information.
  176. The resolved address can either be a huggingface.co-hosted url, or a link to
  177. Cloudfront (a Content Delivery Network, or CDN) for large files which are
  178. more than a few MBs.
  179. Args:
  180. repo_id (`str`):
  181. A namespace (user or an organization) name and a repo name separated
  182. by a `/`.
  183. filename (`str`):
  184. The name of the file in the repo.
  185. subfolder (`str`, *optional*):
  186. An optional value corresponding to a folder inside the repo.
  187. repo_type (`str`, *optional*):
  188. Set to `"dataset"`, `"space"` or `"kernel"` if downloading from a dataset, space or kernel repo,
  189. `None` or `"model"` if downloading from a model. Default is `None`.
  190. revision (`str`, *optional*):
  191. An optional Git revision id which can be a branch name, a tag, or a
  192. commit hash.
  193. Example:
  194. ```python
  195. >>> from huggingface_hub import hf_hub_url
  196. >>> hf_hub_url(
  197. ... repo_id="julien-c/EsperBERTo-small", filename="pytorch_model.bin"
  198. ... )
  199. 'https://huggingface.co/julien-c/EsperBERTo-small/resolve/main/pytorch_model.bin'
  200. ```
  201. > [!TIP]
  202. > Notes:
  203. >
  204. > Cloudfront is replicated over the globe so downloads are way faster for
  205. > the end user (and it also lowers our bandwidth costs).
  206. >
  207. > Cloudfront aggressively caches files by default (default TTL is 24
  208. > hours), however this is not an issue here because we implement a
  209. > git-based versioning system on huggingface.co, which means that we store
  210. > the files on S3/Cloudfront in a content-addressable way (i.e., the file
  211. > name is its hash). Using content-addressable filenames means cache can't
  212. > ever be stale.
  213. >
  214. > In terms of client-side caching from this library, we base our caching
  215. > on the objects' entity tag (`ETag`), which is an identifier of a
  216. > specific version of a resource [1]_. An object's ETag is: its git-sha1
  217. > if stored in git, or its sha256 if stored in git-lfs.
  218. References:
  219. - [1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
  220. """
  221. if subfolder == "":
  222. subfolder = None
  223. if subfolder is not None:
  224. filename = f"{subfolder}/{filename}"
  225. if repo_type not in constants.REPO_TYPES_WITH_KERNEL:
  226. raise ValueError("Invalid repo type")
  227. if repo_type in constants.REPO_TYPES_URL_PREFIXES:
  228. repo_id = constants.REPO_TYPES_URL_PREFIXES[repo_type] + repo_id
  229. if revision is None:
  230. revision = constants.DEFAULT_REVISION
  231. url = constants.HUGGINGFACE_CO_URL_TEMPLATE.format(
  232. repo_id=repo_id, revision=quote(revision, safe=""), filename=quote(filename)
  233. )
  234. # Update endpoint if provided
  235. if endpoint is not None and url.startswith(constants.ENDPOINT):
  236. url = endpoint + url[len(constants.ENDPOINT) :]
  237. return url
  238. def _get_file_length_from_http_response(response: httpx.Response) -> int | None:
  239. """
  240. Get the length of the file from the HTTP response headers.
  241. This function extracts the file size from the HTTP response headers, either from the
  242. `Content-Range` or `Content-Length` header, if available (in that order).
  243. Args:
  244. response (`httpx.Response`):
  245. The HTTP response object.
  246. Returns:
  247. `int` or `None`: The length of the file in bytes, or None if not available.
  248. """
  249. # If HTTP response contains compressed body (e.g. gzip), the `Content-Length` header will
  250. # contain the length of the compressed body, not the uncompressed file size.
  251. # And at the start of transmission there's no way to know the uncompressed file size for gzip,
  252. # thus we return None in that case.
  253. content_encoding = response.headers.get("Content-Encoding", "identity").lower()
  254. if content_encoding != "identity":
  255. # gzip/br/deflate/zstd etc
  256. return None
  257. content_range = response.headers.get("Content-Range")
  258. if content_range is not None:
  259. return int(content_range.rsplit("/")[-1])
  260. content_length = response.headers.get("Content-Length")
  261. if content_length is not None:
  262. return int(content_length)
  263. return None
  264. @validate_hf_hub_args
  265. def http_get(
  266. url: str,
  267. temp_file: BinaryIO,
  268. *,
  269. resume_size: int = 0,
  270. headers: dict[str, Any] | None = None,
  271. expected_size: int | None = None,
  272. displayed_filename: str | None = None,
  273. tqdm_class: type[base_tqdm] | None = None,
  274. _nb_retries: int = 5,
  275. _tqdm_bar: tqdm | None = None,
  276. ) -> None:
  277. """
  278. Download a remote file. Do not gobble up errors, and will return errors tailored to the Hugging Face Hub.
  279. If ConnectionError (SSLError) or ReadTimeout happen while streaming data from the server, it is most likely a
  280. transient error (network outage?). We log a warning message and try to resume the download a few times before
  281. giving up. The method gives up after 5 attempts if no new data has being received from the server.
  282. Args:
  283. url (`str`):
  284. The URL of the file to download.
  285. temp_file (`BinaryIO`):
  286. The file-like object where to save the file.
  287. resume_size (`int`, *optional*):
  288. The number of bytes already downloaded. If set to 0 (default), the whole file is download. If set to a
  289. positive number, the download will resume at the given position.
  290. headers (`dict`, *optional*):
  291. Dictionary of HTTP Headers to send with the request.
  292. expected_size (`int`, *optional*):
  293. The expected size of the file to download. If set, the download will raise an error if the size of the
  294. received content is different from the expected one.
  295. displayed_filename (`str`, *optional*):
  296. The filename of the file that is being downloaded. Value is used only to display a nice progress bar. If
  297. not set, the filename is guessed from the URL or the `Content-Disposition` header.
  298. """
  299. if expected_size is not None and resume_size == expected_size:
  300. # If the file is already fully downloaded, we don't need to download it again.
  301. return
  302. initial_headers = headers
  303. headers = copy.deepcopy(headers) or {}
  304. if resume_size > 0:
  305. headers["Range"] = _adjust_range_header(headers.get("Range"), resume_size)
  306. elif expected_size and expected_size > constants.MAX_HTTP_DOWNLOAD_SIZE:
  307. # Any files over 50GB will not be available through basic http requests.
  308. raise ValueError(
  309. "The file is too large to be downloaded using the regular download method. "
  310. " Install `hf_xet` with `pip install hf_xet` for xet-powered downloads."
  311. )
  312. with http_stream_backoff(
  313. method="GET",
  314. url=url,
  315. headers=headers,
  316. timeout=constants.HF_HUB_DOWNLOAD_TIMEOUT,
  317. retry_on_exceptions=(),
  318. retry_on_status_codes=(429,),
  319. ) as response:
  320. hf_raise_for_status(response)
  321. # If we requested a Range but got 200 back, the server ignored our Range header
  322. # (e.g. CloudFront with Accept-Encoding: gzip). Reset file to avoid corruption.
  323. if resume_size > 0 and response.status_code == 200:
  324. temp_file.seek(0)
  325. temp_file.truncate()
  326. resume_size = 0
  327. total: int | None = _get_file_length_from_http_response(response)
  328. if displayed_filename is None:
  329. displayed_filename = url
  330. content_disposition = response.headers.get("Content-Disposition")
  331. if content_disposition is not None:
  332. match = HEADER_FILENAME_PATTERN.search(content_disposition)
  333. if match is not None:
  334. # Means file is on CDN
  335. displayed_filename = match.groupdict()["filename"]
  336. # Truncate filename if too long to display
  337. if len(displayed_filename) > 40:
  338. displayed_filename = f"(…){displayed_filename[-40:]}"
  339. consistency_error_message = (
  340. f"Consistency check failed: file should be of size {expected_size} but has size"
  341. f" {{actual_size}} ({displayed_filename}).\nThis is usually due to network issues while downloading the file."
  342. " Please retry with `force_download=True`."
  343. )
  344. progress_cm = _get_progress_bar_context(
  345. desc=displayed_filename,
  346. log_level=logger.getEffectiveLevel(),
  347. total=total,
  348. initial=resume_size,
  349. name="huggingface_hub.http_get",
  350. tqdm_class=tqdm_class,
  351. _tqdm_bar=_tqdm_bar,
  352. )
  353. with progress_cm as progress:
  354. new_resume_size = resume_size
  355. try:
  356. for chunk in response.iter_bytes(chunk_size=constants.DOWNLOAD_CHUNK_SIZE):
  357. if chunk: # filter out keep-alive new chunks
  358. progress.update(len(chunk))
  359. temp_file.write(chunk)
  360. new_resume_size += len(chunk)
  361. # Some data has been downloaded from the server so we reset the number of retries.
  362. _nb_retries = 5
  363. except (httpx.ConnectError, httpx.TimeoutException) as e:
  364. # If ConnectionError (SSLError) or ReadTimeout happen while streaming data from the server, it is most likely
  365. # a transient error (network outage?). We log a warning message and try to resume the download a few times
  366. # before giving up. Tre retry mechanism is basic but should be enough in most cases.
  367. if _nb_retries <= 0:
  368. logger.warning("Error while downloading from %s: %s\nMax retries exceeded.", url, str(e))
  369. raise
  370. logger.warning("Error while downloading from %s: %s\nTrying to resume download...", url, str(e))
  371. time.sleep(1)
  372. return http_get(
  373. url=url,
  374. temp_file=temp_file,
  375. resume_size=new_resume_size,
  376. headers=initial_headers,
  377. expected_size=expected_size,
  378. tqdm_class=tqdm_class,
  379. _nb_retries=_nb_retries - 1,
  380. _tqdm_bar=_tqdm_bar,
  381. )
  382. if expected_size is not None and expected_size != temp_file.tell():
  383. raise OSError(
  384. consistency_error_message.format(
  385. actual_size=temp_file.tell(),
  386. )
  387. )
  388. def xet_get(
  389. *,
  390. incomplete_path: Path,
  391. xet_file_data: XetFileData,
  392. headers: dict[str, str],
  393. expected_size: int | None = None,
  394. displayed_filename: str | None = None,
  395. tqdm_class: type[base_tqdm] | None = None,
  396. _tqdm_bar: tqdm | None = None,
  397. ) -> None:
  398. """
  399. Download a file using Xet storage service.
  400. Args:
  401. incomplete_path (`Path`):
  402. The path to the file to download.
  403. xet_file_data (`XetFileData`):
  404. The file metadata needed to make the request to the xet storage service.
  405. headers (`dict[str, str]`):
  406. The headers to send to the xet storage service.
  407. expected_size (`int`, *optional*):
  408. The expected size of the file to download. If set, the download will raise an error if the size of the
  409. received content is different from the expected one.
  410. displayed_filename (`str`, *optional*):
  411. The filename of the file that is being downloaded. Value is used only to display a nice progress bar. If
  412. not set, the filename is guessed from the URL or the `Content-Disposition` header.
  413. **How it works:**
  414. The file download system uses Xet storage, which is a content-addressable storage system that breaks files into chunks
  415. for efficient storage and transfer.
  416. `hf_xet.download_files` manages downloading files by:
  417. - Taking a list of files to download (each with its unique content hash)
  418. - Connecting to a storage server (CAS server) that knows how files are chunked
  419. - Using authentication to ensure secure access
  420. - Providing progress updates during download
  421. Authentication works by regularly refreshing access tokens through `refresh_xet_connection_info` to maintain a valid
  422. connection to the storage server.
  423. The download process works like this:
  424. 1. Create a local cache folder at `~/.cache/huggingface/xet/chunk-cache` to store reusable file chunks
  425. 2. Download files in parallel:
  426. 2.1. Prepare to write the file to disk
  427. 2.2. Ask the server "how is this file split into chunks?" using the file's unique hash
  428. The server responds with:
  429. - Which chunks make up the complete file
  430. - Where each chunk can be downloaded from
  431. 2.3. For each needed chunk:
  432. - Checks if we already have it in our local cache
  433. - If not, download it from cloud storage (S3)
  434. - Save it to cache for future use
  435. - Assemble the chunks in order to recreate the original file
  436. """
  437. try:
  438. from hf_xet import PyXetDownloadInfo, download_files # type: ignore[no-redef]
  439. except ImportError:
  440. raise ValueError(
  441. "To use optimized download using Xet storage, you need to install the hf_xet package. "
  442. 'Try `pip install "huggingface_hub[hf_xet]"` or `pip install hf_xet`.'
  443. )
  444. connection_info = refresh_xet_connection_info(file_data=xet_file_data, headers=headers)
  445. def token_refresher() -> tuple[str, int]:
  446. connection_info = refresh_xet_connection_info(file_data=xet_file_data, headers=headers)
  447. if connection_info is None:
  448. raise ValueError("Failed to refresh token using xet metadata.")
  449. return connection_info.access_token, connection_info.expiration_unix_epoch
  450. xet_download_info = [
  451. PyXetDownloadInfo(
  452. destination_path=str(incomplete_path.absolute()), hash=xet_file_data.file_hash, file_size=expected_size
  453. )
  454. ]
  455. if not displayed_filename:
  456. displayed_filename = incomplete_path.name
  457. # Truncate filename if too long to display
  458. if len(displayed_filename) > 40:
  459. displayed_filename = f"{displayed_filename[:40]}(…)"
  460. progress_cm = _get_progress_bar_context(
  461. desc=displayed_filename,
  462. log_level=logger.getEffectiveLevel(),
  463. total=expected_size,
  464. initial=0,
  465. name="huggingface_hub.xet_get",
  466. tqdm_class=tqdm_class,
  467. _tqdm_bar=_tqdm_bar,
  468. )
  469. xet_headers = headers.copy()
  470. xet_headers.pop("authorization", None)
  471. with progress_cm as progress:
  472. def progress_updater(progress_bytes: float):
  473. progress.update(progress_bytes)
  474. download_files(
  475. xet_download_info,
  476. endpoint=connection_info.endpoint,
  477. token_info=(connection_info.access_token, connection_info.expiration_unix_epoch),
  478. token_refresher=token_refresher,
  479. progress_updater=[progress_updater],
  480. request_headers=xet_headers,
  481. )
  482. def _normalize_etag(etag: str | None) -> str | None:
  483. """Normalize ETag HTTP header, so it can be used to create nice filepaths.
  484. The HTTP spec allows two forms of ETag:
  485. ETag: W/"<etag_value>"
  486. ETag: "<etag_value>"
  487. For now, we only expect the second form from the server, but we want to be future-proof so we support both. For
  488. more context, see `TestNormalizeEtag` tests and https://github.com/huggingface/huggingface_hub/pull/1428.
  489. Args:
  490. etag (`str`, *optional*): HTTP header
  491. Returns:
  492. `str` or `None`: string that can be used as a nice directory name.
  493. Returns `None` if input is None.
  494. """
  495. if etag is None:
  496. return None
  497. return etag.lstrip("W/").strip('"')
  498. def _create_relative_symlink(src: str, dst: str, new_blob: bool = False) -> None:
  499. """Alias method used in `transformers` conversion script."""
  500. return _create_symlink(src=src, dst=dst, new_blob=new_blob)
  501. def _create_symlink(src: str, dst: str, new_blob: bool = False) -> None:
  502. """Create a symbolic link named dst pointing to src.
  503. By default, it will try to create a symlink using a relative path. Relative paths have 2 advantages:
  504. - If the cache_folder is moved (example: back-up on a shared drive), relative paths within the cache folder will
  505. not break.
  506. - Relative paths seems to be better handled on Windows. Issue was reported 3 times in less than a week when
  507. changing from relative to absolute paths. See https://github.com/huggingface/huggingface_hub/issues/1398,
  508. https://github.com/huggingface/diffusers/issues/2729 and https://github.com/huggingface/transformers/pull/22228.
  509. NOTE: The issue with absolute paths doesn't happen on admin mode.
  510. When creating a symlink from the cache to a local folder, it is possible that a relative path cannot be created.
  511. This happens when paths are not on the same volume. In that case, we use absolute paths.
  512. The result layout looks something like
  513. └── [ 128] snapshots
  514. ├── [ 128] 2439f60ef33a0d46d85da5001d52aeda5b00ce9f
  515. │ ├── [ 52] README.md -> ../../../blobs/d7edf6bd2a681fb0175f7735299831ee1b22b812
  516. │ └── [ 76] pytorch_model.bin -> ../../../blobs/403450e234d65943a7dcf7e05a771ce3c92faa84dd07db4ac20f592037a1e4bd
  517. If symlinks cannot be created on this platform (most likely to be Windows), the workaround is to avoid symlinks by
  518. having the actual file in `dst`. If it is a new file (`new_blob=True`), we move it to `dst`. If it is not a new file
  519. (`new_blob=False`), we don't know if the blob file is already referenced elsewhere. To avoid breaking existing
  520. cache, the file is duplicated on the disk.
  521. In case symlinks are not supported, a warning message is displayed to the user once when loading `huggingface_hub`.
  522. The warning message can be disabled with the `DISABLE_SYMLINKS_WARNING` environment variable.
  523. """
  524. try:
  525. os.remove(dst)
  526. except OSError:
  527. pass
  528. abs_src = os.path.abspath(os.path.expanduser(src))
  529. abs_dst = os.path.abspath(os.path.expanduser(dst))
  530. abs_dst_folder = os.path.dirname(abs_dst)
  531. # Use relative_dst in priority
  532. try:
  533. relative_src = os.path.relpath(abs_src, abs_dst_folder)
  534. except ValueError:
  535. # Raised on Windows if src and dst are not on the same volume. This is the case when creating a symlink to a
  536. # local_dir instead of within the cache directory.
  537. # See https://docs.python.org/3/library/os.path.html#os.path.relpath
  538. relative_src = None
  539. try:
  540. commonpath = os.path.commonpath([abs_src, abs_dst])
  541. _support_symlinks = are_symlinks_supported(commonpath)
  542. except ValueError:
  543. # Raised if src and dst are not on the same volume. Symlinks will still work on Linux/Macos.
  544. # See https://docs.python.org/3/library/os.path.html#os.path.commonpath
  545. _support_symlinks = os.name != "nt" and not constants.HF_HUB_DISABLE_SYMLINKS
  546. except PermissionError:
  547. # Permission error means src and dst are not in the same volume (e.g. destination path has been provided
  548. # by the user via `local_dir`. Let's test symlink support there)
  549. _support_symlinks = are_symlinks_supported(abs_dst_folder)
  550. except OSError as e:
  551. # OS error (errno=30) means that the commonpath is readonly on Linux/MacOS.
  552. if e.errno == errno.EROFS:
  553. _support_symlinks = are_symlinks_supported(abs_dst_folder)
  554. else:
  555. raise
  556. # Symlinks are supported => let's create a symlink.
  557. if _support_symlinks:
  558. src_rel_or_abs = relative_src or abs_src
  559. logger.debug(f"Creating pointer from {src_rel_or_abs} to {abs_dst}")
  560. try:
  561. os.symlink(src_rel_or_abs, abs_dst)
  562. return
  563. except FileExistsError:
  564. if os.path.islink(abs_dst) and os.path.realpath(abs_dst) == os.path.realpath(abs_src):
  565. # `abs_dst` already exists and is a symlink to the `abs_src` blob. It is most likely that the file has
  566. # been cached twice concurrently (exactly between `os.remove` and `os.symlink`). Do nothing.
  567. return
  568. else:
  569. # Very unlikely to happen. Means a file `dst` has been created exactly between `os.remove` and
  570. # `os.symlink` and is not a symlink to the `abs_src` blob file. Raise exception.
  571. raise
  572. except PermissionError:
  573. # Permission error means src and dst are not in the same volume (e.g. download to local dir) and symlink
  574. # is supported on both volumes but not between them. Let's just make a hard copy in that case.
  575. pass
  576. # Symlinks are not supported => let's move or copy the file.
  577. if new_blob:
  578. logger.debug(f"Symlink not supported. Moving file from {abs_src} to {abs_dst}")
  579. shutil.move(abs_src, abs_dst, copy_function=_copy_no_matter_what)
  580. else:
  581. logger.debug(f"Symlink not supported. Copying file from {abs_src} to {abs_dst}")
  582. shutil.copyfile(abs_src, abs_dst)
  583. def _cache_commit_hash_for_specific_revision(storage_folder: str, revision: str, commit_hash: str) -> None:
  584. """Cache reference between a revision (tag, branch or truncated commit hash) and the corresponding commit hash.
  585. Does nothing if `revision` is already a proper `commit_hash` or reference is already cached.
  586. """
  587. if revision != commit_hash:
  588. ref_path = Path(storage_folder) / "refs" / revision
  589. ref_path.parent.mkdir(parents=True, exist_ok=True)
  590. if not ref_path.exists() or commit_hash != ref_path.read_text():
  591. # Update ref only if has been updated. Could cause useless error in case
  592. # repo is already cached and user doesn't have write access to cache folder.
  593. # See https://github.com/huggingface/huggingface_hub/issues/1216.
  594. ref_path.write_text(commit_hash)
  595. @validate_hf_hub_args
  596. def repo_folder_name(*, repo_id: str, repo_type: str) -> str:
  597. """Return a serialized version of a hf.co repo name and type, safe for disk storage
  598. as a single non-nested folder.
  599. Example: models--julien-c--EsperBERTo-small
  600. """
  601. # remove all `/` occurrences to correctly convert repo to directory name
  602. parts = [f"{repo_type}s", *repo_id.split("/")]
  603. return constants.REPO_ID_SEPARATOR.join(parts)
  604. def _check_disk_space(expected_size: int, target_dir: str | Path) -> None:
  605. """Check disk usage and log a warning if there is not enough disk space to download the file.
  606. Args:
  607. expected_size (`int`):
  608. The expected size of the file in bytes.
  609. target_dir (`str`):
  610. The directory where the file will be stored after downloading.
  611. """
  612. target_dir = Path(target_dir) # format as `Path`
  613. for path in [target_dir] + list(target_dir.parents): # first check target_dir, then each parents one by one
  614. try:
  615. target_dir_free = shutil.disk_usage(path).free
  616. if target_dir_free < expected_size:
  617. warnings.warn(
  618. "Not enough free disk space to download the file. "
  619. f"The expected file size is: {expected_size / 1e6:.2f} MB. "
  620. f"The target location {target_dir} only has {target_dir_free / 1e6:.2f} MB free disk space."
  621. )
  622. return
  623. except OSError: # raise on anything: file does not exist or space disk cannot be checked
  624. pass
  625. @overload
  626. def hf_hub_download(
  627. repo_id: str,
  628. filename: str,
  629. *,
  630. subfolder: str | None = None,
  631. repo_type: str | None = None,
  632. revision: str | None = None,
  633. library_name: str | None = None,
  634. library_version: str | None = None,
  635. cache_dir: str | Path | None = None,
  636. local_dir: str | Path | None = None,
  637. user_agent: dict | str | None = None,
  638. force_download: bool = False,
  639. etag_timeout: float = constants.DEFAULT_ETAG_TIMEOUT,
  640. token: bool | str | None = None,
  641. local_files_only: bool = False,
  642. headers: dict[str, str] | None = None,
  643. endpoint: str | None = None,
  644. tqdm_class: type[base_tqdm] | None = None,
  645. dry_run: Literal[False] = False,
  646. ) -> str: ...
  647. @overload
  648. def hf_hub_download(
  649. repo_id: str,
  650. filename: str,
  651. *,
  652. subfolder: str | None = None,
  653. repo_type: str | None = None,
  654. revision: str | None = None,
  655. library_name: str | None = None,
  656. library_version: str | None = None,
  657. cache_dir: str | Path | None = None,
  658. local_dir: str | Path | None = None,
  659. user_agent: dict | str | None = None,
  660. force_download: bool = False,
  661. etag_timeout: float = constants.DEFAULT_ETAG_TIMEOUT,
  662. token: bool | str | None = None,
  663. local_files_only: bool = False,
  664. headers: dict[str, str] | None = None,
  665. endpoint: str | None = None,
  666. tqdm_class: type[base_tqdm] | None = None,
  667. dry_run: Literal[True] = True,
  668. ) -> DryRunFileInfo: ...
  669. @overload
  670. def hf_hub_download(
  671. repo_id: str,
  672. filename: str,
  673. *,
  674. subfolder: str | None = None,
  675. repo_type: str | None = None,
  676. revision: str | None = None,
  677. library_name: str | None = None,
  678. library_version: str | None = None,
  679. cache_dir: str | Path | None = None,
  680. local_dir: str | Path | None = None,
  681. user_agent: dict | str | None = None,
  682. force_download: bool = False,
  683. etag_timeout: float = constants.DEFAULT_ETAG_TIMEOUT,
  684. token: bool | str | None = None,
  685. local_files_only: bool = False,
  686. headers: dict[str, str] | None = None,
  687. endpoint: str | None = None,
  688. tqdm_class: type[base_tqdm] | None = None,
  689. dry_run: bool = False,
  690. ) -> str | DryRunFileInfo: ...
  691. @validate_hf_hub_args
  692. def hf_hub_download(
  693. repo_id: str,
  694. filename: str,
  695. *,
  696. subfolder: str | None = None,
  697. repo_type: str | None = None,
  698. revision: str | None = None,
  699. library_name: str | None = None,
  700. library_version: str | None = None,
  701. cache_dir: str | Path | None = None,
  702. local_dir: str | Path | None = None,
  703. user_agent: dict | str | None = None,
  704. force_download: bool = False,
  705. etag_timeout: float = constants.DEFAULT_ETAG_TIMEOUT,
  706. token: bool | str | None = None,
  707. local_files_only: bool = False,
  708. headers: dict[str, str] | None = None,
  709. endpoint: str | None = None,
  710. tqdm_class: type[base_tqdm] | None = None,
  711. dry_run: bool = False,
  712. ) -> str | DryRunFileInfo:
  713. """Download a given file if it's not already present in the local cache.
  714. The new cache file layout looks like this:
  715. - The cache directory contains one subfolder per repo_id (namespaced by repo type)
  716. - inside each repo folder:
  717. - refs is a list of the latest known revision => commit_hash pairs
  718. - blobs contains the actual file blobs (identified by their git-sha or sha256, depending on
  719. whether they're LFS files or not)
  720. - snapshots contains one subfolder per commit, each "commit" contains the subset of the files
  721. that have been resolved at that particular commit. Each filename is a symlink to the blob
  722. at that particular commit.
  723. ```
  724. [ 96] .
  725. └── [ 160] models--julien-c--EsperBERTo-small
  726. ├── [ 160] blobs
  727. │ ├── [321M] 403450e234d65943a7dcf7e05a771ce3c92faa84dd07db4ac20f592037a1e4bd
  728. │ ├── [ 398] 7cb18dc9bafbfcf74629a4b760af1b160957a83e
  729. │ └── [1.4K] d7edf6bd2a681fb0175f7735299831ee1b22b812
  730. ├── [ 96] refs
  731. │ └── [ 40] main
  732. └── [ 128] snapshots
  733. ├── [ 128] 2439f60ef33a0d46d85da5001d52aeda5b00ce9f
  734. │ ├── [ 52] README.md -> ../../blobs/d7edf6bd2a681fb0175f7735299831ee1b22b812
  735. │ └── [ 76] pytorch_model.bin -> ../../blobs/403450e234d65943a7dcf7e05a771ce3c92faa84dd07db4ac20f592037a1e4bd
  736. └── [ 128] bbc77c8132af1cc5cf678da3f1ddf2de43606d48
  737. ├── [ 52] README.md -> ../../blobs/7cb18dc9bafbfcf74629a4b760af1b160957a83e
  738. └── [ 76] pytorch_model.bin -> ../../blobs/403450e234d65943a7dcf7e05a771ce3c92faa84dd07db4ac20f592037a1e4bd
  739. ```
  740. If `local_dir` is provided, the file structure from the repo will be replicated in this location. When using this
  741. option, the `cache_dir` will not be used and a `.cache/huggingface/` folder will be created at the root of `local_dir`
  742. to store some metadata related to the downloaded files. While this mechanism is not as robust as the main
  743. cache-system, it's optimized for regularly pulling the latest version of a repository.
  744. Args:
  745. repo_id (`str`):
  746. A user or an organization name and a repo name separated by a `/`.
  747. filename (`str`):
  748. The name of the file in the repo.
  749. subfolder (`str`, *optional*):
  750. An optional value corresponding to a folder inside the model repo.
  751. repo_type (`str`, *optional*):
  752. Set to `"dataset"`, `"space"` or `"kernel"` if downloading from a dataset, space or kernel repo,
  753. `None` or `"model"` if downloading from a model. Default is `None`.
  754. revision (`str`, *optional*):
  755. An optional Git revision id which can be a branch name, a tag, or a
  756. commit hash.
  757. library_name (`str`, *optional*):
  758. The name of the library to which the object corresponds.
  759. library_version (`str`, *optional*):
  760. The version of the library.
  761. cache_dir (`str`, `Path`, *optional*):
  762. Path to the folder where cached files are stored.
  763. local_dir (`str` or `Path`, *optional*):
  764. If provided, the downloaded file will be placed under this directory.
  765. user_agent (`dict`, `str`, *optional*):
  766. The user-agent info in the form of a dictionary or a string.
  767. force_download (`bool`, *optional*, defaults to `False`):
  768. Whether the file should be downloaded even if it already exists in
  769. the local cache.
  770. etag_timeout (`float`, *optional*, defaults to `10`):
  771. When fetching ETag, how many seconds to wait for the server to send
  772. data before giving up which is passed to `requests.request`.
  773. token (`str`, `bool`, *optional*):
  774. A token to be used for the download.
  775. - If `True`, the token is read from the HuggingFace config
  776. folder.
  777. - If a string, it's used as the authentication token.
  778. local_files_only (`bool`, *optional*, defaults to `False`):
  779. If `True`, avoid downloading the file and return the path to the
  780. local cached file if it exists.
  781. headers (`dict`, *optional*):
  782. Additional headers to be sent with the request.
  783. tqdm_class (`tqdm`, *optional*):
  784. If provided, overwrites the default behavior for the progress bar. Passed
  785. argument must inherit from `tqdm.auto.tqdm` or at least mimic its behavior.
  786. Defaults to the custom HF progress bar that can be disabled by setting
  787. `HF_HUB_DISABLE_PROGRESS_BARS` environment variable.
  788. dry_run (`bool`, *optional*, defaults to `False`):
  789. If `True`, perform a dry run without actually downloading the file. Returns a
  790. [`DryRunFileInfo`] object containing information about what would be downloaded.
  791. Returns:
  792. `str` or [`DryRunFileInfo`]:
  793. - If `dry_run=False`: Local path of file or if networking is off, last version of file cached on disk.
  794. - If `dry_run=True`: A [`DryRunFileInfo`] object containing download information.
  795. Raises:
  796. [`~utils.RepositoryNotFoundError`]
  797. If the repository to download from cannot be found. This may be because it doesn't exist,
  798. or because it is set to `private` and you do not have access.
  799. [`~utils.RevisionNotFoundError`]
  800. If the revision to download from cannot be found.
  801. [`~utils.RemoteEntryNotFoundError`]
  802. If the file to download cannot be found.
  803. [`~utils.LocalEntryNotFoundError`]
  804. If network is disabled or unavailable and file is not found in cache.
  805. [`EnvironmentError`](https://docs.python.org/3/library/exceptions.html#EnvironmentError)
  806. If `token=True` but the token cannot be found.
  807. [`OSError`](https://docs.python.org/3/library/exceptions.html#OSError)
  808. If ETag cannot be determined.
  809. [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
  810. If some parameter value is invalid.
  811. """
  812. if constants.HF_HUB_ETAG_TIMEOUT != constants.DEFAULT_ETAG_TIMEOUT:
  813. # Respect environment variable above user value
  814. etag_timeout = constants.HF_HUB_ETAG_TIMEOUT
  815. if cache_dir is None:
  816. cache_dir = constants.HF_HUB_CACHE
  817. if revision is None:
  818. revision = constants.DEFAULT_REVISION
  819. if isinstance(cache_dir, Path):
  820. cache_dir = str(cache_dir)
  821. if isinstance(local_dir, Path):
  822. local_dir = str(local_dir)
  823. if subfolder == "":
  824. subfolder = None
  825. if subfolder is not None:
  826. # This is used to create a URL, and not a local path, hence the forward slash.
  827. filename = f"{subfolder}/{filename}"
  828. if repo_type is None:
  829. repo_type = "model"
  830. if repo_type not in constants.REPO_TYPES_WITH_KERNEL:
  831. raise ValueError(
  832. f"Invalid repo type: {repo_type}. Accepted repo types are: {str(constants.REPO_TYPES_WITH_KERNEL)}"
  833. )
  834. hf_headers = build_hf_headers(
  835. token=token,
  836. library_name=library_name,
  837. library_version=library_version,
  838. user_agent=user_agent,
  839. headers=headers,
  840. )
  841. if local_dir is not None:
  842. return _hf_hub_download_to_local_dir(
  843. # Destination
  844. local_dir=local_dir,
  845. # File info
  846. repo_id=repo_id,
  847. repo_type=repo_type,
  848. filename=filename,
  849. revision=revision,
  850. # HTTP info
  851. endpoint=endpoint,
  852. etag_timeout=etag_timeout,
  853. headers=hf_headers,
  854. token=token,
  855. # Additional options
  856. cache_dir=cache_dir,
  857. force_download=force_download,
  858. local_files_only=local_files_only,
  859. tqdm_class=tqdm_class,
  860. dry_run=dry_run,
  861. )
  862. else:
  863. return _hf_hub_download_to_cache_dir(
  864. # Destination
  865. cache_dir=cache_dir,
  866. # File info
  867. repo_id=repo_id,
  868. filename=filename,
  869. repo_type=repo_type,
  870. revision=revision,
  871. # HTTP info
  872. endpoint=endpoint,
  873. etag_timeout=etag_timeout,
  874. headers=hf_headers,
  875. token=token,
  876. # Additional options
  877. local_files_only=local_files_only,
  878. force_download=force_download,
  879. tqdm_class=tqdm_class,
  880. dry_run=dry_run,
  881. )
  882. def _hf_hub_download_to_cache_dir(
  883. *,
  884. # Destination
  885. cache_dir: str,
  886. # File info
  887. repo_id: str,
  888. filename: str,
  889. repo_type: str,
  890. revision: str,
  891. # HTTP info
  892. endpoint: str | None,
  893. etag_timeout: float,
  894. headers: dict[str, str],
  895. token: bool | str | None,
  896. # Additional options
  897. local_files_only: bool,
  898. force_download: bool,
  899. tqdm_class: type[base_tqdm] | None,
  900. dry_run: bool,
  901. ) -> str | DryRunFileInfo:
  902. """Download a given file to a cache folder, if not already present.
  903. Method should not be called directly. Please use `hf_hub_download` instead.
  904. """
  905. locks_dir = os.path.join(cache_dir, ".locks")
  906. storage_folder = os.path.join(cache_dir, repo_folder_name(repo_id=repo_id, repo_type=repo_type))
  907. # cross-platform transcription of filename, to be used as a local file path.
  908. relative_filename = os.path.join(*filename.split("/"))
  909. if os.name == "nt":
  910. if relative_filename.startswith("..\\") or "\\..\\" in relative_filename:
  911. raise ValueError(
  912. f"Invalid filename: cannot handle filename '{relative_filename}' on Windows. Please ask the repository"
  913. " owner to rename this file."
  914. )
  915. # if user provides a commit_hash and they already have the file on disk, shortcut everything.
  916. if REGEX_COMMIT_HASH.match(revision):
  917. pointer_path = _get_pointer_path(storage_folder, revision, relative_filename)
  918. if os.path.exists(pointer_path):
  919. if dry_run:
  920. return DryRunFileInfo(
  921. commit_hash=revision,
  922. file_size=os.path.getsize(pointer_path),
  923. filename=filename,
  924. is_cached=True,
  925. local_path=pointer_path,
  926. will_download=force_download,
  927. )
  928. if not force_download:
  929. return pointer_path
  930. # Try to get metadata (etag, commit_hash, url, size) from the server.
  931. # If we can't, a HEAD request error is returned.
  932. (url_to_download, etag, commit_hash, expected_size, xet_file_data, head_call_error) = _get_metadata_or_catch_error(
  933. repo_id=repo_id,
  934. filename=filename,
  935. repo_type=repo_type,
  936. revision=revision,
  937. endpoint=endpoint,
  938. etag_timeout=etag_timeout,
  939. headers=headers,
  940. token=token,
  941. local_files_only=local_files_only,
  942. storage_folder=storage_folder,
  943. relative_filename=relative_filename,
  944. )
  945. # etag can be None for several reasons:
  946. # 1. we passed local_files_only.
  947. # 2. we don't have a connection
  948. # 3. Hub is down (HTTP 500, 503, 504)
  949. # 4. repo is not found -for example private or gated- and invalid/missing token sent
  950. # 5. Hub is blocked by a firewall or proxy is not set correctly.
  951. # => Try to get the last downloaded one from the specified revision.
  952. #
  953. # If the specified revision is a commit hash, look inside "snapshots".
  954. # If the specified revision is a branch or tag, look inside "refs".
  955. if head_call_error is not None:
  956. # Couldn't make a HEAD call => let's try to find a local file
  957. if not force_download:
  958. commit_hash = None
  959. if REGEX_COMMIT_HASH.match(revision):
  960. commit_hash = revision
  961. else:
  962. ref_path = os.path.join(storage_folder, "refs", revision)
  963. if os.path.isfile(ref_path):
  964. with open(ref_path) as f:
  965. commit_hash = f.read()
  966. # Return pointer file if exists
  967. if commit_hash is not None:
  968. pointer_path = _get_pointer_path(storage_folder, commit_hash, relative_filename)
  969. if os.path.exists(pointer_path):
  970. if dry_run:
  971. return DryRunFileInfo(
  972. commit_hash=commit_hash,
  973. file_size=os.path.getsize(pointer_path),
  974. filename=filename,
  975. is_cached=True,
  976. local_path=pointer_path,
  977. will_download=force_download,
  978. )
  979. if not force_download:
  980. return pointer_path
  981. if isinstance(head_call_error, _DEFAULT_RETRY_ON_EXCEPTIONS) or (
  982. isinstance(head_call_error, HfHubHTTPError)
  983. and head_call_error.response.status_code in _DEFAULT_RETRY_ON_STATUS_CODES
  984. ):
  985. logger.info("No local file found. Retrying..")
  986. (url_to_download, etag, commit_hash, expected_size, xet_file_data, head_call_error) = (
  987. _get_metadata_or_catch_error(
  988. repo_id=repo_id,
  989. filename=filename,
  990. repo_type=repo_type,
  991. revision=revision,
  992. endpoint=endpoint,
  993. etag_timeout=_ETAG_RETRY_TIMEOUT,
  994. headers=headers,
  995. token=token,
  996. local_files_only=local_files_only,
  997. storage_folder=storage_folder,
  998. relative_filename=relative_filename,
  999. retry_on_errors=True,
  1000. )
  1001. )
  1002. # If still error, raise
  1003. if head_call_error is not None:
  1004. _raise_on_head_call_error(head_call_error, force_download, local_files_only)
  1005. # From now on, etag, commit_hash, url and size are not None.
  1006. assert etag is not None, "etag must have been retrieved from server"
  1007. assert commit_hash is not None, "commit_hash must have been retrieved from server"
  1008. assert url_to_download is not None, "file location must have been retrieved from server"
  1009. assert expected_size is not None, "expected_size must have been retrieved from server"
  1010. blob_path = os.path.join(storage_folder, "blobs", etag)
  1011. pointer_path = _get_pointer_path(storage_folder, commit_hash, relative_filename)
  1012. if dry_run:
  1013. is_cached = os.path.exists(pointer_path) or os.path.exists(blob_path)
  1014. return DryRunFileInfo(
  1015. commit_hash=commit_hash,
  1016. file_size=expected_size,
  1017. filename=filename,
  1018. is_cached=is_cached,
  1019. local_path=pointer_path,
  1020. will_download=force_download or not is_cached,
  1021. )
  1022. os.makedirs(os.path.dirname(blob_path), exist_ok=True)
  1023. os.makedirs(os.path.dirname(pointer_path), exist_ok=True)
  1024. # Tag cache_dir so backup tools can skip it (CACHEDIR.TAG standard).
  1025. _create_cachedir_tag(Path(cache_dir))
  1026. # if passed revision is not identical to commit_hash
  1027. # then revision has to be a branch name or tag name.
  1028. # In that case store a ref.
  1029. _cache_commit_hash_for_specific_revision(storage_folder, revision, commit_hash)
  1030. # Prevent parallel downloads of the same file with a lock.
  1031. # etag could be duplicated across repos,
  1032. lock_path = os.path.join(locks_dir, repo_folder_name(repo_id=repo_id, repo_type=repo_type), f"{etag}.lock")
  1033. # Some Windows versions do not allow for paths longer than 255 characters.
  1034. # In this case, we must specify it as an extended path by using the "\\?\" prefix.
  1035. if (
  1036. os.name == "nt"
  1037. and len(os.path.abspath(lock_path)) > 255
  1038. and not os.path.abspath(lock_path).startswith("\\\\?\\")
  1039. ):
  1040. lock_path = "\\\\?\\" + os.path.abspath(lock_path)
  1041. if (
  1042. os.name == "nt"
  1043. and len(os.path.abspath(blob_path)) > 255
  1044. and not os.path.abspath(blob_path).startswith("\\\\?\\")
  1045. ):
  1046. blob_path = "\\\\?\\" + os.path.abspath(blob_path)
  1047. Path(lock_path).parent.mkdir(parents=True, exist_ok=True)
  1048. # pointer already exists -> immediate return
  1049. if not force_download and os.path.exists(pointer_path):
  1050. return pointer_path
  1051. # Blob exists but pointer must be (safely) created -> take the lock
  1052. if not force_download and os.path.exists(blob_path):
  1053. with WeakFileLock(lock_path):
  1054. if not os.path.exists(pointer_path):
  1055. _create_symlink(blob_path, pointer_path, new_blob=False)
  1056. return pointer_path
  1057. # Local file doesn't exist or etag isn't a match => retrieve file from remote (or cache)
  1058. with WeakFileLock(lock_path):
  1059. _download_to_tmp_and_move(
  1060. incomplete_path=Path(blob_path + ".incomplete"),
  1061. destination_path=Path(blob_path),
  1062. url_to_download=url_to_download,
  1063. headers=headers,
  1064. expected_size=expected_size,
  1065. filename=filename,
  1066. force_download=force_download,
  1067. etag=etag,
  1068. xet_file_data=xet_file_data,
  1069. tqdm_class=tqdm_class,
  1070. )
  1071. if not os.path.exists(pointer_path):
  1072. _create_symlink(blob_path, pointer_path, new_blob=True)
  1073. return pointer_path
  1074. def _hf_hub_download_to_local_dir(
  1075. *,
  1076. # Destination
  1077. local_dir: str | Path,
  1078. # File info
  1079. repo_id: str,
  1080. repo_type: str,
  1081. filename: str,
  1082. revision: str,
  1083. # HTTP info
  1084. endpoint: str | None,
  1085. etag_timeout: float,
  1086. headers: dict[str, str],
  1087. token: bool | str | None,
  1088. # Additional options
  1089. cache_dir: str,
  1090. force_download: bool,
  1091. local_files_only: bool,
  1092. tqdm_class: type[base_tqdm] | None,
  1093. dry_run: bool,
  1094. ) -> str | DryRunFileInfo:
  1095. """Download a given file to a local folder, if not already present.
  1096. Method should not be called directly. Please use `hf_hub_download` instead.
  1097. """
  1098. # Some Windows versions do not allow for paths longer than 255 characters.
  1099. # In this case, we must specify it as an extended path by using the "\\?\" prefix.
  1100. if os.name == "nt" and len(os.path.abspath(local_dir)) > 255:
  1101. local_dir = "\\\\?\\" + os.path.abspath(local_dir)
  1102. local_dir = Path(local_dir)
  1103. paths = get_local_download_paths(local_dir=local_dir, filename=filename)
  1104. local_metadata = read_download_metadata(local_dir=local_dir, filename=filename)
  1105. # Local file exists + metadata exists + commit_hash matches => return file
  1106. if (
  1107. REGEX_COMMIT_HASH.match(revision)
  1108. and paths.file_path.is_file()
  1109. and local_metadata is not None
  1110. and local_metadata.commit_hash == revision
  1111. ):
  1112. local_file = str(paths.file_path)
  1113. if dry_run:
  1114. return DryRunFileInfo(
  1115. commit_hash=revision,
  1116. file_size=os.path.getsize(local_file),
  1117. filename=filename,
  1118. is_cached=True,
  1119. local_path=local_file,
  1120. will_download=force_download,
  1121. )
  1122. if not force_download:
  1123. return local_file
  1124. # Local file doesn't exist or commit_hash doesn't match => we need the etag
  1125. (url_to_download, etag, commit_hash, expected_size, xet_file_data, head_call_error) = _get_metadata_or_catch_error(
  1126. repo_id=repo_id,
  1127. filename=filename,
  1128. repo_type=repo_type,
  1129. revision=revision,
  1130. endpoint=endpoint,
  1131. etag_timeout=etag_timeout,
  1132. headers=headers,
  1133. token=token,
  1134. local_files_only=local_files_only,
  1135. )
  1136. if head_call_error is not None:
  1137. # No HEAD call but local file exists => default to local file
  1138. if paths.file_path.is_file():
  1139. if dry_run or not force_download:
  1140. logger.warning(
  1141. f"Couldn't access the Hub to check for update but local file already exists. Defaulting to existing file. (error: {head_call_error})"
  1142. )
  1143. local_path = str(paths.file_path)
  1144. if dry_run and local_metadata is not None:
  1145. return DryRunFileInfo(
  1146. commit_hash=local_metadata.commit_hash,
  1147. file_size=os.path.getsize(local_path),
  1148. filename=filename,
  1149. is_cached=True,
  1150. local_path=local_path,
  1151. will_download=force_download,
  1152. )
  1153. if not force_download:
  1154. return local_path
  1155. elif not force_download:
  1156. if isinstance(head_call_error, _DEFAULT_RETRY_ON_EXCEPTIONS) or (
  1157. isinstance(head_call_error, HfHubHTTPError)
  1158. and head_call_error.response.status_code in _DEFAULT_RETRY_ON_STATUS_CODES
  1159. ):
  1160. logger.info("No local file found. Retrying..")
  1161. (url_to_download, etag, commit_hash, expected_size, xet_file_data, head_call_error) = (
  1162. _get_metadata_or_catch_error(
  1163. repo_id=repo_id,
  1164. filename=filename,
  1165. repo_type=repo_type,
  1166. revision=revision,
  1167. endpoint=endpoint,
  1168. etag_timeout=_ETAG_RETRY_TIMEOUT,
  1169. headers=headers,
  1170. token=token,
  1171. local_files_only=local_files_only,
  1172. retry_on_errors=True,
  1173. )
  1174. )
  1175. # If still error, raise
  1176. if head_call_error is not None:
  1177. _raise_on_head_call_error(head_call_error, force_download, local_files_only)
  1178. # From now on, etag, commit_hash, url and size are not None.
  1179. assert etag is not None, "etag must have been retrieved from server"
  1180. assert commit_hash is not None, "commit_hash must have been retrieved from server"
  1181. assert url_to_download is not None, "file location must have been retrieved from server"
  1182. assert expected_size is not None, "expected_size must have been retrieved from server"
  1183. # Local file exists => check if it's up-to-date
  1184. if not force_download and paths.file_path.is_file():
  1185. # etag matches => update metadata and return file
  1186. if local_metadata is not None and local_metadata.etag == etag:
  1187. write_download_metadata(local_dir=local_dir, filename=filename, commit_hash=commit_hash, etag=etag)
  1188. if dry_run:
  1189. return DryRunFileInfo(
  1190. commit_hash=commit_hash,
  1191. file_size=expected_size,
  1192. filename=filename,
  1193. is_cached=True,
  1194. local_path=str(paths.file_path),
  1195. will_download=False,
  1196. )
  1197. return str(paths.file_path)
  1198. # metadata is outdated + etag is a sha256
  1199. # => means it's an LFS file (large)
  1200. # => let's compute local hash and compare
  1201. # => if match, update metadata and return file
  1202. if local_metadata is None and REGEX_SHA256.match(etag) is not None:
  1203. with open(paths.file_path, "rb") as f:
  1204. file_hash = sha_fileobj(f).hex()
  1205. if file_hash == etag:
  1206. write_download_metadata(local_dir=local_dir, filename=filename, commit_hash=commit_hash, etag=etag)
  1207. if dry_run:
  1208. return DryRunFileInfo(
  1209. commit_hash=commit_hash,
  1210. file_size=expected_size,
  1211. filename=filename,
  1212. is_cached=True,
  1213. local_path=str(paths.file_path),
  1214. will_download=False,
  1215. )
  1216. return str(paths.file_path)
  1217. # Local file doesn't exist or etag isn't a match => retrieve file from remote (or cache)
  1218. # If we are lucky enough, the file is already in the cache => copy it
  1219. if not force_download:
  1220. cached_path = try_to_load_from_cache(
  1221. repo_id=repo_id,
  1222. filename=filename,
  1223. cache_dir=cache_dir,
  1224. revision=commit_hash,
  1225. repo_type=repo_type,
  1226. )
  1227. if isinstance(cached_path, str):
  1228. with WeakFileLock(paths.lock_path):
  1229. paths.file_path.parent.mkdir(parents=True, exist_ok=True)
  1230. shutil.copyfile(cached_path, paths.file_path)
  1231. write_download_metadata(local_dir=local_dir, filename=filename, commit_hash=commit_hash, etag=etag)
  1232. if dry_run:
  1233. return DryRunFileInfo(
  1234. commit_hash=commit_hash,
  1235. file_size=expected_size,
  1236. filename=filename,
  1237. is_cached=True,
  1238. local_path=str(paths.file_path),
  1239. will_download=False,
  1240. )
  1241. return str(paths.file_path)
  1242. if dry_run:
  1243. is_cached = paths.file_path.is_file()
  1244. return DryRunFileInfo(
  1245. commit_hash=commit_hash,
  1246. file_size=expected_size,
  1247. filename=filename,
  1248. is_cached=is_cached,
  1249. local_path=str(paths.file_path),
  1250. will_download=force_download or not is_cached,
  1251. )
  1252. # Otherwise, let's download the file!
  1253. with WeakFileLock(paths.lock_path):
  1254. paths.file_path.unlink(missing_ok=True) # delete outdated file first
  1255. _download_to_tmp_and_move(
  1256. incomplete_path=paths.incomplete_path(etag),
  1257. destination_path=paths.file_path,
  1258. url_to_download=url_to_download,
  1259. headers=headers,
  1260. expected_size=expected_size,
  1261. filename=filename,
  1262. force_download=force_download,
  1263. etag=etag,
  1264. xet_file_data=xet_file_data,
  1265. tqdm_class=tqdm_class,
  1266. )
  1267. write_download_metadata(local_dir=local_dir, filename=filename, commit_hash=commit_hash, etag=etag)
  1268. return str(paths.file_path)
  1269. @validate_hf_hub_args
  1270. def try_to_load_from_cache(
  1271. repo_id: str,
  1272. filename: str,
  1273. cache_dir: str | Path | None = None,
  1274. revision: str | None = None,
  1275. repo_type: str | None = None,
  1276. ) -> str | _CACHED_NO_EXIST_T | None:
  1277. """
  1278. Explores the cache to return the latest cached file for a given revision if found.
  1279. This function will not raise any exception if the file in not cached.
  1280. Args:
  1281. cache_dir (`str` or `os.PathLike`):
  1282. The folder where the cached files lie.
  1283. repo_id (`str`):
  1284. The ID of the repo on huggingface.co.
  1285. filename (`str`):
  1286. The filename to look for inside `repo_id`.
  1287. revision (`str`, *optional*):
  1288. The specific model version to use. Will default to `"main"` if it's not provided and no `commit_hash` is
  1289. provided either.
  1290. repo_type (`str`, *optional*):
  1291. The type of the repository. Will default to `"model"`.
  1292. Returns:
  1293. `Optional[str]` or `_CACHED_NO_EXIST`:
  1294. Will return `None` if the file was not cached. Otherwise:
  1295. - The exact path to the cached file if it's found in the cache
  1296. - A special value `_CACHED_NO_EXIST` if the file does not exist at the given commit hash and this fact was
  1297. cached.
  1298. Example:
  1299. ```python
  1300. from huggingface_hub import try_to_load_from_cache, _CACHED_NO_EXIST
  1301. filepath = try_to_load_from_cache()
  1302. if isinstance(filepath, str):
  1303. # file exists and is cached
  1304. ...
  1305. elif filepath is _CACHED_NO_EXIST:
  1306. # non-existence of file is cached
  1307. ...
  1308. else:
  1309. # file is not cached
  1310. ...
  1311. ```
  1312. """
  1313. if revision is None:
  1314. revision = "main"
  1315. if repo_type is None:
  1316. repo_type = "model"
  1317. if repo_type not in constants.REPO_TYPES_WITH_KERNEL:
  1318. raise ValueError(
  1319. f"Invalid repo type: {repo_type}. Accepted repo types are: {str(constants.REPO_TYPES_WITH_KERNEL)}"
  1320. )
  1321. if cache_dir is None:
  1322. cache_dir = constants.HF_HUB_CACHE
  1323. repo_cache = os.path.join(cache_dir, repo_folder_name(repo_id=repo_id, repo_type=repo_type))
  1324. if not os.path.isdir(repo_cache):
  1325. # No cache for this model
  1326. return None
  1327. refs_dir = os.path.join(repo_cache, "refs")
  1328. snapshots_dir = os.path.join(repo_cache, "snapshots")
  1329. no_exist_dir = os.path.join(repo_cache, ".no_exist")
  1330. # Resolve refs (for instance to convert main to the associated commit sha)
  1331. if os.path.isdir(refs_dir):
  1332. revision_file = os.path.join(refs_dir, revision)
  1333. if os.path.isfile(revision_file):
  1334. with open(revision_file) as f:
  1335. revision = f.read()
  1336. # Check if file is cached as "no_exist"
  1337. if os.path.isfile(os.path.join(no_exist_dir, revision, filename)):
  1338. return _CACHED_NO_EXIST
  1339. # Check if revision folder exists
  1340. if not os.path.exists(snapshots_dir):
  1341. return None
  1342. cached_shas = os.listdir(snapshots_dir)
  1343. if revision not in cached_shas:
  1344. # No cache for this revision and we won't try to return a random revision
  1345. return None
  1346. # Check if file exists in cache
  1347. cached_file = os.path.join(snapshots_dir, revision, filename)
  1348. return cached_file if os.path.isfile(cached_file) else None
  1349. @validate_hf_hub_args
  1350. def get_hf_file_metadata(
  1351. url: str,
  1352. token: bool | str | None = None,
  1353. timeout: float | None = constants.HF_HUB_ETAG_TIMEOUT,
  1354. library_name: str | None = None,
  1355. library_version: str | None = None,
  1356. user_agent: dict | str | None = None,
  1357. headers: dict[str, str] | None = None,
  1358. endpoint: str | None = None,
  1359. retry_on_errors: bool = False,
  1360. ) -> HfFileMetadata:
  1361. """Fetch metadata of a file versioned on the Hub for a given url.
  1362. Args:
  1363. url (`str`):
  1364. File url, for example returned by [`hf_hub_url`].
  1365. token (`str` or `bool`, *optional*):
  1366. A token to be used for the download.
  1367. - If `True`, the token is read from the HuggingFace config
  1368. folder.
  1369. - If `False` or `None`, no token is provided.
  1370. - If a string, it's used as the authentication token.
  1371. timeout (`float`, *optional*, defaults to 10):
  1372. How many seconds to wait for the server to send metadata before giving up.
  1373. library_name (`str`, *optional*):
  1374. The name of the library to which the object corresponds.
  1375. library_version (`str`, *optional*):
  1376. The version of the library.
  1377. user_agent (`dict`, `str`, *optional*):
  1378. The user-agent info in the form of a dictionary or a string.
  1379. headers (`dict`, *optional*):
  1380. Additional headers to be sent with the request.
  1381. endpoint (`str`, *optional*):
  1382. Endpoint of the Hub. Defaults to <https://huggingface.co>.
  1383. retry_on_errors (`bool`, *optional*, defaults to `False`):
  1384. Whether to retry on errors (429, 5xx, timeout, network errors).
  1385. If False, no retry for fast fallback to local cache.
  1386. Returns:
  1387. A [`HfFileMetadata`] object containing metadata such as location, etag, size and
  1388. commit_hash.
  1389. """
  1390. hf_headers = build_hf_headers(
  1391. token=token,
  1392. library_name=library_name,
  1393. library_version=library_version,
  1394. user_agent=user_agent,
  1395. headers=headers,
  1396. )
  1397. hf_headers["Accept-Encoding"] = "identity" # prevent any compression => we want to know the real size of the file
  1398. # Retrieve metadata
  1399. response = _httpx_follow_relative_redirects_with_backoff(
  1400. method="HEAD", url=url, headers=hf_headers, timeout=timeout, retry_on_errors=retry_on_errors
  1401. )
  1402. hf_raise_for_status(response)
  1403. # Return
  1404. return HfFileMetadata(
  1405. commit_hash=response.headers.get(constants.HUGGINGFACE_HEADER_X_REPO_COMMIT),
  1406. # We favor a custom header indicating the etag of the linked resource, and we fall back to the regular etag header.
  1407. etag=_normalize_etag(
  1408. response.headers.get(constants.HUGGINGFACE_HEADER_X_LINKED_ETAG) or response.headers.get("ETag")
  1409. ),
  1410. # Either from response headers (if redirected) or defaults to request url
  1411. # Do not use directly `url` as we might have followed relative redirects.
  1412. location=response.headers.get("Location") or str(response.request.url), # type: ignore
  1413. size=_int_or_none(
  1414. response.headers.get(constants.HUGGINGFACE_HEADER_X_LINKED_SIZE) or response.headers.get("Content-Length")
  1415. ),
  1416. xet_file_data=parse_xet_file_data_from_response(response, endpoint=endpoint), # type: ignore
  1417. )
  1418. def _get_metadata_or_catch_error(
  1419. *,
  1420. repo_id: str,
  1421. filename: str,
  1422. repo_type: str,
  1423. revision: str,
  1424. endpoint: str | None,
  1425. etag_timeout: float | None,
  1426. headers: dict[str, str], # mutated inplace!
  1427. token: bool | str | None,
  1428. local_files_only: bool,
  1429. relative_filename: str | None = None, # only used to store `.no_exists` in cache
  1430. storage_folder: str | None = None, # only used to store `.no_exists` in cache
  1431. retry_on_errors: bool = False,
  1432. ) -> (
  1433. # Either an exception is caught and returned
  1434. tuple[None, None, None, None, None, Exception]
  1435. |
  1436. # Or the metadata is returned as
  1437. # `(url_to_download, etag, commit_hash, expected_size, xet_file_data, None)`
  1438. tuple[str, str, str, int, XetFileData | None, None]
  1439. ):
  1440. """Get metadata for a file on the Hub, safely handling network issues.
  1441. Returns either the etag, commit_hash and expected size of the file, or the error
  1442. raised while fetching the metadata.
  1443. NOTE: This function mutates `headers` inplace! It removes the `authorization` header
  1444. if the file is a LFS blob and the domain of the url is different from the
  1445. domain of the location (typically an S3 bucket).
  1446. """
  1447. if local_files_only:
  1448. return (
  1449. None,
  1450. None,
  1451. None,
  1452. None,
  1453. None,
  1454. OfflineModeIsEnabled(
  1455. f"Cannot access file since 'local_files_only=True' as been set. (repo_id: {repo_id}, repo_type: {repo_type}, revision: {revision}, filename: {filename})"
  1456. ),
  1457. )
  1458. url = hf_hub_url(repo_id, filename, repo_type=repo_type, revision=revision, endpoint=endpoint)
  1459. url_to_download: str = url
  1460. etag: str | None = None
  1461. commit_hash: str | None = None
  1462. expected_size: int | None = None
  1463. head_error_call: Exception | None = None
  1464. xet_file_data: XetFileData | None = None
  1465. # Try to get metadata from the server.
  1466. # Do not raise yet if the file is not found or not accessible.
  1467. if not local_files_only:
  1468. try:
  1469. try:
  1470. metadata = get_hf_file_metadata(
  1471. url=url,
  1472. timeout=etag_timeout,
  1473. headers=headers,
  1474. token=token,
  1475. endpoint=endpoint,
  1476. retry_on_errors=retry_on_errors,
  1477. )
  1478. except RemoteEntryNotFoundError as http_error:
  1479. if storage_folder is not None and relative_filename is not None:
  1480. # Cache the non-existence of the file
  1481. commit_hash = http_error.response.headers.get(constants.HUGGINGFACE_HEADER_X_REPO_COMMIT)
  1482. if commit_hash is not None:
  1483. no_exist_file_path = Path(storage_folder) / ".no_exist" / commit_hash / relative_filename
  1484. try:
  1485. no_exist_file_path.parent.mkdir(parents=True, exist_ok=True)
  1486. no_exist_file_path.touch()
  1487. except OSError as e:
  1488. logger.error(
  1489. f"Could not cache non-existence of file. Will ignore error and continue. Error: {e}"
  1490. )
  1491. _cache_commit_hash_for_specific_revision(storage_folder, revision, commit_hash)
  1492. raise
  1493. # Commit hash must exist
  1494. commit_hash = metadata.commit_hash
  1495. if commit_hash is None:
  1496. raise FileMetadataError(
  1497. "Distant resource does not seem to be on huggingface.co. It is possible that a configuration issue"
  1498. " prevents you from downloading resources from https://huggingface.co. Please check your firewall"
  1499. " and proxy settings and make sure your SSL certificates are updated."
  1500. )
  1501. # Etag must exist
  1502. # If we don't have any of those, raise an error.
  1503. etag = metadata.etag
  1504. if etag is None:
  1505. raise FileMetadataError(
  1506. "Distant resource does not have an ETag, we won't be able to reliably ensure reproducibility."
  1507. )
  1508. # Size must exist
  1509. expected_size = metadata.size
  1510. if expected_size is None:
  1511. raise FileMetadataError("Distant resource does not have a Content-Length.")
  1512. xet_file_data = metadata.xet_file_data
  1513. # In case of a redirect, save an extra redirect on the request.get call,
  1514. # and ensure we download the exact atomic version even if it changed
  1515. # between the HEAD and the GET (unlikely, but hey).
  1516. #
  1517. # If url domain is different => we are downloading from a CDN => url is signed => don't send auth
  1518. # If url domain is the same => redirect due to repo rename AND downloading a regular file => keep auth
  1519. if xet_file_data is None and url != metadata.location:
  1520. url_to_download = metadata.location
  1521. if urlparse(url).netloc != urlparse(metadata.location).netloc:
  1522. # Remove authorization header when downloading a LFS blob
  1523. headers.pop("authorization", None)
  1524. except httpx.ProxyError:
  1525. # Actually raise on proxy error
  1526. raise
  1527. except (httpx.ConnectError, httpx.TimeoutException, OfflineModeIsEnabled) as error:
  1528. # Otherwise, our Internet connection is down.
  1529. # etag is None
  1530. head_error_call = error
  1531. except (RevisionNotFoundError, RemoteEntryNotFoundError):
  1532. # The repo was found but the revision or entry doesn't exist on the Hub (never existed or got deleted)
  1533. raise
  1534. except HfHubHTTPError as error:
  1535. # Multiple reasons for an http error:
  1536. # - Repository is private and invalid/missing token sent
  1537. # - Repository is gated and invalid/missing token sent
  1538. # - Hub is down (error 500 or 504)
  1539. # => let's switch to 'local_files_only=True' to check if the files are already cached.
  1540. # (if it's not the case, the error will be re-raised)
  1541. head_error_call = error
  1542. except FileMetadataError as error:
  1543. # Multiple reasons for a FileMetadataError:
  1544. # - Wrong network configuration (proxy, firewall, SSL certificates)
  1545. # - Inconsistency on the Hub
  1546. # => let's switch to 'local_files_only=True' to check if the files are already cached.
  1547. # (if it's not the case, the error will be re-raised)
  1548. head_error_call = error
  1549. if not (local_files_only or etag is not None or head_error_call is not None):
  1550. raise RuntimeError("etag is empty due to uncovered problems")
  1551. return (url_to_download, etag, commit_hash, expected_size, xet_file_data, head_error_call) # type: ignore
  1552. def _raise_on_head_call_error(head_call_error: Exception, force_download: bool, local_files_only: bool) -> NoReturn:
  1553. """Raise an appropriate error when the HEAD call failed and we cannot locate a local file."""
  1554. # No head call => we cannot force download.
  1555. if force_download:
  1556. if local_files_only:
  1557. raise ValueError("Cannot pass 'force_download=True' and 'local_files_only=True' at the same time.")
  1558. elif isinstance(head_call_error, OfflineModeIsEnabled):
  1559. raise ValueError("Cannot pass 'force_download=True' when offline mode is enabled.") from head_call_error
  1560. else:
  1561. raise ValueError("Force download failed due to the above error.") from head_call_error
  1562. # No head call + couldn't find an appropriate file on disk => raise an error.
  1563. if local_files_only:
  1564. raise LocalEntryNotFoundError(
  1565. "Cannot find the requested files in the disk cache and outgoing traffic has been disabled. To enable"
  1566. " hf.co look-ups and downloads online, set 'local_files_only' to False."
  1567. )
  1568. elif isinstance(head_call_error, (RepositoryNotFoundError, GatedRepoError)) or (
  1569. isinstance(head_call_error, HfHubHTTPError) and head_call_error.response.status_code == 401
  1570. ):
  1571. # Repo not found or gated => let's raise the actual error
  1572. # Unauthorized => likely a token issue => let's raise the actual error
  1573. raise head_call_error
  1574. else:
  1575. # Otherwise: most likely a connection issue or Hub downtime => let's warn the user
  1576. raise LocalEntryNotFoundError(
  1577. "An error happened while trying to locate the file on the Hub and we cannot find the requested files"
  1578. " in the local cache. Please check your connection and try again or make sure your Internet connection"
  1579. " is on."
  1580. ) from head_call_error
  1581. def _download_to_tmp_and_move(
  1582. incomplete_path: Path,
  1583. destination_path: Path,
  1584. url_to_download: str,
  1585. headers: dict[str, str],
  1586. expected_size: int | None,
  1587. filename: str,
  1588. force_download: bool,
  1589. etag: str | None,
  1590. xet_file_data: XetFileData | None,
  1591. tqdm_class: type[base_tqdm] | None = None,
  1592. ) -> None:
  1593. """Download content from a URL to a destination path.
  1594. Internal logic:
  1595. - return early if file is already downloaded
  1596. - resume download if possible (from incomplete file)
  1597. - do not resume download if `force_download=True`
  1598. - check disk space before downloading
  1599. - download content to a temporary file
  1600. - set correct permissions on temporary file
  1601. - move the temporary file to the destination path
  1602. Both `incomplete_path` and `destination_path` must be on the same volume to avoid a local copy.
  1603. """
  1604. if destination_path.exists() and not force_download:
  1605. # Do nothing if already exists (except if force_download=True)
  1606. return
  1607. if incomplete_path.exists() and force_download:
  1608. # By default, we will try to resume the download if possible.
  1609. # However, if the user has set `force_download=True`, then we should
  1610. # not resume the download => delete the incomplete file.
  1611. logger.debug(f"Removing incomplete file '{incomplete_path}' (force_download=True)")
  1612. incomplete_path.unlink(missing_ok=True)
  1613. with incomplete_path.open("ab") as f:
  1614. resume_size = f.tell()
  1615. message = f"Downloading '{filename}' to '{incomplete_path}'"
  1616. if resume_size > 0 and expected_size is not None:
  1617. message += f" (resume from {resume_size}/{expected_size})"
  1618. logger.debug(message)
  1619. if expected_size is not None: # might be None if HTTP header not set correctly
  1620. # Check disk space in both tmp and destination path
  1621. _check_disk_space(expected_size, incomplete_path.parent)
  1622. _check_disk_space(expected_size, destination_path.parent)
  1623. if xet_file_data is not None and is_xet_available():
  1624. logger.debug("Xet Storage is enabled for this repo. Downloading file from Xet Storage..")
  1625. xet_get(
  1626. incomplete_path=incomplete_path,
  1627. xet_file_data=xet_file_data,
  1628. headers=headers,
  1629. expected_size=expected_size,
  1630. displayed_filename=filename,
  1631. tqdm_class=tqdm_class,
  1632. )
  1633. else:
  1634. if xet_file_data is not None and not constants.HF_HUB_DISABLE_XET:
  1635. logger.warning(
  1636. "Xet Storage is enabled for this repo, but the 'hf_xet' package is not installed. "
  1637. "Falling back to regular HTTP download. "
  1638. "For better performance, install the package with: `pip install huggingface_hub[hf_xet]` or `pip install hf_xet`"
  1639. )
  1640. http_get(
  1641. url_to_download,
  1642. f,
  1643. resume_size=resume_size,
  1644. headers=headers,
  1645. expected_size=expected_size,
  1646. tqdm_class=tqdm_class,
  1647. )
  1648. logger.debug(f"Download complete. Moving file to {destination_path}")
  1649. _chmod_and_move(incomplete_path, destination_path)
  1650. def _int_or_none(value: str | None) -> int | None:
  1651. try:
  1652. return int(value) # type: ignore
  1653. except (TypeError, ValueError):
  1654. return None
  1655. def _chmod_and_move(src: Path, dst: Path) -> None:
  1656. """Set correct permission before moving a blob from tmp directory to cache dir.
  1657. Do not take into account the `umask` from the process as there is no convenient way
  1658. to get it that is thread-safe.
  1659. See:
  1660. - About umask: https://docs.python.org/3/library/os.html#os.umask
  1661. - Thread-safety: https://stackoverflow.com/a/70343066
  1662. - About solution: https://github.com/huggingface/huggingface_hub/pull/1220#issuecomment-1326211591
  1663. - Fix issue: https://github.com/huggingface/huggingface_hub/issues/1141
  1664. - Fix issue: https://github.com/huggingface/huggingface_hub/issues/1215
  1665. """
  1666. # Get umask by creating a temporary file in the cached repo folder.
  1667. tmp_file = dst.parent.parent / f"tmp_{uuid.uuid4()}"
  1668. try:
  1669. tmp_file.touch()
  1670. cache_dir_mode = Path(tmp_file).stat().st_mode
  1671. os.chmod(str(src), stat.S_IMODE(cache_dir_mode))
  1672. except OSError as e:
  1673. logger.warning(
  1674. f"Could not set the permissions on the file '{src}'. Error: {e}.\nContinuing without setting permissions."
  1675. )
  1676. finally:
  1677. try:
  1678. tmp_file.unlink()
  1679. except OSError:
  1680. # fails if `tmp_file.touch()` failed => do nothing
  1681. # See https://github.com/huggingface/huggingface_hub/issues/2359
  1682. pass
  1683. shutil.move(str(src), str(dst), copy_function=_copy_no_matter_what)
  1684. def _copy_no_matter_what(src: str, dst: str) -> None:
  1685. """Copy file from src to dst.
  1686. If `shutil.copy2` fails, fallback to `shutil.copyfile`.
  1687. """
  1688. try:
  1689. # Copy file with metadata and permission
  1690. # Can fail e.g. if dst is an S3 mount
  1691. shutil.copy2(src, dst)
  1692. except OSError:
  1693. # Copy only file content
  1694. shutil.copyfile(src, dst)
  1695. def _get_pointer_path(storage_folder: str, revision: str, relative_filename: str) -> str:
  1696. # Using `os.path.abspath` instead of `Path.resolve()` to avoid resolving symlinks
  1697. snapshot_path = os.path.join(storage_folder, "snapshots")
  1698. pointer_path = os.path.join(snapshot_path, revision, relative_filename)
  1699. if Path(os.path.abspath(snapshot_path)) not in Path(os.path.abspath(pointer_path)).parents:
  1700. raise ValueError(
  1701. "Invalid pointer path: cannot create pointer path in snapshot folder if"
  1702. f" `storage_folder='{storage_folder}'`, `revision='{revision}'` and"
  1703. f" `relative_filename='{relative_filename}'`."
  1704. )
  1705. return pointer_path