o
    hz)                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlm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 eeZG dd deZG d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dS )a  Contains commands to interact with repositories on the Hugging Face Hub.

Usage:
    # create a new dataset repo on the Hub
    hf repo create my-cool-dataset --repo-type=dataset

    # create a private model repo on the Hub
    hf repo create my-cool-model --private
    N)_SubParsersAction)Optional)	HTTPError)BaseHuggingfaceCLICommand)ANSI)
REPO_TYPESSPACES_SDK_TYPES)HfHubHTTPErrorRepositoryNotFoundErrorRevisionNotFoundError)HfApi)loggingc                   @   s   e Zd ZedefddZdS )RepoCommandsparserc                    s0  | j ddd  jdd} j fddd |j dd	d}|jd
tdd |jdtdd |jdtdtd |jdddd |jdtdd |jdddd |jdtdd |jdd d |j ddd}|jdd d!d"}|j dd#d}|jd
td$d |jdtd%d |jd&d'td(d |jd)td*d |jdtd+d |jdg d,d-d.d/ |jd0d d |j d1d2d}|jd
td3d |jdtd+d |jdg d,d-d.d/ |jd4d d |j d5d6d}|jd
td7d |jdtd8d |jd9d:dd;d |jdtd+d |jdg d,d-d.d/ |jd<d d d S )=NrepozManage repos on the Hub.)helpz%huggingface.co repos related commandsc                    s      S N)
print_helpargsrepo_parser N/var/www/html/ai/venv/lib/python3.10/site-packages/huggingface_hub/cli/repo.py<lambda>0       z2RepoCommands.register_subcommand.<locals>.<lambda>)funccreatez#Create a new repo on huggingface.corepo_idzThe ID of the repo to create to (e.g. `username/repo-name`). The username is optional and will be set to your username if not provided.)typer   z--repo-typezWOptional: set to "dataset" or "space" if creating a dataset or space, default is model.z--space_sdkzOOptional: Hugging Face Spaces SDK type. Required when --type is set to "space".)r   r   choicesz	--private
store_truezhWhether to create a private repository. Defaults to public unless the organization's default is private.)actionr   z--tokenzLHugging Face token. Will default to the locally saved token if not provided.z
--exist-okz-Do not raise an error if repo already exists.z--resource-group-idzoResource group in which to create the repo. Resource groups is only available for Enterprise Hub organizations.c                 S      t | S r   )RepoCreateCommandr   r   r   r   r   X   r   tagz"Manage tags for a repo on the Hub.zTag actions
tag_actionT)r   destrequiredzCreate a tag for a repo.z6The ID of the repo to tag (e.g. `username/repo-name`).zThe name of the tag to create.z-mz	--messagez%The description of the tag to create.z
--revisionzThe git revision to tag.zJA User Access Token generated from https://huggingface.co/settings/tokens.)modeldatasetspacer)   z6Set the type of repository (model, dataset, or space).)r    defaultr   c                 S   r#   r   )RepoTagCreateCommandr   r   r   r   r   o   r   listzList tags for a repo.z@The ID of the repo to list tags for (e.g. `username/repo-name`).c                 S   r#   r   )RepoTagListCommandr   r   r   r   r      r   deletezDelete a tag from a repo.zFThe ID of the repo to delete the tag from (e.g. `username/repo-name`).zThe name of the tag to delete.z-yz--yesz$Answer Yes to prompts automatically.c                 S   r#   r   )RepoTagDeleteCommandr   r   r   r   r      r   )
add_parseradd_subparsersset_defaultsadd_argumentstrr   )r   repo_subparsersrepo_create_parserrepo_tag_parsertag_subparserstag_create_parsertag_list_parsertag_delete_parserr   r   r   register_subcommand*   s   z RepoCommands.register_subcommandN)__name__
__module____qualname__staticmethodr   r>   r   r   r   r   r   )   s    r   c                   @   s$   e Zd ZdejfddZdd ZdS )r$   r   c                 C   sD   |j | _ |j| _|j| _|j| _|j| _|j| _|j| _t | _d S r   )	r   	repo_type	space_sdkprivatetokenexist_okresource_group_idr   _apiselfr   r   r   r   __init__   s   zRepoCreateCommand.__init__c              	   C   sX   | j j| j| j| j| j| j| j| jd}t	dt
|j d t	dt
|  d S )N)r   rC   rE   rF   rG   rH   rD   zSuccessfully created z on the Hub.zYour repo is now available at )rI   create_repor   rC   rE   rF   rG   rH   rD   printr   bold)rK   repo_urlr   r   r   run   s   	zRepoCreateCommand.runN)r?   r@   rA   argparse	NamespacerL   rQ   r   r   r   r   r$      s    
r$   c                   @      e Zd Zdd ZdS )RepoTagCommandc                 C   sR   || _ tt|dd d| _|j| _t|dd| _| jtvr'td td d S d S )NrF   )rF   rC   r)   zInvalid repo --repo-type   )	r   r   getattrapir   rC   r   rN   exitrJ   r   r   r   rL      s   
zRepoTagCommand.__init__N)r?   r@   rA   rL   r   r   r   r   rU          rU   c                   @   rT   )r-   c                 C   sh  t dtt| jj d| j dt| j  z| jj	| j| jjt
| jdd t
| jdd | jd W nh tyR   t | j  dt| j d td Y nK typ   t d	ttt
| jdd  d td Y n- ty } z!|jjd
krt dtt| jj dt| j  td |d }~ww t dtt| jj dt| j  d S )NzYou are about to create tag  on  messagerevision)r   r%   tag_messager^   rC    not found.rV   z	Revision i  Tag z already exists on z created on )rN   r   rO   r6   r   r%   rC   r   rX   
create_tagrW   r
   
