
    ;3h                        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  ej2                  e      Z	 ddlmZ ddlmZ dZ  G d de	      Z" G d d      Z# G d de#      Z$ G d de#      Z% G d de#      Z& G d de#      Z' G d de#      Z(y# e!$ r dZ Y Vw xY w) 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)inquirer)ChoiceTFc                   "    e Zd Zedefd       Zy)UserCommandsparserc                    | j                  dd      }|j                  dt        d       |j                  ddd	
       |j                  d        | j                  dd      }|j                  d        | j                  dd      }|j                  dt        d       |j                  d        | j                  dd      }|j	                  d      }|j                  dd      }|j                  dt        d       |j                  ddd	
       |j                  d        |j                  dd      }|j                  d        y )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                     t        |       S N)LoginCommandargss    ]/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/huggingface_hub/commands/user.py<lambda>z2UserCommands.register_subcommand.<locals>.<lambda>H   s    L4F     )funcwhoamiz;Find out which huggingface.co account you are logged in as.c                     t        |       S r   )WhoamiCommandr!   s    r#   r$   z2UserCommands.register_subcommand.<locals>.<lambda>J       ]45H r%   r   zLog outz--token-namez3Optional: Name of the access token to log out from.c                     t        |       S r   )LogoutCommandr!   s    r#   r$   z2UserCommands.register_subcommand.<locals>.<lambda>R   r*   r%   authz%Other authentication related commandszAuthentication subcommandsswitchzSwitch between access tokensz0Optional: Name of the access token to switch to.c                     t        |       S r   )AuthSwitchCommandr!   s    r#   r$   z2UserCommands.register_subcommand.<locals>.<lambda>a   s    :KD:Q r%   listzList all stored access tokensc                     t        |       S r   )AuthListCommandr!   s    r#   r$   z2UserCommands.register_subcommand.<locals>.<lambda>c   s    8M r%   )
add_parseradd_argumentstrset_defaultsadd_subparsers)r   login_parserwhoami_parserlogout_parserauth_parserauth_subparsersauth_switch_parserauth_list_parsers           r#   register_subcommandz UserCommands.register_subcommand;   s   ((7q(r!!N 	" 	

 	!!%A 	" 	

 	!!'F!G))(9v)w""(H"I))()C""F 	# 	

 	""(H"I''5\']%44:V4W,77Ge7f''C 	( 	

 	''%A 	( 	

 	''-Q'R*55fCb5c%%+M%Nr%   N)__name__
__module____qualname__staticmethodr   r@    r%   r#   r   r   :   s    'O$5 'O 'Or%   r   c                       e Zd Zd Zy)BaseUserCommandc                 0    || _         t               | _        y r   )r"   r	   _api)selfr"   s     r#   __init__zBaseUserCommand.__init__g   s    	G	r%   N)rA   rB   rC   rK   rE   r%   r#   rG   rG   f   s    r%   rG   c                       e Zd Zd Zy)r    c                     t        j                          t        | j                  j                  | j                  j
                         y )N)tokenadd_to_git_credential)r   set_verbosity_infor   r"   rN   rO   rJ   s    r#   runzLoginCommand.runm   s.    ""$))//"&))"A"A	
r%   NrA   rB   rC   rR   rE   r%   r#   r    r    l   s    
r%   r    c                       e Zd Zd Zy)r,   c                 l    t        j                          t        | j                  j                         y )N)
token_name)r   rP   r   r"   rV   rQ   s    r#   rR   zLogoutCommand.runv   s     ""$$))../r%   NrS   rE   r%   r#   r,   r,   u   s    0r%   r,   c                   @    e Zd Zd Zdee   fdZdee   dee   fdZy)r0   c                     t        j                          | j                  j                  }|| j	                         }|t        d       t                t        || j                  j                         y )Nz!No token name provided. Aborting.)rO   )	r   rP   r"   rV   _select_token_nameprintexitr   rO   )rJ   rV   s     r#   rR   zAuthSwitchCommand.run|   sY    ""$YY))
002J56FJdii6U6UVr%   returnc                    t        t               j                               }|st        j	                  d       y t
        r| j                  |      S t        d       t        |d      D ]  \  }}t        | d|         	 	 t        d      }|j                         dk(  ry t        |      dz
  }d|cxk  rt        |      k  rn n||   S t        d       	 W# t        $ r t        d	       Y w xY w)
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.)r1   r   keysloggererror_inquirer_py_available_select_token_name_tuirZ   	enumerateinputlowerintlen
ValueError)rJ   token_namesirV   choiceindexs         r#   rY   z$AuthSwitchCommand._select_token_name   s    ,.3356LLFG!..{;;()&{A6 	(MAzQCr*&'	(
N^_<<>S(Fa0K 00&u--@A   NLMNs   C !)C C C/.C/rj   c                     |D cg c]  }t        ||       }}	 t        j                  d|d       j                         S c c}w # t        $ r t
        j                  d       Y y w xY w)N)namezSelect a token to switch to:)messagechoicesdefaultzToken selection cancelled.)r   r   selectexecuteKeyboardInterruptr`   info)rJ   rj   rV   rq   s       r#   rc   z(AuthSwitchCommand._select_token_name_tui   sk    ITU:6*:6UU	??6 gi	 V ! 	KK45	s   A%A A('A(N)	rA   rB   rC   rR   r   r6   rY   r   rc   rE   r%   r#   r0   r0   {   s5    	WNHSM N4
$s) 
 
r%   r0   c                       e Zd Zd Zy)r3   c                 @    t        j                          t                y r   )r   rP   r   rQ   s    r#   rR   zAuthListCommand.run   s    ""$r%   NrS   rE   r%   r#   r3   r3      s    r%   r3   c                       e Zd Zd Zy)r)   c                 "   t               }|t        d       t                	 | j                  j	                  |      }t        |d          |d   D cg c]  }|d   	 }}|r.t        t        j                  d      dj                  |             t        dk7  rt        dt                y y c c}w # t        $ rR}t        |       t        t        j                  |j                  j                               t        d       Y d }~y d }~ww xY w)	NzNot logged inro   orgszorgs: ,zhttps://huggingface.coz(Authenticated through private endpoint: r   )r   rZ   r[   rI   r'   r   boldjoinr   r   redresponsetext)rJ   rN   rv   orgr{   es         r#   rR   zWhoamiCommand.run   s    =/"F	99##E*D$v,+/<8CCK8D8dii)388D>:33@
KL 4	 9  	!H$((1::??+,GG	s+   0B3 B.AB3 .B3 3	D<AD		DNrS   rE   r%   r#   r)   r)      s    r%   r)   N))__doc__argparser   typingr   r   requests.exceptionsr   huggingface_hub.commandsr   huggingface_hub.constantsr   huggingface_hub.hf_apir	   _loginr   r   r   r   utilsr   r   r   
_cli_utilsr   
get_loggerrA   r`   
InquirerPyr   InquirerPy.base.controlr   rb   ImportErrorr   rG   r    r,   r0   r3   r)   rE   r%   r#   <module>r      s   & ' ! ) > . ( : : 9 9  
		H	%##.!
)O, )OX 
? 
0O 00 0fo O }  #"#s   B3 3B=<B=