o
    id=                     @   sN  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	m
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mZ d dl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#m$Z$ d
dl%m&Z& e'e(Z)ej*+ d  Z,e-e.d< e/dd e,+dD dk rd dl0Z0nd dl1m2Z0 dZ3dZ4z	e0*dZ3dZ4W n
 e0j5y   Y nw dZ6dZ7z	e0*dZ6dZ7W n
 e0j5y   Y nw dd Z8dd Z9			dHde-de-dee- dee- d ee- d!e-fd"d#Z:dId$e-d%ee- d!e-fd&d'Z;dId!ee-e-f fd(d)Z<			dHd*ee- d+ee- d,eee-df d!e-fd-d.Z=G d/d0 d0e>Z?dId1ee- fd2d3Z@	 	4	5	6dJd7e-d$e-d8eAd9eBd:eBd;eBd!ejCfd<d=ZD		 		6	 dKd$e-d>ed?eee-e-f  fd@dAZE								B			dLd$e-d*ee- d+ee- dCee-edf d,eee-df dDee- dEeeFe-df d!ee- fdFdGZGdS )M    N)contextmanager)partial)sha256)Path)BinaryIODictOptionalTupleUnion)tqdm)FileLock)	constants   )__version__)HUGGINGFACE_CO_URL_TEMPLATEHUGGINGFACE_HUB_CACHEREPO_TYPE_DATASETREPO_TYPE_DATASET_URL_PREFIX
REPO_TYPES)HfFolder_PY_VERSIONc                 c   s    | ]}t |V  qd S N)int).0i r   X/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/huggingface_hub/file_download.py	<genexpr>%   s    r   .)      r   zN/AFtorchT
tensorflowc                   C      t S r   )_torch_availabler   r   r   r   is_torch_available;      r%   c                   C   r#   r   )_tf_availabler   r   r   r   is_tf_available?   r&   r(   repo_idfilename	subfolder	repo_typerevisionreturnc                 C   sR   |dur| d| }|t vrtd|tkrt|  } |du r!d}tj| ||dS )a  
    Resolve a model identifier, a file name, and an optional revision id, to a huggingface.co-hosted url, redirecting
    to Cloudfront (a Content Delivery Network, or CDN) for large files (more than a few MBs).

    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' ETag. An object's ETag is:
    its git-sha1 if stored in git, or its sha256 if stored in git-lfs.
    N/zInvalid repo typemain)r)   r-   r*   )r   
ValueErrorr   r   r   format)r)   r*   r+   r,   r-   r   r   r   
hf_hub_urlC   s   r3   urletagc                 C   sN   |  d}t| }|r| d}|dt|  7 }| dr%|d7 }|S )a  
    Convert `url` into a hashed filename in a repeatable way. If `etag` is specified, append its hash to the url's,
    delimited by a period. If the url ends with .h5 (Keras HDF5 weights) adds '.h5' to the name so that TF 2.0 can
    identify it as a HDF5 file (see
    https://github.com/tensorflow/tensorflow/blob/00fad90125b18b80fe054de1055770cfb8fe4ba3/tensorflow/python/keras/engine/network.py#L1380)
    utf-8r   z.h5)encoder   	hexdigestendswith)r4   r5   	url_bytesr*   
etag_bytesr   r   r   url_to_filenamei   s   


r<   c                 C   s   |du rt }t|trt|}tj|| }tj|s#td	||d }tj|s4td	|t
|dd}t|}W d   n1 sJw   Y  |d }|d }||fS )z
    Return the url and etag (which may be ``None``) stored for `filename`. Raise ``EnvironmentError`` if `filename` or
    its stored metadata do not exist.
    Nzfile {} not found.jsonr6   )encodingr4   r5   )r   
isinstancer   strospathjoinexistsEnvironmentErrorr2   openjsonload)r*   	cache_dir
cache_path	meta_path	meta_filemetadatar4   r5   r   r   r   filename_to_url}   s    
rN   library_namelibrary_version
user_agentc                 C   s   | durd | |}nd}|d t7 }|d t7 }t r%|d t7 }t r/|d t7 }t|trF|dd	d	d
 |
 D  7 }|S t|trQ|d| 7 }|S )zF
    Formats a user-agent string with basic info about a request.
    N{}/{}zunknown/Nonez; hf_hub/{}z; python/{}z
; torch/{}z; tensorflow/{}z; c                 s   s     | ]\}}d  ||V  qdS )rR   N)r2   )r   kvr   r   r   r      s    z"http_user_agent.<locals>.<genexpr>)r2   r   r   r%   _torch_versionr(   _tf_versionr?   dictrC   itemsr@   )rO   rP   rQ   uar   r   r   http_user_agent   s   
 