capitalizerY   r   r	   responsestatus_code)rK   er   r   r   rQ      s2   ,
"$*.zRepoTagCreateCommand.runNr?   r@   rA   rQ   r   r   r   r   r-      rZ   r-   c                   @   rT   )r/   c              
   C   s   z| j j| j| jd}W n@ ty*   t| j  dt| j d t	d Y n# t
yL } zt| tt|jj t	d W Y d }~nd }~ww t|jdkr\td t	d td| j dt| j d |jD ]}t|j qod S )	N)r   rC   r\   r`   rV   r   zNo tags foundz	Tags for :)rX   list_repo_refsr   rC   r
   rN   rc   r   rO   rY   r   redrd   textlentagsname)rK   refsrf   r%   r   r   r   rQ      s*   
" 
zRepoTagListCommand.runNrg   r   r   r   r   r/      rZ   r/   c                   @   rT   )r1   c              
   C   s"  t dt| jj d| j dt| j  t| jdds/td	 }|dvr/t d t
  z| jj| j| jj| jd	 W n= ty\   t | j  dt| j d
 t
d Y n  ty{   t dt| jj dt| j  t
d Y nw t dt| jj dt| j  d S )NzYou are about to delete tag r[   r\   yesFzProceed? [Y/n] ) yrp   Abort)r   r%   rC   r`   rV   ra   z not found on z deleted on )rN   r   rO   r   r%   rC   r   rW   inputlowerrY   rX   
delete_tagr
   rc   r   )rK   choicer   r   r   rQ      s    ."&*zRepoTagDeleteCommand.runNrg   r   r   r   r   r1      rZ   r1   )__doc__rR   r   typingr   requests.exceptionsr   huggingface_hub.commandsr   #huggingface_hub.commands._cli_utilsr   huggingface_hub.constantsr   r   huggingface_hub.errorsr	   r
   r   huggingface_hub.hf_apir   huggingface_hub.utilsr   
get_loggerr?   loggerr   r$   rU   r-   r/   r1   r   r   r   r   <module>   s$   

k