
    Zi                        U d 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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mZm Z m!Z!m"Z"m#Z#m$Z$m%Z% ddlm&Z& ddl'm(Z( ddl)m*Z*  e&j+        e,          Z- ed           G d d                      Z. ej/        d          Z0 ej/        d          Z1dee2e2f         dee.         fdZ3dZ4dZ5dZ6 ej/        dej7                  Z8 ej/        dej7                  Z9dej:        ddfd Z;dej:        ddfd!Z<d"ej=        ddfd#Z>dej?        fd$Z@dejA        fd%ZBeg ej?        f         ZCeg ejA        f         ZD ejE                    ZFe@aGeCeHd&<   eBaIeDeHd'<   daJeej?                 eHd(<   d)eCddfd*ZKd+eDddfd,ZLdej?        fd-ZMdejA        fd.ZNdVd/ZO ejP        eO            eQed0          r ejR        eO1           ejS        ejT        fZUeVeWeX         d2f         eHd3<   d4ZYeVeZd2f         eHd5<   d6dd7eUeYd8d9d:e*d;e2d<eZd=e[d>e[d?eeWeX         eVeWeX         d2f         f         d@eeZeVeZd2f         f         dAe\deej=        ddf         fdBZ]d6dd7eUeYdCd:e*d;e2d<eZd=e[d>e[d?eeWeX         eVeWeX         d2f         f         d@eeZeVeZd2f         f         dej=        fdDZ^e
d6dd7eUeYdCd:e*d;e2d<eZd=e[d>e[d?eeWeX         eVeWeX         d2f         f         d@eeZeVeZd2f         f         deej=        ddf         fdE            Z_d8dFd:e*d;e2dGe\dej=        fdHZ`d;e2dIee2         de2fdJZadWd"ej=        dKee2         ddfdLZb ec            Zdd"ej=        ddfdMZedNeWe"         dOe2d"ej=        de"fdPZfdej:        de2fdQZg ej/        dRejh                  ZidSee2         dTeZdee2         fdUZjdS )Xz>Contains utilities to handle HTTP requests in huggingface_hub.    N)contextmanager)	dataclass)quote)AnyCallable	GeneratorMappingOptionalUnion)urlparse)OfflineModeIsEnabled   )	constants)BadRequestErrorBucketNotFoundErrorDisabledRepoErrorGatedRepoErrorHfHubHTTPErrorRemoteEntryNotFoundErrorRepositoryNotFoundErrorRevisionNotFoundError   )logging)SliceFileObj)HTTP_METHOD_TT)frozenc                   f    e Zd ZU dZeed<   eed<   eed<   dZee         ed<   dZ	ee         ed<   dS )RateLimitInfoa  
    Parsed rate limit information from HTTP response headers.

    Attributes:
        resource_type (`str`): The type of resource being rate limited.
        remaining (`int`): The number of requests remaining in the current window.
        reset_in_seconds (`int`): The number of seconds until the rate limit resets.
        limit (`int`, *optional*): The maximum number of requests allowed in the current window.
        window_seconds (`int`, *optional*): The number of seconds in the current window.

    resource_type	remainingreset_in_secondsNlimitwindow_seconds)
__name__
__module____qualname____doc__str__annotations__intr"   r
   r#        V/root/projects/butler/venv/lib/python3.11/site-packages/huggingface_hub/utils/_http.pyr   r   6   sg         
 
 NNNE8C=$(NHSM(((((r,   r   zL\"(?P<resource_type>\w+)\"\s*;\s*r\s*=\s*(?P<r>\d+)\s*;\s*t\s*=\s*(?P<t>\d+)z'q\s*=\s*(?P<q>\d+).*?w\s*=\s*(?P<w>\d+)headersreturnc                 `   d}d}| D ]3}|                                 }|dk    r	| |         }%|dk    r| |         }4|sdS t                              |          }|sdS |                    d          }t	          |                    d                    }t	          |                    d                    }d}	d}
