U
    ×9%eÝÐ  ã                   @   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ZddlZddl	m
Z
 ddlmZ ddlmZmZmZ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 dd	l m!Z!m"Z" dd
l#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* ddl+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl/m9Z9 e/ :e;¡Z<ej= >dd¡ ?¡ e3krrdndZ@dd„ ZAe BdejC De Bdd¡d¡¡ZEejC DeEd¡ZFejC Ge BdejC De Bdd¡d¡¡¡ZHejC DeHd¡ZIejC JeF¡r4ejC JeI¡s4dej=kr4d ej=kr4d!ej=kr4e< Kd"¡ e LeFeI¡ e BdeI¡ZMe Bd eM¡ZNe Bd#eN¡ZOe Bd!eO¡ZPe Bd$ejC DeHd%¡¡ZQd&ZReƒ jSZTe Bd'd¡e3kZUd(ZVd)ZWej= >d*d+¡ ?¡ e3kZXeXr¾d,nd-ZYeYZZej= >d.d¡dk	rôe [d/e\¡ ej= >d.d¡ZZej= >d0eZ¡ZZeZd1 Z]eZd2 Z^e_ƒ Z`d3d4„ Zadreebef ee d5œd6d7„Zcd8d9„ Zddseeebdf ebd:œd;d<„Zeeeb eeb d=œd>d?„Zfdtebebeebedf eeb eeb eeb d@œdAdB„Zgdueebejhf ebeeebejhf  eieieeebebf  eeeiebf  eeb eiebeeb eeebeebebf f  eieieeb dDœdEdF„Zjdveebejhf ebeeebejhf  eieieeebebf  eeeiebf  eeb eiebdGœ
dHdI„ZkdwdJdK„Zldxeebejhf ebeeb eeebebf  eeeiebf  dLœdMdN„ZmG dOdP„ dPƒZndQdRœdSdT„Zoeepebf dUœdVdW„ZqdydXdY„ZrdzdZd[„Zsd\d]„ Ztd^d_„ Zud`da„ Zvd{dbdc„ZwG ddde„ deƒZxejC DePdf¡ZyejC zey¡s\dZ{n@e|eyƒ2Z}zepe} ~¡ ƒZ{W n ek
r   dZ{Y nX W 5 Q R X ejC JeP¡oºe€e eP¡ƒdkZ‚e{dk rde‚rdeAƒ ràe< Kdg¡ n
e< Kdh¡ z ePeIkrewePePƒ newƒ  W nX eƒk
rb Z„ z8di De …e„j†¡¡Z‡e< ˆdje‡› die„j‰j;› dke„› dl¡ W 5 dZ„[„X Y nX e{dk rÊz2ejŠePddm e|eydnƒZ}e} ‹do¡ W 5 Q R X W n( eƒk
rÈ   e< KdpeP› dq¡ Y nX dS )|z?
Hub utilities: utilities related to download and cache models
é    N)Úfutures)ÚPath)ÚDictÚListÚOptionalÚTupleÚUnion)Úurlparse)Úuuid4)ÚCommitOperationAddÚcreate_branchÚcreate_commitÚcreate_repoÚget_hf_file_metadataÚhf_hub_downloadÚ
hf_hub_url)ÚREGEX_COMMIT_HASHÚhttp_get)ÚEntryNotFoundErrorÚGatedRepoErrorÚLocalEntryNotFoundErrorÚRepositoryNotFoundErrorÚRevisionNotFoundErrorÚbuild_hf_headersÚhf_raise_for_status)Ú	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_sagemaker)ÚtqdmZTRANSFORMERS_OFFLINEÚ0TFc                   C   s   t S ©N)Ú_is_offline_mode© r*   r*   úU/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/utils/hub.pyÚis_offline_modeH   s    r,   Z
TORCH_HOMEÚXDG_CACHE_HOMEz~/.cacheZtorchÚtransformersZHF_HOMEZhuggingfaceZhubÚPYTORCH_PRETRAINED_BERT_CACHEÚPYTORCH_TRANSFORMERS_CACHEÚTRANSFORMERS_CACHEa—  In Transformers v4.0.0, the default path to cache downloaded models changed from '~/.cache/torch/transformers' to '~/.cache/huggingface/transformers'. Since you don't seem to have overridden and '~/.cache/torch/transformers' is a directory that exists, we're moving it to '~/.cache/huggingface/transformers' to avoid redownloading models you have already in the cache. You should only see this message once.ÚHUGGINGFACE_HUB_CACHEÚHF_MODULES_CACHEÚmodulesZtransformers_modulesÚDISABLE_TELEMETRYz3https://s3.amazonaws.com/models.huggingface.co/bertzhttps://cdn.huggingface.coZHUGGINGFACE_CO_STAGINGÚNOzhttps://hub-ci.huggingface.cozhttps://huggingface.coÚHUGGINGFACE_CO_RESOLVE_ENDPOINTz‘Using the environment variable `HUGGINGFACE_CO_RESOLVE_ENDPOINT` is deprecated and will be removed in Transformers v5. Use `HF_ENDPOINT` instead.ZHF_ENDPOINTz)/{model_id}/resolve/{revision}/{filename}z/api/telemetry/examplesc                 C   s   t | ƒ}|jdkS )N)ÚhttpÚhttps)r	   Úscheme)Zurl_or_filenameÚparsedr*   r*   r+   Úis_remote_url„   s    r<   )Ú	cache_dirÚreturnc           	   
   C   sÂ   | dkrt } nt| tƒr t| ƒ} tj | ¡s0g S g }t | ¡D ]~}| d¡r>tj 	| |¡}t
|ddP}t |¡}|d }|d }| d¡r²tj | d¡¡d }| |||f¡ W 5 Q R X q>|S )	a  
    Returns a list of tuples representing model binaries that are cached locally. Each tuple has shape `(model_url,
    etag, size_MB)`. Filenames in `cache_dir` are use to get the metadata for each model, only urls ending with *.bin*
    are added.

    Args:
        cache_dir (`Union[str, Path]`, *optional*):
            The cache directory to search for models within. Will default to the transformers cache if unset.

    Returns:
        List[Tuple]: List of tuples each with shape `(model_url, etag, size_MB)`
    Nú.jsonúutf-8©ÚencodingÚurlÚetagz.bing    €„.A)r1   Ú
isinstancer   ÚstrÚosÚpathÚisdirÚlistdirÚendswithÚjoinÚopenÚjsonÚloadÚgetsizeÚstripÚappend)	r=   Zcached_modelsÚfileÚ	meta_pathÚ	meta_fileÚmetadatarC   rD   Zsize_MBr*   r*   r+   Úget_cached_models‰   s$    



rW   c               	   C   sÎ   z2t  tjd ¡ ¡ } | d }| d  d¡d }W n tk
rN   d }d }Y nX t t dd¡¡}d|krndnd	}d
tjkrt d
¡ d¡d nd }t dd ¡t dd ¡t dd¡t dd¡||||dœ}|S )NZECS_CONTAINER_METADATA_URIZImageú:r   ZSM_FRAMEWORK_PARAMSz{}Z*sagemaker_distributed_dataparallel_enabledTFZTRAINING_JOB_ARNé   ZSM_FRAMEWORK_MODULEZ
AWS_REGIONZSM_NUM_GPUSr   ZSM_NUM_CPUS)Zsm_frameworkZ	sm_regionZsm_number_gpuZsm_number_cpuZsm_distributed_trainingZsm_deep_learning_containerZsm_deep_learning_container_tagZsm_account_id)	ÚrequestsÚgetrG   ÚenvironrN   ÚsplitÚ	ExceptionÚloadsÚgetenv)Zinstance_dataZdlc_container_usedZdlc_tagZsagemaker_paramsZruns_distributed_trainingZ
account_idZsagemaker_objectr*   r*   r+   Údefine_sagemaker_information¬   s(    
"



ø
ra   )Ú
user_agentr>   c                 C   sà   dt › dtj ¡ d › dt› }tƒ r4|dt› 7 }tƒ rH|dt› 7 }t	rT|d S t
ƒ r||dd d	d
„ tƒ  ¡ D ƒ¡ 7 }tj dd¡ ¡ tkrš|d7 }t| tƒrÆ|dd dd
„ |  ¡ D ƒ¡ 7 }nt| tƒrÜ|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   s    | ]\}}|› d |› V  qdS ©ú/Nr*   ©Ú.0ÚkÚvr*   r*   r+   Ú	<genexpr>Ò   s     z"http_user_agent.<locals>.<genexpr>ZTRANSFORMERS_IS_CIÚ z; is_ci/truec                 s   s    | ]\}}|› d |› V  qdS rc   r*   re   r*   r*   r+   ri   ×   s     )r   ÚsysÚversionr]   Ú
SESSION_IDr$   r"   r#   r!   r5   r%   rL   ra   ÚitemsrG   r\   r[   Úupperr    rE   ÚdictrF   )rb   Zuar*   r*   r+   Úhttp_user_agentÆ   s      "
"
rq   )Úresolved_fileÚcommit_hashc                 C   sZ   | dks|dk	r|S t t| ƒ ¡ ƒ} t d| ¡}|dkr<dS | ¡ d }t |¡rV|S dS )zP
    Extracts the commit hash from a resolved filename toward a cache file.
    Nzsnapshots/([^/]+)/r   )rF   r   Úas_posixÚreÚsearchÚgroupsr   Úmatch)rr   rs   rv   r*   r*   r+   Úextract_commit_hashÝ   s    ry   )Úrepo_idÚfilenamer=   ÚrevisionÚ	repo_typer>   c              	   C   s*  |dkrd}|dkrt }|  dd¡}|dkr0d}tj ||› d|› ¡}tj |¡sXdS dD ] }tj tj ||¡¡s\ dS q\t tj |d¡¡}||krÂttj |d|¡ƒ}	|	 ¡ }W 5 Q R X tj 	tj |d	||¡¡ràt
S t tj |d
¡¡}
||
krdS tj |d
||¡}tj 	|¡r&|S dS )a	  
    Explores the cache to return the latest cached file for a given revision if found.

    This function will not raise any exception if the file in not cached.

    Args:
        cache_dir (`str` or `os.PathLike`):
            The folder where the cached files lie.
        repo_id (`str`):
            The ID of the repo on huggingface.co.
        filename (`str`):
            The filename to look for inside `repo_id`.
        revision (`str`, *optional*):
            The specific model version to use. Will default to `"main"` if it's not provided and no `commit_hash` is
            provided either.
        repo_type (`str`, *optional*):
            The type of the repo.

    Returns:
        `Optional[str]` or `_CACHED_NO_EXIST`:
            Will return `None` if the file was not cached. Otherwise:
            - The exact path to the cached file if it's found in the cache
            - A special value `_CACHED_NO_EXIST` if the file does not exist at the given commit hash and this fact was
              cached.
    NÚmainrd   ú--Úmodelzs--)ÚrefsÚ	snapshotsr   z	.no_existr‚   )r1   ÚreplacerG   rH   rL   rI   rJ   rM   ÚreadÚisfileÚ_CACHED_NO_EXIST)rz   r{   r=   r|   r}   Z	object_idZ
repo_cacheÚ	subfolderZcached_refsÚfZcached_shasÚcached_filer*   r*   r+   Útry_to_load_from_cacheë   s0     
rŠ   rj   )Úpath_or_repo_idr{   r=   Úforce_downloadÚresume_downloadÚproxiesÚtokenr|   Úlocal_files_onlyr‡   r}   rb   Ú%_raise_exceptions_for_missing_entriesÚ'_raise_exceptions_for_connection_errorsÚ_commit_hashc                 K   sŽ  |  dd¡}|dk	r4t dt¡ |dk	r0tdƒ‚|}tƒ rL|sLt d¡ d}|	dkrXd}	t| ƒ} t	j
 |	|¡}t	j
 | ¡rÌt	j
 t	j
 | |	¡|¡}t	j
 |¡sÈ|rÄt| › d|› d	| › d
|› dƒ‚ndS |S |dkrØt}t|tƒrêt|ƒ}|dk	rD|sDt| ||||
d}|dk	rD|tk	r$|S |s.dS td|› d| › dƒ‚t|ƒ}z8t| |t|	ƒdkrfdn|	|
||||||||d}W n tk
r¼ } ztd| › dƒ|‚W 5 d}~X Y nÎ tk
rð } zt| › dƒ|‚W 5 d}~X Y nš tk
r* } zt|› d| › dƒ|‚W 5 d}~X Y n` tk
r° } zht| |||d}|dk	rl|tkrl| W Y ¢:S |rx|s‚W Y ¢&dS tdt› d| › d|› dƒ|‚W 5 d}~X Y nÚ tk
r } zD|sÒW Y ¢8dS |dkràd}t| › d|› d	| › d
|› dƒ|‚W 5 d}~X Y nx tk
rˆ } zXt| |||d}|dk	rT|tkrT| W Y ¢*S |sdW Y ¢dS td| › d|› ƒ‚W 5 d}~X Y nX |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 (`bool`, *optional*, defaults to `False`):
            Whether or not to delete incompletely received file. Attempts to resume the download if such a file exists.
        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("bert-base-uncased", "pytorch_model.bin")
    ```Úuse_auth_tokenNúVThe `use_auth_token` argument is deprecated and will be removed in v5 of Transformers.úV`token` and `use_auth_token` are both specified. Please set only the argument `token`.z+Offline mode: forcing local_files_only=TrueTrj   ú& does not appear to have a file named z#. Checkout 'https://huggingface.co/rd   z' for available files.)r=   r|   r}   zCould not locate z inside Ú.r   )
r‡   r}   r|   r=   rb   rŒ   rŽ   r   r   r   z]You are trying to access a gated repo.
Make sure to request access at https://huggingface.co/ú† and pass a token having permission to this repo either by logging in with `huggingface-cli login` or by passing `token=<your_token>`.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.©r=   r|   úWe couldn't connect to 'zL' to load this file, couldn't find it in the cached files and it looks like z8 is not the path to a directory containing a file named zš.
Checkout your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/transformers/installation#offline-mode'.r~   z:There was a specific connection error when trying to load z:
)ÚpopÚwarningsÚwarnÚFutureWarningÚ
ValueErrorr,   ÚloggerÚinforF   rG   rH   rL   rI   r…   ÚEnvironmentErrorr1   rE   r   rŠ   r†   rq   r   Úlenr   r   r   r   r7   r   r   )r‹   r{   r=   rŒ   r   rŽ   r   r|   r   r‡   r}   rb   r‘   r’   r“   Údeprecated_kwargsr”   Zfull_filenamerr   ÚeÚerrr*   r*   r+   r‰   .  sÔ    F ÿ	

ÿ
    ÿ

ô
ÿüÿûÿü
ÿû

ÿý
&r‰   )
Úpath_or_repor{   r=   rŒ   r   rŽ   r   r|   r   r‡   c
                 K   sT   |
  dd¡}|dk	r4t dt¡ |dk	r0tdƒ‚|}t| |||||||||	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 (`bool`, *optional*, defaults to `False`):
            Whether or not to delete incompletely received file. Attempts to resume the download if such a file exists.
        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("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("xlm-roberta-base", "tokenizer_config.json")
    ```r”   Nr•   r–   F)r‹   r{   r=   rŒ   r   rŽ   r   r|   r   r‡   r‘   r’   )rž   rŸ   r    r¡   r¢   r‰   )rª   r{   r=   rŒ   r   rŽ   r   r|   r   r‡   r§   r”   r*   r*   r+   Úget_file_from_repoñ  s.    B ÿôr«   c              	   C   sF   t  d| › d¡ t ¡ d }t|dƒ}t| ||d W 5 Q R X |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).r   Úwb)rŽ   )rŸ   r    ÚtempfileÚmkstemprM   r   )rC   rŽ   Ztmp_filerˆ   r*   r*   r+   Údownload_urlL  s    
