
    g+                        d Z ddl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mZmZmZ ddlmZ d	d
lmZmZmZmZmZmZmZmZ d	dlmZmZmZ ddlm Z   ejB                  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+      Z0 G d  d!e+      Z1y# e)$ r dZ(Y aw xY w)"a  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

    # create a new dataset repo on the Hub
    huggingface-cli repo create mydataset --type=dataset

    N)_SubParsersAction)ListOptional)	HTTPError)BaseHuggingfaceCLICommand)ENDPOINT
REPO_TYPESREPO_TYPES_URL_PREFIXESSPACES_SDK_TYPES)HfApi   )NOTEBOOK_LOGIN_PASSWORD_HTMLNOTEBOOK_LOGIN_TOKEN_HTML_ENDNOTEBOOK_LOGIN_TOKEN_HTML_START	auth_listauth_switchloginlogoutnotebook_login)get_stored_tokens	get_tokenlogging   )ANSI)inquirer)ChoiceTFc                   "    e Zd Zedefd       Zy)UserCommandsparserc                 4   | 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        | j                  dd       }|j	                  d!      }	|	j                  d"d#      }
|
j                  d$t        d%       |
j                  d&t        d'       |
j                  d(t        d)       |
j                  d*t        d+t
        ,       |
j                  d-d.dd/
       |
j                  d0        y )1Nr   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    R/var/www/openai/venv/lib/python3.12/site-packages/huggingface_hub/commands/user.py<lambda>z2UserCommands.register_subcommand.<locals>.<lambda>V   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>X   	    ]45Hr,   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>`   r1   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>o   
    :KD:Qr,   listzList all stored access tokensc                     t        |       S r&   )AuthListCommandr(   s    r*   r+   z2UserCommands.register_subcommand.<locals>.<lambda>q   s	    8Mr,   repoz={create} Commands to interact with your huggingface.co repos.z%huggingface.co repos related commandscreatez#Create a new repo on huggingface.conamezPName for your repo. Will be namespaced under your username to build the repo id.z--typezbOptional: repo_type: set to "dataset" or "space" if creating a dataset or space, default is model.z--organizationz!Optional: organization namespace.z--space_sdkzOOptional: Hugging Face Spaces SDK type. Required when --type is set to "space".)r"   r!   choicesz-yz--yesz"Optional: answer Yes to the promptc                     t        |       S r&   )RepoCreateCommandr(   s    r*   r+   z2UserCommands.register_subcommand.<locals>.<lambda>   r8   r,   )
add_parseradd_argumentstrset_defaultsadd_subparsersr   )r   login_parserwhoami_parserlogout_parserauth_parserauth_subparsersauth_switch_parserauth_list_parserrepo_parserrepo_subparsersrepo_create_parsers              r*   register_subcommandz UserCommands.register_subcommandI   ss   ((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%N''5t'u%44:a4b,77Gl7m''c 	( 	

 	''u 	( 	

 	''(8sIl'm''b$	 	( 	
 	''5	 	( 	
 	''-Q'Rr,   N)__name__
__module____qualname__staticmethodr   rQ    r,   r*   r   r   H   s"    CS$5 CS CS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__   s    	G	r,   N)rR   rS   rT   r\   rV   r,   r*   rX   rX      s    r,   rX   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)   r_   r`   r[   s    r*   runzLoginCommand.run   s.    ""$))//"&))"A"A	
r,   NrR   rS   rT   rc   rV   r,   r*   r'   r'      s    
r,   r'   c                       e Zd Zd Zy)r3   c                 l    t        j                          t        | j                  j                         y )N)
token_name)r   ra   r   r)   rg   rb   s    r*   rc   zLogoutCommand.run   s     ""$$))../r,   Nrd   rV   r,   r*   r3   r3      s    0r,   r3   c                   @    e Zd Zd Zdee   fdZdee   dee   fdZy)r7   c                     t        j                          | j                  j                  }|| j	                         }|t        d       t                t        || j                  j                         y )Nz!No token name provided. Aborting.)r`   )	r   ra   r)   rg   _select_token_nameprintexitr   r`   )r[   rg   s     r*   rc   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.)r9   r   keysloggererror_inquirer_py_available_select_token_name_tuirk   	enumerateinputlowerintlen
ValueError)r[   token_namesirg   choiceindexs         r*   rj   z$AuthSwitchCommand._select_token_name   s    ,.3356LLFG!..{;;()&{A6MAzQCr*&' 7
N^_<<>S(Fa0K 00&u--@A   NLMNs   C !)C C C/.C/r{   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)r>   zSelect a token to switch to:)messager?   defaultzToken selection cancelled.)r   r   selectexecuteKeyboardInterruptrq   info)r[   r{   rg   r?   s       r*   rt   z(AuthSwitchCommand._select_token_name_tui   sl    ITU:6*:6U	??6 gi	 V ! 	KK45	s   A%A A('A(N)	rR   rS   rT   rc   r   rD   rj   r   rt   rV   r,   r*   r7   r7      s5    	WNHSM N4
$s) 
 
r,   r7   c                       e Zd Zd Zy)r;   c                 @    t        j                          t                y r&   )r   ra   r   rb   s    r*   rc   zAuthListCommand.run   s    ""$r,   Nrd   rV   r,   r*   r;   r;      s    r,   r;   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)	NNot logged inr>   orgszorgs: ,zhttps://huggingface.coz(Authenticated through private endpoint: r   )r   rk   rl   rZ   r.   r   boldjoinr   r   redresponsetext)r[   r_   r   orgr   es         r*   rc   zWhoamiCommand.run   s    =/"F	99##E*D$v,+/<8<CCK<D8dii)388D>:33@
KL 4	 9  	!H$((1::??+,GG	s+   0B3 B.AB3 .B3 3	D<AD		DNrd   rV   r,   r*   r0   r0      s    r,   r0   c                       e Zd Zd Zy)rA   c                    t               }|t        d       t        d       	 t        j                  ddg      j                  d      }t        t        j                  |j                                      	 t        j                  ddg      j                  d      }t        t        j                  |j                                      t        d	       | j                  j                  |      d
   }| j                  j                  | j                  j                  n|}| d| j                  j                   }| j                  j                   t"        vrt        d       t        d       | j                  j                   t$        v r!t$        | j                  j                      |z   }n|}t        dt        j&                  |              | j                  j(                  s=t+        d      j-                         }|d	k(  s|dk(  s|dk(  st        d       t                	 | j                  j/                  ||| j                  j                   | j                  j0                        }t        d       t        dt        j&                                t        d       t        d|        t        d	       y # t        $ r t        d       Y Zw xY w# t        $ r" t        t        j                  d             Y 5w xY w# t2        $ rR}	t        |	       t        t        j                  |	j4                  j6                               t        d       Y d }	~	d }	~	ww xY w)Nr   r   gitz	--versionzutf-8z9Looks like you do not have git installed, please install.zgit-lfszLooks like you do not have git-lfs installed, please install. You can install from https://git-lfs.github.com/. Then run `git lfs install` (you only have to do this once). r>   /zInvalid repo --typezYou are about to create zProceed? [Y/n] yyesAbort)repo_idr_   	repo_type	space_sdkz
