
    `iGd                        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mZ ddl	m
Z
 ddlmZmZmZmZmZmZmZ ddlZddl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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* ddl+m,Z,  e&j-        e.          Z/dZ0dZ1dZ2 ej3        dej4                  Z5 G d de          Z6 G d de          Z7dej8        fdZ9eg ej8        f         Z:e9a;e:e<d<   e9fde:ddfdZ=dej8        fd Z>d@d!Z?ed"e@d#e@dej8        fd$            ZAd%dd&ejB        ejC        ejD        jE        fd'd(d)e,d*eFd+e@d,eGd-eGd.eeeH         eeeH         d/f         f         d0ee@ee@d/f         f         defd1ZId*eFd2eeF         deFfd3ZJdAd4ed5eeF         ddfd6ZKd7ee#         d8eFd4ede#fd9ZLd:ej        deFfd;ZM ej3        d<ejN                  ZOd=eeF         d>e@deeF         fd?ZPdS )Bz>Contains utilities to handle HTTP requests in Huggingface Hub.    N)	lru_cache)quote)AnyCallableListOptionalTupleTypeUnion)	HTTPErrorResponse)HTTPAdapter)PreparedRequest)OfflineModeIsEnabled   )	constants)BadRequestErrorDisabledRepoErrorEntryNotFoundErrorGatedRepoErrorHfHubHTTPErrorRepositoryNotFoundErrorRevisionNotFoundError   )logging)JSONDecodeError)SliceFileObj)HTTP_METHOD_TX-Amzn-Trace-Idzx-request-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/(.+)
        )
    )flagsc                   6     e Zd ZdZ fdZdedef fdZ xZS )UniqueRequestIdAdapterr   c                     t                      j        |fi | t          |j        vrL|j                            t
                    pt          t          j                              |j        t          <   t          t          |j                            dd                              dk    }t                              d|j        t                    d|j         d|j         d| d	           d S )	Nauthorization r   zRequest :  z (authenticated: ))superadd_headersX_AMZN_TRACE_IDheadersgetX_REQUEST_IDstruuiduuid4lenloggerdebugmethodurl)selfrequestkwargs	has_token	__class__s       o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/huggingface_hub/utils/_http.pyr*   z"UniqueRequestIdAdapter.add_headersN   s    G..v... '/11/6/B/B</P/P/eTWX\XbXdXdTeTeGOO, GO//DDEEFFJ	vw7vv7>vvGKvvjsvvv	
 	
 	
 	
 	
    r8   returnc                 H   t           j        r*t                              dt	          |                      	  t                      j        |g|R i |S # t          j        $ r<}|j	        
                    t                    }|g |j        d| dR |_         d}~ww xY w)zSCatch any RequestException to append request id to the error message for debugging.zSend: Nz(Request ID: r(   )r   HF_DEBUGr3   r4   _curlifyr)   sendrequestsRequestExceptionr,   r-   r+   args)r7   r8   rE   r9   e
request_idr;   s         r<   rB   zUniqueRequestIdAdapter.send[   s     	7LL5(7"3"355666	577<9$999&999( 	 	 	 ,,_==J%A16A#@:#@#@#@AA	s   A B!%7BB!)	__name__
__module____qualname__r+   r*   r   r   rB   __classcell__)r;   s   @r<   r"   r"   K   si        'O
 
 
 
 
O           r=   r"   c                       e Zd ZdedefdZdS )OfflineAdapterr8   r>   c                 2    t          d|j         d          )NzCannot reach za: offline mode is enabled. To disable it, please unset the `HF_HUB_OFFLINE` environment variable.)r   r6   )r7   r8   rE   r9   s       r<   rB   zOfflineAdapter.sendj   s0    " KGK  K  K  K
 
 	
r=   N)rH   rI   rJ   r   r   rB    r=   r<   rM   rM   i   s6        
O 
 
 
 
 
 
 
r=   rM   r>   c                  V   t          j                    } t          j        rE|                     dt                                 |                     dt                                 nD|                     dt                                 |                     dt                                 | S )Nzhttp://zhttps://)rC   Sessionr   HF_HUB_OFFLINEmountrM   r"   )sessions    r<   _default_backend_factoryrU   p   s      G <i!1!1222j."2"23333i!7!9!9:::j"8":":;;;Nr=   _GLOBAL_BACKEND_FACTORYbackend_factoryc                 &    | a t                       dS )a  
    Configure the HTTP backend by providing a `backend_factory`. Any HTTP calls made by `huggingface_hub` will use a
    Session object instantiated by this factory. 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 configured Session. Since `requests.Session` is not guaranteed to be thread-safe,
    `huggingface_hub` creates 1 Session instance per thread. They are all instantiated using the same `backend_factory`
    set in [`configure_http_backend`]. A LRU cache is used to cache the created sessions (and connections) between
    calls. Max size is 128 to avoid memory leaks if thousands of threads are spawned.

    See [this issue](https://github.com/psf/requests/issues/2766) to know more about thread-safety in `requests`.

    Example:
    ```py
    import requests
    from huggingface_hub import configure_http_backend, get_session

    # Create a factory function that returns a Session with configured proxies
    def backend_factory() -> requests.Session:
        session = requests.Session()
        session.proxies = {"http": "http://10.10.1.10:3128", "https": "https://10.10.1.11:1080"}
        return session

    # Set it as the default session factory
    configure_http_backend(backend_factory=backend_factory)

    # In practice, this is mostly done internally in `huggingface_hub`
    session = get_session()
    ```
    N)rV   reset_sessions)rW   s    r<   configure_http_backendrZ      s    @ .r=   c                  h    t          t          j                    t          j                              S )a  
    Get a `requests.Session` object, using the session factory from the user.

    Use [`get_session`] to get a configured Session. Since `requests.Session` is not guaranteed to be thread-safe,
    `huggingface_hub` creates 1 Session instance per thread. They are all instantiated using the same `backend_factory`
    set in [`configure_http_backend`]. A LRU cache is used to cache the created sessions (and connections) between
    calls. Max size is 128 to avoid memory leaks if thousands of threads are spawned.

    See [this issue](https://github.com/psf/requests/issues/2766) to know more about thread-safety in `requests`.

    Example:
    ```py
    import requests
    from huggingface_hub import configure_http_backend, get_session

    # Create a factory function that returns a Session with configured proxies
    def backend_factory() -> requests.Session:
        session = requests.Session()
        session.proxies = {"http": "http://10.10.1.10:3128", "https": "https://10.10.1.11:1080"}
        return session

    # Set it as the default session factory
    configure_http_backend(backend_factory=backend_factory)

    # In practice, this is mostly done internally in `huggingface_hub`
    session = get_session()
    ```
    
process_id	thread_id)_get_session_from_cacheosgetpid	threading	get_identrO   r=   r<   get_sessionrd      s&    : #bikkYEXEZEZ[[[[r=   c                  8    t                                            dS )zReset the cache of sessions.

    Mostly used internally when sessions are reconfigured or an SSLError is raised.
    See [`configure_http_backend`] for more details.
    N)r_   cache_clearrO   r=   r<   rY   rY      s     '')))))r=   r]   r^   c                     t                      S )z
    Create a new session per thread using global factory. Using LRU cache (maxsize 128) to avoid memory leaks when
    using thousands of threads. Cache is cleared when `configure_http_backend` is called.
    )rV   r\   s     r<   r_   r_      s     #$$$r=         )i  i  i  i  )max_retriesbase_wait_timemax_wait_timeretry_on_exceptionsretry_on_status_codesr5   r6   rj   rk   rl   rm   .rn   c          	         t          |t                    r|f}t          |t                    r|f}d}|}	d}
d|v rAt          |d         t          j        t
          f          r|d                                         }
t                      }	 |dz  }	 |
|d                             |
            |j	        d| |d|}|j
        |vr|S t                              d|j
         d|  d	|            ||k    r|                                 |S ne# |$ r]}t                              d
| d|  d	|            t          |t          j                  rt!                       ||k    r|Y d}~nd}~ww xY wt                              d|	 d| d| d           t#          j        |	           t'          ||	dz            }	5)a7  Wrapper around requests 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 `requests.Timeout`, `requests.ConnectionError` and `requests.exceptions.ChunkedEncodingError`.
        retry_on_status_codes (`int` or `Tuple[int]`, *optional*, defaults to `(500, 502, 503, 504)`):
            Define on which status codes the request must be retried. By default, 5xx errors are retried.
        **kwargs (`dict`, *optional*):
            kwargs to pass to `requests.request`.

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

    # Same usage as "requests.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).
    r   NdataTr   )r5   r6   zHTTP Error z thrown while requesting r'   'z' thrown while requesting zRetrying in z	s [Retry /z].r   rO   )
isinstancetypeintioIOBaser   tellrd   seekr8   status_coder3   warningraise_for_statusrC   ConnectionErrorrY   timesleepmin)r5   r6   rj   rk   rl   rm   rn   r9   nb_tries
sleep_timeio_obj_initial_posrT   responseerrs                 r<   http_backoffr      s?   H %t,, 524'-- 9!6 8HJ
 Jvf~	<7PQQ#F^0022mmG#8A	 "-v##$6777 'wHf#HHHHH#+@@@ NNf)=ffX^ffadffggg+%%))+++  	 & # 	 	 	NNLsLLfLLsLLMMM#x788 !   +%%	 &%%%%	 	UjUU8UUkUUUVVV
: 
Q77
G#8s    7D 	AD E0AE++E0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.
    rr   )rstripr   ENDPOINT_HF_DEFAULT_ENDPOINT_HF_DEFAULT_STAGING_ENDPOINTreplace)r6   r   s     r<   fix_hf_endpoint_in_urlr   P  si    
 (0Gxs###Y5GH	6	8^___kk)8(CCkk)@(KKJr=   r   endpoint_namec                 b   	 |                                   dS # t          $ r}| 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    sH| j        dk    rp|dk    rj| j        c| j        j        Wt                              | 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          t           ||           || j        dk    rY| j        j                            d          }| d| d | j                            d!           d}t          t           ||           |t          t           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 `HfHubHTTPError`.

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


    Example:
    ```py
        import requests
        from huggingface_hub.utils import get_session, hf_raise_for_status, HfHubHTTPError

        response = get_session().post(...)
        try:
            hf_raise_for_status(response)
        except HfHubHTTPError as e:
            print(str(e)) # formatted message
            e.request_id, e.server_message # details returned by server

            # Complete the error message with additional information once it's raised
            e.append_to_message("
`create_commit` expects the repository to exist.")
            raise
    ```

    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 find.
    >     - [`~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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 
RepoNotFoundi  z+Invalid credentials in Authorization headerNz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/authenticationi  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.i  Rangez. Requested range: z. Content-Range: zContent-Range)r|   r   r,   r-   rz   r6   _formatr   r   r   r   r8   REPO_API_REGEXsearchr   r   r   r/   )r   r   rF   
error_codeerror_messagemessagerange_headers          r<   hf_raise_for_statusr   ]  s   jJ?!!##### H? H? H?%)).99
 (,,->??+++!-===FIwhphtIwIwIwwG/(CCJ?**!-===FItemeqItItIttG,gx@@aG;&&'777&@CvgogsCvCvCvv  .'8<<!CDDD'777EhlEEEF  9	9  +Wh??QF>)) C''!NNN , $0%%h&6&:;;G '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((#+377@@Lvv|vvhN^NbNbcrNsNsvvvG.'8<<!C nc!ffh77Q>QH?s    J.JJ))J.
error_typecustom_messagec                 H   g }|j                             d          }||                    |           	 |                                }|                    d          }|@t	          |t
                    r|                    |           n|                    |           |                    d          }|$|D ]!}d|v r|                    |d                    "nb# t          $ 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 rO   )r/   strip).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 IDz (r&   r(   )r   server_message)r,   r-   appendjsonrs   listextendr   textlowerdictfromkeysjoinr.   r+   X_AMZ_CF_IDr/   indexr   )r   r   r   server_errorsfrom_headersrp   r   r   content_typer   final_error_messagerG   request_id_messageheaderlabelvaluenewline_indexs                    r<   r   r     s8   M #''(9::L\***0}}!!%&& ,$$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ls   B&C AD;:D;r8   c                    dd| j         fg}t          | j                                                  D ];\  }}|                                dk    rd}|dd                    ||          fgz  }<| j        r| j        }t          |t                    r|	                    dd	          }nt          |d
          rd}t          |          dk    r|dd         dz   }|d|                    dd          fgz  }|d| j        fgz  }g }|D ]M\  }}|r"|                    t          |                     |r"|                    t          |                     Nd                    |          S )zConvert a `requests.PreparedRequest` 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-Xr$   z<TOKEN>z-Hz{0}: {1}zutf-8ignore)r   readz<file-like object>i  Nz ... [truncated]z-dr   r%   r'   )r5   sortedr,   itemsr   formatbodyrs   bytesdecodehasattrr2   r   r6   r   r   r   )r8   partskvr   