ÿr¯   )rª   r{   r|   rŽ   r   c              
   K   sn  |  dd¡}|dk	r4t dt¡ |dk	r0tdƒ‚|}tj | ¡rVtj tj 	| |¡¡S t
| ||d}t|tƒ d}tj||d|dd	}	zt|	ƒ W d
S  tk
rÖ }
 z$t |
¡ t| › d| › dƒ|
‚W 5 d}
~
X Y n” tk
r }
 zt |
¡ t| › dƒ‚W 5 d}
~
X Y nZ tk
rP }
 z"t |
¡ t|› d| › dƒ‚W 5 d}
~
X Y n tjk
rh   Y dS X dS )aV  
    Checks if a repo contains a given file without downloading it. Works for remote repos and local folders.

    <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–   )r{   r|   )r   rb   Fé
   )ÚheadersÚallow_redirectsrŽ   ÚtimeoutTzN is a gated repository. Make sure to request access at https://huggingface.co/r™   zE is not a local folder or a valid repository name on 'https://hf.co'.rš   r›   )rž   rŸ   r    r¡   r¢   rG   rH   rI   r…   rL   r   r   rq   rZ   Úheadr   r   r£   Úerrorr¥   r   r   r   )rª   r{   r|   rŽ   r   r§   r”   rC   r±   Úrr¨   r*   r*   r+   Úhas_filef  sD     ÿ
