o
    h                  $   @   s  d Z ddlZddlZddlZddlZddlZddl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 ddlZddlZddlmZmZmZmZmZmZmZmZmZmZmZmZ dd	l m!Z!m"Z" dd
l#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< e3=e>Z?ejj@ZAdd ZBeCdejDEeCdddZFejGZGeCdejHZIeCdeIZJeCdeJZKeCdejDEejLdZMdZNe jOZPdD ]ZQeCeQdureRdeQ deS qdZTd ZUejVWd!d"X e7v ZYeYrd#nd$ZZeZZ[ejVWd%ddur-eRd&eS ejVWd%dZ[ejVWd'e[Z[e[d( Z\e[d) Z]	djd*e^d+e^d,ee^e
df d-ee^ fd.d/Z_d0d1 Z`d2d3 Zadkd4eebe^df d5e^fd6d7Zcd8ee^ d9ee^ d5ee^ fd:d;Zdd*ee^ejef d<e^d5ee^ fd=d>Zf		?					?	@			A	A	A	dld*ee^ejef dBege^ d,eee^ejef  dCehdDeeh dEeebe^e^f  dFeeehe^f  d-ee^ dGehdHe^dIee^ d4eee^ebe^e^f f  dJehdKehdLehdMee^ d5ee^ f"dNdOZidPdQ ZjdkdRdSZk			dmd?dddTdUee^ejef d<e^d-ee^ dEeebe^e^f  dFeeehe^f  dGehd,ee^e
df dIee^ fdVdWZlG dXdY dYZmdZd[d\d]Znd^eeoe^f fd_d`Zp		?			?				@	dndadbZq			?dodce^ddeege^  dFee^ deehfdfdgZrG dhdi diZsdS )pz?
Hub utilities: utilities related to download and cache models
    N)futures)Path)OptionalUnion)urlparse)uuid4)_CACHED_NO_EXISTCommitOperationAdd	ModelCardModelCardData	constantscreate_branchcreate_commitcreate_repohf_hub_download
hf_hub_urlsnapshot_downloadtry_to_load_from_cache)REGEX_COMMIT_HASHhttp_get)EntryNotFoundErrorGatedRepoErrorHfHubHTTPErrorLocalEntryNotFoundErrorOfflineModeIsEnabledRepositoryNotFoundErrorRevisionNotFoundErrorbuild_hf_headersget_sessionhf_raise_for_statussend_telemetry)	HTTPError   )__version__logging)working_or_temp_dir)ENV_VARS_TRUE_VALUES_tf_version_torch_versionis_tf_availableis_torch_availableis_training_run_on_sagemakerc                   C   s   t S N)_is_offline_mode r.   r.   j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/utils/hub.pyis_offline_modeO   s   r0   
TORCH_HOMEXDG_CACHE_HOMEz~/.cachetorchPYTORCH_PRETRAINED_BERT_CACHEPYTORCH_TRANSFORMERS_CACHETRANSFORMERS_CACHEHF_MODULES_CACHEmodulestransformers_modules)r4   r5   r6   zUsing `zQ` is deprecated and will be removed in v5 of Transformers. Use `HF_HOME` instead.z3https://s3.amazonaws.com/models.huggingface.co/bertzhttps://cdn.huggingface.coHUGGINGFACE_CO_STAGINGNOzhttps://hub-ci.huggingface.cozhttps://huggingface.coHUGGINGFACE_CO_RESOLVE_ENDPOINTzUsing the environment variable `HUGGINGFACE_CO_RESOLVE_ENDPOINT` is deprecated and will be removed in Transformers v5. Use `HF_ENDPOINT` instead.HF_ENDPOINTz)/{model_id}/resolve/{revision}/{filename}z/api/telemetry/examplespath_or_repo_idfull_filename	cache_dirrevisionc                 C   s(   t | |||d}|d ur|tkr|S d S )N)r@   rA   )r   r   )r>   r?   r@   rA   resolved_filer.   r.   r/   _get_cache_file_to_return   s   rC   c                 C   s   t | }|jdv S )N)httphttps)r   scheme)url_or_filenameparsedr.   r.   r/   is_remote_url   s   
rI   c               	   C   s   zt tjd  } | d }| d dd }W n ty&   d }d }Y nw ttdd}d|v r6dnd	}d
tjv rGtd
dd nd }tdd tdd tddtdd||||d}|S )NECS_CONTAINER_METADATA_URIImage:r"   SM_FRAMEWORK_PARAMSz{}*sagemaker_distributed_dataparallel_enabledTFTRAINING_JOB_ARN   SM_FRAMEWORK_MODULE
AWS_REGIONSM_NUM_GPUSr   SM_NUM_CPUS)sm_framework	sm_regionsm_number_gpusm_number_cpusm_distributed_trainingsm_deep_learning_containersm_deep_learning_container_tagsm_account_id)	requestsgetosenvironjsonsplit	Exceptionloadsgetenv)instance_datadlc_container_useddlc_tagsagemaker_paramsruns_distributed_training
account_idsagemaker_objectr.   r.   r/   define_sagemaker_information   s*   "




