o
    *j|3                     @   s  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mZ d dlmZm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 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& e" Z'd)deee(e)f  de(fddZ*				d*de(de(de+de,de,de,dej-fddZ.	 d+dej-fdd Z/	d,deej- fd"d#Z0		$		%	$		$		 		&	$			d-de(fd'd(Z1dS ).    N)contextmanager)partial)Path)OptionalUnion)urljoinurlparse)tqdm)config)hash_url_to_filename"get_authentication_headers_for_urlfsspec_head
fsspec_get)FileLock)MS_DATASETS_CACHE)
get_logger)HubApi)__version__
user_agentreturnc                 C   s   dt  }|dtj 7 }|dtj 7 }tjr |dtj 7 }tjr+|dtj 7 }tjr6|dtj	 7 }t
| trN|dddd	 |  D  7 }|S t
| trY|d|  7 }|S )
Nz	datasets/z	; python/z
; pyarrow/z; torch/z; tensorflow/z; jax/z; c                 s   s"    | ]\}}| d | V  qdS )/N ).0kvr   r   j/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/msdatasets/utils/hf_file_utils.py	<genexpr>-   s     z-get_datasets_user_agent_ms.<locals>.<genexpr>)r   r
   Z
PY_VERSIONZPYARROW_VERSIONZTORCH_AVAILABLEZTORCH_VERSIONZTF_AVAILABLEZ
TF_VERSIONZJAX_AVAILABLEZJAX_VERSION
isinstancedictjoinitemsstr)r   Zuar   r   r   get_datasets_user_agent_ms"   s   

"
r"            ?      $@methodurlmax_retriesbase_wait_timemax_wait_timetimeoutc                 K   s   d\}}d}	|s_|d7 }zt jd
|  ||d|}	d}W n> t jjt jjfy\ }
 z,||kr2|
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_DATASETS_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 resource 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 (additional keyword arguments): Params to pass to :obj:`requests.request`.
    )r   FN   )r&   r'   r+   Tz request to z timed out, retrying... []r#   r   )requestsrequestupper
exceptionsConnectTimeoutConnectionErrorloggerinfomintimesleep)r&   r'   r(   r)   r*   r+   paramstriessuccessresponseerrZ
sleep_timer   r   r   _request_with_retry_ms3   s"    r>   Tc              
   C   s>   t |pi }t|dd|d< td| ||||||d}|S )N
user-agentr   HEAD)r&   r'   proxiesheaderscookiesallow_redirectsr+   r(   )copydeepcopyr"   getr>   )r'   rB   rC   rD   rE   r+   r(   r<   r   r   r   http_head_msZ   s   
rI         Y@c	              
   C   s   |d urt |ni }t|dd|d< |dkr!d|dd|d< td| d	|||||d
}	|d u r3|	S |	jdkr:d S |	jd}
|
d urJ|t|
 nd }t||d	d|pTdd}|	jddD ]}|	t
| || q]|  d S )Nr?   r@   r   zbytes=d-RangeGETT)r&   r'   streamrB   rC   rD   r(   r+   i  zContent-LengthBZDownloading)totalinitialZ
unit_scaleunitdesci   )
chunk_size)r   r"   rH   r>   status_coderC   intr	   iter_contentupdatelenwriteclose)r'   	temp_filerB   resume_sizerC   rD   r+   r(   rT   r<   content_lengthrQ   progresschunkr   r   r   http_get_msl   s2   

rb   Fd   
deprecatedc           %         s@  |dkrt d| dt |}
|du rt}t|trt|}tj|dd |r1t	| t
| j}n| }d}d}d}d}d}d}t|dd}tj||}|du rVd	| d
 }tj|rb|sb|sb|S t| |