ÿü

ÿr·   c                   @   sÞ   e Zd ZdZdeee eeeef  ee ee edœdd„Zeee	j
f dœdd„Zdeee	j
f eeeef ee eeeef  eed
œdd„Zdeee ee ee eeeef  eeeef  eeeedœ
dd„ZdS )ÚPushToHubMixinzW
    A Mixin containing the functionality to push a model or tokenizer to the hub.
    N)rz   Úprivater   Úrepo_urlÚorganizationr>   c                 C   sŠ   |dk	r4t  d¡ |dk	r"tdƒ‚| t› dd¡}|dk	rtt  d¡ | |¡std|krf| 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`.rd   rj   zºThe `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)rz   r   r¹   Úexist_ok)	rŸ   r    r¢   rƒ   r7   Ú
startswithr]   r   rz   )Úselfrz   r¹   r   rº   r»   rC   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*   ©rG   rH   ÚgetmtimerL   ©rf   rˆ   rÁ   r*   r+   Ú
<dictcomp>Ì  s      z8PushToHubMixin._get_files_timestamps.<locals>.<dictcomp>)rG   rJ   )r¿   rÂ   r*   rÁ   r+   Ú_get_files_timestampsÈ  s    z$PushToHubMixin._get_files_timestampsF)rÂ   rz   Úfiles_timestampsÚcommit_messager   Ú	create_prr|   c              
      sl  |dkrpd| j jkrd}nVd| j jkr,d}nDd| j jkr>d}n2d| j jkrPd	}n d