rm   
user_agentreturnc                 C   s   dt  dtj d  dt }t r|dt 7 }t r$|dt 7 }t	j
r+|d S t r?|ddd	d
 t  D  7 }tjdd tv rN|d7 }t| tre|dddd
 |  D  7 }|S t| trp|d|  7 }|S )zF
    Formats a user-agent string with basic info about a request.
    ztransformers/z	; python/r   z; session_id/z; torch/z; tensorflow/z; telemetry/offz; c                 s   "    | ]\}}| d | V  qdS /Nr.   .0kvr.   r.   r/   	<genexpr>        z"http_user_agent.<locals>.<genexpr>TRANSFORMERS_IS_CI z; is_ci/truec                 s   rp   rq   r.   rs   r.   r.   r/   rw      rx   )r#   sysversionrb   
SESSION_IDr*   r(   r)   r'   r   HF_HUB_DISABLE_TELEMETRYr+   joinrm   itemsr_   r`   r^   upperr&   
isinstancedictstr)rn   uar.   r.   r/   http_user_agent   s"    "
 
r   rB   commit_hashc                 C   sZ   | du s|dur
|S t t|  } td| }|du rdS | d }t|r+|S dS )zP
    Extracts the commit hash from a resolved filename toward a cache file.
    Nzsnapshots/([^/]+)/r   )r   r   as_posixresearchgroupsr   match)rB   r   r   r.   r.   r/   extract_commit_hash   s   r   filenamec                 K   s2   t d| |gd|}|dur|d }|S |}|S )a
  
    Tries to locate a file in a local folder and repo, downloads and cache it if necessary.

    Args:
        path_or_repo_id (`str` or `os.PathLike`):
            This can be either:
            - a string, the *model id* of a model repo on huggingface.co.
            - a path to a *directory* potentially containing the file.
        filename (`str`):
            The name of the file to locate in `path_or_repo`.
        cache_dir (`str` or `os.PathLike`, *optional*):
            Path to a directory in which a downloaded pretrained model configuration should be cached if the standard
            cache should not be used.
        force_download (`bool`, *optional*, defaults to `False`):
            Whether or not to force to (re-)download the configuration files and override the cached versions if they
            exist.
        resume_download:
            Deprecated and ignored. All downloads are now resumed by default when possible.
            Will be removed in v5 of Transformers.
        proxies (`Dict[str, str]`, *optional*):
            A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',
            'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request.
        token (`str` or *bool*, *optional*):
            The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
            when running `huggingface-cli login` (stored in `~/.huggingface`).
        revision (`str`, *optional*, defaults to `"main"`):
            The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
            git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any
            identifier allowed by git.
        local_files_only (`bool`, *optional*, defaults to `False`):
            If `True`, will only try to load the tokenizer configuration from local files.
        subfolder (`str`, *optional*, defaults to `""`):
            In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can
            specify the folder name here.
        repo_type (`str`, *optional*):
            Specify the repo type (useful when downloading from a space for instance).

    <Tip>

    Passing `token=True` is required when you want to use a private model.

    </Tip>

    Returns:
        `Optional[str]`: Returns the resolved file (to the cache folder if downloaded from a repo).

    Examples:

    ```python
    # Download a model weight from the Hub and cache it.
    model_weights_file = cached_file("google-bert/bert-base-uncased", "pytorch_model.bin")
    ```
    )r>   	filenamesNr   r.   )cached_files)r>   r   kwargsfiler.   r.   r/   cached_file   s
   :r   Frz   Tr   force_downloadresume_downloadproxiestokenlocal_files_only	subfolder	repo_type _raise_exceptions_for_gated_repo%_raise_exceptions_for_missing_entries'_raise_exceptions_for_connection_errors_commit_hashc                    s  | dd}|durtdt |durtd|}t r&|s&td d}du r,dfdd	|D }tg }|D ]@}t	j
