U
    9%e+                     @   s   d dl Z d dl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 dd	lmZ d
dlmZ 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dS )    N)_SubParsersAction)	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loginlogoutnotebook_login)HfFolder   )ANSIc                   @   s   e Zd Zeed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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d$d
 |jd%d d d S )&Nr   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   s   t | S N)LoginCommandargs r   \/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/huggingface_hub/commands/user.py<lambda>6       z2UserCommands.register_subcommand.<locals>.<lambda>)funcwhoamiz;Find out which huggingface.co account you are logged in as.c                 S   s   t | S r   )WhoamiCommandr   r   r   r   r    8   r!   r   zLog outc                 S   s   t | S r   )LogoutCommandr   r   r   r   r    :   r!   repozG{create, ls-files} 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   s   t | S r   )RepoCreateCommandr   r   r   r   r    Z   r!   )
add_parseradd_argumentstrset_defaultsadd_subparsersr   )r   Zlogin_parserZwhoami_parserZlogout_parserZrepo_parserZrepo_subparsersZrepo_create_parserr   r   r   register_subcommand)   s\    z UserCommands.register_subcommandN)__name__
__module____qualname__staticmethodr   r0   r   r   r   r   r   (   s   r   c                   @   s   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)r1   r2   r3   r8   r   r   r   r   r5   ]   s   r5   c                   @   s   e Zd Zdd ZdS )r   c                 C   s   t | jj| jjd d S )N)tokenadd_to_git_credential)r   r   r9   r:   r7   r   r   r   rund   s    zLoginCommand.runNr1   r2   r3   r<   r   r   r   r   r   c   s   r   c                   @   s   e Zd Zdd ZdS )r%   c                 C   s
   t   d S r   )r   r;   r   r   r   r<   i   s    zLogoutCommand.runNr=   r   r   r   r   r%   h   s   r%   c                   @   s   e Zd Zdd ZdS )r$   c              
   C   s   t  }|d krtd t  z^| j|}t|d  dd |d D }|rdttdd| t	dkrztd	t	  W nD t
k
r } z&t| tt|jj td
 W 5 d }~X Y nX d S )NNot logged inr(   c                 S   s   g | ]}|d  qS )r(   r   ).0orgr   r   r   
<listcomp>v   s     z%WhoamiCommand.run.<locals>.<listcomp>orgszorgs: ,zhttps://huggingface.coz(Authenticated through private endpoint: r   )r   	get_tokenprintexitr6   r#   r   boldjoinr   r   redresponsetext)r7   r9   inforB   er   r   r   r<   n   s     zWhoamiCommand.runNr=   r   r   r   r   r$   m   s   r$   c                   @   s   e Zd Zdd ZdS )r*   c           
   
   C   s6  t  }|d kr td td z*tddgd}tt|	  W n t
k
rf   td Y nX z*tddgd}tt|	  W n" t
k
r   ttd Y nX td	 | j|d
 }| jjd k	r| jjn|}| d| jj }| jjtkrtd td | jjtkr6t| 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 nF tk
r }	 z&t|	 tt|	jj td W 5 d }	~	X Y nX td tdt|  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yesZAbort)repo_idr9   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   rD   rE   rF   
subprocesscheck_outputdecoder   graystripFileNotFoundErrorrI   r6   r#   r   Zorganizationr(   r   r   r   rG   rR   inputlowerZcreate_reporT   r   rJ   rK   )
r7   r9   stdoutuser	namespacerS   Zprefixed_repo_idchoiceurlrM   r   r   r   r<      sd    


zRepoCreateCommand.runNr=   r   r   r   r   r*      s   r*   )rU   argparser   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   utilsr   Z
_cli_utilsr   r   r5   r   r%   r$   r*   r   r   r   r   <module>   s    5