
    rhq                        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j4                  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show_deprecation_warning)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    p/var/www/html/ai-insurance-compliance-backend/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   rA    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)rB   rC   rD   rL   rF   r&   r$   rH   rH   f   s    r&   rH   c                       e Zd Zd Zy)r!   c                     t        dd       t        j                          t        | j                  j
                  | j                  j                         y )Nzhuggingface-cli loginzhf auth login)tokenadd_to_git_credential)r   r   set_verbosity_infor   r#   rO   rP   rK   s    r$   runzLoginCommand.runm   s9     !8/J""$))//"&))"A"A	
r&   NrB   rC   rD   rS   rF   r&   r$   r!   r!   l   s    
r&   r!   c                       e Zd Zd Zy)r-   c                     t        dd       t        j                          t        | j                  j
                         y )Nzhuggingface-cli logoutzhf auth logout)
token_name)r   r   rQ   r   r#   rW   rR   s    r$   rS   zLogoutCommand.runx   s,     !9;KL""$$))../r&   NrT   rF   r&   r$   r-   r-   w   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)r1   c                    t        dd       t        j                          | j                  j                  }|| j                         }|t        d       t                t        || j                  j                         y )Nzhuggingface-cli auth switchzhf auth switchz!No token name provided. Aborting.)rP   )
r   r   rQ   r#   rW   _select_token_nameprintexitr   rP   )rK   rW   s     r$   rS   zAuthSwitchCommand.run   se     !>@PQ""$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.)r2   r   keysloggererror_inquirer_py_available_select_token_name_tuir[   	enumerateinputlowerintlen
ValueError)rK   token_namesirW   choiceindexs         r$   rZ   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/rk   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KeyboardInterruptra   info)rK   rk   rW   rr   s       r$   rd   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)	rB   rC   rD   rS   r   r7   rZ   r   rd   rF   r&   r$   r1   r1      s5    WNHSM N4
$s) 
 
r&   r1   c                       e Zd Zd Zy)r4   c                 X    t        dd       t        j                          t                y )Nzhuggingface-cli auth listzhf auth list)r   r   rQ   r   rR   s    r$   rS   zAuthListCommand.run   s     !<nM""$r&   NrT   rF   r&   r$   r4   r4      s    r&   r4   c                       e Zd Zd Zy)r*   c                 b   t        dd       t               }|t        d       t                	 | j                  j                  |      }t        t        j                  d      |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huggingface-cli whoamizhf auth whoamizNot logged inzuser: rp   orgszorgs: ,zhttps://huggingface.coz(Authenticated through private endpoint: r   )r   r   r[   r\   rJ   r(   r   boldjoinr   r   redresponsetext)rK   rO   rw   orgr|   es         r$   rS   zWhoamiCommand.run   s     !9;KL=/"F	99##E*D$))H%tF|4+/<8CCK8D8dii)388D>:33@
KL 4	 9  	!H$((1::??+,GG	s,   AC 3C?AC C 	D.AD))D.NrT   rF   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   r   
get_loggerrB   ra   
InquirerPyr   InquirerPy.base.controlr   rc   ImportErrorr   rH   r!   r-   r1   r4   r*   rF   r&   r$   <module>r      s   & ' ! ) > . ( : : 9 9 6 
		H	%##.!
)O, )OX 
? 
0O 02 2jo O M  #"#s   B5 5B?>B?