r}t	j
|}t	j
|sx|ru|t	j
d
krudu rcdn}t d| d d| d dS || q=t|t|kr|S  du rt t trt  g }d}|dur|s|D ]/}t| ||
d}|dur|tur|d7 }|| q|s|d7 }qtd| d dq|t|krt|dkr|S dS t|}z3t|dkr
t|d tdkrdn|
 ||||||d nt||
 ||||||d W n ty } zt|tr7t|ts7t d|t|trHt d d| fdd	|D }tdd |D re|W  Y d}~S t|tr|suW Y d}~dS td dt| |t|tr|sW Y d}~dS |rtd t d!|n t|t rt|t!s|sW Y d}~dS td" d#| W Y d}~nd}~ww  fd$d	|D }t"d%d |D r0|r0d&d	 t#||D }t|dkr|d t	j
d
krdS du rdn}t|dkrd'|d  nd(g |R  }t$ d)| d d| d*d+d	 |D }t|dkrBd}|S |}|S ),ah  
    Tries to locate several files in a local folder and repo, downloads and cache them if necessary.

    Args:
        path_or_repo_id (`str` or `os.PathLike`):
            This can be either:
            - a string, the *model id* of a model repo on huggingface.co.
            - a path to a *directory* potentially containing the file.
        filenames (`List[str]`):
            The name of all the files to locate in `path_or_repo`.
        cache_dir (`str` or `os.PathLike`, *optional*):
            Path to a directory in which a downloaded pretrained model configuration should be cached if the standard
            cache should not be used.
        force_download (`bool`, *optional*, defaults to `False`):
            Whether or not to force to (re-)download the configuration files and override the cached versions if they
            exist.
        resume_download:
            Deprecated and ignored. All downloads are now resumed by default when possible.
            Will be removed in v5 of Transformers.
        proxies (`Dict[str, str]`, *optional*):
            A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',
            'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request.
        token (`str` or *bool*, *optional*):
            The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
            when running `huggingface-cli login` (stored in `~/.huggingface`).
        revision (`str`, *optional*, defaults to `"main"`):
            The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
            git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any
            identifier allowed by git.
        local_files_only (`bool`, *optional*, defaults to `False`):
            If `True`, will only try to load the tokenizer configuration from local files.
        subfolder (`str`, *optional*, defaults to `""`):
            In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can
            specify the folder name here.
        repo_type (`str`, *optional*):
            Specify the repo type (useful when downloading from a space for instance).

    Private args:
        _raise_exceptions_for_gated_repo (`bool`):
            if False, do not raise an exception for gated repo error but return None.
        _raise_exceptions_for_missing_entries (`bool`):
            if False, do not raise an exception for missing entries but return None.
        _raise_exceptions_for_connection_errors (`bool`):
            if False, do not raise an exception for connection errors but return None.
        _commit_hash (`str`, *optional*):
            passed when we are chaining several calls to various files (e.g. when loading a tokenizer or
            a pipeline). If files are cached for this commit hash, avoid calls to head and get from the cache.

    <Tip>

    Passing `token=True` is required when you want to use a private model.

    </Tip>

    Returns:
        `Optional[str]`: Returns the resolved file (to the cache folder if downloaded from a repo).

    Examples:

    ```python
    # Download a model weight from the Hub and cache it.
    model_weights_file = cached_file("google-bert/bert-base-uncased", "pytorch_model.bin")
    ```
    use_auth_tokenNrThe `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.V`token` and `use_auth_token` are both specified. Please set only the argument `token`.z+Offline mode: forcing local_files_only=TrueTrz   c                    s   g | ]	}t j |qS r.   r_   pathr   rt   r   )r   r.   r/   
<listcomp>s  s    z cached_files.<locals>.<listcomp>zconfig.jsonmainz& does not appear to have a file named z#. Checkout 'https://huggingface.co/z/tree/z' for available files.r   )r@   rA   r   r"   zCould not locate z inside .)
r   r   rA   r@   rn   r   r   r   r   r   )
allow_patternsr   rA   r@   rn   r   r   r   r   r   a   is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models'
If this is a private repository, make sure to pass a token having permission to this repo either by logging in with `huggingface-cli login` or by passing `token=<your_token>` is not a valid git identifier (branch name, tag name or commit id) that exists for this model name. Check the model page at 'https://huggingface.co/' for available revisions.c                       g | ]	}t | qS r.   rC   rt   r   r@   r>   rA   r.   r/   r         c                 s   s    | ]}|d uV  qd S r,   r.   r   r.   r.   r/   rw         zcached_files.<locals>.<genexpr>z`You are trying to access a gated repo.
Make sure to have access to it at https://huggingface.co/z.
zWe couldn't connect to 'z' to load the files, and couldn't find them in the cached files.
Checkout your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/transformers/installation#offline-mode'.z:There was a specific connection error when trying to load z:
c                    r   r.   r   r   r   r.   r/   r     r   c                 s   s    | ]}|d u V  qd S r,   r.   r   r.   r.   r/   rw     r   c                 S   s   g | ]
\}}|d u r|qS r,   r.   )rt   originalresolvedr.   r.   r/   r         za file named zfiles named z does not appear to have z'for available files.c                 S   s   g | ]}|d ur|qS r,   r.   r   r.   r.   r/   r     s    )%popwarningswarnFutureWarning
ValueErrorr0   loggerinfor   r_   r   isdirr   isfileOSErrorappendlenr6   r   r   r   r   r   r   r   rc   r   r   r   allr   r<   r!   r   anyzipEnvironmentError)r>   r   r@   r   r   r   r   rA   r   r   r   rn   r   r   r   r   deprecated_kwargsr   full_filenamesexisting_filesr   rB   	revision_file_countereresolved_filesmissing_entriesmsgr.   )r@   r>   rA   r   r/   r     s2  S