| j jkrbd}nd| j j› }‡ ‡fdd„t ˆ¡D ƒ}‡fdd„|D ƒ}g }	|D ]|}
tj tj ˆ|
¡¡rt tj ˆ|
¡¡D ],}|	 ttj ˆ|
|¡tj |
|¡d¡ qÔq¤|	 ttj ˆ|
¡|
d¡ q¤|dk	r<t	|||dd t
 d|› dd |¡› ¡ t||	||||dS )zh
        Uploads all modified files in `working_dir` to `repo_id`, based on `files_timestamps`.
        NZModelzUpload modelZConfigzUpload configÚ	TokenizerzUpload tokenizerZFeatureExtractorzUpload feature extractorZ	ProcessorzUpload processorzUpload c                    s6   g | ].}|ˆ ks.t j t j ˆ|¡¡ˆ | kr|‘qS r*   rÃ   rÅ   ©rÈ   rÂ   r*   r+   Ú
<listcomp>è  s    þz9PushToHubMixin._upload_modified_files.<locals>.<listcomp>c                    s<   g | ]4}t j t j ˆ |¡¡s4t j t j ˆ |¡¡r|‘qS r*   )rG   rH   r…   rL   rI   rÅ   rÁ   r*   r+   rÍ   ï  s    þ)Zpath_or_fileobjZpath_in_repoT)rz   Úbranchr   r½   z!Uploading the following files to ú: ú,)rz   Ú
operationsrÉ   r   rÊ   r|   )Ú	__class__Ú__name__rG   rJ   rH   rI   rL   rR   r   r   r£   r¤   r   )r¿   rÂ   rz   rÈ   rÉ   r   rÊ   r|   Zmodified_filesrÑ   rS   rˆ   r*   rÌ   r+   Ú_upload_modified_filesÎ  sT    þ
þ ÿÿÿ
úz%PushToHubMixin._upload_modified_filesÚ10GB)
rz   Úuse_temp_dirrÉ   r¹   r   Úmax_shard_sizerÊ   Úsafe_serializationr|   r>   c
              
   K   sB  |
  dd¡}|dk	r4t dt¡ |dk	r0tdƒ‚|}|
  dd¡}|dk	rœt dt¡ |dk	rdtdƒ‚tj |¡rˆ| tjj	¡d }|}qª|}| d	¡d }n| d	¡d }|
  d