|r`t
                              |          }|rDt	          |                    d                    }	t	          |                    d                    }
t          ||||	|
	          S )
a*  Parse rate limit information from HTTP response headers.

    Follows IETF draft: https://www.ietf.org/archive/id/draft-ietf-httpapi-ratelimit-headers-09.html
    Only a subset is implemented.

    Example:
    ```python
    >>> from huggingface_hub.utils import parse_ratelimit_headers
    >>> headers = {
    ...     "ratelimit": '"api";r=0;t=55',
    ...     "ratelimit-policy": '"fixed window";"api";q=500;w=300',
    ... }
    >>> info = parse_ratelimit_headers(headers)
    >>> info.remaining
    0
    >>> info.reset_in_seconds
    55
    ```
    N	ratelimitzratelimit-policyr   rtqw)r   r    r!   r"   r#   )lower_RATELIMIT_REGEXsearchgroupr*   _RATELIMIT_POLICY_REGEXr   )r.   r1   policykey	lower_keymatchr   r    r!   r"   r#   policy_matchs               r-   parse_ratelimit_headersr@   R   sP   *  $I F " "IIKK	##II,,,S\F t##I..E tKK00MEKK$$%%I5;;s++,,E$(N :.55f== 	:**3//00E !3!3C!8!899N#)%   r,   zx-request-idzX-Amzn-Trace-Idzx-amz-cf-ida  
        # staging or production endpoint
        ^https://[^/]+
        (
            # on /api/repo_type/repo_id
            /api/(models|datasets|spaces)/(.+)
            |
            # or /repo_id/resolve/revision/...
            /(.+)/resolve/(.+)
        )
    )flagszz
        # staging or production endpoint
        ^https?://[^/]+
        # on /api/buckets/...
        /api/buckets/
    requestc           	      :   t          j                    rt          d| j         d          t          | j        vrL| j                            t                    pt          t          j
                              | j        t          <   | j                            t                    }t                              d|| j        | j        | j                            d          du           t           j        r(t                              dt          |                      |S )z
    Event hook that will be used to make HTTP requests to the Hugging Face Hub.

    What it does:
    - Block requests if offline mode is enabled
    - Add a request ID to the request headers
    - Log the request if debug mode is enabled
    zCannot reach za: offline mode is enabled. To disable it, please unset the `HF_HUB_OFFLINE` environment variable.z%Request %s: %s %s (authenticated: %s)authorizationNzSend: %s)r   is_offline_moder   urlX_AMZN_TRACE_IDr.   getX_REQUEST_IDr(   uuiduuid4loggerdebugmethodHF_DEBUG_curlify)rB   
request_ids     r-   hf_request_event_hookrR      s
     "" 
" KGK  K  K  K
 
 	

 go--+2?+>+>|+L+L+aPSTXT^T`T`PaPa($$_55J LL/O,,D8    4Z'!2!2333r,   c                 $   K   t          |           S )z3
    Async version of `hf_request_event_hook`.
    )rR   )rB   s    r-   async_hf_request_event_hookrT      s       !)))r,   responsec                    K   | j         dk    rXd| j        v rQ	 t          | j        d                   }n# t          $ r Y d S w xY w|dk     r |                                  d {V  d S d S d S d S )N  zContent-lengthi@B )status_coder.   r*   
ValueErroraread)rU   lengths     r-   async_hf_response_event_hookr\      s      s"" x///X-.>?@@    	!!nn&&&&&&&&&&& #" 0/ "!s   3 
A Ac                  @    t          j        dt          gidd          S )zQ
    Factory function to create a `httpx.Client` with the default transport.
    rB   TNevent_hooksfollow_redirectstimeout)httpxClientrR   r+   r,   r-   default_client_factoryrd      s/     <!6 78   r,   c                  N    t          j        t          gt          gddd          S )zV
    Factory function to create a `httpx.AsyncClient` with the default transport.
    )rB   rU   TNr^   )rb   AsyncClientrT   r\   r+   r,   r-   default_async_client_factoryrg      s6     !< =LhKijj   r,   _GLOBAL_CLIENT_FACTORY_GLOBAL_ASYNC_CLIENT_FACTORY_GLOBAL_CLIENTclient_factoryc                 f    t           5  t                       | addd           dS # 1 swxY w Y   dS )a  
    Set the HTTP client factory to be used by `huggingface_hub`.

    The client factory is a method that returns a `httpx.Client` object. On the first call to [`get_client`] the client factory
    will be used to create a new `httpx.Client` object that will be shared between all calls made by `huggingface_hub`.

    This can be useful if you are running your scripts in a specific environment requiring custom configuration (e.g. custom proxy or certifications).

    Use [`get_client`] to get a correctly configured `httpx.Client`.
    N)_CLIENT_LOCKclose_sessionrh   )rk   s    r-   set_client_factoryro     s}     
 0 0!/0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0s   &**async_client_factoryc                 
    | a dS )a  
    Set the HTTP async client factory to be used by `huggingface_hub`.

    The async client factory is a method that returns a `httpx.AsyncClient` object.
    This can be useful if you are running your scripts in a specific environment requiring custom configuration (e.g. custom proxy or certifications).
    Use [`get_async_client`] to get a correctly configured `httpx.AsyncClient`.

    <Tip warning={true}>

    Contrary to the `httpx.Client` that is shared between all calls made by `huggingface_hub`, the `httpx.AsyncClient` is not shared.
    It is recommended to use an async context manager to ensure the client is properly closed when the context is exited.

    </Tip>
    Nri   )rp   s    r-   set_async_client_factoryrs     s      $8   r,   c                  x    t           -t          5  t                      a ddd           n# 1 swxY w Y   t           S )a  
    Get a `httpx.Client` object, using the transport factory from the user.

    This client is shared between all calls made by `huggingface_hub`. Therefore you should not close it manually.

    Use [`set_client_factory`] to customize the `httpx.Client`.
    N)rj   rm   rh   r+   r,   r-   get_sessionru   &  sv      	6 	6355N	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6s   *..c                      t                      S )a  
    Return a `httpx.AsyncClient` object, using the transport factory from the user.

    Use [`set_async_client_factory`] to customize the `httpx.AsyncClient`.

    <Tip warning={true}>

    Contrary to the `httpx.Client` that is shared between all calls made by `huggingface_hub`, the `httpx.AsyncClient` is not shared.
    It is recommended to use an async context manager to ensure the client is properly closed when the context is exited.

    </Tip>
    rr   r+   r,   r-   get_async_sessionrw   5  s     ()))r,   c                      t           } da | L	 |                                  dS # t          $ r(}t                              d|            Y d}~dS d}~ww xY wdS )z
    Close the global `httpx.Client` used by `huggingface_hub`.

    If a Client is closed, it will be recreated on the next call to [`get_session`].

    Can be useful if e.g. an SSL certificate has been updated.
    NzError closing client: )rj   close	ExceptionrL   warning)clientes     r-   rn   rn   E  s     F N 	9LLNNNNN 	9 	9 	9NN7A77888888888	9 s   # 
AAAregister_at_fork)after_in_child._DEFAULT_RETRY_ON_EXCEPTIONS)  i  i  i  i  _DEFAULT_RETRY_ON_STATUS_CODES      F)max_retriesbase_wait_timemax_wait_timeretry_on_exceptionsretry_on_status_codesstreamrN   rF   r   r   r   r   r   r   c          	   +   `   K   t          |t                    r|f}t          t                    rfd|}	dd}
d|v rAt          |d         t          j        t
          f          r|d                                         }
