o
    h'*                 -   @   s  U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZ d dlmZ d dlmZmZmZmZmZmZmZmZ d dlmZmZ d dlZddlmZmZ ddlmZm Z m!Z! dd	lm"Z"m#Z# dd
l$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZG ddlHmIZI ddlJmKZKmLZL ddlMmNZN ddlOmPZP ddlQmRZR eBSeTZUeV ZWeZXeYdZZeYdZ[eYdZ\i Z]ee^e_f e`d< d}dee^edf de_fddZaeddG dd dZbeGdddddde^de^d ee^ d!ee^ d"ee^ d#ee^ de^fd$d%Zcd&d'd(eNd)e^d*e_dejdfd+d,Zedd dddd-dd.d)e^d/ed0ee d1efd2eee^ef  d3eef d4ee^ d5efd6eeF ddfd7d8Zgdddd9d:ed;e0d2ee^e^f d3eef d4ee^ d6eeF ddfd<d=Zhd>ee^ dee^ fd?d@Zid~dAe^dBe^dCe_ddfdDdEZjd~dAe^dBe^dCe_ddfdFdGZkdHe^d"e^dIe^ddfdJdKZleGde^d!e^de^fdLdMZmd3efdNee^ef ddfdOdPZneGddddddddd&dejodd&dddddQdRde^de^d ee^ d!ee^ d"ee^ dSee^ dTee^ dee^edf dUee^edf dVeee^df dWe_d0ee dXepdYee_e^df dZe_d2eee^e^f  d#ee^ d[ee_ d\ee^ d]ee_edQ f de^f*d^d_Zqde^de^de^d!e^d"e^d#ee^ dXepd2ee^e^f d0ee dYeee_e^f  dZe_dWe_de^fd`daZrdUee^ef de^d!e^de^d"e^d#ee^ dXepd2ee^e^f d0ee dYee_e^df de^dWe_dZe_de^fdbdcZseG			dde^de^dee^edf d"ee^ d!ee^ dee^eXdf fdddeZteGddejuddddfd)e^dYee_e^df d0ee dfeep dSee^ dTee^ dVeee^df d2eee^e^f  debfdgdhZvdddide^de^d!e^d"e^d#ee^ d0ee dXeep d2ee^e^f dYee_e^df dZe_djee^ dHee^ deedddddewf ee^e^e^efee0 df f fdkdlZxdmewdWe_dZe_defdndoZyd:edpedqe^d0ee d2ee^e^f d3eef de^dWe_d>ee^ d;ee0 ddfdrdsZzdtee^ deef fdudvZ{dAedBeddfdwdxZ|dAe^dBe^ddfdydzZ}dHe^d"e^dje^de^fd{d|Z~dS )    N)	dataclass)Path)AnyBinaryIODictLiteralNoReturnOptionalTupleUnion)quoteurlparse   )__version__	constants)get_local_download_pathsread_download_metadatawrite_download_metadata)HUGGINGFACE_CO_URL_TEMPLATEHUGGINGFACE_HUB_CACHE)EntryNotFoundErrorFileMetadataErrorGatedRepoErrorHfHubHTTPErrorLocalEntryNotFoundErrorRepositoryNotFoundErrorRevisionNotFoundError)OfflineModeIsEnabledSoftTemporaryDirectoryWeakFileLockXetFileDatabuild_hf_headersget_fastai_versionget_fastcore_versionget_graphviz_versionget_jinja_versionget_pydot_versionget_sessionget_tf_versionget_torch_versionhf_raise_for_statusis_fastai_availableis_fastcore_availableis_graphviz_availableis_jinja_availableis_pydot_availableis_tf_availableis_torch_availablelogging!parse_xet_file_data_from_responserefresh_xet_connection_inforeset_sessionstqdmvalidate_hf_hub_args)_adjust_range_header)_PY_VERSIONis_xet_available)HTTP_METHOD_T)sha_fileobj)_get_progress_bar_contextzfilename="(?P<filename>.*?)";z^[0-9a-f]{40}$z^[0-9a-f]{64}$_are_symlinks_supported_in_dir	cache_dirreturnc              	   C   s  | du rt j} tt|   } | tvrdt| < tj| dd t	| d^}t|d }|
  t|d }tjj|tj|d}zt|| W n$ tyn   dt| < t jsld	|  d
}tjdkrg|d7 }t| Y nw W d   t|  S W d   t|  S 1 sw   Y  t|  S )a  Return whether the symlinks are supported on the machine.

    Since symlinks support can change depending on the mounted disk, we need to check
    on the precise cache folder. By default, the default HF cache directory is checked.

    Args:
        cache_dir (`str`, `Path`, *optional*):
            Path to the folder where cached files are stored.

    Returns: [bool] Whether symlinks are supported in the directory.
    NTexist_ok)dirdummy_file_srcdummy_file_dst)startFz`huggingface_hub` cache-system uses symlinks by default to efficiently store duplicated files but your machine does not support them in a%  . Caching files will still work but in a degraded version that might require more space on your disk. This warning can be disabled by setting the `HF_HUB_DISABLE_SYMLINKS_WARNING` environment variable. For more details, see https://huggingface.co/docs/huggingface_hub/how-to-cache#limitations.nta  
To support symlinks on Windows, you either need to activate Developer Mode or to run Python as an administrator. In order to activate developer mode, see this article: https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development)r   HF_HUB_CACHEstrr   
expanduserresolver>   osmakedirsr   touchpathrelpathdirnamesymlinkOSErrorHF_HUB_DISABLE_SYMLINKS_WARNINGnamewarningswarn)r?   tmpdirsrc_pathdst_pathrelative_srcmessage r]   q/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/huggingface_hub/file_download.pyare_symlinks_supportedZ   sF   



