a
    hq                     @   s@  d Z ddlm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 dd
lmZmZmZ ddlmZmZ eeZz ddlmZ ddlm Z  dZ!W n e"y   dZ!Y n0 G dd de	Z#G dd dZ$G dd de$Z%G dd de$Z&G dd de$Z'G dd de$Z(G dd de$Z)dS ) aD  Contains commands to authenticate to the Hugging Face Hub and interact with your repositories.

Usage:
    # login and save token locally.
    huggingface-cli login --token=hf_*** --add-to-git-credential

    # switch between tokens
    huggingface-cli auth switch

    # list all tokens
    huggingface-cli auth list

    # logout from a specific token, if no token-name is provided, all tokens will be deleted from your machine.
    huggingface-cli logout --token-name=your_token_name

    # find out which huggingface.co account you are logged in as
    huggingface-cli whoami
    )_SubParsersAction)ListOptional)	HTTPError)BaseHuggingfaceCLICommand)ENDPOINT)HfApi   )	auth_listauth_switchloginlogout)get_stored_tokens	get_tokenlogging   )ANSIshow_deprecation_warning)inquirerChoiceTFc                   @   s   e Zd ZeedddZdS )UserCommands)parserc                 C   s  | j ddd}|jdtdd |jddd	d
 |jdd d | j ddd}|jdd d | j ddd}|jdtdd |jdd d | j ddd}|jdd}|j ddd}|jdtdd |jddd	d
 |jdd d |j ddd}|jdd d d S ) Nr   z8Log in using a token from huggingface.co/settings/tokens)helpz--tokenz;Token generated from https://huggingface.co/settings/tokens)typer   z--add-to-git-credential
store_truez.Optional: Save token to git credential helper.)actionr   c                 S   s   t | S N)LoginCommandargs r!   Y/var/www/html/assistant/venv/lib/python3.9/site-packages/huggingface_hub/commands/user.py<lambda>H       z2UserCommands.register_subcommand.<locals>.<lambda>)funcwhoamiz;Find out which huggingface.co account you are logged in as.c                 S   s   t | S r   )WhoamiCommandr   r!   r!   r"   r#   J   r$   r   zLog outz--token-namez3Optional: Name of the access token to log out from.c                 S   s   t | S r   )LogoutCommandr   r!   r!   r"   r#   R   r$   authz%Other authentication related commandszAuthentication subcommandsswitchzSwitch between access tokensz0Optional: Name of the access token to switch to.c                 S   s   t | S r   )AuthSwitchCommandr   r!   r!   r"   r#   a   r$   listzList all stored access tokensc                 S   s   t | S r   )AuthListCommandr   r!   r!   r"   r#   c   r$   )
add_parseradd_argumentstrset_defaultsadd_subparsers)r   Zlogin_parserZwhoami_parserZlogout_parserZauth_parserZauth_subparsersZauth_switch_parserZauth_list_parserr!   r!   r"   register_subcommand;   sJ    z UserCommands.register_subcommandN)__name__
__module____qualname__staticmethodr   r3   r!   r!   r!   r"   r   :   s   r   c                   @   s   e Zd Zdd ZdS )BaseUserCommandc                 C   s   || _ t | _d S r   )r    r   _api)selfr    r!   r!   r"   __init__g   s    zBaseUserCommand.__init__N)r4   r5   r6   r;   r!   r!   r!   r"   r8   f   s   r8   c                   @   s   e Zd Zdd ZdS )r   c                 C   s*   t dd t  t| jj| jjd d S )Nzhuggingface-cli loginzhf auth login)tokenadd_to_git_credential)r   r   set_verbosity_infor   r    r<   r=   r:   r!   r!   r"   runm   s    
zLoginCommand.runNr4   r5   r6   r@   r!   r!   r!   r"   r   l   s   r   c                   @   s   e Zd Zdd ZdS )r(   c                 C   s$   t dd t  t| jjd d S )Nzhuggingface-cli logoutzhf auth logout)
token_name)r   r   r>   r   r    rB   r?   r!   r!   r"   r@   x   s    
zLogoutCommand.runNrA   r!   r!   r!   r"   r(   w   s   r(   c                   @   s>   e Zd Zdd Zee dddZee ee dddZd	S )
r+   c                 C   sT   t dd t  | jj}|d u r*|  }|d u r@td t  t|| jj	d d S )Nzhuggingface-cli auth switchzhf auth switchz!No token name provided. Aborting.)r=   )
r   r   r>   r    rB   _select_token_nameprintexitr   r=   )r:   rB   r!   r!   r"   r@      s    
zAuthSwitchCommand.run)returnc                 C   s   t t  }|s td d S tr.| |S td t|dD ]\}}t| d|  q@zXt	d}|
 dkrxW d S t|d }d|  krt|k rn n