t                      }	 dz  d	 |
|d                             |
           dt          j
        dt          f fd}|rG |j        d d	|5 } ||          s|V  	 ddd           dS 	 ddd           n# 1 swxY w Y   n! |j        d d	|} ||          s|V  dS ne# |$ r]}t                              d
| d  d            t          |t          j                  rt#                       k    r|Y d}~nd}~ww xY w7t%                    dz   }t                              d| d d d           n&|	}t                              d| d d d           t'          j        |           t+          ||	dz            }	)zfInternal implementation of HTTP backoff logic shared between `http_backoff` and `http_stream_backoff`.r   NdataTr   rU   r/   c                     | j         vrdS t                              d| j          d d            k    rt          |            dS | j         dk    rt	          | j                  }||j        dS )zNHandle response and return True if should retry, False if should return/yield.FzHTTP Error z thrown while requesting  r   NT)rX   rL   r{   hf_raise_for_statusr@   r.   r!   )rU   ratelimit_infor   rN   nb_triesratelimit_resetr   rF   s     r-   _should_retryz)_http_backoff_base.<locals>._should_retry  s     '/DDD 5 jX-Ajj\bjjehjjkkkk))'111 !5 '3..%<X=M%N%NN%1*8*Itr,   rN   rF   'z' thrown while requesting r   zRate limited. Waiting zs before retry [Retry /z].zRetrying in z	s [Retry r   r+   )
isinstancetyper*   ioIOBaser   tellru   seekrb   Responseboolr   rB   rL   r{   ConnectErrorrn   floattimesleepmin)rN   rF   r   r   r   r   r   r   kwargs
sleep_timeio_obj_initial_posr|   r   rU   erractual_sleepr   r   s   ```   `         @@r-   _http_backoff_baser   d  s]      %t,, 524'-- 9!6 8HJ%)O
 Jvf~	<7PQQ#F^0022]]F?8A0	 "-v##$6777 4           .  	"V]D&cDDVDD (=22 &                     
 *6>KSKKFKK$}X.. "NNNF" 	 	 	NNLsLLfLLsLLMMM#u122  +%%	 &%%%%	 & 11A5LNNrLrrX`rrcnrrrssss%LNN[,[[[[K[[[\\\
