U
    ”Ê¹jð8  ã                   @   sÊ   d Z ddlZddlZddlZddlmZmZ ddlmZ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 dd
lmZ ddlmZmZ ddlmZ ddlmZ e e¡ZG dd„ deƒZ dS )aa  Contains command to upload a repo or file with the CLI.

Usage:
    # Upload file (implicit)
    huggingface-cli upload my-cool-model ./my-cool-model.safetensors

    # Upload file (explicit)
    huggingface-cli upload my-cool-model ./my-cool-model.safetensors  model.safetensors

    # Upload directory (implicit). If `my-cool-model/` is a directory it will be uploaded, otherwise an exception is raised.
    huggingface-cli upload my-cool-model

    # Upload directory (explicit)
    huggingface-cli upload my-cool-model ./models/my-cool-model .

    # Upload filtered directory (example: tensorboard logs except for the last run)
    huggingface-cli upload my-cool-model ./model/training /logs --include "*.tfevents.*" --exclude "*20230905*"

    # Upload with wildcard
    huggingface-cli upload my-cool-model "./model/training/*.safetensors"

    # Upload private dataset
    huggingface-cli upload Wauplin/my-cool-dataset ./data . --repo-type=dataset --private

    # Upload with token
    huggingface-cli upload Wauplin/my-cool-model --token=hf_****

    # Sync local Space with Hub (upload new files, delete removed files)
    huggingface-cli upload Wauplin/space-example --repo-type=space --exclude="/logs/*" --delete="*" --commit-message="Sync local Space with Hub"

    # Schedule commits every 30 minutes
    huggingface-cli upload Wauplin/my-cool-model --every=30