2&*r   c                  O   s    t d t| dddd|S )a  
    Tries to locate a file in a local folder and repo, downloads and cache it if necessary.

    Args:
        path_or_repo (`str` or `os.PathLike`):
            This can be either:

            - a string, the *model id* of a model repo on huggingface.co.
            - a path to a *directory* potentially containing the file.
        filename (`str`):
            The name of the file to locate in `path_or_repo`.
        cache_dir (`str` or `os.PathLike`, *optional*):
            Path to a directory in which a downloaded pretrained model configuration should be cached if the standard
            cache should not be used.
        force_download (`bool`, *optional*, defaults to `False`):
            Whether or not to force to (re-)download the configuration files and override the cached versions if they
            exist.
        resume_download:
            Deprecated and ignored. All downloads are now resumed by default when possible.
            Will be removed in v5 of Transformers.
        proxies (`Dict[str, str]`, *optional*):
            A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',
            'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request.
        token (`str` or *bool*, *optional*):
            The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
            when running `huggingface-cli login` (stored in `~/.huggingface`).
        revision (`str`, *optional*, defaults to `"main"`):
            The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
            git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any
            identifier allowed by git.
        local_files_only (`bool`, *optional*, defaults to `False`):
            If `True`, will only try to load the tokenizer configuration from local files.
        subfolder (`str`, *optional*, defaults to `""`):
            In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can
            specify the folder name here.

    <Tip>

    Passing `token=True` is required when you want to use a private model.

    </Tip>

    Returns:
        `Optional[str]`: Returns the resolved file (to the cache folder if downloaded from a repo) or `None` if the
        file does not exist.

    Examples:

    ```python
    # Download a tokenizer configuration from huggingface.co and cache.
    tokenizer_config = get_file_from_repo("google-bert/bert-base-uncased", "tokenizer_config.json")
    # This model does not have a tokenizer config so the result will be None.
    tokenizer_config = get_file_from_repo("FacebookAI/xlm-roberta-base", "tokenizer_config.json")
    ```
    zb`get_file_from_repo` is deprecated and will be removed in version 4.51. Use `cached_file` instead.F)r   r   r   )r   warningr   )argsr   r.   r.   r/   get_file_from_repo  s   ;r   c                 C   s`   t d|  dt t \}}t|d}t| ||d W d   |S 1 s)w   Y  |S )aw  
    Downloads a given url in a temporary file. This function is not safe to use in multiple processes. Its only use is
    for deprecated behavior allowing to download config/models with a single url instead of using the Hub.

    Args:
        url (`str`): The url of the file to download.
        proxies (`Dict[str, str]`, *optional*):
            A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',
            'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request.

    Returns:
        `str`: The location of the temporary file where the url was downloaded.
    z5Using `from_pretrained` with the url of a file (here aY  ) is deprecated and won't be possible anymore in v5 of Transformers. You should host your file on the Hub (hf.co) instead and use the repository ID. Note that this is not compatible with the caching system (your file will be downloaded at each execution) or multiple processes (each process will download the file in a different temporary file).wb)r   N)r   r   r   tempfilemkstempr_   fdopenr   )urlr   tmp_fdtmp_filefr.   r.   r/   download_urlZ  s   

r   )r   r@   r   path_or_repoc             
   K   s  | dd}	|	durtdt |durtd|	}tj| r+tjtj	| |S t
| ||||d}
t|
t}|r=|S zt jt| |||dt|t dd|d	d
}W n tjjtjjfyc     tjjtjjtfyt   | Y S w zt| W dS  ty } zt| t|  d|  d|d}~w ty } zt| t|  d|d}~w ty } zt| t| d|  d|d}~w t y   Y dS  tj!y   | Y S w )a  
    Checks if a repo contains a given file without downloading it. Works for remote repos and local folders.

    If offline mode is enabled, checks if the file exists in the cache.

    <Tip warning={false}>

    This function will raise an error if the repository `path_or_repo` is not valid or if `revision` does not exist for
    this repo, but will return False for regular connection errors.

    </Tip>
    r   Nr   r   )repo_idr   rA   r   r@   )r   rA   r   )r   rn   F
   )headersallow_redirectsr   timeoutTzN is a gated repository. Make sure to request access at https://huggingface.co/z and pass a token having permission to this repo either by logging in with `huggingface-cli login` or by passing `token=<your_token>`.zE is not a local folder or a valid repository name on 'https://hf.co'.r   r   )"r   r   r   r   r   r_   r   r   r   r   r   r   r   r   headr   r   r   r]   
exceptionsSSLError
ProxyErrorConnectionErrorTimeoutr   r   r   r   errorr   r   r   r   r!   )r   r   rA   r   r   r   r@   r   r   r   cached_pathhas_file_in_cacheresponser   r.   r.   r/   has_fileu  s   





r   c                   @   sP  e Zd ZdZ				ddedee deeeef  dee dee defd	d
Zdeee	j
f fddZ					ddeee	j
f dedeeef dee deeeef  dedee dee fddZ										d dedee dee dee deeeef  deeeef  dededee dee deee  defddZdS )!PushToHubMixinzW
    A Mixin containing the functionality to push a model or tokenizer to the hub.
    Nr   privater   repo_urlorganizationro   c                 C   s   |durt d |durtd|t dd}|dur:t d ||s:d|v r3|dd }| d| }t|||dd	}|jS )
z
        Create the repo if needed, cleans up repo_id with deprecated kwargs `repo_url` and `organization`, retrieves
        the token.
        NzgThe `repo_url` argument is deprecated and will be removed in v5 of Transformers. Use `repo_id` instead.zT`repo_id` and `repo_url` are both specified. Please set only the argument `repo_id`.rr   rz   zThe `organization` argument is deprecated and will be removed in v5 of Transformers. Set your organization directly in the `repo_id` passed instead (`repo_id={organization}/{model_id}`).T)r   r   r   exist_ok)	r   r   r   replacer<   
startswithrb   r   r   )selfr   r   r   r   r   r   r.   r.   r/   _create_repo  s&   
zPushToHubMixin._create_repoworking_dirc                    s    fddt  D S )zS
        Returns the list of files with their last modification timestamp.
        c              	      s$   i | ]}|t jt j |qS r.   r_   r   getmtimer   rt   r   r   r.   r/   
<dictcomp>  s   $ z8PushToHubMixin._get_files_timestamps.<locals>.<dictcomp>)r_   listdir)r   r   r.   r   r/   _get_files_timestamps  s   z$PushToHubMixin._get_files_timestampsFfiles_timestampscommit_message	create_prrA   commit_descriptionc	              
      s  |du r8d| j jv rd}n+d| j jv rd}n"d| j jv rd}nd| j jv r(d	}nd
| j jv r1d}nd| j j } fddtD }	fdd|	D }	g }
|	D ]=}tjtj|rttj|D ]}|
ttj||tj||d qiqR|
ttj||d qR|dur|	dsz
t
|||dd W n ty } z|jjdkr|rn W Y d}~nd}~ww td| dd|	  t||
|||||dS )zh
        Uploads all modified files in `working_dir` to `repo_id`, based on `files_timestamps`.
        NModelzUpload modelConfigzUpload config	TokenizerzUpload tokenizerFeatureExtractorzUpload feature extractor	ProcessorzUpload processorzUpload c                    s6   g | ]}| vst jt j| | kr|qS r.   r   r   r   r   r.   r/   r   "  s
    &z9PushToHubMixin._upload_modified_files.<locals>.<listcomp>c                    s<   g | ]}t jt j |st jt j |r|qS r.   )r_   r   r   r   r   r   r   r.   r/   r   )  s    )path_or_fileobjpath_in_repozrefs/prT)r   branchr   r   i  z!Uploading the following files to z: ,)r   
operationsr   r   r   r   rA   )	__class____name__r_   r   r   r   r   r   r	   r   r   r   r   status_coder   r   r   )r   r   r   r   r   r   r   rA   r   modified_filesr
  r   r   r   r.   r  r/   _upload_modified_files  sf   
	z%PushToHubMixin._upload_modified_files5GBTuse_temp_dirmax_shard_sizesafe_serializationtagsc                 K   s  | dd}| dd}|dur tdt |durtd|}| dd}|durTtdt |dur8td	tj|rJ|tjj	d
 }|}n|}|dd
 }n|dd
 }| dd}| dd}| j
|||||d}t||||d}|du rtj| }t||d,}| |}| j|||d |tj|d | j|||||||	|
dW  d   S 1 sw   Y  dS )u!  
        Upload the {object_files} to the 🤗 Model Hub.

        Parameters:
            repo_id (`str`):
                The name of the repository you want to push your {object} to. It should contain your organization name
                when pushing to a given organization.
            use_temp_dir (`bool`, *optional*):
                Whether or not to use a temporary directory to store the files saved before they are pushed to the Hub.
                Will default to `True` if there is no directory named like `repo_id`, `False` otherwise.
            commit_message (`str`, *optional*):
                Message to commit while pushing. Will default to `"Upload {object}"`.
            private (`bool`, *optional*):
                Whether to make the repo private. If `None` (default), the repo will be public unless the organization's default is private. This value is ignored if the repo already exists.
            token (`bool` or `str`, *optional*):
                The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
                when running `huggingface-cli login` (stored in `~/.huggingface`). Will default to `True` if `repo_url`
                is not specified.
            max_shard_size (`int` or `str`, *optional*, defaults to `"5GB"`):
                Only applicable for models. The maximum size for a checkpoint before being sharded. Checkpoints shard
                will then be each of size lower than this size. If expressed as a string, needs to be digits followed
                by a unit (like `"5MB"`). We default it to `"5GB"` so that users can easily load models on free-tier
                Google Colab instances without any CPU OOM issues.
            create_pr (`bool`, *optional*, defaults to `False`):
                Whether or not to create a PR with the uploaded files or directly commit.
            safe_serialization (`bool`, *optional*, defaults to `True`):
                Whether or not to convert the model weights in safetensors format for safer serialization.
            revision (`str`, *optional*):
                Branch to push the uploaded files to.
            commit_description (`str`, *optional*):
                The description of the commit that will be created
            tags (`List[str]`, *optional*):
                List of tags to push on the Hub.

        Examples:

        ```python
        from transformers import {object_class}

        {object} = {object_class}.from_pretrained("google-bert/bert-base-cased")

        # Push the {object} to your namespace with the name "my-finetuned-bert".
        {object}.push_to_hub("my-finetuned-bert")

        # Push the {object} to an organization with the name "my-finetuned-bert".
        {object}.push_to_hub("huggingface/my-finetuned-bert")
        ```
        r   Nignore_metadata_errorsFr   r   repo_path_or_namezpThe `repo_path_or_name` argument is deprecated and will be removed in v5 of Transformers. Use `repo_id` instead.z]`repo_id` and `repo_path_or_name` are both specified. Please set only the argument `repo_id`.r   rr   r   r   )r   r   r   r   r   r  )r   r  )r  r  z	README.md)r   r   r   rA   r   )r   r   r   r   r   r_   r   r   rb   sepr   create_and_tag_model_cardr%   r   save_pretrainedsaver   r  )r   r   r  r   r   r   r  r   r  rA   r   r  r   r   r  r  r   r   r   
model_cardwork_dirr   r.   r.   r/   push_to_hubV  sj   ?

$zPushToHubMixin.push_to_hub)NNNN)NNFNN)
NNNNr  FTNNN)r  
__module____qualname____doc__r   r   boolr   r   r_   PathLiker   r   floatr  intlistr  r.   r.   r.   r/   r     s    
#
	
R	

r   pytorch)	frameworkc                G   s   t  rdS | |d}|D ]N}dd |j D }d|v r,|d }tj|s,|d |d< d|v r7|d |d< qd|v rZ| d	d
dd
dd
}|dd
}| d|d  |d< qtddtt	|d dS )a  
    Sends telemetry that helps tracking the examples use.

    Args:
        example_name (`str`): The name of the example.
        *example_args (dataclasses or `argparse.ArgumentParser`): The arguments to the script. This function will only
            try to extract the model and dataset name from those. Nothing else is tracked.
        framework (`str`, *optional*, defaults to `"pytorch"`): The framework for the example.
    N)exampler(  c                 S   s(   i | ]\}}| d s|dur||qS )_N)r   rs   r.   r.   r/   r     s   ( z*send_example_telemetry.<locals>.<dictcomp>model_name_or_path
model_namedataset_name	task_nametf_rz   flax_run__no_trainer-examplestransformers)topiclibrary_namelibrary_versionrn   )
r0   __dict__r   r_   r   r   r   r    r#   r   )example_namer(  example_argsdatar   args_as_dictr,  script_namer.   r.   r/   send_example_telemetry  s&   


r?  sizec                 C   s$  t | tr| S |  drt| dd d S |  dr)t| dd d S |  dr:t| dd d S |  d	rVt| dd
 d }| drT|d S |S |  drrt| dd
 d }| drp|d S |S |  drt| dd
 d }| dr|d S |S td)a'  
    Converts a size expressed as a string with digits an unit (like `"5MB"`) to an integer (in bytes).

    Args:
        size (`int` or `str`): The size to convert. Will be directly returned if an `int`.

    Example:
    ```py
    >>> convert_file_size_to_int("1MiB")
    1048576
    ```
    GIBNi   @MIBi   KIBi   GBi ʚ;b   MBi@B KBi  zR`size` is not in a valid format. Use an integer followed by the unit, e.g., '5GB'.)r   r%  r   endswithr   )r@  int_sizer.   r.   r/   convert_file_size_to_int  s$   
rM  c                    s$  ddl }|dd}|durtdt |durtd|}tj|s/td| d  dt	|}|
| }W d   n1 sEw   Y  tt|d	  }|d
 }t|d	  |d< |d	  |d	< tj r~ fdd|D }||fS t |||||||||	|d}||fS )a  
    For a given model:

    - download and cache all the shards of a sharded checkpoint if `pretrained_model_name_or_path` is a model ID on the
      Hub
    - returns the list of paths to all the shards, as well as some metadata.

    For the description of each arg, see [`PreTrainedModel.from_pretrained`]. `index_filename` is the full path to the
    index (downloaded and cached if `pretrained_model_name_or_path` is a model ID on the Hub).
    r   Nr   r   r   zCan't find a checkpoint index (z) in r   
weight_mapmetadataall_checkpoint_keysc                    s   g | ]
}t j |qS r.   r   r   pretrained_model_name_or_pathr   r.   r/   r   V  r   z.get_checkpoint_shard_files.<locals>.<listcomp>)
r@   r   r   r   r   r   rn   rA   r   r   )ra   r   r   r   r   r   r_   r   r   openrd   readsortedsetvaluesr&  keyscopyr   r   )rR  index_filenamer@   r   r   r   r   r   rn   rA   r   r   r   ra   r   r   indexshard_filenamessharded_metadatacached_filenamesr.   rQ  r/   get_checkpoint_shard_files$  sJ   
r_  r   r  r  c                 C   s   z
t j| ||d}W n ty(   d}t|du rg n|dd}t j||d}Y nw |durI|jjdu r7g |j_|D ]}||jjvrH|jj| q9|S )a  
    Creates or loads an existing model card and tags it.

    Args:
        repo_id (`str`):
            The repo_id where to look for the model card.
        tags (`List[str]`, *optional*):
            The list of tags to add in the model card
        token (`str`, *optional*):
            Authentication token, obtained with `huggingface_hub.HfApi.login` method. Will default to the stored token.
        ignore_metadata_errors (`bool`, *optional*, defaults to `False`):
            If True, errors while parsing the metadata section will be ignored. Some information might be lost during
            the process. Use it at your own risk.
    r  u   This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.Nr5  )r  r7  )model_description)r
   loadr   r   from_templater<  r  r   )r   r  r   r  r  r`  	card_data	model_tagr.   r.   r/   r  m  s   r  c                   @   sD   e Zd ZdZddeej ddfddZdd Zd	d
 Z	dddZ