<    
Q77
?8sD   AE *D;E 	E DE D#E F%AF  F%)r   r   r   r   r   c                F    t          t          d| ||||||dd|          S )a0  Wrapper around httpx to retry calls on an endpoint, with exponential backoff.

    Endpoint call is retried on exceptions (ex: connection timeout, proxy error,...)
    and/or on specific status codes (ex: service unavailable). If the call failed more
    than `max_retries`, the exception is thrown or `raise_for_status` is called on the
    response object.

    Re-implement mechanisms from the `backoff` library to avoid adding an external
    dependencies to `hugging_face_hub`. See https://github.com/litl/backoff.

    Args:
        method (`Literal["GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"]`):
            HTTP method to perform.
        url (`str`):
            The URL of the resource to fetch.
        max_retries (`int`, *optional*, defaults to `5`):
            Maximum number of retries, defaults to 5 (no retries).
        base_wait_time (`float`, *optional*, defaults to `1`):
            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`, *optional*, defaults to `8`):
            Maximum duration (in seconds) to wait before retrying.
        retry_on_exceptions (`type[Exception]` or `tuple[type[Exception]]`, *optional*):
            Define which exceptions must be caught to retry the request. Can be a single type or a tuple of types.
            By default, retry on `httpx.TimeoutException` and `httpx.NetworkError`.
        retry_on_status_codes (`int` or `tuple[int]`, *optional*, defaults to `(429, 500, 502, 503, 504)`):
            Define on which status codes the request must be retried. By default, retries
            on rate limit (429) and server errors (5xx).
        **kwargs (`dict`, *optional*):
            kwargs to pass to `httpx.request`.

    Example:
    ```
    >>> from huggingface_hub.utils import http_backoff

    # Same usage as "httpx.request".
    >>> response = http_backoff("GET", "https://www.google.com")
    >>> response.raise_for_status()

    # If you expect a Gateway Timeout from time to time
    >>> http_backoff("PUT", upload_url, data=data, retry_on_status_codes=504)
    >>> response.raise_for_status()
    ```

    > [!WARNING]
    > When using `requests` it is possible to stream data by passing an iterator to the
    > `data` argument. On http backoff this is a problem as the iterator is not reset
    > after a failed call. This issue is mitigated for file objects or any IO streams
    > by saving the initial position of the cursor (with `data.tell()`) and resetting the
    > cursor between each call (with `data.seek()`). For arbitrary iterators, http backoff
    > will fail. If this is a hard constraint for you, please let us know by opening an
    > issue on [Github](https://github.com/huggingface/huggingface_hub).
    FrN   rF   r   r   r   r   r   r   r+   )nextr   rN   rF   r   r   r   r   r   r   s           r-   http_backoffr     sP    B  
	
#)' 3"7
	
 
	
 
	
 
	
  r,   c             +   @   K   t          d| ||||||dd|E d{V  dS )a  Wrapper around httpx to retry calls on an endpoint, with exponential backoff.

    Endpoint call is retried on exceptions (ex: connection timeout, proxy error,...)
    and/or on specific status codes (ex: service unavailable). If the call failed more
    than `max_retries`, the exception is thrown or `raise_for_status` is called on the
    response object.

    Re-implement mechanisms from the `backoff` library to avoid adding an external
    dependencies to `hugging_face_hub`. See https://github.com/litl/backoff.

    Args:
        method (`Literal["GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"]`):
            HTTP method to perform.
        url (`str`):
            The URL of the resource to fetch.
        max_retries (`int`, *optional*, defaults to `5`):
            Maximum number of retries, defaults to 5 (no retries).
        base_wait_time (`float`, *optional*, defaults to `1`):
            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`, *optional*, defaults to `8`):
            Maximum duration (in seconds) to wait before retrying.
        retry_on_exceptions (`type[Exception]` or `tuple[type[Exception]]`, *optional*):
            Define which exceptions must be caught to retry the request. Can be a single type or a tuple of types.
            By default, retry on `httpx.TimeoutException` and `httpx.NetworkError`.
        retry_on_status_codes (`int` or `tuple[int]`, *optional*, defaults to `(429, 500, 502, 503, 504)`):
            Define on which status codes the request must be retried. By default, retries
            on rate limit (429) and server errors (5xx).
        **kwargs (`dict`, *optional*):
            kwargs to pass to `httpx.request`.

    Example:
    ```
    >>> from huggingface_hub.utils import http_stream_backoff

    # Same usage as "httpx.stream".
    >>> with http_stream_backoff("GET", "https://www.google.com") as response:
    ...     for chunk in response.iter_bytes():
    ...         print(chunk)

    # If you expect a Gateway Timeout from time to time
    >>> with http_stream_backoff("PUT", upload_url, data=data, retry_on_status_codes=504) as response:
    ...     response.raise_for_status()
    ```

    <Tip warning={true}>

    When using `httpx` it is possible to stream data by passing an iterator to the
    `data` argument. On http backoff this is a problem as the iterator is not reset
    after a failed call. This issue is mitigated for file objects or any IO streams
    by saving the initial position of the cursor (with `data.tell()`) and resetting the
    cursor between each call (with `data.seek()`). For arbitrary iterators, http backoff
    will fail. If this is a hard constraint for you, please let us know by opening an
    issue on [Github](https://github.com/huggingface/huggingface_hub).

    </Tip>
    Tr   Nr+   )r   r   s           r-   http_stream_backoffr     so      L " 
