
    rh                        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) a  Contains commands to authenticate to the Hugging Face Hub and interact with your repositories.

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

    # switch between tokens
    hf auth switch

    # list all tokens
    hf auth list

    # logout from all tokens
    hf auth logout

    # check which account you are logged in as
    hf auth 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)AuthCommandsparserc                    | j                  dd      j                  d      }j                  fd       |j                  dd      }|j                  d	t        d
       |j                  d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 ) Nauthz,Manage authentication (login, logout, etc.).)helpzAuthentication subcommandsc                 $    j                         S N)
print_help)argsauth_parsers    k/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/huggingface_hub/cli/auth.py<lambda>z2AuthCommands.register_subcommand.<locals>.<lambda>B   s    ;3I3I3K     )funcr   z8Log in using a token from huggingface.co/settings/tokensz--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 r   )	AuthLoginr   s    r!   r"   z2AuthCommands.register_subcommand.<locals>.<lambda>R   s
    IdO r#   r   zLog outz--token-namez3Optional: Name of the access token to log out from.c                     t        |       S r   )
AuthLogoutr*   s    r!   r"   z2AuthCommands.register_subcommand.<locals>.<lambda>[       Z5E r#   whoamiz;Find out which huggingface.co account you are logged in as.c                     t        |       S r   )
AuthWhoamir*   s    r!   r"   z2AuthCommands.register_subcommand.<locals>.<lambda>a   r-   r#   switchzSwitch between access tokensz0Optional: Name of the access token to switch to.c                     t        |       S r   )
AuthSwitchr*   s    r!   r"   z2AuthCommands.register_subcommand.<locals>.<lambda>o   s    *T:J r#   listzList all stored access tokensc                     t        |       S r   )AuthListr*   s    r!   r"   z2AuthCommands.register_subcommand.<locals>.<lambda>r   s
     r#   )
add_parseradd_subparsersset_defaultsadd_argumentstr)r   auth_subparserslogin_parserlogout_parserwhoami_parserauth_switch_parserauth_list_parserr    s          @r!   register_subcommandz AuthCommands.register_subcommand;   s    ''5c'd%44:V4W 	  &K L '11T 2 
 	!!N 	" 	

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

 	!!'C!D (228)2L""F 	# 	

 	""(E"F (22X 3 
 	""(E"F -77Ge7f''C 	( 	

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

 	''-J'K*55fCb5c%%+F%Gr#   N)__name__
__module____qualname__staticmethodr   rB    r#   r!   r   r   :   s    6H$5 6H 6Hr#   r   c                       e Zd Zd Zy)BaseAuthCommandc                 0    || _         t               | _        y r   )r   r	   _api)selfr   s     r!   __init__zBaseAuthCommand.__init__v   s    	G	r#   N)rC   rD   rE   rM   rG   r#   r!   rI   rI   u   s    r#   rI   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   rP   rQ   rL   s    r!   runzAuthLogin.run|   s.    ""$))//"&))"A"A	
r#   NrC   rD   rE   rT   rG   r#   r!   r)   r)   {   s    
r#   r)   c                       e Zd Zd Zy)r,   c                 l    t        j                          t        | j                  j                         y )N)
token_name)r   rR   r   r   rX   rS   s    r!   rT   zAuthLogout.run   s     ""$$))../r#   NrU   rG   r#   r!   r,   r,      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)r3   c                     t        j                          | j                  j                  }|| j	                         }|t        d       t                t        || j                  j                         y )Nz!No token name provided. Aborting.)rQ   )	r   rR   r   rX   _select_token_nameprintexitr   rQ   )rL   rX   s     r!   rT   zAuthSwitch.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.)r4   r   keysloggererror_inquirer_py_available_select_token_name_tuir\   	enumerateinputlowerintlen
ValueError)rL   token_namesirX   choiceindexs         r!   r[   zAuthSwitch._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/rl   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KeyboardInterruptrb   info)rL   rl   rX   rs   s       r!   re   z!AuthSwitch._select_token_name_tui   sk    ITU:6*:6UU	??6 gi	 V ! 	KK45	s   A%A A('A(N)	rC   rD   rE   rT   r   r;   r[   r   re   rG   r#   r!   r3   r3      s5    	WNHSM N4
$s) 
 
r#   r3   c                       e Zd Zd Zy)r6   c                 @    t        j                          t                y r   )r   rR   r   rS   s    r!   rT   zAuthList.run   s    ""$r#   NrU   rG   r#   r!   r6   r6      s    r#   r6   c                       e Zd Zd Zy)r0   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 inrq   orgszorgs: ,zhttps://huggingface.coz(Authenticated through private endpoint: r   )r   r\   r]   rK   r.   r   boldjoinr   r   redresponsetext)rL   rP   rx   orgr}   es         r!   rT   zAuthWhoami.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NrU   rG   r#   r!   r0   r0      s    r#   r0   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_loggerrC   rb   
InquirerPyr   InquirerPy.base.controlr   rd   ImportErrorr   rI   r)   r,   r3   r6   r0   rG   r#   r!   <module>r      s   & ' ! ) > . ( : : 9 9  
		H	%##.!
8H, 8Hv 
 
0 00 0f  [  #"#s   B3 3B=<B=