
    sg                        d dl Z d dlmZ d dlmZmZ d dlmZmZm	Z	 d dl
mZ ddlmZ  G d d	e      Z G d
 d      Zdeeeeef         dee   defd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y)    N)ArgumentParser)ListUnion)HfFoldercreate_repowhoami)	HTTPError   )BaseTransformersCLICommandc                   "    e Zd Zedefd       Zy)UserCommandsparserc                    | j                  dd      }|j                  d        | j                  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ddd       |j                  d        y )Nloginz6Log in using the same credentials as on huggingface.co)helpc                     t        |       S N)LoginCommandargss    M/var/www/html/venv/lib/python3.12/site-packages/transformers/commands/user.py<lambda>z2UserCommands.register_subcommand.<locals>.<lambda>   s    L4F     )funcr   z;Find out which huggingface.co account you are logged in as.c                     t        |       S r   )WhoamiCommandr   s    r   r   z2UserCommands.register_subcommand.<locals>.<lambda>       ]45H r   logoutzLog outc                     t        |       S r   )LogoutCommandr   s    r   r   z2UserCommands.register_subcommand.<locals>.<lambda>!   r   r   repoz_Deprecated: use `huggingface-cli` instead. Commands to interact with your huggingface.co repos.zPDeprecated: use `huggingface-cli` instead. huggingface.co repos related commandscreatezNDeprecated: use `huggingface-cli` instead. Create a new repo on huggingface.conamezYName for your model's repo. Will be namespaced under your username to build the model id.)typer   z--organizationz!Optional: organization namespace.z-yz--yes
store_truez"Optional: answer Yes to the prompt)actionr   c                     t        |       S r   )RepoCreateCommandr   s    r   r   z2UserCommands.register_subcommand.<locals>.<lambda>5   s    :KD:Q r   )
add_parserset_defaultsadd_subparsersadd_argumentstr)r   login_parserwhoami_parserlogout_parserrepo_parserrepo_subparsersrepo_create_parsers          r   register_subcommandz UserCommands.register_subcommand   s/   ((7o(p!!'F!G))(9v)w""(H"I))()C""(H"I ''r ( 
 &44c 5 
 -77k 8 
 	''l 	( 	

 	''(8sIl'm''glQu'v''-Q'Rr   N)__name__
__module____qualname__staticmethodr   r4    r   r   r   r      s    SN S Sr   r   c                   P    e Zd ZdZdZdZdZdZed        Z	ed        Z
ed        Zy	)
ANSIz;
    Helper for en.wikipedia.org/wiki/ANSI_escape_code
    z[1mz[31mz[90mz[0mc                 :    | j                    | | j                   S r   )_bold_resetclsss     r   boldz	ANSI.boldB       ))QC

|,,r   c                 R    | j                    | j                   | | j                   S r   )r=   _redr>   r?   s     r   redzANSI.redF   s%    ))SXXJqc#**66r   c                 :    | j                    | | j                   S r   )_grayr>   r?   s     r   grayz	ANSI.grayJ   rC   r   N)r5   r6   r7   __doc__r=   rE   rH   r>   classmethodrB   rF   rI   r9   r   r   r;   r;   8   sW     EDEF- - 7 7 - -r   r;   rowsheadersreturnc           	         t        g | | D cg c]  }t        d |D               }} dt        |      z  j                  | }g }|j	                   |j                  |        |j	                   |j                  |D cg c]  }d|z  	 c}        | D ]   }|j	                   |j                  |        " dj                  |      S c c}w c c}w )z
    Inspired by:

    - stackoverflow.com/a/8356620/593036
    - stackoverflow.com/questions/9535954/printing-lists-as-tabular-data
    c              3   D   K   | ]  }t        t        |              y wr   )lenr-   ).0xs     r   	<genexpr>ztabulate.<locals>.<genexpr>V   s     /ac#a&k/s    z{{:{}}} -
)zipmaxrQ   formatappendjoin)rL   rM   col
col_widths
row_formatlineswrows           r   tabulaterb   O   s     <?;N;Ng;NOC#/3//OJO3*s7|+33Z@JE	LL"""G,-	LL"""j$AS1W$ABC .&Z&&,-.99U P %Bs   CCc                       e Zd Zd Zy)BaseUserCommandc                     || _         y r   r   )selfr   s     r   __init__zBaseUserCommand.__init__a   s	    	r   N)r5   r6   r7   rg   r9   r   r   rd   rd   `   s    r   rd   c                       e Zd Zd Zy)r   c                 @    t        t        j                  d             y )NzERROR! `huggingface-cli login` uses an outdated login mechanism that is not compatible with the Hugging Face Hub backend anymore. Please use `huggingface-cli login instead.printr;   rF   rf   s    r   runzLoginCommand.runf   s    HH=	
r   Nr5   r6   r7   rm   r9   r   r   r   r   e       
r   r   c                       e Zd Zd Zy)r   c                    t        t        j                  d             t        j                         }|t        d       t                	 t        |      \  }}t        |       |r/t        t        j                  d      dj                  |             y y # t        $ rR}t        |       t        t        j                  |j                  j                               t        d       Y d }~y d }~ww xY w)NzxWARNING! `transformers-cli whoami` is deprecated and will be removed in v5. Please use `huggingface-cli whoami` instead.Not logged inzorgs: ,r
   )rk   r;   rF   r   	get_tokenexitr   rB   r[   r	   responsetext)rf   tokenuserorgses        r   rm   zWhoamiCommand.runq   s    HH4	
 ""$=/"F	JD$$Kdii)388D>:  	!H$((1::??+,GG	s   A	B 	C1AC,,C1Nrn   r9   r   r   r   r   p   s    r   r   c                       e Zd Zd Zy)r    c                 @    t        t        j                  d             y )NzERROR! `transformers-cli logout` uses an outdated logout mechanism that is not compatible with the Hugging Face Hub backend anymore. Please use `huggingface-cli logout instead.rj   rl   s    r   rm   zLogoutCommand.run   s    HH>	