%#/3
 
 
 
 
 
 
 
 
 
 
 
 
r,   )retry_on_errorsr   c                N   |ri nddd}	 t          d| |d|ddi|}t          |           d|j        cxk    rdk    rcn n`t          |j        d	                   }|j        d
k    r;t          |                              |j                                                  }	 |S )aF  Perform an HTTP request with backoff and follow relative redirects only.

    Used to fetch HEAD /resolve on repo or bucket files.

    This is useful to follow a redirection to a renamed repository without following redirection to a CDN.

    A backoff mechanism retries the HTTP call on errors (429, 5xx, timeout, network errors).

    Args:
        method (`str`):
            HTTP method, such as 'GET' or 'HEAD'.
        url (`str`):
            The URL of the resource to fetch.
        retry_on_errors (`bool`, *optional*, defaults to `False`):
            Whether to retry on errors. If False, no retry is performed (fast fallback to local cache).
            If True, uses default retry behavior (429, 5xx, timeout, network errors).
        **httpx_kwargs (`dict`, *optional*):
            Params to pass to `httpx.request`.
    r+   )r   r   Tr   r`   Fi,  i  Location )path)	r   r   rX   r   r.   netloc_replacer   geturl)rN   rF   r   httpx_kwargsno_retry_kwargsrU   parsed_targets          r-   -_httpx_follow_relative_redirects_with_backoffr   h  s    0 [2XZ#[#[  

 
 
 
 #	
 

 
 
 	H%%% (&----#-----$X%5j%ABBM#r))smm,,-2D,EELLNN 	Or,   endpointc                     |r|                     d          nt          j        }|t          j        t          j        fvr@|                     t          j        |          } |                     t          j        |          } | S )zReplace the default endpoint in a URL by a custom one.

    This is useful when using a proxy and the Hugging Face Hub returns a URL with the default endpoint.
    r   )rstripr   ENDPOINT_HF_DEFAULT_ENDPOINT_HF_DEFAULT_STAGING_ENDPOINTreplace)rF   r   s     r-   fix_hf_endpoint_in_urlr     si    
 (0Gxs###Y5GH	6	8^___kk)8(CCkk)@(KKJr,   endpoint_namec           	      b   	 t          |            n,# t          $ r t                              dd           Y nw xY w	 |                                  dS # t
          j        $ r}| j        dz  dk    rY d}~dS | j        	                    d          }| j        	                    d          }|d	k    r0| j         d
dz   d| j
         dz   }t          t          ||           ||dk    r0| j         d