rZ   c                   @   s   e Zd ZdS )OfflineModeIsEnabledN)__name__
__module____qualname__r   r   r   r   r[      s    r[   msgc                 C   s(   t jrt| du rddt|  dS )z[Raise a OfflineModeIsEnabled error (subclass of ConnectionError) if HF_HUB_OFFLINE is True.NzOffline mode is enabled.zOffline mode is enabled. )r   HF_HUB_OFFLINEr[   r@   )r_   r   r   r   !_raise_if_offline_mode_is_enabled   s   
ra         ?         $@methodmax_retriesbase_wait_timemax_wait_timetimeoutc                 K   s   t d|  d\}}|s`|d7 }ztjd|  ||d|}	d}W n: tjjy] }
 z,||kr3|
t|  d| d||  d t||d	|d   }t	
| W Y d
}
~
nd
}
~
ww |r|	S )a  Wrapper around requests to retry in case it fails with a ConnectTimeout, with exponential backoff.

    Note that if the environment variable HF_HUB_OFFLINE is set to 1, then a OfflineModeIsEnabled error is raised.

    Args:
        method (str): HTTP method, such as 'GET' or 'HEAD'
        url (str): The URL of the ressource to fetch
        max_retries (int): Maximum number of retries, defaults to 0 (no retries)
        base_wait_time (float): Duration (in seconds) to wait before retrying the first time. Wait time between
            retries then grows exponentially, capped by max_wait_time.
        max_wait_time (float): Maximum amount of time between two retries, in seconds
        **params: Params to pass to `requests.request`
    zTried to reach )r   Fr   )re   r4   ri   Tz request to z timed out, retrying... []rc   Nr   )ra   requestsrequestupper
exceptionsConnectTimeoutloggerinfomintimesleep)re   r4   rf   rg   rh   ri   paramstriessuccessresponseerr
sleep_timer   r   r   _request_with_retry   s2   
r{   	temp_fileheadersc              	   C   s   t |}|dkrd|f |d< td| d||||d}|  |jd}|dur/|t| nd}	td	d|	|d
tt	
 tjkd}
|jddD ]}|rX|
t| || qH|
  dS )z8
    Donwload remote file. Do not gobble up errors.
    r   z	bytes=%d-RangeGETT)re   r4   streamproxiesr}   ri   rf   zContent-LengthNBDownloading)unit
unit_scaletotalinitialdescdisablei   )
chunk_size)copydeepcopyr{   raise_for_statusr}   getr   r   boolrp   getEffectiveLevelloggingNOTSETiter_contentupdatelenwriteclose)r4   r|   r   resume_sizer}   ri   rf   rcontent_lengthr   progresschunkr   r   r   http_get   s:   
	
r   
   rI   force_filenameuse_auth_tokenc              
      s  |du rt }t|trt|}tj|dd dt|||di}t|
tr,d|
|d< n|
rAt	 }|du r:t
dd||d< | }d}|sz6td	| |d
||d}|  |jdpa|jd}|du rjtdd|j  krudkr|n n|jd }W n tjjtjjfy     tjjtjjtfy   Y nw |dur|nt| |}tj||}|du rtj|r|s|S dd tt||dd d D }t |dkr|s|du rtj||d S |rt!dt!dtj|r|s|S |d }t"| tj|r|s|W  d   S |	r8|d  t#d) fdd}|}tj r5t$ j%}nd}nt&t'j(d |d
d!}d}| }t)*d"| |j+ t,|||||d# W d   n	1 scw   Y  t)*d$| | t-|j+| |du rt)*d%| | |d&}|d' }t.|d(}t/0|| W d   n1 sw   Y  W d   |S W d   |S W d   |S 1 sw   Y  |S )*ar  
    Given a URL, look for the corresponding file in the local cache. If it's not there, download it. Then return the
    path to the cached file.

    Return:
        Local path (string) of file or if networking is off, last version of file cached on disk.

    Raises:
        In case of non-recoverable file (non-existent or inaccessible url + no cache on disk).
    NT)exist_okz
user-agent)rO   rP   rQ   z	Bearer {}authorizationzIYou specified use_auth_token=True, but a huggingface token was not found.HEADF)re   r4   r}   allow_redirectsr   ri   zX-Linked-EtagETagz\Distant resource does not have an ETag, we won't be able to reliably ensure reproducibility.i,  i  Locationc                 S   s$   g | ]}| d s| ds|qS )r=   .lock)r9   )r   filer   r   r   
<listcomp>y  s    z#cached_download.<locals>.<listcomp>r   r   z.*zCannot find the requested files in the cached path and outgoing traffic has been disabled. To enable model look-ups and downloads online, set 'local_files_only' to False.zConnection error, and we cannot find the requested files in the cached path. Please try again or make sure your Internet connection is on.r   z.incompleter.   io.BufferedWriterc                  3   s8    t  d} | V  W d    d S 1 sw   Y  d S )Nab)rF   )fincomplete_pathr   r   _resumable_file_manager  s   "z0cached_download.<locals>._resumable_file_managerwb)modedirdeletezdownloading %s to %s)r   r   r}   zstoring %s in cache at %szcreating metadata file for %s)r4   r5   r=   w)r.   r   )1r   r?   r   r@   rA   makedirsrZ   r2   r   	get_tokenrE   r{   r   r}   r   OSErrorstatus_coderk   rn   SSLError
ProxyErrorConnectionErrorTimeoutr[   r<   rB   rC   rD   fnmatchfilterlistdirsplitr   r1   r   r   statst_sizer   tempfileNamedTemporaryFilerp   rq   namer   replacerF   rG   dump)r4   rO   rP   rI   rQ   force_downloadr   r   etag_timeoutresume_downloadr   local_files_onlyr}   tokenurl_to_downloadr5   r   r*   rJ   matching_files	lock_pathr   temp_file_managerr   r|   metarK   rL   r   r   r   cached_download  s   








 
1
1
11r   )NNNr   )r   rb   rc   rd   )Nr   Nrd   r   )NNNNFNNr   FNF)Hr   r   iorG   r   rA   sysr   rs   
contextlibr   	functoolsr   hashlibr   pathlibr   typingr   r   r   r	   r
   	tqdm.autor   rk   filelockr   huggingface_hubr    r   r   r   r   r   r   hf_apir   	getLoggerr\   rp   versionr   r   r@   __annotations__tupleimportlib_metadataimportlib.metadatarM   rU   r$   PackageNotFoundErrorrV   r'   r%   r(   r3   r<   rN   rZ   r   r[   ra   r   floatResponser{   r   r   r   r   r   r   r   <module>   s  
 




&

0
,