wL i"dZddlmZmZmZddlmZddlmZddl m Z ddl m Z dd l mZmZmZmZmZmZmZmZmZmZdd lmZe d d d eddddddddeeeefdeedeedeeedfdeeeefd edeeeffdZdeeeefdeefdZdddddeedeedeeedfdefdZdedefdZy)zIContains utilities to handle headers to send in calls to Huggingface Hub.)DictOptionalUnion)LocalTokenNotFoundError) constants) get_token)_deprecate_arguments) get_fastai_versionget_fastcore_versionget_hf_hub_versionget_python_versionget_tf_versionget_torch_versionis_fastai_availableis_fastcore_availableis_tf_availableis_torch_available)validate_hf_hub_argsz1.0is_write_actionz\This argument is ignored and we let the server handle the permission error instead (if any).)versiondeprecated_argscustom_messageNF)token library_namelibrary_version user_agentheadersrrrrrrreturncvt|}dt|||i}|d||d<||j||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-agentrrrzBearer authorization)get_token_to_send_http_user_agentupdate)rrrrrr token_to_send hf_headerss d/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/huggingface_hub/utils/_headers.pybuild_hf_headersr*'s_n&e,M &%+! J (/ &? ?#'" ct|tr|S|duryt}|dur| td|Stj ry|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 `hf auth login` or `huggingface_hub.login`. See https://huggingface.co/settings/tokens.) isinstancestrr rrHF_HUB_DISABLE_IMPLICIT_TOKEN)r cached_tokens r)r$r$sg%  ~;L }  );  .. r+r"c>||d|}nd}|dtz }|dtz }tjsht r|dt z }t r|dtz }tr|dtz }tr|dtz }t|tr-|d d jd |jDzz }nt|t r|d |zz }tj"}||d |zz }t%|S) aFormat 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. /z unknown/Nonez ; hf_hub/z ; python/z; torch/z ; tensorflow/z ; fastai/z ; fastcore/; c30K|]\}}|d|yw)r2N).0kvs r) z#_http_user_agent..sIda!AaSzIsz ; origin/)rrrHF_HUB_DISABLE_TELEMETRYrrrrrr rr r-dictjoinitemsr.HF_HUB_USER_AGENT_ORIGIN_deduplicate_user_agent)rrruaorigins r)r%r%s;&~Q/ 0 I(*+ ,,BI(*+ ,,B  - -   H.012 2B   M."2!34 4B  I0234 4B " K 4 678 8B*d# dTYYIj6F6F6HIIII J $ dZ / /F  kF"" "2 &&r+cdj|jdDcic]}|jdc}jScc}w)z>Deduplicate redundant information in the generated user-agent.r3;N)r<splitstripkeys)rkeys r)r?r?s@ 99:3C3CC3HICciik4'INNP QQIsA ) __doc__typingrrrhuggingface_hub.errorsrr_authr _deprecationr _runtimer r rrrrrrrr _validatorsrboolr.r*r$r%r?r5r+r)rQs{P((:.   . %q )-"&%))-(,!_ E$)$ %_3-_c] _ dCo& _ d38n % __ #s(^_  _DXeD#I&67HSMD#'%))- .'3-.'c].'dCo& .'  .'bRRRr+