dz   d| j
         dz   }t          t          ||           ||dk    r0| j         d
dz   d| j
         dz   }t          t          ||           ||dk    r6| j         d
dz   d| j
         dz   dz   dz   }t          t          ||           ||dk    rz| j        s| j        j
        gt"                              t'          | j        j
                            6| j         d
dz   d| j
         dz   dz   dz   }t          t(          ||           ||dk    sU| j        dk    r}|dk    rw| j        p| j        j
        dt*                              t'          | j        j
                            3| j         d
dz   d| j
         dz   dz   }t          t,          ||           || j        dk    r!|d| dnd }t          t.          ||           || j        d!k    r4d| j         d"| dd#| j
         dz   d$z   }t          t0          ||           || j        d%k    rt3          | j                  }|Zd&|j         d'}|d(|j         d)z  }|j        &|j        |d*|j         d+|j         d,|j         d-z  }n|dz  }|d.| j
         dz  }nd/| j
         d}t          t0          ||           || j        d0k    rY| j        j        	                    d1          }| d2| d3| j        	                    d4           d}t          t0          ||           |t          t0          t'          |          |           |d}~ww xY w)5a  
    Internal version of `response.raise_for_status()` that will refine a potential HTTPError.
    Raised exception will be an instance of [`~errors.HfHubHTTPError`].

    This helper is meant to be the unique method to raise_for_status when making a call to the Hugging Face Hub.

    Args:
        response (`Response`):
            Response from the server.
        endpoint_name (`str`, *optional*):
            Name of the endpoint that has been called. If provided, the error message will be more complete.

    > [!WARNING]
    > Raises when the request has failed:
    >
    >     - [`~utils.RepositoryNotFoundError`]
    >         If the repository to download from cannot be found. This may be because it
    >         doesn't exist, because `repo_type` is not set correctly, or because the repo
    >         is `private` and you do not have access.
    >     - [`~utils.GatedRepoError`]
    >         If the repository exists but is gated and the user is not on the authorized
    >         list.
    >     - [`~utils.RevisionNotFoundError`]
    >         If the repository exists but the revision couldn't be found.
    >     - [`~utils.EntryNotFoundError`]
    >         If the repository exists but the entry (e.g. the requested file) couldn't be
    >         find.
    >     - [`~utils.BadRequestError`]
    >         If request failed with a HTTP 400 BadRequest error.
    >     - [`~utils.HfHubHTTPError`]
    >         If request failed for a reason not listed above.
    zFailed to parse warning headersT)exc_infod      NzX-Error-CodeX-Error-MessageRevisionNotFoundz Client Error.

zRevision Not Found for url: .EntryNotFoundzEntry Not Found for url: 	GatedRepoz!Cannot access gated repo for url z$Access to this resource is disabled.z!Cannot access repository for url 
RepoNotFoundzBucket Not Found for url: zG
Please make sure you specified the correct bucket id (namespace/name).z;
If the bucket is private, make sure you are authenticated.i  z+Invalid credentials in Authorization headerzRepository Not Found for url: z
Please make sure you specified the correct `repo_id` and `repo_type`.
If you are trying to access a private or gated repo, make sure you are authenticated. For more details, see https://huggingface.co/docs/huggingface_hub/authenticationrW   z

Bad request for z
 endpoint:z

Bad request:i  z Forbidden: z
Cannot access content at: z2
Make sure your token has the correct permissions.r   z0

429 Too Many Requests: you have reached your 'z' rate limit.z
Retry after z seconds (r   z requests remaining in current z
s window).z
Url: z!