|| W S td W q\ ty   td	 Y q\0 q\d S )
Nz+No stored tokens found. Please login first.zAvailable stored tokens:r   z. z=Enter the number of the token to switch to (or 'q' to quit): qr   z$Invalid selection. Please try again.z4Invalid input. Please enter a number or 'q' to quit.)r,   r   keysloggererror_inquirer_py_available_select_token_name_tuirD   	enumerateinputlowerintlen
ValueError)r:   token_namesirB   choiceindexr!   r!   r"   rC      s&    


z$AuthSwitchCommand._select_token_name)rS   rF   c                 C   sH   dd |D }zt jd|d d W S  tyB   td Y d S 0 d S )Nc                 S   s   g | ]}t ||d qS )namer   ).0rB   r!   r!   r"   
<listcomp>   r$   z<AuthSwitchCommand._select_token_name_tui.<locals>.<listcomp>zSelect a token to switch to:)messagechoicesdefaultzToken selection cancelled.)r   selectexecuteKeyboardInterruptrI   info)r:   rS   r\   r!   r!   r"   rL      s    
z(AuthSwitchCommand._select_token_name_tuiN)	r4   r5   r6   r@   r   r0   rC   r   rL   r!   r!   r!   r"   r+      s   r+   c                   @   s   e Zd Zdd ZdS )r-   c                 C   s   t dd t  t  d S )Nzhuggingface-cli auth listzhf auth list)r   r   r>   r
   r?   r!   r!   r"   r@      s    
zAuthListCommand.runNrA   r!   r!   r!   r"   r-      s   r-   c                   @   s   e Zd Zdd ZdS )r'   c              
   C   s   t dd t }|d u r&td t  zf| j|}ttd|d  dd |d D }|rtttd	d
| t	dkrtdt	  W nF t
y } z.t| tt|jj td W Y d }~n
d }~0 0 d S )Nzhuggingface-cli whoamizhf auth whoamizNot logged inzuser: rX   c                 S   s   g | ]}|d  qS rW   r!   )rY   orgr!   r!   r"   rZ      r$   z%WhoamiCommand.run.<locals>.<listcomp>orgszorgs: ,zhttps://huggingface.coz(Authenticated through private endpoint: r   )r   r   rD   rE   r9   r&   r   boldjoinr   r   redresponsetext)r:   r<   ra   rc   er!   r!   r"   r@      s"    
zWhoamiCommand.runNrA   r!   r!   r!   r"   r'      s   r'   N)*__doc__argparser   typingr   r   Zrequests.exceptionsr   Zhuggingface_hub.commandsr   Zhuggingface_hub.constantsr   Zhuggingface_hub.hf_apir   Z_loginr
   r   r   r   utilsr   r   r   Z
_cli_utilsr   r   Z
get_loggerr4   rI   Z
InquirerPyr   ZInquirerPy.base.controlr   rK   ImportErrorr   r8   r   r(   r+   r-   r'   r!   r!   r!   r"   <module>   s.   

,5