
    -:j                     :	   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
mZm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 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%m&Z& ddlm'Z' ddl(m)Z) ddl*m+Z+  e'jX                  e-      Z. ed       G d d             Z/ ej`                  d      Z1 ej`                  d      Z2dee3e3f   de/dz  fdZ4dZ5dZ6dZ7 ej`                  dejp                        Z9 ej`                  dejp                        Z: ej`                  d       Z; ej`                  d!      Z< ej`                  d"      Z=h d#Z>d$e3de?e3dz  e3dz  f   fd%Z@d$e3de3dz  fd&ZAd$e3de3dz  fd'ZBd(ej                  ddfd)ZDd(ej                  ddfd*ZEd+ej                  ddfd,ZGdej                  fd-ZIdej                  fd.ZKe
g ej                  f   ZLe
g ej                  f   ZM ej                         ZOeIaPeLeQd/<   eKaReMeQd0<   daSej                  dz  eQd1<   d2eLddfd3ZTd4eMddfd5ZUdej                  fd6ZVdej                  fd7ZWdjd8ZX ej                  eX        eZed9      r ej                  eX:       ej                  ej                  fZ^e?e_e`   d;f   eQd<<   d=Zae?ebd;f   eQd><   d?dd@e^eadAdBdCe+d$e3dDebdEecdFecdGe_e`   e?e_e`   d;f   z  dHebe?ebd;f   z  dIeddeej                  ddf   fdJZed?dd@e^eadKdCe+d$e3dDebdEecdFecdGe_e`   e?e_e`   d;f   z  dHebe?ebd;f   z  dej                  fdLZfed?dd@e^eadKdCe+d$e3dDebdEecdFecdGe_e`   e?e_e`   d;f   z  dHebe?ebd;f   z  deej                  ddf   fdM       ZgdAdNdCe+d$e3dOeddej                  fdPZhd$e3dQe3dz  de3fdRZidkd+ej                  dSe3dz  ddfdTZj ek       Zld+ej                  ddfdUZm edVe"W      ZndXe_en   dYe3d+ej                  dZedenf
d[Zod\ZpepD  cg c]9  }  ej`                  d]|  d^      d_f ej`                  d`|  da      dbffD ]  }| ; c}} Zqdce3de3fddZrd(ej                  de3fdeZs ej`                  dfej                        Zudge3dz  dhebde3dz  fdiZvyc c}} w )lz>Contains utilities to handle HTTP requests in huggingface_hub.    N)Callable	GeneratorMapping)contextmanager)	dataclass)quote)AnyTypeVar)urlparse)OfflineModeIsEnabled   )	constants)	BadRequestErrorBucketNotFoundErrorDisabledRepoErrorGatedRepoErrorHfHubHTTPErrorJobNotFoundErrorRemoteEntryNotFoundErrorRepositoryNotFoundErrorRevisionNotFoundError   )logging)SliceFileObj)HTTP_METHOD_TT)frozenc                   X    e Zd ZU dZeed<   eed<   eed<   dZedz  ed<   dZedz  ed<   y)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#        ]/var/www/html/tokenscope/api/venv/lib/python3.12/site-packages/huggingface_hub/utils/_http.pyr   r   7   s6    
 NE3:!%NC$J%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 ](  }|j                         }|dk(  r| |   }|dk(  s$| |   }* |syt        j                  |      }|sy|j                  d      }t	        |j                  d            }t	        |j                  d            }d}	d}
|rKt
        j                  |      }|r4t	        |j                  d            }	t	        |j                  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@   S   s   * !IF "IIK	#I,,S\F" ##I.EKK0MEKK$%I5;;s+,E!%N.55f=**3/0E !3!3C!8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/
    z:^https?://[^/]+/api/(?:scheduled-jobs|jobs)/[^/]+/([^/?]+)zA^https?://[^/]+/api/(models|datasets|spaces)/([^/]+)(?:/([^/]+))?z)^https?://[^/]+/api/buckets/([^/]+/[^/]+)>	   rawblobrefstreecommitresolverevisionsettingsdiscussionsurlc                    t         j                  |       }|syt        j                  j	                  |j                  d            }|j                  d      |j                  d      }}|r|t        vr| d| }||fS |}||fS )a,  Extract (repo_type, repo_id) from an API URL.

    Returns canonical repo_type values: "model", "dataset", "space" (or None).

    Examples:
        >>> _parse_repo_info_from_url("https://huggingface.co/api/models/user/repo")
        ("model", "user/repo")
        >>> _parse_repo_info_from_url("https://huggingface.co/api/datasets/user/repo/resolve/main/data.csv")
        ("dataset", "user/repo")
        >>> _parse_repo_info_from_url("https://huggingface.co/api/models/bert-base-cased/resolve/main/config.json")
        ("model", "bert-base-cased")
    NNr   r      /)_REPO_ID_FROM_URL_REGEXr8   r   REPO_TYPES_MAPPINGgetr9   _REPO_URL_SUBPATHS)rK   r>   	repo_typefirstsecondrepo_ids         r-   _parse_repo_info_from_urlrX      s     $**3/E,,00Q@IKKNEKKN6E& 22G1VH% g gr,   c                 V    t         j                  |       }|r|j                  d      S dS )z9Extract bucket_id (namespace/name) from a bucket API URL.r   N)_BUCKET_ID_FROM_URL_REGEXr8   r9   rK   r>   s     r-   _parse_bucket_id_from_urlr\      s'    %,,S1E"5;;q>,,r,   c                 V    t         j                  |       }|r|j                  d      S dS )z>Extract the job_id from a (scheduled) job API URL, if present.r   N)_JOB_ID_FROM_URL_REGEXr8   r9   r[   s     r-   _parse_job_id_from_urlr_      s'    "))#.E"5;;q>,,r,   requestc           	      N   t        j                         rt        d| j                   d      t        | j
                  vrO| j
                  j                  t              xs t        t        j                               | j
                  t        <   | j
                  j                  t              }t        j                  d|| j                  | j                  | j
                  j                  d      du       t         j                  rt        j                  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   rK   X_AMZN_TRACE_IDr.   rR   X_REQUEST_IDr(   uuiduuid4loggerdebugmethodHF_DEBUG_curlify)r`   
request_ids     r-   hf_request_event_hookrn      s       ""GKK=  )J  K
 	

 goo-+2??+>+>|+L+aPSTXT^T^T`Pa($$_5J LL/O,D8 Z'!23r,   c                     K   t        |       S w)z3
    Async version of `hf_request_event_hook`.
    )rn   )r`   s    r-   async_hf_request_event_hookrp     s      !))s   responsec                    K   | j                   dk\  rGd| j                  v r8	 t        | j                  d         }|dk  r| j	                          d {    y y y y # t        $ r Y y w xY w7 w)N  zContent-lengthi@B )status_coder.   r*   
ValueErroraread)rq   lengths     r-   async_hf_response_event_hookrx   	  s     s" x///X--.>?@ 	!nn&&& " 0 #   's3   A+A A+A)A+	A&#A+%A&&A+c                  @    t        j                  dt        gidd      S )zQ
    Factory function to create a `httpx.Client` with the default transport.
    r`   TNevent_hooksfollow_redirectstimeout)httpxClientrn   r+   r,   r-   default_client_factoryr     s'     <<!6 78 r,   c                  L    t        j                  t        gt        gddd      S )zV
    Factory function to create a `httpx.AsyncClient` with the default transport.
    )r`   rq   TNrz   )r~   AsyncClientrp   rx   r+   r,   r-   default_async_client_factoryr   "  s,     !< =LhKij r,   _GLOBAL_CLIENT_FACTORY_GLOBAL_ASYNC_CLIENT_FACTORY_GLOBAL_CLIENTclient_factoryc                 R    t         5  t                | addd       y# 1 sw Y   yxY w)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_session`] 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_session`] to get a correctly configured `httpx.Client`.
    N)_CLIENT_LOCKclose_sessionr   )r   s    r-   set_client_factoryr   6  s'     
 0!/0 0 0s   &async_client_factoryc                     | a y)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r   )r   s    r-   set_async_client_factoryr   G  s
      $8 r,   c                  z    t         t        5  t               a ddd       t         S t         S # 1 sw Y   t         S xY w)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)r   r   r   r+   r,   r-   get_sessionr   Z  s6      	635N	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>
    r   r+   r,   r-   get_async_sessionr   i  s     ())r,   c                      t         } da | 	 | j                          yy# t        $ r"}t        j	                  d|        Y d}~yd}~ww xY w)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: )r   close	Exceptionrh   warning)clientes     r-   r   r   y  sT     F N 	9LLN   	9NN3A3788	9s    	A	AA	register_at_fork)after_in_child._DEFAULT_RETRY_ON_EXCEPTIONS)i    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streamrj   r   r   r   r   r   r   c          	   +      K   t        |t              r|f}t        t              rfd|}	dd}
