o
    h                     @   s   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	 d	eZG d
d dZdeeeeef   dee defddZG dd dZG dd deZG dd deZG dd deZG dd deZdS )    N)ArgumentParser)ListUnion)HfFoldercreate_repowhoami)	HTTPError   )BaseTransformersCLICommandc                   @   s   e Zd ZedefddZdS )UserCommandsparserc                 C   s   | j ddd}|jdd d | j ddd}|jd	d d | j d
dd}|jdd d | j ddd}|jdd}|j ddd}|jdtdd |jdtdd |jddddd |jdd d d S )Nloginz6Log in using the same credentials as on huggingface.co)helpc                 S      t | S N)LoginCommandargs r   P/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/commands/user.py<lambda>       z2UserCommands.register_subcommand.<locals>.<lambda>)funcr   z;Find out which huggingface.co account you are logged in as.c                 S   r   r   )WhoamiCommandr   r   r   r   r      r   logoutzLog outc                 S   r   r   )LogoutCommandr   r   r   r   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                 S   r   r   )RepoCreateCommandr   r   r   r   r   5   r   )
add_parserset_defaultsadd_subparsersadd_argumentstr)r   login_parserwhoami_parserlogout_parserrepo_parserrepo_subparsersrepo_create_parserr   r   r   register_subcommand   s0   z UserCommands.register_subcommandN)__name__
__module____qualname__staticmethodr   r.   r   r   r   r   r      s    r   c                   @   sD   e Zd ZdZdZdZdZdZedd Z	edd	 Z
ed
d ZdS )ANSIz;
    Helper for en.wikipedia.org/wiki/ANSI_escape_code
    z[1mz[31mz[90mz[0mc                 C      | j  | | j S r   )_bold_resetclssr   r   r   boldB      z	ANSI.boldc                 C   s   | j  | j | | j S r   )r5   _redr6   r7   r   r   r   redF   s   zANSI.redc                 C   r4   r   )_grayr6   r7   r   r   r   grayJ   r;   z	ANSI.grayN)r/   r0   r1   __doc__r5   r<   r>   r6   classmethodr:   r=   r?   r   r   r   r   r3   8   s    

r3   rowsheadersreturnc                 C   s   dd t g | |R  D }dt| j| }g }||j|  ||jdd |D   | D ]
}||j|  q0d|S )z
    Inspired by:

    - stackoverflow.com/a/8356620/593036
    - stackoverflow.com/questions/9535954/printing-lists-as-tabular-data
    c                 S   s   g | ]}t d d |D qS )c                 s   s    | ]	}t t|V  qd S r   )lenr'   ).0xr   r   r   	<genexpr>V   s    z&tabulate.<locals>.<listcomp>.<genexpr>)max)rF   colr   r   r   
<listcomp>V   s    ztabulate.<locals>.<listcomp>z{{:{}}} c                 S   s   g | ]}d | qS )-r   )rF   wr   r   r   rK   Z   s    
)ziprE   formatappendjoin)rB   rC   
col_widths
row_formatlinesrowr   r   r   tabulateO   s   
rW   c                   @      e Zd Zdd ZdS )BaseUserCommandc                 C   s
   || _ d S r   r   )selfr   r   r   r   __init__a   s   
zBaseUserCommand.__init__N)r/   r0   r1   r[   r   r   r   r   rY   `       rY   c                   @   rX   )r   c                 C      t td d S )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.printr3   r=   rZ   r   r   r   runf   
   zLoginCommand.runNr/   r0   r1   ra   r   r   r   r   r   e   r\   r   c                   @   rX   )r   c              
   C   s   t td t }|d u rt d t  zt|\}}t | |r1t tdd| W d S W d S  t	yW } zt | t t|j
j td W Y d }~d S d }~ww )NzxWARNING! `transformers-cli whoami` is deprecated and will be removed in v5. Please use `huggingface-cli whoami` instead.Not logged inzorgs: ,r	   )r_   r3   r=   r   	get_tokenexitr   r:   rR   r   responsetext)rZ   tokenuserorgser   r   r   ra   q   s*   zWhoamiCommand.runNrc   r   r   r   r   r   p   r\   r   c                   @   rX   )r   c                 C   r]   )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.r^   r`   r   r   r   ra      rb   zLogoutCommand.runNrc   r   r   r   r   r      r\   r   c                   @   rX   )r"   c           
   
   C   s  t td t }|d u rt d td ztddgd}t t	|
  W n ty9   t d Y nw ztddgd}t t	|
  W n ty_   t td	 Y nw t d
 t|\}}| jjd urt| jjn|}| d| jj }t dt|  | jjstd }|d
ks|dks|dkst d t  zt|| jj| jjd}W n# ty }	 zt |	 t t|	jj td W Y d }	~	nd }	~	ww t d t dt|  t d t d|  t d
 d S )NzlWARNING! Managing repositories through transformers-cli is deprecated. Please use `huggingface-cli` instead.rd   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)r   organizationz
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_   r3   r=   r   rf   rg   
subprocesscheck_outputdecoder?   stripFileNotFoundErrorr   r   rt   r   r:   rr   inputlowerr   r   rh   ri   )
rZ   rj   stdoutrk   _	namespace	full_namechoiceurlrm   r   r   r   ra      s`   zRepoCreateCommand.runNrc   r   r   r   r   r"      r\   r"   )ru   argparser   typingr   r   huggingface_hub.hf_apir   r   r   requests.exceptionsr   ro   r
   r   r3   r'   intrW   rY   r   r   r   r"   r   r   r   r   <module>   s   *