a
    h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 eeZG dd deZdS )a  Contains command to upload a repo or file with the CLI.

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

    # Upload file (explicit)
    hf 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.
    hf upload my-cool-model

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

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

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

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

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

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

    # Schedule commits every 30 minutes
    hf 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_availablec                   @   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g d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uploadzLUpload a file or a folder to the Hub. Recommended for single-commit uploads.)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spacer   z/Type of the repo to upload to (e.g. `dataset`).)choicesdefaultr   z
--revisionzAn 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&   V/var/www/html/assistant/venv/lib/python3.9/site-packages/huggingface_hub/cli/upload.pyregister_subcommandE   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 ur jdkrtd j d j| _ j dd }|  |   jd urt fdd	d
D r jd urtd jd ur jdkrtdd| _ j| _d| _nЈ jd u rBtj|rB|| _|| _n jd u rjtj|rj|| _d| _n jd u rtd| dnb jd u rtj jr j| _tj j| _n, jd u rڈ j| _d| _n j| _ j| _d S )NZhf)tokenZlibrary_namer   z'`every` must be a positive value (got 'z')/c                 3   s   | ]}| j v V  qd S )N)r   ).0cr)   r&   r'   	<genexpr>       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&   r0   r'   __init__   sT    $
zUploadCommand.__init__)r*   c                 C   sr   | j rRt  t & td t|   W d    n1 s@0    Y  t  nt	  t|   t
  d S )Nignore)r@   r   warningscatch_warningssimplefilterprint_uploadr   r   Zset_verbosity_infoZset_verbosity_warning)rJ   r&   r&   r'   run   s    

*zUploadCommand.runc                 C   s  t j| jrt| jd ur0t| jdkr0td | jd urRt| jdkrRtd | j	d urtt| j	dkrttd t
 ststd | jd urt j| jrt j| j}| j| jr| jd t| j  n| j}| jg}g }nH| j}| j}| jpg }| jpg }| j	d u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y   |  Y dS 0 t j| jst j| jstd| j d| jj| j| jd| j| jdkrdnd dj}| jd urt| j stz| jj!|| j| jd W n@ t"yr   td| j d | jj#|| j| jdd Y n0 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.zConsider 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   r6   r7   allow_patternsignore_patternsr   r8   rA   Zhf_apizScheduling commits every z minutes to r4   d   zStopped scheduled commits.zNo such file or directory: 'z'.Tr   Zgradio)r   r6   exist_okr8   Z	space_sdk)r   r6   r7   zBranch 'z' not found. Creating it...)r   r6   branchrX   )Zpath_or_fileobjr   r   r6   r7   r<   r=   r>   )rT   r   r   r6   r7   r<   r=   r>   rU   rV   Zdelete_patterns)(rE   rF   rG   r   r9   lenrN   warnr:   r;   r   r	   loggerinforA   dirnamer   endswithr   r   r6   r7   r8   r?   rQ   timesleepKeyboardInterruptstoprH   FileNotFoundErrorZcreate_repor>   Z	repo_infor
   Zcreate_branchZupload_filer<   r=   Zupload_folder)rJ   rT   r   rU   rV   Z	schedulerr   r&   r&   r'   rR      s    





 zUploadCommand._upload)__name__
__module____qualname__staticmethodr   r(   r   rL   rS   r#   rR   r&   r&   r&   r'   r   D   s
   G;r   )__doc__rE   r`   rN   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
get_loggerre   r\   r   r&   r&   r&   r'   <module>   s   "