d|v r6t        |d   t        j                  t
        f      r|d   j                         }
t               }	 dz  d	 |
|d   j                  |
       dt        j                  dt        f fd}|r6 |j                  d d|5 } ||      s| 	 ddd       y	 ddd       n" |j                  d d|} ||      s| y.t%              dz   }t        j                  d| d d d       n!|	}t        j                  d| d d d       t'        j(                  |       t+        ||	dz        }	# 1 sw Y   xY w# |$ rS}t        j                  d	| d
  d        t        |t        j                         r
t#                kD  r|Y d}~d}~ww xY ww)zfInternal implementation of HTTP backoff logic shared between `http_backoff` and `http_stream_backoff`.r   Ndatar   rq   r/   c                     | j                   vryt        j                  d| j                    d d        kD  rt        |        y| j                   dk(  r#t	        | j
                        }||j                  y)zNHandle response and return True if should retry, False if should return/yield.FzHTTP Error z thrown while requesting  r   T)rt   rh   r   hf_raise_for_statusr@   r.   r!   )rq   ratelimit_infor   rj   nb_triesratelimit_resetr   rK   s     r-   _should_retryz)_http_backoff_base.<locals>._should_retry  s     ''/DD  X-A-A,BB[\b[ccdehdijkk)'1 ! ''3.%<X=M=M%NN%1*8*I*Ir,   rj   rK   'z' thrown while requesting r   zRate limited. Waiting zs before retry [Retry rO   z].zRetrying in z	s [Retry r   r+   )
isinstancetyper*   ioIOBaser   tellr   seekr~   Responseboolr   r`   rh   r   ConnectErrorr   floattimesleepmin)rj   rK   r   r   r   r   r   r   kwargs
sleep_timeio_obj_initial_posr   r   rq   erractual_sleepr   r   s   ```   `         @@r-   _http_backoff_baser     s2     %t,24'-!6 8HJ"&O
 Jvf~		<7PQ#F^002]F
A0	 "-v##$67 4  . "V]]D&cDVD (2& 2 
 *6>>KSKFK$X."N & 1A5LNN3L>AWX`Waabcnbooqrs%LNN\,y
!K=XZ[\

<  
Q7
 D  # 	NNQse#=fXQseLM#u112+%	 &	sW   A;G3AF F F (G3**F A8G3FF G0A	G+&G3+G00G3)r   r   r   r   r   c                <    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rj   rK   r   r   r   r   r   r   r+   )nextr   rj   rK   r   r   r   r   r   r   s           r-   http_backoffr     sA    B  
	
#)' 3"7
	
 
	
 r,   c             +   H   K   t        d| ||||||dd|E d{    y7 w)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   I  sB     L " 
%#/3
 
 
 
s   " ")retry_on_errorsr   c                D   |ri nddd}	 t        d| |d|ddi|}t        |       d|j                  cxk  rdk  r`n 	 |S t        |j                  d         }|j
                  d	k(  r4t        |      j                  |j                  
      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   r   r|   Fi,  i  Location )path)	r   r   rt   r   r.   netloc_replacer   geturl)rj   rK   r   httpx_kwargsno_retry_kwargsrq   parsed_targets          r-   -_httpx_follow_relative_redirects_with_backoffr     s    0 2XZ#[   

 
 #	

 
 	H% (&&-#- 	O %X%5%5j%ABM##r)sm,,-2D2D,ELLN 	Or,   endpointc                    |r|j                  d      nt        j                  }|t        j                  t        j                  fvr@| j                  t        j                  |      } | 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.
    rO   )rstripr   ENDPOINT_HF_DEFAULT_ENDPOINT_HF_DEFAULT_STAGING_ENDPOINTreplace)rK   r   s     r-   fix_hf_endpoint_in_urlr     sf    
 (0xs#Y5G5GH	66	8^8^__kk)88(Ckk)@@(KJr,   endpoint_namec           	      	   	 t        |        	 | j	                          y# t        $ r t        j                  dd       Y 4w xY w# t
        j                  $ r}| j                  dz  dk(  rY d}~y| j                  j                  d      }| j                  j                  d      }| j                  5| j                  j                  t        | j                  j                        nd}|rt        |      nd	\  }}|d
k(  r8| j                   ddz   d| j                   dz   }t        t        || ||      ||dk(  r8| j                   ddz   d| j                   dz   }t        t         || ||      ||dk(  r8| j                   ddz   d| j                   dz   }t        t"        || ||      ||dk(  r;| j                   ddz   d| j                   dz   dz   dz   }t        t$        ||       ||dk(  r]|[t&        j)                  |      F| j                   ddz   d| j                   dz   dz   dz   }t        t*        || t-        |            || j                  dk(  rI|Gt/        |      x}	:| j                   ddz   d| j                   dz   dz   }t        t0        || |	      ||dk(  s+| j                  d k(  rZ|d!k7  rU|St2        j)                  |      >| j                   ddz   d"| j                   dz   d#z   d$z   }t        t4        || ||      || j                  d%k(  r|d&| d'nd(}t        t6        ||       || j                  d)k(  r9d| j                   d*| dd+| j                   dz   d,z   }t        t8        ||       || j                  d-k(  rt;        | j                        }
|
d.|
j<                   d/}|d0|
j>                   d1z  }|
j@                  :|
jB                  .|d2|
jD                   d3|
j@                   d4|
jB                   d5z  }n|dz  }|d6| j                   dz  }nd7| j                   d}t        t8        ||       || j                  d8k(  r[| j                  j                  j                  d9      }| d:| d;| j                  j                  d<       d}t        t8        ||       |t        t8        t        |      |       |d}~ww xY w)=a  
    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   rN   NzX-Error-CodeX-Error-MessagerM   RevisionNotFoundz Client Error.

zRevision Not Found for url: .)rT   rW   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).zg
If the bucket is private, make sure you are authenticated and your token has the required permissions.)	bucket_idi  zJob Not Found for url: zA
Please make sure you specified the correct job ID and namespace.)job_idi  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 and your token has the required permissions.zQ
For more details, see https://huggingface.co/docs/huggingface_hub/authenticationrs   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 (rO   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_headersr   rh   ri   raise_for_statusr~   HTTPStatusErrorrt   r.   rR   r`   rK   r(   rX   _formatr   r   r   r   BUCKET_API_REGEXr8   r   r\   r_   r   REPO_API_REGEXr   r   r   r@   r   r!   r"   r#   r    )rq   r   r   
error_codeerror_messagerequest_urlrT   rW   messager   r   range_headers               r-   r   r     s   BG *
B?!!#  G6FG    @?3&!+%%)).9
 ((,,->? *2)9)9)E(JZJZJ^J^JjC  $$%pt 	 HS6{CXd	7++!--.n=FKghphthtguuvIwwG/(iahiopp?*!--.n=FKdemeqeqdrrsIttG2GXQZdklrss;&''(7&@EfgogsgsfttuCvv  .'8yZabhiiDD''(75hll^1EF  9	9  +Wh?QF .(' ''4@ ''(7.x||nA>? ]] }	}  #WhB[\gBh
   C''1+>>K ''(7+HLL>;< WW  *GXfMSTT>)  C'!NN'%%k2> ''(728<<.BC`` gg  17HPYcjkqrr!!S(DQD]&}oZ@cu  /7H=1D!!S(x++,LqI0a@AGH 
 .'8<!C!!S(4X5E5EFN)HIeIeHffst  ^N,K,K+LHUU!''38U8U8a^556a8L8L7M N''5'D'D&EZQG
 sNGWX\\N!44?~QO.'8<!C!!S(#++3377@L.|n<MhN^N^NbNbcrNsMttuvG.'8<!C nc!fh7Q>A@?s-    A  AAS!S0Q,SS!c                 .   | j                   j                  d      }|D ]u  }d|v r|j                  dd      nd|f\  }}|j                         }|t        vs9|j                         }|sLt        j                  |       t        j                  |       w y)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addrh   r   )rq   server_warningsserver_warningtopicr   s        r-   r   r     s     &&//?O) (9<9N--c15UWYgThw&mmoG""5)w'(r,   _HfHubHTTPErrorT)bound