d¡}|
  dd¡}| j
|||||d}|dkrìtj |¡ }t||d@}|  |¡}| j|||d | j|||||||	dW  5 Q R £ S Q R X 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 or not the repository created should be private.
            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 `"10GB"`):
                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"`).
            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 `False`):
                Whether or not to convert the model weights in safetensors format for safer serialization.
            revision (`str`, *optional*):
                Branch to push the uploaded files to.

        Examples:

        ```python
        from transformers import {object_class}

        {object} = {object_class}.from_pretrained("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”   Nr•   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¼   rd   rº   r»   )r¹   r   rº   r»   )rÂ   rÖ   )r×   rØ   )rÉ   r   rÊ   r|   )rž   rŸ   r    r¡   r¢   rG   rH   rI   r]   ÚseprÀ   r   rÇ   Zsave_pretrainedrÔ   )r¿   rz   rÖ   rÉ   r¹   r   r×   rÊ   rØ   r|   r§   r”   rÙ   rÂ   rº   r»   Zwork_dirrÈ   r*   r*   r+   Úpush_to_hub  sd    8 ÿÿýÿ    ÿ
ùzPushToHubMixin.push_to_hub)NNNN)NNFN)NNNNrÕ   FFN)rÓ   Ú
__module__Ú__qualname__Ú__doc__rF   r   Úboolr   rÀ   rG   ÚPathLikerÇ   r   ÚfloatrÔ   ÚintrÛ   r*   r*   r*   r+   r¸      s^       úù#    ø
øG        öôr¸   Zpytorch)Ú	frameworkc          
      G   sö   t ƒ r
dS | |dœ}|D ]œ}dd„ |j ¡ D ƒ}d|krX|d }tj |¡sX|d |d< d|krn|d |d< qd|kr|  d	d
¡ dd
¡ dd
¡}| dd
¡}|› d|d › |d< qdt|ƒi}ztj	t
|d}	|	 ¡  W n tk
rð   Y nX 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)Zexamplerã   c                 S   s(   i | ] \}}|  d ¡s|dk	r||“qS )Ú_N)r¾   re   r*   r*   r+   rÆ   ™  s
     
   z*send_example_telemetry.<locals>.<dictcomp>Zmodel_name_or_pathÚ
model_nameZdataset_nameZ	task_nameZtf_rj   Zflax_Zrun_Z_no_trainerú-z
user-agent)r±   )r,   Ú__dict__rn   rG   rH   rI   rƒ   rq   rZ   r´   Ú!HUGGINGFACE_CO_EXAMPLES_TELEMETRYÚraise_for_statusr^   )
Zexample_namerã   Zexample_argsÚdataÚargsZargs_as_dictrå   Úscript_namer±   r¶   r*   r*   r+   Úsend_example_telemetryŠ  s*    

rí   )Úsizec                 C   s,  t | tƒr| S |  ¡  d¡r0t| dd… ƒd S |  ¡  d¡rRt| dd… ƒd S |  ¡  d¡rtt| dd… ƒd S |  ¡  d	¡r¬t| dd
… ƒd }|  d¡r¨|d S |S |  ¡  d¡rät| dd
… ƒd }|  d¡rà|d S |S |  ¡  d¡r t| dd
… ƒd }|  d¡r|d S |S tdƒ‚dS )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
    ```
    ZGIBNéýÿÿÿi   @ZMIBi   ZKIBi   ÚGBéþÿÿÿi Êš;Úbé   ÚMBi@B ZKBiè  zR`size` is not in a valid format. Use an integer followed by the unit, e.g., '5GB'.)rE   râ   ro   rK   r¢   )rî   Zint_sizer*   r*   r+   Úconvert_file_size_to_int°  s$    
