o
    vZh+                     @   sp  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!e"Z#zddl$m%Z% ddl&m'Z' dZ(W n e)yv   dZ(Y nw G dd de
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/G dd de+Z0G d 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                   @   s   e Zd ZedefddZdS )UserCommandsparserc                 C   s  | j ddd}|jdtdd |jddd	d
 |jdd d | j ddd}|jdd d | j ddd}|jdtdd |jdd d | j ddd}|jdd}|j ddd}|jdtdd |jd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)td*d |
jd+td,td- |
jd.d/dd0d
 |
jd1d d d S )2Nr   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                 S      t | S N)LoginCommandargs r(   L/var/www/auris/lib/python3.10/site-packages/huggingface_hub/commands/user.py<lambda>V       z2UserCommands.register_subcommand.<locals>.<lambda>)funcwhoamiz;Find out which huggingface.co account you are logged in as.c                 S   r#   r$   )WhoamiCommandr&   r(   r(   r)   r*   X   r+   r   zLog outz--token-namez3Optional: Name of the access token to log out from.c                 S   r#   r$   )LogoutCommandr&   r(   r(   r)   r*   `   r+   authz%Other authentication related commandszAuthentication subcommandsswitchzSwitch between access tokensz0Optional: Name of the access token to switch to.c                 S   r#   r$   )AuthSwitchCommandr&   r(   r(   r)   r*   o   r+   listzList all stored access tokensc                 S   r#   r$   )AuthListCommandr&   r(   r(   r)   r*   q   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                 S   r#   r$   )RepoCreateCommandr&   r(   r(   r)   r*      r+   )
add_parseradd_argumentstrset_defaultsadd_subparsersr
   )r   Zlogin_parserZwhoami_parserZlogout_parserZauth_parserZauth_subparsersZauth_switch_parserZauth_list_parserZrepo_parserZrepo_subparsersZrepo_create_parserr(   r(   r)   register_subcommandI   s   z UserCommands.register_subcommandN)__name__
__module____qualname__staticmethodr   r?   r(   r(   r(   r)   r   H   s    r   c                   @      e Zd Zdd ZdS )BaseUserCommandc                 C   s   || _ t | _d S r$   )r'   r   _api)selfr'   r(   r(   r)   __init__   s   zBaseUserCommand.__init__N)r@   rA   rB   rH   r(   r(   r(   r)   rE          rE   c                   @   rD   )r%   c                 C   s    t   t| jj| jjd d S )N)tokenadd_to_git_credential)r   set_verbosity_infor   r'   rJ   rK   rG   r(   r(   r)   run   s
   
zLoginCommand.runNr@   rA   rB   rN   r(   r(   r(   r)   r%      rI   r%   c                   @   rD   )r/   c                 C   s   t   t| jjd d S )N)
token_name)r   rL   r   r'   rP   rM   r(   r(   r)   rN      s   zLogoutCommand.runNrO   r(   r(   r(   r)   r/      rI   r/   c                   @   s@   e Zd Zdd Zdee fddZdee dee fddZd	S )
r2   c                 C   sJ   t   | jj}|d u r|  }|d u rtd t  t|| jjd d S )Nz!No token name provided. Aborting.)rK   )	r   rL   r'   rP   _select_token_nameprintexitr   rK   )rG   rP   r(   r(   r)   rN      s   zAuthSwitchCommand.runreturnc                 C   s   t t  }|std d S tr| |S td t|dD ]\}}t| d|  q 	 z,t	d}|
 dkr=W d S t|d }d|  krOt|k rVn n|| W S td	 W n tyh   td
 Y nw q/)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.)r3   r   keysloggererror_inquirer_py_available_select_token_name_tuirR   	enumerateinputlowerintlen
ValueError)rG   token_namesirP   choiceindexr(   r(   r)   rQ      s,   


z$AuthSwitchCommand._select_token_namera   c                 C   sD   dd |D }zt jd|d d W S  ty!   td Y d S w )Nc                 S   s   g | ]}t ||d qS )r7   r   ).0rP   r(   r(   r)   
<listcomp>   s    z<AuthSwitchCommand._select_token_name_tui.<locals>.<listcomp>zSelect a token to switch to:)messager8   defaultzToken selection cancelled.)r   selectexecuteKeyboardInterruptrW   info)rG   ra   r8   r(   r(   r)   rZ      s   
z(AuthSwitchCommand._select_token_name_tuiN)	r@   rA   rB   rN   r   r<   rQ   r   rZ   r(   r(   r(   r)   r2      s    r2   c                   @   rD   )r4   c                 C   s   t   t  d S r$   )r   rL   r   rM   r(   r(   r)   rN      s   
zAuthListCommand.runNrO   r(   r(   r(   r)   r4      rI   r4   c                   @   rD   )r.   c              
   C   s   t  }|d u rtd t  z3| j|}t|d  dd |d D }|r1ttdd| tdkr?td	t  W d S W d S  t	ye } zt| tt
|jj td
 W Y d }~d S d }~ww )NNot logged inr7   c                 S   s   g | ]}|d  qS re   r(   )rf   orgr(   r(   r)   rg      s    z%WhoamiCommand.run.<locals>.<listcomp>orgszorgs: ,zhttps://huggingface.coz(Authenticated through private endpoint: r   )r   rR   rS   rF   r-   r   boldjoinr   r   redresponsetext)rG   rJ   rm   rp   er(   r(   r)   rN      s&   zWhoamiCommand.runNrO   r(   r(   r(   r)   r.      rI   r.   c                   @   rD   )r9   c           
   
   C   s$  t  }|d u rtd td ztddgd}tt|  W n t	y1   td Y nw ztddgd}tt|  W n t	yW   tt
d Y nw td	 | j|d
 }| jjd urn| jjn|}| d| jj }| jjtvrtd td | jjtv rt| jj | }n|}tdt|  | jjstd }|d	ks|dks|dkstd t  z| jj||| 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 )Nrn   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). r7   /zInvalid repo --typezYou are about to create zProceed? [Y/n] yyesZAbort)repo_idrJ   Z	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   rR   rS   
subprocesscheck_outputdecoder   graystripFileNotFoundErrorrt   rF   r-   r'   Zorganizationr7   r    r   r	   rr   r|   r\   r]   Zcreate_repor~   r   ru   rv   )
rG   rJ   stdoutuser	namespacer}   Zprefixed_repo_idrc   urlrw   r(   r(   r)   rN      sl   
zRepoCreateCommand.runNrO   r(   r(   r(   r)   r9      rI   r9   )2__doc__r   argparser   typingr   r   Zrequests.exceptionsr   Zhuggingface_hub.commandsr   Zhuggingface_hub.constantsr   r   r	   r
   Zhuggingface_hub.hf_apir   Z_loginr   r   r   r   r   r   r   r   utilsr   r   r   Z
_cli_utilsr   Z
get_loggerr@   rW   Z
InquirerPyr   ZInquirerPy.base.controlr   rY   ImportErrorr   rE   r%   r/   r2   r4   r.   r9   r(   r(   r(   r)   <module>   s6   (

H	3