error_typecustom_messageattrsc                    g }|j                   j                  d      }||j                  |       	 	 |j                         }|j                  d      }|j                  d      }|Mt        |t              r|j                  |       n>||j                  | d|        n%|j                  |       n||j                  |       |j                  d      }	|	 |	D ]  }d|v s|j                  |d           |D cg c]5  }t        |      j                         st        |      j                         7 }}t        t         j#                  |            }d
j%                  |      }|}|r5|j                         |j                         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 ]8  \  }}|j                   j                  |      }|s$t        |      }d| d| d} n |rI|j                         |j                         vr)d
|v r |j-                  d
      }|d | |z   ||d  z   }n||z  } | |j                         ||xs d       }|j/                         D ]  \  }}t1        |||        |S # t        j
                  $ r7 	 |j                          |j                         }n# t        $ r i }Y nw xY wY w xY w# t        j                  $ rY |j                   j                  dd      }
|j                  r-d	|
j                         vr|j                  |j                         Y Ew xY wc c}w )Nr   errorerror_description: errorsr   zContent-Typer   htmlr   r   z
Request IDzAmzn Trace IDz	Amz CF IDr   ))rq   server_message)r.   rR   appendjsonr~   ResponseNotReadreadRuntimeErrorr   listextendJSONDecodeErrortextr6   r(   r   dictfromkeysjoinre   rd   X_AMZ_CF_IDindexitemssetattr)r  r  rq   r	  server_errorsfrom_headersr   r  r  r  content_typeliner  final_error_messagerm   request_id_messageheaderlabelvaluenewline_indexr   kvs                          r-   r   r     s|    M ##''(9:L\*+0	==?D ! HH%89%&$$U+".$$wb1B0C%DE $$U+*  !23(# ;%!((y)9:; 4AV4CIOODUSY__&VMV }56M YY}-N )...08L8L8NN^#4.#886N#:: J	|$	/*	k" 	
   $$V,UJ#%eWBugQ!7	 j&&(0C0I0I0KK&&/55d;M#N]36HHK^_l_mKnn    #55 (..08TbTjfj
