o
    h"                     @   sl  d 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 dd	lmZmZmZmZmZmZmZmZmZmZ dd
lmZ eddddeddddddddeeeef  dee dee deeedf deeeef  dedeeef fddZdeeeef  dee fddZdddddee dee deeedf defddZdedefdd ZdS )!zIContains utilities to handle headers to send in calls to Huggingface Hub.    )DictOptionalUnion)LocalTokenNotFoundError   )	constants   )	get_token)_deprecate_arguments)
get_fastai_versionget_fastcore_versionget_hf_hub_versionget_python_versionget_tf_versionget_torch_versionis_fastai_availableis_fastcore_availableis_tf_availableis_torch_available)validate_hf_hub_argsz1.0is_write_actionz\This argument is ignored and we let the server handle the permission error instead (if any).)versiondeprecated_argscustom_messageNF)tokenlibrary_namelibrary_version
user_agentheadersr   r   r   r   r   r   returnc                 C   sF   t | }dt|||di}|durd| |d< |dur!|| |S )aW  
    Build headers dictionary to send in a HF Hub call.

    By default, authorization token is always provided either from argument (explicit
    use) or retrieved from the cache (implicit use). To explicitly avoid sending the
    token to the Hub, set `token=False` or set the `HF_HUB_DISABLE_IMPLICIT_TOKEN`
    environment variable.

    In case of an API call that requires write access, an error is thrown if token is
    `None` or token is an organization token (starting with `"api_org***"`).

    In addition to the auth header, a user-agent is added to provide information about
    the installed packages (versions of python, huggingface_hub, torch, tensorflow,
    fastai and fastcore).

    Args:
        token (`str`, `bool`, *optional*):
            The token to be sent in authorization header for the Hub call:
                - if a string, it is used as the Hugging Face token
                - if `True`, the token is read from the machine (cache or env variable)
                - if `False`, authorization header is not set
                - if `None`, the token is read from the machine only except if
                  `HF_HUB_DISABLE_IMPLICIT_TOKEN` env variable is set.
        library_name (`str`, *optional*):
            The name of the library that is making the HTTP request. Will be added to
            the user-agent header.
        library_version (`str`, *optional*):
            The version of the library that is making the HTTP request. Will be added
            to the user-agent header.
        user_agent (`str`, `dict`, *optional*):
            The user agent info in the form of a dictionary or a single string. It will
            be completed with information about the installed packages.
        headers (`dict`, *optional*):
            Additional headers to include in the request. Those headers take precedence
            over the ones generated by this function.
        is_write_action (`bool`):
            Ignored and deprecated argument.

    Returns:
        A `Dict` of headers to pass in your API call.

    Example:
    ```py
        >>> build_hf_headers(token="hf_***") # explicit token
        {"authorization": "Bearer hf_***", "user-agent": ""}

        >>> build_hf_headers(token=True) # explicitly use cached token
        {"authorization": "Bearer hf_***",...}

        >>> build_hf_headers(token=False) # explicitly don't use cached token
        {"user-agent": ...}

        >>> build_hf_headers() # implicit use of the cached token
        {"authorization": "Bearer hf_***",...}

        # HF_HUB_DISABLE_IMPLICIT_TOKEN=True # to set as env variable
        >>> build_hf_headers() # token is not sent
        {"user-agent": ...}

        >>> build_hf_headers(library_name="transformers", library_version="1.2.3")
        {"authorization": ..., "user-agent": "transformers/1.2.3; hf_hub/0.10.2; python/3.10.4; tensorflow/1.55"}
    ```

    Raises:
        [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
            If organization token is passed and "write" access is required.
        [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
            If "write" access is required but token is not passed and not saved locally.
        [`EnvironmentError`](https://docs.python.org/3/library/exceptions.html#EnvironmentError)
            If `token=True` but token is not saved locally.
    z
user-agentr   r   r   NzBearer authorization)get_token_to_send_http_user_agentupdate)r   r   r   r   r   r   token_to_send
hf_headers r'   r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/huggingface_hub/utils/_headers.pybuild_hf_headers'   s   W
r)   c                 C   sJ   t | tr| S | du rdS t }| du r|du rtd|S tjr#dS |S )z:Select the token to send from either `token` or the cache.FNTzToken is required (`token=True`), but no token found. You need to provide a token or be logged in to Hugging Face with `huggingface-cli login` or `huggingface_hub.login`. See https://huggingface.co/settings/tokens.)
isinstancestrr	   r   r   HF_HUB_DISABLE_IMPLICIT_TOKEN)r   cached_tokenr'   r'   r(   r"      s   
r"   r    c                 C   s   | dur|  d| }nd}|dt   7 }|dt  7 }tjsMt r,|dt  7 }t r7|dt  7 }t rB|dt	  7 }t
 rM|d	t  7 }t|trc|d
d
dd | D  7 }nt|trn|d
| 7 }tj}|dur{|d| 7 }t|S )a  Format a user-agent string containing information about the installed packages.

    Args:
        library_name (`str`, *optional*):
            The name of the library that is making the HTTP request.
        library_version (`str`, *optional*):
            The version of the library that is making the HTTP request.
        user_agent (`str`, `dict`, *optional*):
            The user agent info in the form of a dictionary or a single string.

    Returns:
        The formatted user-agent string.
    N/zunknown/Nonez	; hf_hub/z	; python/z; torch/z; tensorflow/z	; fastai/z; fastcore/; c                 s   s"    | ]\}}| d | V  qdS )r.   Nr'   ).0kvr'   r'   r(   	<genexpr>   s     z#_http_user_agent.<locals>.<genexpr>z	; origin/)r   r   r   HF_HUB_DISABLE_TELEMETRYr   r   r   r   r   r   r   r   r*   dictjoinitemsr+   HF_HUB_USER_AGENT_ORIGIN_deduplicate_user_agent)r   r   r   uaoriginr'   r'   r(   r#      s,   
"
r#   c                 C   s   d dd | dD  S )z>Deduplicate redundant information in the generated user-agent.r/   c                 S   s   i | ]}|  d qS )N)strip)r0   keyr'   r'   r(   
<dictcomp>   s    z+_deduplicate_user_agent.<locals>.<dictcomp>;)r6   splitkeys)r   r'   r'   r(   r9      s   r9   ) __doc__typingr   r   r   huggingface_hub.errorsr    r   _authr	   _deprecationr
   _runtimer   r   r   r   r   r   r   r   r   r   _validatorsr   boolr+   r)   r"   r#   r9   r'   r'   r'   r(   <module>   sb   0
"b"
1