"
""r_   T)frozenc                   @   sJ   e Zd ZU dZee ed< ee ed< eed< ee ed< ee ed< dS )HfFileMetadataa  Data structure containing information about a file versioned on the Hub.

    Returned by [`get_hf_file_metadata`] based on a URL.

    Args:
        commit_hash (`str`, *optional*):
            The commit_hash related to the file.
        etag (`str`, *optional*):
            Etag of the file on the server.
        location (`str`):
            Location where to download the file. Can be a Hub url or not (CDN).
        size (`size`):
            Size of the file. In case of an LFS file, contains the size of the actual
            LFS file, not the pointer.
        xet_file_data (`XetFileData`, *optional*):
            Xet information for the file. This is only set if the file is stored using Xet storage.
    commit_hashetaglocationsizexet_file_dataN)	__name__
__module____qualname____doc__r	   rI   __annotations__intr    r]   r]   r]   r^   ra      s   
 ra   )	subfolder	repo_typerevisionendpointrepo_idfilenamerm   rn   ro   rp   c                C   s   |dkrd}|dur| d| }|t jvrtd|t jv r&t j| |  } |du r-t j}tj| t|ddt|d}|durP|t j	rP||t
t j	d  }|S )a|  Construct the URL of a file from the given information.

    The resolved address can either be a huggingface.co-hosted url, or a link to
    Cloudfront (a Content Delivery Network, or CDN) for large files which are
    more than a few MBs.

    Args:
        repo_id (`str`):
            A namespace (user or an organization) name and a repo name separated
            by a `/`.
        filename (`str`):
            The name of the file in the repo.
        subfolder (`str`, *optional*):
            An optional value corresponding to a folder inside the repo.
        repo_type (`str`, *optional*):
            Set to `"dataset"` or `"space"` if downloading from a dataset or space,
            `None` or `"model"` if downloading from a model. Default is `None`.
        revision (`str`, *optional*):
            An optional Git revision id which can be a branch name, a tag, or a
            commit hash.

    Example:

    ```python
    >>> from huggingface_hub import hf_hub_url

    >>> hf_hub_url(
    ...     repo_id="julien-c/EsperBERTo-small", filename="pytorch_model.bin"
    ... )
    'https://huggingface.co/julien-c/EsperBERTo-small/resolve/main/pytorch_model.bin'
    ```

    <Tip>

    Notes:

        Cloudfront is replicated over the globe so downloads are way faster for
        the end user (and it also lowers our bandwidth costs).

        Cloudfront aggressively caches files by default (default TTL is 24
        hours), however this is not an issue here because we implement a
        git-based versioning system on huggingface.co, which means that we store
        the files on S3/Cloudfront in a content-addressable way (i.e., the file
        name is its hash). Using content-addressable filenames means cache can't
        ever be stale.

        In terms of client-side caching from this library, we base our caching
        on the objects' entity tag (`ETag`), which is an identifier of a
        specific version of a resource [1]_. An object's ETag is: its git-sha1
        if stored in git, or its sha256 if stored in git-lfs.

    </Tip>

    References:

    -  [1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
     N/zInvalid repo type)safe)rq   ro   rr   )r   
REPO_TYPES
ValueErrorREPO_TYPES_URL_PREFIXESDEFAULT_REVISIONr   formatr   
startswithENDPOINTlen)rq   rr   rm   rn   ro   rp   urlr]   r]   r^   
hf_hub_url   s    C

r   F)follow_relative_redirectsmethodr~   r   c                K   s   |r?t d| |dd|}d|j  krdkr=n |S t|jd }|jdkr=t|j|jd }t d| |dd|S |S t j	d| |d	|}t
| |S )a  Wrapper around requests methods to follow relative redirects if `follow_relative_redirects=True` even when
    `allow_redirection=False`.

    Args:
        method (`str`):
            HTTP method, such as 'GET' or 'HEAD'.
        url (`str`):
            The URL of the resource to fetch.
        follow_relative_redirects (`bool`, *optional*, defaults to `False`)
            If True, relative redirection (redirection to the same site) will be resolved even when `allow_redirection`
            kwarg is set to False. Useful when we want to follow a redirection to a renamed repository without
            following redirection to a CDN.
        **params (`dict`, *optional*):
            Params to pass to `requests.request`.
    F)r   r~   r   i,  i  Locationrs   )rO   T)r   r~   Nr]   )_request_wrapperstatus_coder   headersnetloc_replacerO   geturlr'   requestr*   )r   r~   r   paramsresponseparsed_targetnext_urlr]   r]   r^   r   	  s$   	