kC 1QJo $$ 
		}}  	
	F  0''++NB?==V<+=+=+??  /	0 Wsg   J B K $K >MMK J<;K<K
K	K

KK KK A(M ?M )subject_tokenaccess_tokenrefresh_tokenclient_secretdevice_codez("z"\s*:\s*")[^"]*(")z\1<REDACTED>\2z(^|&)(z=)[^&]*z\1\2<REDACTED>bodyc                 F    t         D ]  \  }}|j                  ||       }  | S )zRRedact OAuth credential values from a JSON or form-urlencoded request body string.)_SENSITIVE_BODY_PATTERNSsub)r3  patternreplacements      r-   _redact_sensitive_bodyr9    s+     8 .{{;-.Kr,   c                    dd| j                   fg}t        | j                  j                               D ]'  \  }}|j	                         dk(  rd}|d| d| fgz  }) d}	 | j
                  >| j
                  j                  dd	
      }t        |      dkD  r|dd  d}t        |      }||d|j                  dd      fgz  }|d| j                  fgz  }g }|D ]P  \  }}|r#|j                  t        t        |                   |s.|j                  t        t        |                   R dj!                  |      S # t        j                  $ r d}Y w xY w)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-Xrb   z<TOKEN>z-Hr  Nzutf-8ignore)r  i  z ... [truncated]z<streaming body>z-dr   r   r   )rj   sortedr.   r   r6   contentdecodelenr9  r~   RequestNotReadr   rK   r  r   r(   r  )r`   partsr,  r-  r3  