r   Nrn   r9   r   r   r    r       ro   r   r    c                       e Zd Zd Zy)r(   c                    t        t        j                  d             t        j                         }|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
       t        |      \  }}| j                  j                  | j                  j                  n|}| d| j                  j                   }t        dt        j!                  |              | j                  j"                  s=t%        d      j'                         }|d
k(  s|dk(  s|dk(  st        d       t                	 t)        ||      }t        d       t        dt        j!                                t        d       t        d|        t        d
       y # t        $ r t        d       Y w xY w# t        $ r" t        t        j                  d	             Y zw xY w# t*        $ rR}	t        |	       t        t        j                  |	j,                  j.                               t        d       Y d }	~	d }	~	ww xY w)NzlWARNING! Managing repositories through transformers-cli is deprecated. Please use `huggingface-cli` instead.rr   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). /zYou are about to create zProceed? [Y/n] yyesAbort)repo_idrx   z
Your repo now lives at:z  zK
You can clone it locally with the command below, and commit/push as usual.z
  git clone )rk   r;   rF   r   rt   ru   
subprocesscheck_outputdecoderI   stripFileNotFoundErrorr   r   organizationr#   rB   r   inputlowerr   r	   rv   rw   )
rf   rx   stdoutry   _	namespace	full_namechoiceurlr{   s
             r   rm   zRepoCreateCommand.run   s4   HH8	
 ""$=/"G	O,,e[-ABII'RF$))FLLN+,
	,,i-EFMMgVF$))FLLN+, 	b	-a.2ii.D.D.PDII**VZ	 k499>>"23	(9)=(>?@yy}},-335FbLFcMVu_g	iu=C
 	)*499S>"#$\]se$%b	I ! 	OMN	O ! 	S	,  	!H$((1::??+,GG	s?   AH& AI I/ &H>=H>'I,+I,/	K
8AKK
Nrn   r9   r   r   r(   r(      s    2r   r(   )r   argparser   typingr   r   huggingface_hub.hf_apir   r   r   requests.exceptionsr	   r   r   r   r;   r-   intrb   rd   r   r   r    r(   r9   r   r   <module>r      s     #  @ @ ) (S- S>- -.4U38_-. c s " 

? 
O .
O 
3 3r   