Your repo now lives at:z  zK
You can clone it locally with the command below, and commit/push as usual.z
  git clone )r   rk   rl   
subprocesscheck_outputdecoder   graystripFileNotFoundErrorr   rZ   r.   r)   organizationr>   r"   r	   r
   r   r   rv   rw   create_repor   r   r   r   )
r[   r_   stdoutuser	namespacer   prefixed_repo_idr}   urlr   s
             r*   rc   zRepoCreateCommand.run   s   =/"G	O,,e[-ABII'RF$))FLLN+,
	,,i-EFMMgVF$))FLLN+, 	b	yy&v..2ii.D.D.PDII**VZ	Kq 0199>>+'(G99>>446tyy~~FP&(3C)D(EFGyy}},-335FbLFcMVu_g
	))''))..))--	 ( C 	)*499S>"#$\]se$%b	i ! 	OMN	O ! 	S	L  	!H$((1::??+,GG	s?   AJ9 7AK !AL 9KK'K?>K?	MAMMNrd   rV   r,   r*   rA   rA      s    <r,   rA   )2__doc__r   argparser   typingr   r   requests.exceptionsr   huggingface_hub.commandsr   huggingface_hub.constantsr   r	   r
   r   huggingface_hub.hf_apir   _loginr   r   r   r   r   r   r   r   utilsr   r   r   
_cli_utilsr   
get_loggerrR   rq   
InquirerPyr   InquirerPy.base.controlr   rs   ImportErrorr   rX   r'   r3   r7   r;   r0   rA   rV   r,   r*   <module>r      s   .  & ! ) > e e (	 	 	 : 9  
		H	%##.!
ES, ESP 
? 
0O 00 0fo O *= =_  #"#s   *C CC