é    N)Ú	NamespaceÚ_SubParsersAction)ÚListÚOptional)Úlogging)ÚCommitScheduler)ÚBaseHuggingfaceCLICommand)ÚHF_HUB_ENABLE_HF_TRANSFER)ÚRevisionNotFoundError)ÚHfApi)Údisable_progress_barsÚenable_progress_bars)Úis_xet_availableé   )Úshow_deprecation_warningc                   @   sJ   e Zd Zeedœdd„ƒZeddœdd„Zddœd	d
„Ze	dœdd„Z
dS )ÚUploadCommand)Úparserc                 C   s  | j ddd�}|jdtdd� |jddd	d
� |jdddd
� |jddddgddd� |jdtdd� |jdddd� |jddtdd� |jddtdd� |jddtd d� |jd!td"d� |jd#td$d� |jd%dd&d� |jd'td(d� |jd)td*d� |jd+dd,d� |jtd-� d S ).NÚuploadz.Upload a file or a folder to a repo on the Hub)ÚhelpÚrepo_idz<The ID of the repo to upload to (e.g. `username/repo-name`).)Útyper   Ú
local_pathú?zkLocal path to the file or folder to upload. Wildcard patterns are supported. Defaults to current directory.)Únargsr   Úpath_in_repoz\Path of the file or folder in the repo. Defaults to the relative path of the file or folder.z--repo-typeÚmodelZdatasetÚspacez/Type of the repo to upload to (e.g. `dataset`).)ÚchoicesÚdefaultr   z
--revisionz°An optional Git revision to push to. It can be a branch name or a PR reference. If revision does not exist and `--create-pr` is not set, a branch will be automatically created.z	--privateÚ
store_truezfWhether to create a private repo if repo doesn't exist on the Hub. Ignored if the repo already exists.)Úactionr   z	--includeÚ*z'Glob patterns to match files to upload.)r   r   r   z	--excludez.Glob patterns to exclude from files to upload.z--deletezDGlob patterns for file to be deleted from the repo while committing.z--commit-messagez9The summary / title / first line of the generated commit.z--commit-descriptionz(The description of the generated commit.z--create-prz0Whether to upload content as a new Pull Request.z--everyzNIf set, a background job is scheduled to create commits every `every` minutes.z--tokenzIA User Access Token generated from https://huggingface.co/settings/tokensz--quietzWIf True, progress bars are disabled and only the path to the uploaded files is printed.)Úfunc)Ú
add_parserÚadd_argumentÚstrÚfloatÚset_defaultsr   )r   Zupload_parser© r(   ú^/var/www/html/bwave/python/venv/lib/python3.8/site-packages/huggingface_hub/commands/upload.pyÚregister_subcommandG   s�      ÿýýüüü   ÿü  ÿ  ÿý  ÿýz!UploadCommand.register_subcommandN)ÚargsÚreturnc                    sî  ˆ j | _ ˆ j| _ˆ j| _ˆ j| _ˆ j| _ˆ j| _ˆ j| _ˆ j| _ˆ j| _ˆ j	| _	t
ˆ jdd�| _ˆ j| _ˆ jd k	rŽˆ jdkrŽtdˆ j› d�ƒ‚ˆ j| _ˆ j  d¡d }|  |  ˆ jd k	�rt‡ fdd	„d
D ƒƒ�rˆ jd k	rätdƒ‚ˆ jd k	�rˆ jdk�rtdƒ‚d| _ˆ j| _d| _nÐˆ jd k�rBtj |¡�rB|| _|| _n¨ˆ jd k�rjtj |¡�rj|| _d| _n€ˆ jd k�rˆtd|› d�ƒ‚nbˆ jd k�r¾tj ˆ j¡�r¾ˆ j| _tj ˆ j¡| _n,ˆ jd k�rÚˆ j| _d| _nˆ j| _ˆ j| _d S )Nzhuggingface-cli)ÚtokenZlibrary_namer   z'`every` must be a positive value (got 'z')ú/éÿÿÿÿc                 3   s   | ]}|ˆ j kV  qd S )N)r   )Ú.0Úc©r+   r(   r)   Ú	<genexpr>§   s     z)UploadCommand.__init__.<locals>.<genexpr>)r!   r   ú[zICannot set `--include` when passing a `local_path` containing a wildcard.Ú.zLCannot set `path_in_repo` when passing a `local_path` containing a wildcard.ú'zD' is not a local file or folder. Please set `local_path` explicitly.)r   Ú	repo_typeÚrevisionÚprivateÚincludeÚexcludeÚdeleteÚcommit_messageÚcommit_descriptionÚ	create_prr   r-   ÚapiÚquietÚeveryÚ
ValueErrorÚsplitr   Úanyr   ÚosÚpathÚisfileÚisdirÚbasename)Úselfr+   Ú	repo_namer(   r2   r)   Ú__init__�   sT    $
zUploadCommand.__init__)r,   c              	   C   sh   t ddƒ | jrHtƒ  t ¡ � t d¡ t|  ¡ ƒ W 5 Q R X tƒ  nt	 
¡  t|  ¡ ƒ t	 ¡  d S )Nzhuggingface-cli uploadz	hf uploadÚignore)r   rA   r   ÚwarningsÚcatch_warningsÚsimplefilterÚprintÚ_uploadr   r   Zset_verbosity_infoZset_verbosity_warning)rK   r(   r(   r)   ÚrunÈ   s    


zUploadCommand.runc                 C   sì  t j | j¡rt| jd k	r0t| jƒdkr0t d¡ | jd k	rRt| jƒdkrRt d¡ | j	d k	rtt| j	ƒdkrtt d¡ t
ƒ sˆtsˆt d¡ | jd k	�r¦t j | j¡rèt j | j¡}| j | j¡rÔ| jd t| jƒ … n| j}| jg}g }nH| j}| j}| jpüg }| j�pg }| j	d k	�r0t| j	ƒdk�r0t d¡ t|| j| j| j|||| j| j| jd�
}td| j› d	|j› d
�ƒ zt d¡ �qtW n  tk
�r¤   | ¡  Y dS X t j | j¡�sØt j | j¡�sØtd| j› d�ƒ‚| jj| j| jd| j| jdk�rüdnd d�j}| jd k	�rx| j �sxz| jj!|| j| jd� W nB t"k
�rv   t d| j› d�¡ | jj#|| j| jdd� Y nX t j | j¡�r²| jj$| j| j|| j| j| j%| j&| j d�S | jj'| j| j|| j| j| j%| j&| j | j| j| j	d�S d S )Nr   z5Ignoring `--include` since a single file is uploaded.z5Ignoring `--exclude` since a single file is uploaded.z4Ignoring `--delete` since a single file is uploaded.z©Consider using `hf_transfer` for faster uploads. This solution comes with some limitations. See https://huggingface.co/docs/huggingface_hub/hf_transfer for more details.z:Ignoring `--delete` when uploading with scheduled commits.)
Úfolder_pathr   r7   r8   Úallow_patternsÚignore_patternsr   r9   rB   Zhf_apizScheduling commits every z minutes to r5   éd   zStopped scheduled commits.zNo such file or directory: 'z'.Tr   Zgradio)r   r7   Úexist_okr9   Z	space_sdk)r   r7   r8   zBranch 'z' not found. Creating it...)r   r7   ÚbranchrY   )Zpath_or_fileobjr   r   r7   r8   r=   r>   r?   )rU   r   r   r7   r8   r=   r>   r?   rV   rW   Zdelete_patterns)(rF   rG   rH   r   r:   ÚlenrO   Úwarnr;   r<   r   r	   ÚloggerÚinforB   Údirnamer   Úendswithr   r   r7   r8   r9   r@   rR   ÚtimeÚsleepÚKeyboardInterruptÚstoprI   ÚFileNotFoundErrorZcreate_repor?   Z	repo_infor
   Zcreate_branchZupload_filer=   r>   Zupload_folder)rK   rU   r   rV   rW   Z	schedulerr   r(   r(   r)   rS   Ö   s¦    



ÿÿý

ö ûøõzUploadCommand._upload)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr   r*   r   rM   rT   r%   rS   r(   r(   r(   r)   r   F   s
   E;r   )!Ú__doc__rF   ra   rO   Úargparser   r   Útypingr   r   Zhuggingface_hubr   Z!huggingface_hub._commit_schedulerr   Zhuggingface_hub.commandsr   Zhuggingface_hub.constantsr	   Zhuggingface_hub.errorsr
   Zhuggingface_hub.hf_apir   Zhuggingface_hub.utilsr   r   Zhuggingface_hub.utils._runtimer   Z
_cli_utilsr   Z
get_loggerrf   r]   r   r(   r(   r(   r)   Ú<module>   s    "
