wL i^ vUdZddlZddlZddlZddlZddlmZddlmZddl m Z m Z ddl m Z dd lmZmZd aeZdae eed <ej,eZd e efd Zd e efdZd e efdZd e efdZd e eeffdZde eefd dfdZded e efdZdeded dfdZ de ed e efdZ!y)zWContains an helper to get the token from machine (env variable, secret or config file).N)Path)Lock)DictOptional) constants)is_colab_enterpriseis_google_colabF_GOOGLE_COLAB_SECRETreturncFtxstxs tS)aA Get token if user is logged in. Note: in most cases, you should use [`huggingface_hub.utils.build_hf_headers`] instead. This method is only useful if you want to retrieve the token for other purposes than sending an HTTP request. Token is retrieved in priority from the `HF_TOKEN` environment variable. Otherwise, we read the token file located in the Hugging Face home folder. Returns None if user is not logged in. To log in, use [`login`] or `hf auth login`. Returns: `str` or `None`: The token, `None` if it doesn't exist. )_get_token_from_google_colab_get_token_from_environment_get_token_from_filea/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/huggingface_hub/utils/_auth.py get_tokenr#s ( ) d-H-J dNbNddrc(tr tryt5trtcdddS ddlm}ddlm} |jd}t|ad atcdddS#t$r YdddywxYw#|j$rtjddaYP|j$rtjddaYw|$r.}tjdt!|d daYd}~d}~wwxYw#1swYyxYw) zGet token from Google Colab secrets vault using `google.colab.userdata.get(...)`. Token is read from the vault only once per session and then stored in a global variable to avoid re-requesting access to the vault. Nr)userdata)ErrorHF_TOKENz Access to the secret `HF_TOKEN` has not been granted on this notebook. You will not be requested again. Please restart the session if you want to be prompted again.a The secret `HF_TOKEN` does not exist in your Colab secrets. To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session. You will be able to reuse this secret in all of your notebooks. Please note that authentication is recommended but still optional to access public models or datasets.z@ Error while fetching `HF_TOKEN` secret value from your vault: 'z'. You are not authenticated with the Hugging Face Hub in this notebook. If the error persists, please let us know by opening an issue on GitHub (https://github.com/huggingface/huggingface_hub/issues/new).T)r r _GOOGLE_COLAB_SECRET_LOCK_IS_GOOGLE_COLAB_CHECKEDr google.colabrgoogle.colab.errorsr ImportErrorget _clean_tokenNotebookAccessErrorwarningswarnSecretNotFoundErrorstr)r ColabErrortokenes rrr4s+   3 5 #.$ $' .$.$  - ? (LL,E#/#6 <$( #].$.$ .$.$  ++ ( MMQ  $( ++ ( MM{ $(  ( MMSTWXYTZS[\OO  $(  (G.$.$sj D A-BD- B6DBD&D*D,%DDD$D;DDDDcttjjdxstjjdS)NrHUGGING_FACE_HUB_TOKEN)r osenvironrrrrrrts-  z2^bjjnnE]6^ __rc tttjj S#t $rYywxYw)N)r rr HF_TOKEN_PATH read_textFileNotFoundErrorrrrrrys8D!8!89CCEFF s /2 >>cttj}|jsi}t j } |j ||jDcic]}||j|d}}|Scc}w#tj$r%}tjd|i}Yd}~|Sd}~wwxYw)aA Returns the parsed INI file containing the access tokens. The file is located at `HF_STORED_TOKENS_PATH`, defaulting to `~/.cache/huggingface/stored_tokens`. If the file does not exist, an empty dictionary is returned. Returns: `Dict[str, str]` Key is the token name and value is the token. hf_tokenz"Error parsing stored tokens file: N) rrHF_STORED_TOKENS_PATHexists configparser ConfigParserreadsectionsrrloggererror) tokens_path stored_tokensconfig token_namer(s rget_stored_tokensr?sy667K      & & (F K Z`ZiZiZklJVZZ J%GGl l  m    9!=> s*#B$B>BBB?B::B?r<cttj}tj}t |j D])}|j||j|d||+|jjdd|jd5}|j|dddy#1swYyxYw)z Saves the given configuration to the stored tokens file. Args: stored_tokens (`Dict[str, str]`): The stored tokens to save. Key is the token name and value is the token. r2T)parentsexist_okwN) rrr3r5r6sortedkeys add_sectionsetparentmkdiropenwrite)r<stored_tokens_pathr=r> config_files r_save_stored_tokensrNsi==> & & (F]//12F :& :z=+DEF##D4#@   %" [!"""s !B<<Cr>c<t}||vryt||S)z Get the token by name. Args: token_name (`str`): The name of the token to get. Returns: `str` or `None`: The token, `None` if it doesn't exist. N)r?r )r>r<s r_get_token_by_namerPs'&'M&  j1 22rr'cttj}t}|||<t |t j d|d|y)z Save the given token. If the stored tokens file does not exist, it will be created. Args: token (`str`): The token to save. token_name (`str`): The name of the token. z The token `z` has been saved to N)rrr3r?rNr9info)r'r>r;r<s r _save_tokenrSsHy667K%'M %M* & KK+j\)=k]KLrcp|y|jddjddjxsdS)zuClean token by removing trailing and leading spaces and newlines. If token is an empty string, return None. N  )replacestrip)r's rr r s8  } ==r " * *4 4 : : < DDr)"__doc__r5loggingr+r"pathlibr threadingrtypingrrrVr_runtimer r rrr r%__annotations__ getLogger__name__r9rrrrr?rNrPrSr rrrrcs^ !:! F&*hsm*   8 $e8C=e"=$hsm=$@`Xc]` hsm4S>,"tCH~"$"*3338C=3$MsMMM$E E(3-Er