rõ   c                    s´  ddl }| dd¡}|dk	r<t dt¡ |dk	r8tdƒ‚|}tj |¡s^td|› dˆ › dƒ‚t	|d	ƒ}| 
| ¡ ¡}W 5 Q R X tt|d
  ¡ ƒƒ}|d }t|d
  ¡ ƒ|d< |d
  ¡ |d
< tj ˆ ¡rê‡ ‡fdd„|D ƒ}||fS g }tˆ |d ||d}|dkp|}t|d| dD ]Š}z$tˆ |||||||||	ˆ|d}W nT tk
rr   tˆ › d|› dƒ‚Y n, tk
rœ   tdt› d|› dƒ‚Y nX | |¡ q ||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˜   r¶   Z
weight_maprV   Zall_checkpoint_keysc                    s   g | ]}t j ˆ ˆ|¡‘qS r*   )rG   rH   rL   rÅ   ©Úpretrained_model_name_or_pathr‡   r*   r+   rÍ     s     z.get_checkpoint_shard_files.<locals>.<listcomp>r¼   rœ   zDownloading shards)ÚdescÚdisable)
r=   rŒ   rŽ   r   r   r   rb   r|   r‡   r“   r—   z5 which is required according to the checkpoint index.r   z
' to load z?. You should try again after checking your internet connection.)rN   rž   rŸ   r    r¡   r¢   rG   rH   r…   rM   r_   r„   ÚsortedÚsetÚvaluesÚlistÚkeysÚcopyrI   rŠ   r&   r‰   r   r¥   r   r7   rR   )r÷   Zindex_filenamer=   rŒ   rŽ   r   r   r   rb   r|   r‡   r“   r§   rN   r”   rˆ   ÚindexZshard_filenamesZsharded_metadataZcached_filenamesZ
last_shardZshow_progress_barZshard_filenameZcached_filenamer*   rö   r+   Úget_checkpoint_shard_filesÑ  sn     ÿ   ÿô
ÿÿ
r  c              
   C   s¬   | dkrt } nt| ƒ} tj | ¡s&g S g }t | ¡D ]r}tj | |› d¡}tj |¡sZq4t|dd:}t	 