429 Too Many Requests for url: i  Rangez. Requested range: z. Content-Range: zContent-Range)_warn_on_warning_headersrz   rL   rM   raise_for_statusrb   HTTPStatusErrorrX   r.   rH   rF   _formatr   r   r   r   rB   BUCKET_API_REGEXr8   r(   r   REPO_API_REGEXr   r   r   r@   r   r!   r"   r#   r    )rU   r   r}   
error_codeerror_messagemessager   range_headers           r-   r   r     s   BG **** G G G6FFFFFGo?!!#####  m? m? m?3&!++FFFFF%)).99
 (,,->??+++!-===FIwhphtIwIwIwwG/(CCJ?**!-===FItemeqItItIttG2GXFFAM;&&'777&@CvgogsCvCvCvv  .'8<<!CDDD'777EhlEEEF  9	9  +Wh??QF .(( , $0 ''H,<,@(A(ABBN '777>x|>>>? ]] Q	Q  -wAAqH>)) C''!NNN , $0%%c(*:*>&?&?@@L '777B8<BBBCNN  17HEE1L!S((DQD]@}@@@@cu  /7H==1D!S((Ix+IIIII@@@@AGH 
 .'8<<!C!S((4X5EFFN)tIettt  UN,KUUUU!'38U8aQ^5 Q Q8L Q Q'5'DQ Q QGG
 sNG4X\4444OOOO.'8<<!C!S((#+377@@Lvv|vvhN^NbNbcrNsNsvvvG.'8<<!C nc!ffh77Q>[m?s*    &;;A P.%P)9N0P))P.c                 T   | j                             d          }|D ]}d|v r|                    dd          nd|f\  }}|                                }|t          vrJ|                                }|r4t                              |           t                              |           dS )a  
    Emit warnings if warning headers are present in the HTTP response.

    Expected header format: 'X-HF-Warning: topic; message'

    Only the first warning for each topic will be shown. Topic is optional and can be empty. Note that several warning
    headers can be present in a single response.

    Args:
        response (`httpx.Response`):
            The HTTP response to check for warning headers.
    zX-HF-Warning;r   r   N)r.   get_listsplitstrip_WARNED_TOPICSaddrL   r{   )rU   server_warningsserver_warningtopicr   s        r-   r   r   D  s     &//??O) ( (9<9N9N--c1555UWYgThw&&mmooG (""5)))w'''( (r,   
error_typecustom_messagec                    g }|j                             d          }||                    |           	 	 |                                }nQ# t          j        $ r? 	 |                                 |                                }n# t          $ r i }Y nw xY wY nw xY w|                    d          }|@t          |t                    r|
                    |           n|                    |           |                    d          }|$|D ]!}d|v r|                    |d                    "ng# t          j        $ rU |j                             dd          }|j        r0d|                                vr|                    |j                   Y nw xY wd |D             }t          t                              |                    }d	                    |          }	|}
|	r=|	                                |                                vrd
|v r	|
d	|	z   z  }
n|
d
|	z   z  }
d}d}t"          dft$          dft&          dffD ];\  }}|j                             |          }|rt)          |          }d| d| d} n<|r_|                                |
                                vr7d	|
v r.|
                    d	          }|
d |         |z   |
|d          z   }
n|
|z  }
 | |
                                ||	pd           S )Nr   errorerrorsr   zContent-Typer   htmlc                     g | ]D}t          |                                          #t          |                                          ES r+   )r(   r   ).0lines     r-   
<listcomp>z_format.<locals>.<listcomp>  s=    VVV4CIIOODUDUVSYY__&&VVVr,   r   r   z
Request IDzAmzn Trace IDz	Amz CF IDr   : ))rU   server_message)r.   rH   appendjsonrb   ResponseNotReadreadRuntimeErrorr   listextendJSONDecodeErrortextr6   dictfromkeysjoinrI   rG   X_AMZ_CF_IDr(   indexr   )r   r   rU   server_errorsfrom_headersr   r   r   content_typer   final_error_messagerQ   request_id_messageheaderlabelvaluenewline_indexs                    r-   r   r   \  s   M #''(9::L\***$0	==??DD$ 
	 
	 
		}}    	
	 !!%&& ,$$U++++ $$U+++(## ; ;%%!((y)9::: 0 0 0'++NB??= 	0V<+=+=+?+???  ///	0 WV=VVVM }5566M YY}--N ) ;...008L8L8N8NNN^##4.#886N#:: J	|$	/*	k" 	 	
  $$V,, 	UJ!7e!7!7u!7!7!7E	  6j&&((0C0I0I0K0KKK&&&/55d;;M#N]N36HHK^_l_m_mKnn    #55 :)//11HUcUkgkllllsU   A 
D/ B(BBBBBBD/ BBD/ /A!FFc                    dd| j         fg}t          | j                                                  D ],\  }}|                                dk    rd}|d| d| fgz  }-d}	 | j        <| j                            dd	
          }t          |          dk    r|dd          d}n# t          j	        $ r d}Y nw xY w||d|
                    dd          fgz  }|d| j        fgz  }g }|D ]g\  }}|r/|                    t          t          |                               |r/|                    t          t          |                               hd                    |          S )zConvert a `httpx.Request` into a curl command (str).

    Used for debug purposes only.

    Implementation vendored from https://github.com/ofw/curlify/blob/master/curlify.py.
    MIT License Copyright (c) 2016 Egor.
    )curlNz-XrD   z<TOKEN>z-Hr   Nzutf-8ignore)r   i  z ... [truncated]z<streaming body>z-dr   r   r   )rN   sortedr.   itemsr6   contentdecodelenrb   RequestNotReadr   rF   r   r   r(   r   )rB   partskvbody