flat_partss         r-   rl   rl   "  sz    		w~~$E
 w,,./ '1779'A4A3b%&&'
 D"??&??))'()CD4y4u+&67)$/D 4dB/011	tW[[!""EJ -1eCFm,eCFm,	- 88J  "!"s   !A
D? ?EE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        j                  |       }|st        d| d      |j	                         \  }}|s:|st        d| d      t        |      |z
  }d| }|d	k  rt        d
|d      |S t        |      }||z   }|r)t        |      }d| d| }||kD  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 - )ru   RANGE_REGEXr>   r  groupsr*   )rD  rE  r>   startend
new_suffix	new_range	new_starts           r-   _adjust_range_headerrP  P  s'    }A&&
n6~6HH\]^^n-E4^4FaHIIJE3!88J!LMMX+
j\*	?!3I=BCCJE#I
#hYKq.	s?!3I=BCCI;a  r,   )r/   N)N)wr'   atexitr   r  osre	threadingr   rf   collections.abcr   r   r   
contextlibr   dataclassesr   shlexr   typingr	   r
   urllib.parser   r~   huggingface_hub.errorsr   r   r   r  r   r   r   r   r   r   r   r   r   r   _lfsr   _typingr   
get_loggerr$   rh   r   compiler7   r:   r(   r@   re   rd   r  VERBOSEr   r   r^   rP   rZ   rS   tuplerX   r\   r_   Requestrn   rp   r   rx   r   r   r   r   CLIENT_FACTORY_TASYNC_CLIENT_FACTORY_TLockr   r   r)   r   r   r   r   r   r   r   registerhasattrr   TimeoutExceptionNetworkErrorr   r   r   r   r*   r   r   r   r   r   r   r   r   setr   r   r  r   _SENSITIVE_BODY_KEYSr5  r9  rl   
IGNORECASErI  rP  )r<   r7  s   00r-   <module>rm     s   E  	  	 	    8 8 % !   !  7 
 
 
   " 
		H	% $& & &, 2::mn $"**%OP 8WS#X%6 8=4;O 8z #
 ** 2:: **  $$ab  %"**%ij  'BJJ'ST  q 3 5tS4Z1G+H 2-3 -3: -- -d
 -5== T B*u}} * *' 'D ' e&7&7  B,- !"e&7&7"78 y~~+A ( A7S 4 S&*t# *0'7 0D 0"83I 8d 8&U\\ *5,, * 9,  
2!"B}5 >C=S=SUZUgUg<h eDOS$89 h2P c3h P Ie3Q^8^8	^8 	^8
 ^8 ^8 i5i#1E+FF^8 sCx0^8 ^8 u~~tT)*^8J Ie3QMM	M 	M
 M M i5i#1E+FFM sCx0M ^^M` 
 Ie3QOO	O 	O
 O O i5i#1E+FFO sCx0O u~~tT)*O Of AF00 #09=0
^^0f
 
sTz 
c 
i?%.. i?t i?W[ i?X (u~~ ($ (0 -^D f%&f8;fGL~~f`cffT j  $	r#01	24EF	vcU'*	+->? 	    ' emm '  ' V bjjA2==Q"!t "!# "!#PT* "!s   >R