r      )proxiesresume_sizer   expected_sizedisplayed_filename_nb_retries	_tqdm_bar	temp_filer   r   r   r   r   r   r   c                C   s  |dur
||kr
dS d}	t jr5|dkrtd n|dur#td nzddl}	W n ty4   tdw |}
t|p=i }|dkrLt	|
d||d< td| d||t jd	}t| |j
d
}|durk|t| nd}|du r| }|j
d}|durt|}|dur| d }t|dkrd|dd  }d| d| d}t|t ||d|d}|}|	r/|dur/|dt j kr/dt|	jjv }|std z|	jd!| |jt jt j|ddd|rd|jini  W n ty } zt d|d}~ww |s
|| |dur&|t!j"#|jkr&t$|j%t!j"#|jd	 W d   dS |}z$|j&t jdD ]}|rR|t| |'| |t|7 }d}q9W nL t(j)t(j*fy } z;|dkrqt+d| t,|  t+d| t,| t-.d t/  t0| ||||
||d |d W  Y d}~W  d   S d}~ww W d   n	1 sw   Y  |dur||1 krt$|j%|1 ddS dS )"a  
    Download a remote file. Do not gobble up errors, and will return errors tailored to the Hugging Face Hub.

    If ConnectionError (SSLError) or ReadTimeout happen while streaming data from the server, it is most likely a
    transient error (network outage?). We log a warning message and try to resume the download a few times before
    giving up. The method gives up after 5 attempts if no new data has being received from the server.

    Args:
        url (`str`):
            The URL of the file to download.
        temp_file (`BinaryIO`):
            The file-like object where to save the file.
        proxies (`dict`, *optional*):
            Dictionary mapping protocol to the URL of the proxy passed to `requests.request`.
        resume_size (`int`, *optional*):
            The number of bytes already downloaded. If set to 0 (default), the whole file is download. If set to a
            positive number, the download will resume at the given position.
        headers (`dict`, *optional*):
            Dictionary of HTTP Headers to send with the request.
        expected_size (`int`, *optional*):
            The expected size of the file to download. If set, the download will raise an error if the size of the
            received content is different from the expected one.
        displayed_filename (`str`, *optional*):
            The filename of the file that is being downloaded. Value is used only to display a nice progress bar. If
            not set, the filename is guessed from the URL or the `Content-Disposition` header.
    Nr   zU'hf_transfer' does not support `resume_size`: falling back to regular download methodzQ'hf_transfer' does not support `proxies`: falling back to regular download methodzFast download using 'hf_transfer' is enabled (HF_HUB_ENABLE_HF_TRANSFER=1) but 'hf_transfer' package is not available in your environment. Try `pip install hf_transfer`.RangeGETT)r   r~   streamr   r   timeoutContent-LengthzContent-Dispositionrr   (      (…)iz1Consistency check failed: file should be of size z but has size {actual_size} (zm).
This is usually due to network issues while downloading the file. Please retry with `force_download=True`.zhuggingface_hub.http_getdesc	log_leveltotalinitialrU   r   r   callbackzYou are using an outdated version of `hf_transfer`. Consider upgrading to latest version to enable progress bars using `pip install -U hf_transfer`.   )r~   rr   	max_files
chunk_sizer   parallel_failuresmax_retrieszAn error occurred while downloading using `hf_transfer`. Consider disabling HF_HUB_ENABLE_HF_TRANSFER for better error handling.)actual_size)r   z9Error while downloading from %s: %s
Max retries exceeded.z@Error while downloading from %s: %s
Trying to resume download...r   )r~   r   r   r   r   r   r   r   r]   )2r   HF_HUB_ENABLE_HF_TRANSFERrV   rW   hf_transferImportErrorrw   copydeepcopyr8   getr   HF_HUB_DOWNLOAD_TIMEOUTr*   r   rl   HEADER_FILENAME_PATTERNsearch	groupdictr}   r=   loggergetEffectiveLevelDOWNLOAD_CHUNK_SIZEinspect	signaturedownload
parametersrU   HF_TRANSFER_CONCURRENCYupdate	ExceptionRuntimeErrorrL   rO   getsizeEnvironmentErrorrz   iter_contentwriterequestsConnectionErrorReadTimeoutwarningrI   timesleepr5   http_gettell)r~   r   r   r   r   r   r   r   r   r   initial_headersrcontent_lengthr   content_dispositionmatchconsistency_error_messageprogress_cmprogresssupports_callbackenew_resume_sizechunkr]   r]   r^   r   9  s   &
	 


"


+@r   )r   r   r   incomplete_pathrf   c                    s  z
ddl m}m} W n ty   tdw t d}dtttf f fdd}	|t| 	 j
|dg}
|s<| j}t|d	krK|d
d	  d}t|t |dd|d}|!dtffdd}||
|j|j|jf|	|gd W d
   d
S 1 s}w   Y  d
S )a+	  
    Download a file using Xet storage service.

    Args:
        incomplete_path (`Path`):
            The path to the file to download.
        xet_file_data (`XetFileData`):
            The file metadata needed to make the request to the xet storage service.
        headers (`Dict[str, str]`):
            The headers to send to the xet storage service.
        expected_size (`int`, *optional*):
            The expected size of the file to download. If set, the download will raise an error if the size of the
            received content is different from the expected one.
        displayed_filename (`str`, *optional*):
            The filename of the file that is being downloaded. Value is used only to display a nice progress bar. If
            not set, the filename is guessed from the URL or the `Content-Disposition` header.

    **How it works:**
        The file download system uses Xet storage, which is a content-addressable storage system that breaks files into chunks
        for efficient storage and transfer.

        `hf_xet.download_files` manages downloading files by:
        - Taking a list of files to download (each with its unique content hash)
        - Connecting to a storage server (CAS server) that knows how files are chunked
        - Using authentication to ensure secure access
        - Providing progress updates during download

        Authentication works by regularly refreshing access tokens through `refresh_xet_connection_info` to maintain a valid
        connection to the storage server.

        The download process works like this:
        1. Create a local cache folder at `~/.cache/huggingface/xet/chunk-cache` to store reusable file chunks
        2. Download files in parallel:
            2.1. Prepare to write the file to disk
            2.2. Ask the server "how is this file split into chunks?" using the file's unique hash
                The server responds with:
                - Which chunks make up the complete file
                - Where each chunk can be downloaded from
            2.3. For each needed chunk:
                - Checks if we already have it in our local cache
                - If not, download it from cloud storage (S3)
                - Save it to cache for future use
                - Assemble the chunks in order to recreate the original file

    r   )PyPointerFiledownload_fileszTo use optimized download using Xet storage, you need to install the hf_xet package. Try `pip install huggingface_hub[hf_xet]` or `pip install hf_xet`.	file_datar   r@   c                     s(   t  d} | d u rtd| j| jfS )Nr   z+Failed to refresh token using xet metadata.)r4   rw   access_tokenexpiration_unix_epoch)connection_info)r   rf   r]   r^   token_refresher%  s   z xet_get.<locals>.token_refresher)rO   hashfilesizer   Nr   zhuggingface_hub.xet_getr   progress_bytesc                    s     |  d S N)r   )r   )r   r]   r^   progress_updaterA  s   z!xet_get.<locals>.progress_updater)rp   