flat_partss         r-   rP   rP     s    		w~$E
 w,,..// ' '17799''A4A%&&D"?&?))'()CCD4yy4uu+777 " " "!"4dB//011	tW[!""EJ - -1 	-eCFFmm,,, 	-eCFFmm,,,88Js   "AB& &B:9B:z%^\s*bytes\s*=\s*(\d*)\s*-\s*(\d*)\s*$original_rangeresume_sizec                    | sd| dS d| v rt          d| d          t                              |           }|st          d| d          |                                \  }}|sG|st          d| d          t          |          |z
  }d| }|d	k    rt          d
|d          |S t          |          }||z   }|r2t          |          }d| d| }||k    rt          d
|d          |S d| dS )zB
    Adjust HTTP Range header to account for resume position.
    zbytes=-,zMultiple ranges detected - z, not supported yet.zInvalid range format - r   zbytes=-r   zEmpty new range - )rY   RANGE_REGEXr>   r   groupsr*   )r  r  r>   startend
new_suffix	new_range	new_starts           r-   _adjust_range_headerr&    sq     '&&&&&
n]~]]]^^^n--E JH^HHHIIIJE3  	NLLLLMMMXX+
*j**	??BIBBBCCCJJE#I
 #hh.Y....	s??BIBBBCCC I    r,   )r/   N)N)kr'   atexitr   r   osre	threadingr   rJ   
contextlibr   dataclassesr   shlexr   typingr   r   r   r	   r
   r   urllib.parser   rb   huggingface_hub.errorsr   r   r   r   r   r   r   r   r   r   r   r   r   _lfsr   _typingr   
get_loggerr$   rL   r   compiler7   r:   r(   r@   rI   rG   r  VERBOSEr   r   RequestrR   rT   r   r\   rc   rd   rf   rg   CLIENT_FACTORY_TASYNC_CLIENT_FACTORY_TLockrm   rh   r)   ri   rj   ro   rs   ru   rw   rn   registerhasattrr~   TimeoutExceptionNetworkErrorr   tupler   rz   r   r*   r   r   r   r   r   r   r   r   setr   r   r   rP   
IGNORECASEr  r&  r+   r,   r-   <module>rA     s   E D D  				  				 				       % % % % % % ! ! ! ! ! !       E E E E E E E E E E E E E E E E ! ! ! ! ! !  7 7 7 7 7 7      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	             " " " " " " 
	H	%	% $) ) ) ) ) ) ) ), 2:mnn $"*%OPP 8WS#X%6 88M;R 8 8 8 8z #
 *   2: *   5= T    B*u} * * * * *' 'D ' ' ' '    e&7     B,- !"e&7"78 y~+A ( A A A7S 4 S S S)-& - - -0'7 0D 0 0 0 0"83I 8d 8 8 8 8&U\    *5, * * * * 9 9 9 9,    
72!"" 6B}5555 >C=SUZUg<h eDOS$89 h h h2K c3h K K K Ok9W^8 ^8 ^8^8	^8 	^8
 ^8 ^8 tId9os6J0KKL^8 !eCHo!56^8 ^8 u~tT)*^8 ^8 ^8 ^8J Ok9WM M MM	M 	M
 M M tId9os6J0KKLM !eCHo!56M ^M M M M` 
 Ok9WO O OO	O 	O
 O O tId9os6J0KKLO !eCHo!56O u~tT)*O O O Of AF0 0 00 #09=0
^0 0 0 0f
 
x} 
 
 
 
 
V? V?%. V?# V?Z^ V? V? V? V?r (u~ ($ ( ( ( (0Zm^, Zmc ZmU^ Zm`n Zm Zm Zm Zmz& em &  &  &  &  & T bjA2=QQ"!# "!S "!XVY] "! "! "! "! "! "!r,   