
    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j!        e"          Z#	 ddl$m%Z% ddl&m'Z' dZ(n# e)$ r dZ(Y nw xY w 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+          Z1dS )"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dS )UserCommandsparserc                    |                      dd          }|                    dt          d           |                    ddd	
           |                    d            |                      dd          }|                    d            |                      dd          }|                    dt          d           |                    d            |                      dd          }|                    d          }|                     dd          }|                    dt          d           |                    ddd	
           |                    d            |                     dd          }|                    d            |                      dd           }|                    d!          }	|	                     d"d#          }
|
                    d$t          d%           |
                    d&t          d'           |
                    d(t          d)           |
                    d*t          d+t
          ,           |
                    d-d.dd/
           |
                    d0            d S )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    Y/var/www/html/ai-engine/env/lib/python3.11/site-packages/huggingface_hub/commands/user.py<lambda>z2UserCommands.register_subcommand.<locals>.<lambda>V   s    L4F4F     )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       ]45H5H 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>`   r2   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:Q:Q r-   listzList all stored access tokensc                      t          |           S r'   )AuthListCommandr)   s    r+   r,   z2UserCommands.register_subcommand.<locals>.<lambda>q   s    8M8M r-   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>   r9   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   s-   ((7q(rr!!N 	" 	
 	
 	

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

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

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

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

 	''-Q-Q'RRR*55fCb5cc%%+M+M%NNN''5t'uu%44:a4bb,77Gl7mm''c 	( 	
 	
 	

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

 	''(8sIl'mmm''b$	 	( 	
 	
 	
 	''5	 	( 	
 	
 	
 	''-Q-Q'RRRRRr-   N)__name__
__module____qualname__staticmethodr   rR    r-   r+   r   r   H   sH        CS$5 CS CS CS \CS CS CSr-   r   c                       e Zd Zd ZdS )BaseUserCommandc                 :    || _         t                      | _        d S r'   )r*   r   _api)selfr*   s     r+   __init__zBaseUserCommand.__init__   s    	GG			r-   N)rS   rT   rU   r]   rW   r-   r+   rY   rY      s#            r-   rY   c                       e Zd Zd ZdS )r(   c                 v    t          j                     t          | j        j        | j        j                   d S )N)tokenadd_to_git_credential)r   set_verbosity_infor   r*   r`   ra   r\   s    r+   runzLoginCommand.run   sA    "$$$)/"&)"A	
 	
 	
 	
 	
 	