dS )PushInProgresszj
    Internal class to keep track of a push in progress (which might contain multiple `Future` jobs).
    Njobsro   c                 C   s   |d u r	g | _ d S || _ d S r,   rf  )r   rf  r.   r.   r/   __init__  s   zPushInProgress.__init__c                 C   s   t dd | jD S )Nc                 s   s    | ]}|  V  qd S r,   )donert   jobr.   r.   r/   rw     r   z)PushInProgress.is_done.<locals>.<genexpr>)r   rf  r   r.   r.   r/   is_done  s   zPushInProgress.is_donec                 C   s   t | j d S r,   )r   waitrf  rl  r.   r.   r/   wait_until_done  s   zPushInProgress.wait_until_donec                 C   s   dd | j D | _ d S )Nc                 S   s    g | ]}|  s| s|qS r.   )cancelri  rj  r.   r.   r/   r     s    z)PushInProgress.cancel.<locals>.<listcomp>rg  rl  r.   r.   r/   rp    s   zPushInProgress.cancelr,   )ro   N)r  r  r   r!  r   r   Futurerh  rm  ro  rp  r.   r.   r.   r/   re    s    re  )NNr,   )NFNNNNFrz   NNTTTN)NNN)
NFNNFNNNrz   N)NNF)tr!  ra   r_   r   r{   r   r   
concurrentr   pathlibr   typingr   r   urllib.parser   uuidr   huggingface_hubr]   r   r	   r
   r   r   r   r   r   r   r   r   r   huggingface_hub.file_downloadr   r   huggingface_hub.utilsr   r   r   r   r   r   r   r   r   r   r    requests.exceptionsr!   rz   r#   r$   genericr%   import_utilsr&   r'   r(   r)   r*   r+   
get_loggerr  r   HF_HUB_OFFLINEr-   r0   re   r   r   torch_cache_homedefault_cache_pathHF_HUB_CACHEr4   r5   r6   HF_HOMEr7    TRANSFORMERS_DYNAMIC_MODULE_NAMEhexr}   keyr   r   S3_BUCKET_PREFIXCLOUDFRONT_DISTRIB_PREFIXr`   r^   r   _staging_mode_default_endpointr<   HUGGINGFACE_CO_PREFIX!HUGGINGFACE_CO_EXAMPLES_TELEMETRYr   rC   rI   rm   r   r   r   r#  r   r&  r"  r   r   r   r   r   r?  r%  rM  r_  r  re  r.   r.   r.   r/   <module>   sh  84 





"
B	

  
G	
d  	#$
K

(