|¡}|d }|d  dd¡}| |||d	œ¡ W 5 Q R X q4|S )
zH
    Returns a list for all files cached with appropriate metadata.
    Nr?   r@   rA   rC   rD   ú"rj   )rS   rC   rD   )r1   rF   rG   rH   rI   rJ   rL   r…   rM   rN   rO   rƒ   rR   )r=   Úcached_filesrS   rT   rU   rV   rC   rD   r*   r*   r+   Úget_all_cached_files3  s     
r  c                 C   sH   t  d| ¡}|dkrdS | ¡ \}}}d dg| d¡ ¡}|||dœS )z?
    Extract repo_name, revision and filename from an url.
    z3^https://huggingface\.co/(.*)/resolve/([^/]*)/(.*)$Nr   Úmodelsrd   )Úrepor|   r{   )ru   rv   rw   rL   r]   )rC   rv   r  r|   r{   Z
cache_repor*   r*   r+   Úextract_info_from_urlM  s    r  c                 C   s6   | | › d| › dfD ]}t j |¡rt  |¡ qdS )z>
    Remove, if they exist, file, file.json and file.lock
    r?   z.lockN)rG   rH   r…   Úremove)rS   rˆ   r*   r*   r+   Úclean_files_forY  s    r	  c           
   	   C   sð   t j|dd t jt j |d¡dd ||kr^t j |d|¡}t|dƒ}| |¡ W 5 Q R X t jt j |d¡dd t j |d|¡}t | |¡ t jt j |d¡dd t jt j |d|¡dd t j |d||¡}	tj	 
||	¡ t| ƒ dS )zQ
    Move file to repo following the new huggingface hub cache organization.
    T©r½   r   ÚwZblobsr‚   N)rG   ÚmakedirsrH   rL   rM   ÚwriteÚshutilÚmoveÚhuggingface_hubZfile_downloadZ_create_relative_symlinkr	  )
rS   r  r{   r|   rD   rs   Zref_pathrˆ   Z	blob_pathZpointer_pathr*   r*   r+   Úmove_to_new_cacheb  s    r  c              	   C   sV  |d krt }| d kr@tt ƒjd }tj t|ƒ¡r<t|ƒ} n|} t| d}t 	dt
|ƒ› d¡ i }t|ƒD ]ä}| d¡}||kr²zt||d||< W n tjk
r°   Y qlY nX || j|| j }}	|d ksl|	d krÚql|d |kr ttj | |d ¡ƒ qlt|ƒ}
|
d krqltj ||
d	 ¡}ttj | |d ¡||
d
 |
d ||	d qld S )Nr.   )r=   zMoving z files to the new cache systemrC   )r   rD   rS   r  r{   r|   )rS   r  r{   r|   rD   rs   )r1   r   ÚparentrG   rH   rI   rF   r  r£   r¤   r¦   r&   rž   r   rZ   r   rD   rs   r	  rL   r  r  )r=   Znew_cache_dirr   Z	old_cacher  Zhub_metadataÚ	file_inforC   rD   rs   Zurl_infor  r*   r*   r+   Ú
move_cache|  sF    




