o
    h:                  .   @   s  d dl Z d dlmZ d dlmZmZmZmZmZ d dl	Z	d dl
mZ d dlmZ ddlmZ ddlmZmZmZmZ dd	lmZmZmZ dd
lmZmZmZmZ ddlm Z m!Z!m"Z"m#Z# ddlmZ$ e"%e&Z'e#ddddddddej(ddddddddddddde)dee) dee) dee)edf dee)edf dee) dee) deeee)f  dee de*de+deee+e)f  de+deeee) e)f  deeee) e)f  de,d ee d!eee)e)f  d"ee) d#ee+ed f d$ee+ d%e)f,d&d'Z-dS )(    N)Path)DictListLiteralOptionalUnion)tqdm)
thread_map   )	constants)GatedRepoErrorLocalEntryNotFoundErrorRepositoryNotFoundErrorRevisionNotFoundError)REGEX_COMMIT_HASHhf_hub_downloadrepo_folder_name)DatasetInfoHfApi	ModelInfo	SpaceInfo)OfflineModeIsEnabledfilter_repo_objectsloggingvalidate_hf_hub_argsF   auto)	repo_typerevision	cache_dir	local_dirlibrary_namelibrary_version
user_agentproxiesetag_timeoutforce_downloadtokenlocal_files_onlyallow_patternsignore_patternsmax_workers
tqdm_classheadersendpointlocal_dir_use_symlinksresume_downloadrepo_idr   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   returnc          !         s   du rt j |du rt j}t trt  du rdt jvr/td dtt j tj	
 td}d}d}|sztd}|j|d}W nC tjjtjjfyb     tjjtjjtfy| } z|}W Y d}~n d}~w ty     tjy } z|}W Y d}~nd}~ww |du r-dt|r|n&tj	
|d|}tj	|rt|}| W d   n1 sw   Y  durtj	
|d	}tj	|r|S durt rt rt d
 d| d t! S |rt"dt|trt"d|t|t#s&t|t$r(|t"d||j%dus7J d|j&dusAJ dt't(dd |j&D ||d}|j%tj	
|d	}|krtj	
|d|}z(tj)tj	*|dd t|d}|+ W d   n	1 sw   Y  W n t,y } zt d| d| d W Y d}~nd}~ww dtf 	
fdd}t j-r|D ]} ||  qnt.||dt/| d||pt0d  durttj	1S |S )!a(  Download repo files.

    Download a whole snapshot of a repo's files at the specified revision. This is useful when you want all files from
    a repo, because you don't know which ones you will need a priori. All files are nested inside a folder in order
    to keep their actual filename relative to that folder. You can also filter which files to download using
    `allow_patterns` and `ignore_patterns`.

    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.

    An alternative would be to clone the repo but this requires git and git-lfs to be installed and properly
    configured. It is also not possible to filter which files to download when cloning a repository using git.

    Args:
        repo_id (`str`):
            A user or an organization name and a repo name separated by a `/`.
        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.
        cache_dir (`str`, `Path`, *optional*):
            Path to the folder where cached files are stored.
        local_dir (`str` or `Path`, *optional*):
            If provided, the downloaded files will be placed under this directory.
        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 (`str`, `dict`, *optional*):
            The user-agent info in the form of a dictionary or a string.
        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`.
        force_download (`bool`, *optional*, defaults to `False`):
            Whether the file should be downloaded even if it already exists in the local cache.
        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.
        headers (`dict`, *optional*):
            Additional headers to include in the request. Those headers take precedence over the others.
        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.
        allow_patterns (`List[str]` or `str`, *optional*):
            If provided, only files matching at least one pattern are downloaded.
        ignore_patterns (`List[str]` or `str`, *optional*):
            If provided, files matching any of the patterns are not downloaded.
        max_workers (`int`, *optional*):
            Number of concurrent threads to download files (1 thread = 1 file download).
            Defaults to 8.
        tqdm_class (`tqdm`, *optional*):
            If provided, overwrites the default behavior for the progress bar. Passed
            argument must inherit from `tqdm.auto.tqdm` or at least mimic its behavior.
            Note that the `tqdm_class` is not passed to each individual download.
            Defaults to the custom HF progress bar that can be disabled by setting
            `HF_HUB_DISABLE_PROGRESS_BARS` environment variable.

    Returns:
        `str`: folder path of the repo snapshot.

    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.
        [`EnvironmentError`](https://docs.python.org/3/library/exceptions.html#EnvironmentError)
            If `token=True` and 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.
    NmodelzInvalid repo type: z. Accepted repo types are: )r1   r   )r!   r"   r#   r.   r-   )r1   r   r   r'   refs	snapshotszReturning existing local_dir `z<` as remote repo cannot be accessed in `snapshot_download` (z).zCannot find an appropriate cached snapshot folder for the specified revision on the local disk and outgoing traffic has been disabled. To enable repo look-ups and downloads online, pass 'local_files_only=False' as input.zCannot find an appropriate cached snapshot folder for the specified revision on the local disk and outgoing traffic has been disabled. To enable repo look-ups and downloads online, set 'HF_HUB_OFFLINE=0' as environment variable.zAn error happened while trying to locate the files on the Hub and we cannot find the appropriate snapshot folder for the specified revision on the local disk. Please check your internet connection and try again.z8Repo info returned from server must have a revision sha.z9Repo info returned from server must have a siblings list.c                 S   s   g | ]}|j qS  )	rfilename).0fr6   r6   v/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/huggingface_hub/_snapshot_download.py
<listcomp>   s    z%snapshot_download.<locals>.<listcomp>)itemsr)   r*   T)exist_okwz+Ignored error while writing commit hash to z: .	repo_filec                    sl   t fi d| dddd dd	dd	d
d
dddddS )Nfilenamer   r   r.   r   r    r/   r!   r"   r#   r$   r%   r0   r&   r'   r-   )r   )r@   r   commit_hashr.   r%   r&   r-   r!   r"   r    r/   r$   r1   r   r0   r'   r#   r6   r:   _inner_hf_hub_download  sF   	
z1snapshot_download.<locals>._inner_hf_hub_downloadz	Fetching z files)descr+   r,   )2r   HF_HUB_CACHEDEFAULT_REVISION
isinstancer   str
REPO_TYPES
ValueErrorospathjoinr   r   	repo_inforequests
exceptionsSSLError
ProxyErrorConnectionErrorTimeoutr   r   	HTTPErrorr   matchexistsopenreadis_diranyiterdirloggerwarningresolver   r   r   shasiblingslistr   makedirsdirnamewriteOSErrorHF_HUB_ENABLE_HF_TRANSFERr	   lenhf_tqdmrealpath)!r1   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   storage_folderrO   api_call_errorapierrorref_pathr9   snapshot_folderfiltered_repo_fileserD   filer6   rB   r:   snapshot_download   s   l







$0
	ru   ).rL   pathlibr   typingr   r   r   r   r   rP   	tqdm.autor   	base_tqdmtqdm.contrib.concurrentr	    r   errorsr   r   r   r   file_downloadr   r   r   hf_apir   r   r   r   utilsr   r   r   r   rj   
get_logger__name__r^   DEFAULT_ETAG_TIMEOUTrI   floatboolintru   r6   r6   r6   r:   <module>   s    
	