token_infor   r   )hf_xetr   r   r   rw   r4   r
   rI   rl   absolute	file_hashrU   r}   r=   r   r   floatrp   r   r   )r   rf   r   r   r   r   r   r   r   r   pointer_filesr   r   r]   )r   r   rf   r^   xet_get  sB   6	
"r   rc   c                 C   s   | du rdS |  ddS )aO  Normalize ETag HTTP header, so it can be used to create nice filepaths.

    The HTTP spec allows two forms of ETag:
      ETag: W/"<etag_value>"
      ETag: "<etag_value>"

    For now, we only expect the second form from the server, but we want to be future-proof so we support both. For
    more context, see `TestNormalizeEtag` tests and https://github.com/huggingface/huggingface_hub/pull/1428.

    Args:
        etag (`str`, *optional*): HTTP header

    Returns:
        `str` or `None`: string that can be used as a nice directory name.
        Returns `None` if input is None.
    NzW/")lstripstrip)rc   r]   r]   r^   _normalize_etagM  s   r   srcdstnew_blobc                 C   s   t | ||dS )z6Alias method used in `transformers` conversion script.r   r   r   )_create_symlinkr   r]   r]   r^   _create_relative_symlinkc  s   r   c              
   C   s  zt | W n	 ty   Y nw t jt j| }t jt j|}t j|}z	t j||}W n ty?   d}Y nw zt j	||g}t
|}W n7 ty\   t jdk}Y n* tyh   t
|}Y n ty }	 z|	jtjkrzt
|}n W Y d}	~	nd}	~	ww |r|p|}
td|
 d|  z	t |
| W dS  ty   t j|rt j|t j|krY dS   ty   Y nw |rtd| d|  tj||td dS td| d|  t|| dS )u  Create a symbolic link named dst pointing to src.

    By default, it will try to create a symlink using a relative path. Relative paths have 2 advantages:
    - If the cache_folder is moved (example: back-up on a shared drive), relative paths within the cache folder will
      not break.
    - Relative paths seems to be better handled on Windows. Issue was reported 3 times in less than a week when
      changing from relative to absolute paths. See https://github.com/huggingface/huggingface_hub/issues/1398,
      https://github.com/huggingface/diffusers/issues/2729 and https://github.com/huggingface/transformers/pull/22228.
      NOTE: The issue with absolute paths doesn't happen on admin mode.
    When creating a symlink from the cache to a local folder, it is possible that a relative path cannot be created.
    This happens when paths are not on the same volume. In that case, we use absolute paths.


    The result layout looks something like
        └── [ 128]  snapshots
            ├── [ 128]  2439f60ef33a0d46d85da5001d52aeda5b00ce9f
            │   ├── [  52]  README.md -> ../../../blobs/d7edf6bd2a681fb0175f7735299831ee1b22b812
            │   └── [  76]  pytorch_model.bin -> ../../../blobs/403450e234d65943a7dcf7e05a771ce3c92faa84dd07db4ac20f592037a1e4bd

    If symlinks cannot be created on this platform (most likely to be Windows), the workaround is to avoid symlinks by
    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
    (`new_blob=False`), we don't know if the blob file is already referenced elsewhere. To avoid breaking existing
    cache, the file is duplicated on the disk.

    In case symlinks are not supported, a warning message is displayed to the user once when loading `huggingface_hub`.
    The warning message can be disabled with the `DISABLE_SYMLINKS_WARNING` environment variable.
    NrG   zCreating pointer from z to z(Symlink not supported. Moving file from copy_functionz)Symlink not supported. Copying file from )rL   removerS   rO   abspathrJ   rQ   rP   rw   
commonpathr_   rU   PermissionErrorerrnoEROFSr   debugrR   FileExistsErrorislinkrealpathinfoshutilmove_copy_no_matter_whatcopyfile)r   r   r   abs_srcabs_dstabs_dst_folderr[   r   _support_symlinksr   src_rel_or_absr]   r]   r^   r   h  sZ   
$r   storage_folderrb   c                 C   sR   ||kr%t | d | }|jjddd | r|| kr'|| dS dS dS )zCache reference between a revision (tag, branch or truncated commit hash) and the corresponding commit hash.

    Does nothing if `revision` is already a proper `commit_hash` or reference is already cached.
    refsTparentsrB   N)r   parentmkdirexists	read_text
write_text)r  ro   rb   ref_pathr]   r]   r^   (_cache_commit_hash_for_specific_revision  s   r  c                 C   s"   | dg|  d}tj|S )zReturn a serialized version of a hf.co repo name and type, safe for disk storage
    as a single non-nested folder.

    Example: models--julien-c--EsperBERTo-small
    srt   )splitr   REPO_ID_SEPARATORjoin)rq   rn   partsr]   r]   r^   repo_folder_name  s   r  