úr  c                   @   sF   e Zd ZdZdeej ddœdd„Zdd„ Zdd	„ Z	dd
œdd„Z
dS )ÚPushInProgresszj
    Internal class to keep track of a push in progress (which might contain multiple `Future` jobs).
    N)Újobsr>   c                 C   s   |d krg n|| _ d S r(   ©r  )r¿   r  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©rf   Zjobr*   r*   r+   ri   ´  s     z)PushInProgress.is_done.<locals>.<genexpr>)Úallr  ©r¿   r*   r*   r+   Úis_done³  s    zPushInProgress.is_donec                 C   s   t  | j¡ d S r(   )r   Úwaitr  r  r*   r*   r+   Úwait_until_done¶  s    zPushInProgress.wait_until_done)r>   c                 C   s   dd„ | j D ƒ| _ d S )Nc                 S   s    g | ]}|  ¡ s| ¡ s|‘qS r*   )Úcancelr  r  r*   r*   r+   rÍ   º  s    ýz)PushInProgress.cancel.<locals>.<listcomp>r  r  r*   r*   r+   r   ¹  s    þzPushInProgress.cancel)N)rÓ   rÜ   rÝ   rÞ   r   r   ÚFuturer  r  r  r   r*   r*   r*   r+   r  «  s
   r  zversion.txtau  You are offline and the cache for model files in Transformers v4.22.0 has been updated while your local cache seems to be the one of a previous version. It is very likely that all your calls to any `from_pretrained()` method will fail. Remove the offline mode and enable internet connection to have your cache be updated automatically, then you can go back to offline mode.zçThe cache for model files in Transformers v4.22.0 has been updated. Migrating your old cache. This is a one-time only operation. You can interrupt this and resume the migration later on by calling `transformers.utils.move_cache()`.Ú
z5There was a problem when trying to move your cache:

rÏ   zš

Please file an issue at https://github.com/huggingface/transformers/issues/new/choose and copy paste this whole message and we will do our best to help.r
  r  Ú1z?There was a problem when trying to write in your cache folder (zV). You should set the environment variable TRANSFORMERS_CACHE to a writable directory.)N)N)NNN)NFFNNNFrj   NNTTN)NFFNNNFrj   )N)NNN)
NFNFFNNNrj   N)N)NNN)ŒrÞ   rN   rG   ru   r  rk   r­   Ú	tracebackrŸ   Ú
concurrentr   Úpathlibr   Útypingr   r   r   r   r   Úurllib.parser	   Úuuidr
   r  rZ   r   r   r   r   r   r   r   Zhuggingface_hub.file_downloadr   r   Zhuggingface_hub.utilsr   r   r   r   r   r   r   Zrequests.exceptionsr   rj   r   r   Zgenericr   Zimport_utilsr    r!   r"   r#   r$   r%   r&   Z
get_loggerrÓ   r£   r\   r[   ro   r)   r,   r`   rH   rL   Ztorch_cache_homeZold_default_cache_pathÚ
expanduserZhf_cache_homeZdefault_cache_pathrI   Úwarningr  r/   r0   r2   r1   r3   Z TRANSFORMERS_DYNAMIC_MODULE_NAMEÚhexrm   r5   ZS3_BUCKET_PREFIXZCLOUDFRONT_DISTRIB_PREFIXZ_staging_modeZ_default_endpointr7   r    r¡   ZHUGGINGFACE_CO_PREFIXrè   Úobjectr†   r<   rF   rW   ra   rq   ry   rŠ   rà   rß   r‰   r«   r¯   r·   r¸   rí   râ   rõ   r  r  r  r	  r  r  r  Zcache_version_filer…   Zcache_versionrM   rˆ   r„   r¢   r¦   rJ   Zcache_is_not_emptyr^   r¨   Ú	format_tbÚ__traceback__Útracerµ   rÒ   r  r  r*   r*   r*   r+   Ú<module>   s–  $	$	 
 ÿ
ÿ
þýüûÿý#   ûúF             ññ G        öö[
   ûû: k&$          ô
b
	
/
 ÿÿ

ÿ

ÿ