r-   NrS   rT   rU   rd   rW   r-   r+   r(   r(      s#        
 
 
 
 
r-   r(   c                       e Zd Zd ZdS )r4   c                 `    t          j                     t          | j        j                   d S )N)
token_name)r   rb   r   r*   rh   rc   s    r+   rd   zLogoutCommand.run   s-    "$$$$).//////r-   Nre   rW   r-   r+   r4   r4      s#        0 0 0 0 0r-   r4   c                   T    e Zd Zd Zdee         fdZdee         dee         fdZdS )r8   c                     t          j                     | j        j        }||                                 }|t          d           t                       t          || j        j                   d S )Nz!No token name provided. Aborting.)ra   )	r   rb   r*   rh   _select_token_nameprintexitr   ra   )r\   rh   s     r+   rd   zAuthSwitchCommand.run   sn    "$$$Y)
0022J5666FFFJdi6UVVVVVVr-   returnc                 f   t          t                                                                }|st                              d           d S t
          r|                     |          S t          d           t          |d          D ]\  }}t          | d|            	 	 t          d          }|
                                dk    rd S t          |          dz
  }d|cxk    rt          |          k     rn n||         S t          d	           n# t          $ r t          d
           Y nw xY w)Nz+No stored tokens found. Please login first.zAvailable stored tokens:r   z. Tz=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_tuirl   	enumerateinputlowerintlen
ValueError)r\   token_namesirh   choiceindexs         r+   rk   z$AuthSwitchCommand._select_token_name   sk   ,..335566 	LLFGGG4! 	<..{;;;()))&{A66 	( 	(MAzQ&&*&&''''	N
N^__<<>>S((4Fa0000K 0 000000&u--@AAAA N N NLMMMMMN	Ns   #'D 6D D D/.D/r|   c                     d |D             }	 t          j        d|d                                           S # t          $ r t                              d           Y d S w xY w)Nc                 0    g | ]}t          ||           S )r?   r   ).0rh   s     r+   
<listcomp>z<AuthSwitchCommand._select_token_name_tui.<locals>.<listcomp>   s%    UUU:6*:666UUUr-   zSelect a token to switch to:)messager@   defaultzToken selection cancelled.)r   selectexecuteKeyboardInterruptrr   info)r\   r|   r@   s      r+   ru   z(AuthSwitchCommand._select_token_name_tui   s~    UUUUU	?6   gii	
 ! 	 	 	KK455544	s   (7 $AAN)	rS   rT   rU   rd   r   rE   rk   r   ru   rW   r-   r+   r8   r8      st        	W 	W 	WNHSM N N N N4
$s) 
 
 
 
 
 
 
r-   r8   c                       e Zd Zd ZdS )r<   c                 H    t          j                     t                       d S r'   )r   rb   r   rc   s    r+   rd   zAuthListCommand.run   s    "$$$r-   Nre   rW   r-   r+   r<   r<      s#            r-   r<   c                       e Zd Zd ZdS )r1   c                 \   t                      }|t          d           t                       	 | j                            |          }t          |d                    d |d         D             }|r5t          t          j        d          d                    |                     t          dk    rt          dt                      d S d S # t          $ rT}t          |           t          t          j
        |j        j                             t          d	           Y d }~d S d }~ww xY w)
NNot logged inr?   c                     g | ]
}|d          S r   rW   )r   orgs     r+   r   z%WhoamiCommand.run.<locals>.<listcomp>   s    888CCK888r-   orgszorgs: ,zhttps://huggingface.coz(Authenticated through private endpoint: r   )r   rl   rm   r[   r/   r   boldjoinr   r   redresponsetext)r\   r`   r   r   es        r+   rd   zWhoamiCommand.run   s$   =/"""FFF	9##E**D$v,884<888D ;di))388D>>:::333KKKLLLLL 43 	 	 	!HHH$(1:?++,,,GGGGGGGGG	s   BC 
D+A	D&&D+Nre   rW   r-   r+   r1   r1      s#            r-   r1   c                       e Zd Zd ZdS )rB   c                    t                      }|t          d           t          d           	 t          j        ddg                              d          }t          t          j        |                                                     n# t          $ r t          d           Y nw xY w	 t          j        ddg                              d          }t          t          j        |                                                     n1# t          $ r$ t          t          j
        d                     Y nw xY wt          d	           | 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        sPt+          d                                          }|d	k    s)|dk    s#|dk    st          d           t                       	 | j                            ||| j        j        | j        j                  }n`# t2          $ rS}	t          |	           t          t          j
        |	j        j                             t          d           Y d }	~	nd }	~	ww xY wt          d           t          dt          j        |                      t          d           t          d|            t          d	           d S )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   rl   rm   
subprocesscheck_outputdecoder   graystripFileNotFoundErrorr   r[   r/   r*   organizationr?   r#   r	   r
   r   r   rw   rx   create_repor   r   r   r   )
r\   r`   stdoutuser	namespacer   prefixed_repo_idr~   urlr   s
             r+   rd   zRepoCreateCommand.run   s]   =/"""GGG	O,e[-ABBII'RRF$)FLLNN++,,,,  	O 	O 	OMNNNNN	O
	,i-EFFMMgVVF$)FLLNN++,,,,  	 	 	S     	 	b			y&&v..2i.D.PDI**VZ	11119>++'(((GGG9>4446ty~FP&F3C)D)DFFGGGy} 	,--3355FbLLFcMMVu__g
	)'').)-	 (  CC  	 	 	!HHH$(1:?++,,,GGGGGGGG	 	)***#49S>>##$$$\]]]$s$$%%%b					s>   AB B)(B)-AD
 
+D87D872J* *
L4A	LLNre   rW   r-   r+   rB   rB      s#        < < < < <r-   rB   )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_loggerrS   rr   
InquirerPyr   InquirerPy.base.controlr   rt   ImportErrorr   rY   r(   r4   r8   r<   r1   rB   rW   r-   r+   <module>r      s   .     & & & & & & ! ! ! ! ! ! ! ! ) ) ) ) ) ) > > > > > > e e e e e e e e e e e e ( ( ( ( ( (	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 : 9 9 9 9 9 9 9 9 9       
	H	%	%#######......! # # #"#ES ES ES ES ES, ES ES ESP       
 
 
 
 
? 
 
 
0 0 0 0 0O 0 0 00 0 0 0 0 0 0 0f    o       O   *= = = = = = = = = =s   (A7 7B B