target_dirc              
   C   sz   t |}|gt|j D ].}z#t|j}|| k r.td| d dd| d|d dd W  dS  ty:   Y qw dS )a$  Check disk usage and log a warning if there is not enough disk space to download the file.

    Args:
        expected_size (`int`):
            The expected size of the file in bytes.
        target_dir (`str`):
            The directory where the file will be stored after downloading.
    zLNot enough free disk space to download the file. The expected file size is: g    .Az.2fz MB. The target location z
 only has z MB free disk space.N)	r   listr  r  
disk_usagefreerV   rW   rS   )r   r  rO   target_dir_freer]   r]   r^   _check_disk_space  s&   
r!  auto)rm   rn   ro   library_namelibrary_versionr?   	local_dir
user_agentforce_downloadr   etag_timeouttokenlocal_files_onlyr   rp   resume_downloadforce_filenamelocal_dir_use_symlinksr#  r$  r%  r&  r'  r(  r)  r*  r+  r,  r-  c                C   s:  t jt jkr	t j}|durtdt |durtdt |du r$t j}|du r+t j}t|t	r4t
|}t|t	r=t
|}|dkrCd}|durN| d| }|du rTd}|t jvrftd| dt
t j t||||	|d	}|dur|d
kr|td t|| ||||||||||
|dS t|| ||||||||||
dS )u  Download a given file if it's not already present in the local cache.

    The new cache file layout looks like this:
    - The cache directory contains one subfolder per repo_id (namespaced by repo type)
    - inside each repo folder:
        - refs is a list of the latest known revision => commit_hash pairs
        - blobs contains the actual file blobs (identified by their git-sha or sha256, depending on
          whether they're LFS files or not)
        - snapshots contains one subfolder per commit, each "commit" contains the subset of the files
          that have been resolved at that particular commit. Each filename is a symlink to the blob
          at that particular commit.

    ```
    [  96]  .
    └── [ 160]  models--julien-c--EsperBERTo-small
        ├── [ 160]  blobs
        │   ├── [321M]  403450e234d65943a7dcf7e05a771ce3c92faa84dd07db4ac20f592037a1e4bd
        │   ├── [ 398]  7cb18dc9bafbfcf74629a4b760af1b160957a83e
        │   └── [1.4K]  d7edf6bd2a681fb0175f7735299831ee1b22b812
        ├── [  96]  refs
        │   └── [  40]  main
        └── [ 128]  snapshots
            ├── [ 128]  2439f60ef33a0d46d85da5001d52aeda5b00ce9f
            │   ├── [  52]  README.md -> ../../blobs/d7edf6bd2a681fb0175f7735299831ee1b22b812
            │   └── [  76]  pytorch_model.bin -> ../../blobs/403450e234d65943a7dcf7e05a771ce3c92faa84dd07db4ac20f592037a1e4bd
            └── [ 128]  bbc77c8132af1cc5cf678da3f1ddf2de43606d48
                ├── [  52]  README.md -> ../../blobs/7cb18dc9bafbfcf74629a4b760af1b160957a83e
                └── [  76]  pytorch_model.bin -> ../../blobs/403450e234d65943a7dcf7e05a771ce3c92faa84dd07db4ac20f592037a1e4bd
    ```

    If `local_dir` is provided, the file structure from the repo will be replicated in this location. When using this
    option, the `cache_dir` will not be used and a `.cache/huggingface/` folder will be created at the root of `local_dir`
    to store some metadata related to the downloaded files. While this mechanism is not as robust as the main
    cache-system, it's optimized for regularly pulling the latest version of a repository.

    Args:
        repo_id (`str`):
            A user or an organization name and a repo name separated by a `/`.
        filename (`str`):
            The name of the file in the repo.
        subfolder (`str`, *optional*):
            An optional value corresponding to a folder inside the model repo.
        repo_type (`str`, *optional*):
            Set to `"dataset"` or `"space"` if downloading from a dataset or space,
            `None` or `"model"` if downloading from a model. Default is `None`.
        revision (`str`, *optional*):
            An optional Git revision id which can be a branch name, a tag, or a
            commit hash.
        library_name (`str`, *optional*):
            The name of the library to which the object corresponds.
        library_version (`str`, *optional*):
            The version of the library.
        cache_dir (`str`, `Path`, *optional*):
            Path to the folder where cached files are stored.
        local_dir (`str` or `Path`, *optional*):
            If provided, the downloaded file will be placed under this directory.
        user_agent (`dict`, `str`, *optional*):
            The user-agent info in the form of a dictionary or a string.
        force_download (`bool`, *optional*, defaults to `False`):
            Whether the file should be downloaded even if it already exists in
            the local cache.
        proxies (`dict`, *optional*):
            Dictionary mapping protocol to the URL of the proxy passed to
            `requests.request`.
        etag_timeout (`float`, *optional*, defaults to `10`):
            When fetching ETag, how many seconds to wait for the server to send
            data before giving up which is passed to `requests.request`.
        token (`str`, `bool`, *optional*):
            A token to be used for the download.
                - If `True`, the token is read from the HuggingFace config
                  folder.
                - If a string, it's used as the authentication token.
        local_files_only (`bool`, *optional*, defaults to `False`):
            If `True`, avoid downloading the file and return the path to the
            local cached file if it exists.
        headers (`dict`, *optional*):
            Additional headers to be sent with the request.

    Returns:
        `str`: Local path of file or if networking is off, last version of file cached on disk.

    Raises:
        [`~utils.RepositoryNotFoundError`]
            If the repository to download from cannot be found. This may be because it doesn't exist,
            or because it is set to `private` and you do not have access.
        [`~utils.RevisionNotFoundError`]
            If the revision to download from cannot be found.
        [`~utils.EntryNotFoundError`]
            If the file to download cannot be found.
        [`~utils.LocalEntryNotFoundError`]
            If network is disabled or unavailable and file is not found in cache.
        [`EnvironmentError`](https://docs.python.org/3/library/exceptions.html#EnvironmentError)
            If `token=True` but the token cannot be found.
        [`OSError`](https://docs.python.org/3/library/exceptions.html#OSError)
            If ETag cannot be determined.
        [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
            If some parameter value is invalid.

    NzThe `force_filename` parameter is deprecated as a new caching system, which keeps the filenames as they are on the Hub, is now in place.z`resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.rs   rt   modelInvalid repo type: . Accepted repo types are: r)  r#  r$  r&  r   r"  ae  `local_dir_use_symlinks` parameter is deprecated and will be ignored. The process to download files to a local folder has been updated and do not rely on symlinks anymore. You only need to pass a destination folder as`local_dir`.
For more details, check out https://huggingface.co/docs/huggingface_hub/main/en/guides/download#download-files-to-local-folder.)r%  rq   rn   rr   ro   rp   r(  r   r   r)  r?   r'  r*  )r?   rq   rr   rn   ro   rp   r(  r   r   r)  r*  r'  )r   HF_HUB_ETAG_TIMEOUTDEFAULT_ETAG_TIMEOUTrV   rW   FutureWarningrH   ry   
isinstancer   rI   rv   rw   r!   _hf_hub_download_to_local_dir_hf_hub_download_to_cache_dir)rq   rr   rm   rn   ro   r#  r$  r?   r%  r&  r'  r   r(  r)  r*  r   rp   r+  r,  r-  
hf_headersr]   r]   r^   hf_hub_download  s   {


r9  c                 C   s>  t j| d}t j| t||d}t jj|d }t jdkr1|ds)d|v r1td| dt	|rFt
|||}t j|rF|sF|S t|||||||||	|
||d	\}}}}}}|d
ur|sd
}t	|rl|}n&t j|d|}t j|rt|}| }W d
   n1 sw   Y  |d
urt
|||}t j|r|s|S t|||
 |d
usJ d|d
usJ d|d
usJ d|d
usJ dt j|d|}t
|||}t jt j|dd t jt j|dd t||| |st j|r|S t j|rt||dd |S t j|t||d| d}t jdkr:tt j|dkr:dt j| }t jdkrStt j|dkrSdt j| }t|jjddd t|3 tt|d t|||||||||d
 t j|st||dd W d
   |S W d
   |S 1 sw   Y  |S )zDownload a given file to a cache folder, if not already present.

    Method should not be called directly. Please use `hf_hub_download` instead.
    z.locks)rq   rn   rt   rG   z..\z\..\z*Invalid filename: cannot handle filename 'zB' on Windows. Please ask the repository owner to rename this file.)rq   rr   rn   ro   rp   r   r(  r   r)  r*  r  relative_filenameNr  )etag must have been retrieved from server0commit_hash must have been retrieved from server2file location must have been retrieved from server2expected_size must have been retrieved from serverblobsTrA   F)r   z.lock   \\?\r  z.incomplete
r   destination_pathurl_to_downloadr   r   r   rr   r'  rc   rf   )rL   rO   r  r  r  rU   r{   rw   REGEX_COMMIT_HASHr   _get_pointer_pathr  _get_metadata_or_catch_errorisfileopenread_raise_on_head_call_errorrM   rQ   r  r   r}   r   r   r  r  r   _download_to_tmp_and_move)r?   rq   rr   rn   ro   rp   r(  r   r   r)  r*  r'  	locks_dirr  r:  pointer_pathrD  rc   rb   r   rf   head_call_errorr  f	blob_path	lock_pathr]   r]   r^   r7    s   





""



r7  c                 C   s  t jdkrtt j| dkrdt j|  } t| } t| |d}t| |d}|sAt	|rA|j
 rA|durA|j|krAt|j
S t|||||||||	|d
\}}}}}}|durt|sn|j
 rntd| d t|j
S t||| |dus|J d	|dusJ d
|dusJ d|dusJ d|s|j
 r|dur|j|krt| |||d t|j
S |du rt	|durt|j
d}t| }W d   n1 sw   Y  ||krt| |||d t|j
S |s.t|||
||d}t|tr.t|j |j
jjddd t||j
 W d   n	1 sw   Y  t| |||d t|j
S t|j! |j
j dd t!|"||j
||||||||d
 W d   n	1 sXw   Y  t| |||d t|j
S )zDownload a given file to a local folder, if not already present.

    Method should not be called directly. Please use `hf_hub_download` instead.
    rG   r@  rA  )r%  rr   N)
rq   rr   rn   ro   rp   r   r(  r   r)  r*  zpCouldn't access the Hub to check for update but local file already exists. Defaulting to existing file. (error: )r;  r<  r=  r>  )r%  rr   rb   rc   rb)rq   rr   r?   ro   rn   Tr  
missing_okrB  )#rL   rU   r}   rO   r   r   r   r   rE  r   	file_pathis_filerb   rI   rG  r   r   rK  rc   r   REGEX_SHA256rI  r<   hextry_to_load_from_cacher5  r   rR  r  r  r  r  unlinkrL  r   )r%  rq   rn   rr   ro   rp   r(  r   r   r)  r?   r'  r*  pathslocal_metadatarD  rc   rb   r   rf   rO  rP  r   cached_pathr]   r]   r^   r6  j  s   







r6  c                 C   sd  |du rd}|du rd}|t jvrtd| dtt j |du r%t j}| dd}tj|| d| }tj	|s?dS tj|d	}tj|d
}tj|d}	tj	|rtj||}
tj
|
rt|
}| }W d   n1 szw   Y  tj
tj|	||rtS tj|sdS t|}||vrdS tj|||}tj
|r|S dS )a  
    Explores the cache to return the latest cached file for a given revision if found.

    This function will not raise any exception if the file in not cached.

    Args:
        cache_dir (`str` or `os.PathLike`):
            The folder where the cached files lie.
        repo_id (`str`):
            The ID of the repo on huggingface.co.
        filename (`str`):
            The filename to look for inside `repo_id`.
        revision (`str`, *optional*):
            The specific model version to use. Will default to `"main"` if it's not provided and no `commit_hash` is
            provided either.
        repo_type (`str`, *optional*):
            The type of the repository. Will default to `"model"`.

    Returns:
        `Optional[str]` or `_CACHED_NO_EXIST`:
            Will return `None` if the file was not cached. Otherwise:
            - The exact path to the cached file if it's found in the cache
            - A special value `_CACHED_NO_EXIST` if the file does not exist at the given commit hash and this fact was
              cached.

    Example:

    ```python
    from huggingface_hub import try_to_load_from_cache, _CACHED_NO_EXIST

    filepath = try_to_load_from_cache()
    if isinstance(filepath, str):
        # file exists and is cached
        ...
    elif filepath is _CACHED_NO_EXIST:
        # non-existence of file is cached
        ...
    else:
        # file is not cached
        ...
    ```
    Nmainr.  r/  r0  rt   z--zs--r  	snapshots	.no_exist)r   rv   rw   rI   rH   replacerL   rO   r  isdirrH  rI  rJ  _CACHED_NO_EXISTr  listdir)rq   rr   r?   ro   rn   	object_id
repo_cacherefs_dirsnapshots_dirno_exist_dirrevision_filerP  cached_shascached_filer]   r]   r^   r[    s<   2



r[  r   c           
   	   C   s   t |||||d}d|d< td| |dd||d}	t|	 t|	jtjt|	jtj	p0|	jd|	jd	p:|	j
jt|	jtjpG|	jd
t|	dS )a!  Fetch metadata of a file versioned on the Hub for a given url.

    Args:
        url (`str`):
            File url, for example returned by [`hf_hub_url`].
        token (`str` or `bool`, *optional*):
            A token to be used for the download.
                - If `True`, the token is read from the HuggingFace config
                  folder.
                - If `False` or `None`, no token is provided.
                - If a string, it's used as the authentication token.
        proxies (`dict`, *optional*):
            Dictionary mapping protocol to the URL of the proxy passed to
            `requests.request`.
        timeout (`float`, *optional*, defaults to 10):
            How many seconds to wait for the server to send metadata before giving up.
        library_name (`str`, *optional*):
            The name of the library to which the object corresponds.
        library_version (`str`, *optional*):
            The version of the library.
        user_agent (`dict`, `str`, *optional*):
            The user-agent info in the form of a dictionary or a string.
        headers (`dict`, *optional*):
            Additional headers to be sent with the request.

    Returns:
        A [`HfFileMetadata`] object containing metadata such as location, etag, size and
        commit_hash.
    r1  identityzAccept-EncodingHEADFT)r   r~   r   allow_redirectsr   r   r   ETagr   r   )rb   rc   rd   re   rf   )r!   r   r*   ra   r   r   r    HUGGINGFACE_HEADER_X_REPO_COMMITr    HUGGINGFACE_HEADER_X_LINKED_ETAGr   r~   _int_or_none HUGGINGFACE_HEADER_X_LINKED_SIZEr3   )
r~   r)  r   r   r#  r$  r&  r   r8  r   r]   r]   r^   get_hf_file_metadataG  s6   (	rw  )r:  r  r:  c                 C   s  |	rdddddt d|  d| d| d| d	fS t| ||||d}|}d}d}d}d}d}|	s7zzt|||||d}W nZ ty } zN|dur|
dur|jjtj}|durt	|d	 | |
 }z|j
jd
d
d |  W n ty } ztd|  W Y d}~nd}~ww t|||  d}~ww |j}|du rtd|j}|du rtd|j}|du rtd|j}|du r||jkr|j}t|jt|jjkr|dd W n\ tjjtjjfy     tjjtjjt fy } z|}W Y d}~n8d}~w t tfy     tj!y" } z|}W Y d}~nd}~w ty6 } z|}W Y d}~nd}~ww |	sH|dusH|dusHt"d||||||fS )a  Get metadata for a file on the Hub, safely handling network issues.

    Returns either the etag, commit_hash and expected size of the file, or the error
    raised while fetching the metadata.

    NOTE: This function mutates `headers` inplace! It removes the `authorization` header
          if the file is a LFS blob and the domain of the url is different from the
          domain of the location (typically an S3 bucket).
    NzHCannot access file since 'local_files_only=True' as been set. (repo_id: z, repo_type: z, revision: z, filename: rS  )rn   ro   rp   )r~   r   r   r   r)  rb  Tr  zNCould not cache non-existence of file. Will ignore error and continue. Error: a  Distant resource does not seem to be on huggingface.co. It is possible that a configuration issue prevents you from downloading resources from https://huggingface.co. Please check your firewall and proxy settings and make sure your SSL certificates are updated.z\Distant resource does not have an ETag, we won't be able to reliably ensure reproducibility.z0Distant resource does not have a Content-Length.authorizationz'etag is empty due to uncovered problems)#r   r   rw  r   r   r   r   r   rs  r   r  r  rN   rS   r   errorr  rb   r   rc   re   rf   rd   r   r   popr   
exceptionsSSLError
ProxyErrorr   Timeoutr   	HTTPErrorr   )rq   rr   rn   ro   rp   r   r(  r   r)  r*  r:  r  r~   rD  rc   rb   r   head_error_callrf   metadata
http_errorno_exist_file_pathr   ry  r]   r]   r^   rG    s   

rG  rO  c                 C   sl   |r|rt dt| trt d| t d| |rtdt| ttfs/t| tr1| jjdkr1| td| )zWRaise an appropriate error when the HEAD call failed and we cannot locate a local file.zOCannot pass 'force_download=True' and 'local_files_only=True' at the same time.z?Cannot pass 'force_download=True' when offline mode is enabled.z-Force download failed due to the above error.zCannot find the requested files in the disk cache and outgoing traffic has been disabled. To enable hf.co look-ups and downloads online, set 'local_files_only' to False.i  zAn error happened while trying to locate the file on the Hub and we cannot find the requested files in the local cache. Please check your connection and try again or make sure your Internet connection is on.)	rw   r5  r   r   r   r   r   r   r   )rO  r'  r*  r]   r]   r^   rK  &  s(   


rK  rC  rD  c
              	   C   sl  |  r|sdS |   r4|stjr4|s4d|  d}
|r |
d7 }
n	tjr)|s)|
d7 }
t|
 | jdd | df}| }d	| d
|  d}
|dkrZ|durZ|
d| d| d7 }
t|
 |durot|| j	 t||j	 |	durt
 rtd t| |	|||d n|	durtd t||||||d W d   n1 sw   Y  td|  t| | dS )a0  Download content from a URL to a destination path.

    Internal logic:
    - return early if file is already downloaded
    - resume download if possible (from incomplete file)
    - do not resume download if `force_download=True` or `HF_HUB_ENABLE_HF_TRANSFER=True`
    - check disk space before downloading
    - download content to a temporary file
    - set correct permissions on temporary file
    - move the temporary file to the destination path

    Both `incomplete_path` and `destination_path` must be on the same volume to avoid a local copy.
    NzRemoving incomplete file ''z (force_download=True)z (hf_transfer=True)TrU  abzDownloading 'z' to 'r   z (resume from rt   rS  zIXet Storage is enabled for this repo. Downloading file from Xet Storage..)r   rf   r   r   r   zXet Storage is enabled for this repo, but the 'hf_xet' package is not installed. Falling back to regular HTTP download. For better performance, install the package with: `pip install huggingface_hub[hf_xet]` or `pip install hf_xet`)r   r   r   r   z"Download complete. Moving file to )r  r   r   r   r  r\  rI  r   r!  r  r:   r   r   r   _chmod_and_move)r   rC  rD  r   r   r   rr   r'  rc   rf   r\   rP  r   r]   r]   r^   rL  F  sX   




%rL  valuec              	   C   s$   zt | W S  ttfy   Y d S w r   )rl   	TypeErrorrw   )r  r]   r]   r^   ru    s
   
ru  c                 C   s   |j j dt   }zHz|  t| j}tt	| t
| W n tyB } ztd|  d| d W Y d}~nd}~ww W z|  W n tyS   Y nw z|  W w  tyc   Y w w tjt	| t	|td dS )aV  Set correct permission before moving a blob from tmp directory to cache dir.

    Do not take into account the `umask` from the process as there is no convenient way
    to get it that is thread-safe.

    See:
    - About umask: https://docs.python.org/3/library/os.html#os.umask
    - Thread-safety: https://stackoverflow.com/a/70343066
    - About solution: https://github.com/huggingface/huggingface_hub/pull/1220#issuecomment-1326211591
    - Fix issue: https://github.com/huggingface/huggingface_hub/issues/1141
    - Fix issue: https://github.com/huggingface/huggingface_hub/issues/1215
    tmp_z+Could not set the permissions on the file 'z
'. Error: z).
Continuing without setting permissions.Nr   )r  uuiduuid4rN   r   statst_moderL   chmodrI   S_IMODErS   r   r   r\  r  r  r  )r   r   tmp_filecache_dir_moder   r]   r]   r^   r    s.   r  c                 C   s4   z	t | | W dS  ty   t | | Y dS w )z\Copy file from src to dst.

    If `shutil.copy2` fails, fallback to `shutil.copyfile`.
    N)r  copy2rS   r  )r   r   r]   r]   r^   r    s
   r  c                 C   s`   t j| d}t j|||}tt j|tt j|jvr.td|  d| d| d|S )Nra  zXInvalid pointer path: cannot create pointer path in snapshot folder if `storage_folder='z'`, `revision='z'` and `relative_filename='z'`.)rL   rO   r  r   r   r  rw   )r  ro   r:  snapshot_pathrN  r]   r]   r^   rF    s   "rF  r   )F)NNN)r   r   r   rL   rer  r  r   r  rV   dataclassesr   pathlibr   typingr   r   r   r   r   r	   r
   r   urllib.parser   r   r   rs   r   r   _local_folderr   r   r   r   r   errorsr   r   r   r   r   r   r   utilsr   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   utils._httpr8   utils._runtimer9   r:   utils._typingr;   	utils.shar<   
utils.tqdmr=   
get_loggerrg   r   objectre  _CACHED_NO_EXIST_Tcompiler   rE  rY  r>   rI   boolrk   r_   ra   r   Responser   rl   r   r   r   r   r   r  r  r!  r3  r   r9  r7  r6  r[  DEFAULT_REQUEST_TIMEOUTrw  r   rG  rK  rL  ru  r  r  rF  r]   r]   r]   r^   <module>   s  
 ($t	



;Y
4	

 2

h^	
 [


 



 \	Y
	

  
	

R"