flat_partss         r<   rA   rA   0  s    		w~$E
 w,,..// 3 317799''A4**1a00122| 2|dE"" 	(;;wx;88DDT6"" 	('Dt99t;!33D4dB//011	tW[!""EJ ( (1 	(eAhh''' 	(eAhh'''88Jr=   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 - )
ValueErrorRANGE_REGEXmatchRuntimeErrorgroupsru   )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)Q__doc__rv   r`   rerb   r~   r0   	functoolsr   shlexr   typingr   r   r   r   r	   r
   r   rC   r   r   requests.adaptersr   requests.modelsr   huggingface_hub.errorsr   r%   r   r   r   r   r   r   r   r   r   r   _fixesr   _lfsr   _typingr   
get_loggerrH   r3   r+   r.   r   compileVERBOSEr   r"   rM   rQ   rU   BACKEND_FACTORY_TrV   __annotations__rZ   rd   rY   ru   r_   Timeoutr}   
exceptionsChunkedEncodingErrorr/   float	Exceptionr   r   r   r   rA   
IGNORECASEr   r   rO   r=   r<   <module>r      s1   E D D 				 				 				                   D D D D D D D D D D D D D D D D D D  ( ( ( ( ( ( ( ( ) ) ) ) ) ) + + + + + + 7 7 7 7 7 7                              # # # # # #       " " " " " " 
	H	%	%
 $
 *       [   <
 
 
 
 
[ 
 
 
("2     R!112 -E * E E E AY ! !,= !]a ! ! ! !H\X% \ \ \ \@* * * * % % %@P % % % %  0P
 :Nx8 x8 x8x8	x8 	x8
 x8 x8 tId9os6J0KKLx8 !eCHo!56x8 x8 x8 x8 x8v
 
x} 
 
 
 
 
? ?( ?8C= ?TX ? ? ? ?DNm^, Nmc NmX NmZh Nm Nm Nm Nmb% h. % 3 %  %  %  % R bjA2=QQ"!# "!S "!XVY] "! "! "! "! "! "!r=   