d}|durp||d< |sTt
| j}|dvrt| |d}|r|ddp|ddnd}d}zt j|
d}t| d|||	||d}|jdkr|r|jdnd}|j D ]\}}|drd| v r| d| 7 } |j}qd}d| v rd| vr| d7 } nX|jdkrd| v s|jdkrd| v s|jdkr
td| std|js|jdkr d| v r d}td |   n|jd!kr9t j!| v r9|
du r9t"d"|  d#W n t#t$j%j&fyS } z|}W Y d}~nd}~ww |stj|rc|sc|S |rnt'd$| d%|dur|jd&krt'd'|  |durt"d(|  d)t(| d*|durt"d(|  d+|j d*t"d(|  t||}tj||}tj|r|s|S |d, }t)| tj|r|s|W  d   S |d-  t*d? fd/d0	}d1}|rt+|d2d3}tj rt, j-}| "} |dvrt.| | ||d4 nt/| | |||||	|d5 W d   n	1 s,w   Y  td6|  d7|  t01| j2| t3d8}!t3|! t4|d8|! @  td9|  | |d:}"|d; }#t5|#d<d=d>}$t67|"|$ W d   n1 sw   Y  W d   |S W d   |S 1 sw   Y  |S )@a  
    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)

    Raises:
        FileNotFoundError: in case of non-recoverable file
            (non-existent or no cache on disk)
        ConnectionError: in case of unreachable url
            and no cache on disk
    rd   z'use_auth_token' was deprecated in favor of 'token' in version 2.14.0 and will be removed in 3.0.0.
You can remove this warning by passing 'token=z
' instead.NT)exist_okF)etagzDownloading [r-   )tokenr?   )httphttps)storage_optionsETagrf   )Zaccess_token)rE   rB   r+   r(   rC   rD      Zdownload_warningzdrive.google.comz	&confirm=zconfirm=z
&confirm=ti  zfirebasestorage.googleapis.comi  i  z7^https?://github.com/.*?/.*?/releases/download/.*?/.*?$z#^https://.*?s3.*?amazonaws.com/.*?$zndownloader.figstatic.comz"Couldn't get ETag version for url i  zUnauthorized for URL zU. Please use the parameter `token=True` after logging in with `huggingface-cli login`z6Cannot find the requested files in the cached path at zi and outgoing traffic has been disabled. To enable file online look-ups, set 'local_files_only' to False.i  zCouldn't find file at zCouldn't reach z ()z (error z.lockz.incompletew+bc                 3   s8    t  | }|V  W d    d S 1 sw   Y  d S N)open)modefZincomplete_pathr   r   temp_file_manager0  s   "z,get_from_cache_ms.<locals>.temp_file_managerr   za+b)rq   )rj   rT   )r]   rB   r^   rC   rD   r(   rT   zstoring z in cache at i  zcreating metadata file for )r'   rf   z.jsonwzutf-8)encoding)rn   )8warningswarnFutureWarningr   r   r   r!   osmakedirsr   r   pathr   r   existsr   schemer   rH   r   Zget_cookiesrI   rV   rC   rD   r    
startswithrematchr'   r4   r5   r
   ZHF_ENDPOINTr3   OSErrorr.   r1   TimeoutFileNotFoundErrorreprr   r   r   statst_sizer   rb   shutilmovenameumaskchmodrp   jsondump)%r'   	cache_dirZforce_downloadrB   Zetag_timeoutZresume_downloadr   Zlocal_files_onlyZuse_etagr(   rg   Zuse_auth_tokenZignore_url_paramsrj   Zdownload_descZdisable_tqdmZ
cached_url	connectedr<   rD   rf   Z
head_errorr~   filename
cache_pathrC   r   r   eZ	lock_pathrt   r^   r]   r   meta	meta_pathZ	meta_filer   rs   r   get_from_cache_ms   s"  

 
	
"









 
1
11r   ro   )r#   r$   r#   r%   )NNNTr%   r   )Nr   NNrJ   r   N)NFNrc   FNFTr   Nrd   FNNN)2r   rz   r   rF   r   r7   rw   
contextlibr   	functoolsr   pathlibr   typingr   r   urllib.parser   r   r.   Z	tqdm.autor	   Zdatasetsr
   Zdatasets.utils.file_utilsr   r   r   r   filelockr   Zmodelscope.utils.config_dsr   Zmodelscope.utils.loggerr   Zmodelscope.hub.apir   Z
modelscoper   r4   r!   r   r"   rW   floatResponser>   rI   rb   r   r   r   r   r   <module>   s    
(

"