o
    Zhv                     @   sx  d dl Z d dlZd dlZd dlmZmZmZ d dlmZ d dl	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mZ ddlmZ ddlmZ dd	lmZmZ dd
lmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& ddl'm(Z( e
rrd dl)m*Z* e$ ryd dl+Z+e# rd dl,m-Z. d dl,m/Z0 e%1e2Z3edddZ4dZ5eG dd dZ6G dd dZ7G dd de7Z8ded de9ddfddZ:dS )    N)asdict	dataclassis_dataclass)Path)	TYPE_CHECKINGAnyDictListOptionalTypeTypeVarUnionget_args   )CONFIG_NAMEPYTORCH_WEIGHTS_NAMESAFETENSORS_SINGLE_FILE)hf_hub_download)HfApi)	ModelCardModelCardData)EntryNotFoundErrorHfHubHTTPErrorSoftTemporaryDirectoryis_jsonableis_safetensors_availableis_torch_availableloggingvalidate_hf_hub_args)_deprecate_arguments)DataclassInstance)
load_model)
save_modelTModelHubMixin)bounda  
---
# For reference on model card metadata, see the spec: https://github.com/huggingface/hub-docs/blob/main/modelcard.md?plain=1
# Doc / guide: https://huggingface.co/docs/hub/model-cards
{{ card_data }}
---

This model has been pushed to the Hub using **{{ library_name }}**:
- Repo: {{ repo_url | default("[More Information Needed]", true) }}
- Docs: {{ docs_url | default("[More Information Needed]", true) }}
c                   @   sR   e Zd ZU dZee ed< dZeee  ed< dZ	ee ed< dZ
ee ed< dS )	MixinInfoNlibrary_nametagsrepo_urldocs_url)__name__
__module____qualname__r'   r
   str__annotations__r(   r	   r)   r*    r0   r0   P/var/www/html/lang_env/lib/python3.10/site-packages/huggingface_hub/hub_mixin.pyr&   6   s
   
 r&   c                       s  e Zd ZU dZdZeeedf  ed< e	ed< e
eejf ed< e
eef ed< eed< ddddd	d
ee deee  dee dee ddf
 fddZd= fddZdddddeeef deeedf  dee dedee f
ddZdeddfddZeedddddddddee deeef d ed!ed"ee
 d#eeeef  d$eeeef  d%ed&ee defd'd(Zedee d)ed&ee d$eeeef  d ed"ee
 d!ed%ed#eeeef  defd*d+Zed,d-gd.d/edd0ddddddddd1
dedeeedf  d2ed3ed#ee d4ee d5ee d6eeee ef  d7eeee ef  d8eeee ef  d-ee defd9d:Zdefd;d<Z  Z S )>r$   aE  
    A generic mixin to integrate ANY machine learning framework with the Hub.

    To integrate your framework, your model class must inherit from this class. Custom logic for saving/loading models
    have to be overwritten in  [`_from_pretrained`] and [`_save_pretrained`]. [`PyTorchModelHubMixin`] is a good example
    of mixin integration with the Hub. Check out our [integration guide](../guides/integrations) for more instructions.

    When inheriting from [`ModelHubMixin`], you can define class-level attributes. These attributes are not passed to
    `__init__` but to the class definition itself. This is useful to define metadata about the library integrating
    [`ModelHubMixin`].

    Args:
        library_name (`str`, *optional*):
            Name of the library integrating ModelHubMixin. Used to generate model card.
        tags (`List[str]`, *optional*):
            Tags to be added to the model card. Used to generate model card.
        repo_url (`str`, *optional*):
            URL of the library repository. Used to generate model card.
        docs_url (`str`, *optional*):
            URL of the library documentation. Used to generate model card.

    Example:

    ```python
    >>> from huggingface_hub import ModelHubMixin

    # Inherit from ModelHubMixin
    >>> class MyCustomModel(
    ...         ModelHubMixin,
    ...         library_name="my-library",
    ...         tags=["x-custom-tag"],
    ...         repo_url="https://github.com/huggingface/my-cool-library",
    ...         docs_url="https://huggingface.co/docs/my-cool-library",
    ...         # ^ optional metadata to generate model card
    ...     ):
    ...     def __init__(self, size: int = 512, device: str = "cpu"):
    ...         # define how to initialize your model
    ...         super().__init__()
    ...         ...
    ...
    ...     def _save_pretrained(self, save_directory: Path) -> None:
    ...         # define how to serialize your model
    ...         ...
    ...
    ...     @classmethod
    ...     def from_pretrained(
    ...         cls: Type[T],
    ...         pretrained_model_name_or_path: Union[str, Path],
    ...         *,
    ...         force_download: bool = False,
    ...         resume_download: bool = False,
    ...         proxies: Optional[Dict] = None,
    ...         token: Optional[Union[str, bool]] = None,
    ...         cache_dir: Optional[Union[str, Path]] = None,
    ...         local_files_only: bool = False,
    ...         revision: Optional[str] = None,
    ...         **model_kwargs,
    ...     ) -> T:
    ...         # define how to deserialize your model
    ...         ...

    >>> model = MyCustomModel(size=256, device="gpu")

    # Save model weights to local directory
    >>> model.save_pretrained("my-awesome-model")

    # Push model weights to the Hub
    >>> model.push_to_hub("my-awesome-model")

    # Download and initialize weights from the Hub
    >>> reloaded_model = MyCustomModel.from_pretrained("username/my-awesome-model")
    >>> reloaded_model._hub_mixin_config
    {"size": 256, "device": "gpu"}

    # Model card has been correctly populated
    >>> from huggingface_hub import ModelCard
    >>> card = ModelCard.load("username/my-awesome-model")
    >>> card.data.tags
    ["x-custom-tag", "pytorch_model_hub_mixin", "model_hub_mixin"]
    >>> card.data.library_name
    "my-library"
    ```
    Nr    _hub_mixin_config_hub_mixin_info_hub_mixin_init_parameters"_hub_mixin_jsonable_default_values_hub_mixin_inject_configr'   r(   r)   r*   r'   r(   r)   r*   returnc                   sp   t    |pg }|d t||||d| _tt| jj	| _
dd | j
 D | _dt| jj	v | _dS )zIInspect __init__ signature only once when subclassing + handle modelcard.Zmodel_hub_mixinr7   c                 S   s.   i | ]}|j tjjurt|j r|j|j qS r0   )defaultinspect	Parameteremptyr   name.0paramr0   r0   r1   
<dictcomp>   s
    z3ModelHubMixin.__init_subclass__.<locals>.<dictcomp>configN)super__init_subclass__appendr&   r3   dictr:   	signature__init__
parametersr4   valuesr5   _from_pretrainedr6   )clsr'   r(   r)   r*   	__class__r0   r1   rD      s   
	
zModelHubMixin.__init_subclass__c                    s   t  | }|jdur|S i dd tt| jdd |D |}t|dr0|d |_|S i | jdd |	 D }|
di  |d}t|trS|| |i krZ||_|S )aA  Create a new instance of the class and handle config.

        3 cases:
        - If `self._hub_mixin_config` is already set, do nothing.
        - If `config` is passed as a dataclass, set it as `self._hub_mixin_config`.
        - Otherwise, build `self._hub_mixin_config` from default values and passed values.
        Nc                 S   s   i | ]\}}||qS r0   r0   r?   keyvaluer0   r0   r1   rA      s    z)ModelHubMixin.__new__.<locals>.<dictcomp>r   rB   c                 S   s   i | ]\}}t |r||qS r0   )r   rO   r0   r0   r1   rA      s    )rC   __new__r2   ziplistr4   r   getr5   itemspop
isinstancerF   update)rL   argskwargsinstanceZpassed_valuesZinit_configZprovided_configrM   r0   r1   rR      s8   
	



zModelHubMixin.__new__F)rB   repo_idpush_to_hubsave_directoryrB   r]   r^   c          
      K   s   t |}|jddd |t }|jdd | | |du r!| j}|dur>t|r-t|}| s>t	j
|ddd}|| |d }| sO|  |d  |rn| }	|dur]||	d< |du rd|j}| jd
d	|i|	S dS )aE  
        Save weights in local directory.

        Args:
            save_directory (`str` or `Path`):
                Path to directory in which the model weights and configuration will be saved.
            config (`dict` or `DataclassInstance`, *optional*):
                Model configuration specified as a key/value dictionary or a dataclass instance.
            push_to_hub (`bool`, *optional*, defaults to `False`):
                Whether or not to push your model to the Huggingface Hub after saving it.
            repo_id (`str`, *optional*):
                ID of your repository on the Hub. Used only if `push_to_hub=True`. Will default to the folder name if
                not provided.
            kwargs:
                Additional key word arguments passed along to the [`~ModelHubMixin.push_to_hub`] method.
        T)parentsexist_ok)
missing_okN   )	sort_keysindentz	README.mdrB   r]   r0   )r   mkdirr   unlink_save_pretrainedr2   r   r   existsjsondumps
write_textgenerate_model_cardsavecopyr=   r^   )
selfr_   rB   r]   r^   Zpush_to_hub_kwargsZconfig_pathZ
config_strZmodel_card_pathr[   r0   r0   r1   save_pretrained   s0   

zModelHubMixin.save_pretrainedc                 C      t )aD  
        Overwrite this method in subclass to define how to save your model.
        Check out our [integration guide](../guides/integrations) for instructions.

        Args:
            save_directory (`str` or `Path`):
                Path to directory in which the model weights and configuration will be saved.
        NotImplementedError)rp   r_   r0   r0   r1   rh   +  s   	zModelHubMixin._save_pretrained)force_downloadresume_downloadproxiestoken	cache_dirlocal_files_onlyrevisionrL   pretrained_model_name_or_pathru   rv   rw   rx   ry   rz   r{   c                K   sZ  t |}
d}tj|
r*tt|
v rtj|
t}n=tt dt	|

   n.zt|
t|||||||d	}W n tyW } ztt dt |  W Y d}~nd}~ww d}|durt|ddd}t|}W d   n1 svw   Y  | j D ]}|j|	vr|j|v r||j |	|j< qd| jv r| jd j}|tjju rnt|rt||}nt|D ]}t|rt||} nq||	d< t| r| jD ]}||	vr||v r|| |	|< qntd	d
 | j D r| D ]\}}||	vr||	|< q| jr||	d< | jdt |
|||||||d|	}|dur+t |dddi fv r+||_!|S )a  
        Download a model from the Huggingface Hub and instantiate it.

        Args:
            pretrained_model_name_or_path (`str`, `Path`):
                - Either the `model_id` (string) of a model hosted on the Hub, e.g. `bigscience/bloom`.
                - Or a path to a `directory` containing model weights saved using
                    [`~transformers.PreTrainedModel.save_pretrained`], e.g., `../path/to/my_model_directory/`.
            revision (`str`, *optional*):
                Revision of the model on the Hub. Can be a branch name, a git tag or any commit id.
                Defaults to the latest commit on `main` branch.
            force_download (`bool`, *optional*, defaults to `False`):
                Whether to force (re-)downloading the model weights and configuration files from the Hub, overriding
                the existing cache.
            resume_download (`bool`, *optional*, defaults to `False`):
                Whether to delete incompletely received files. Will attempt 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 every request.
            token (`str` or `bool`, *optional*):
                The token to use as HTTP bearer authorization for remote files. By default, it will use the token
                cached when running `huggingface-cli login`.
            cache_dir (`str`, `Path`, *optional*):
                Path to the folder where cached files are stored.
            local_files_only (`bool`, *optional*, defaults to `False`):
                If `True`, avoid downloading the file and return the path to the local cached file if it exists.
            model_kwargs (`Dict`, *optional*):
                Additional kwargs to pass to the model during initialization.
        Nz not found in 	r]   filenamer{   ry   ru   rw   rv   rx   rz   z# not found on the HuggingFace Hub: rzutf-8)encodingrB   c                 s   s    | ]
}|j tjjkV  qd S )N)kindr:   r;   VAR_KEYWORDr>   r0   r0   r1   	<genexpr>  s    z0ModelHubMixin.from_pretrained.<locals>.<genexpr>)model_idr{   ry   ru   rw   rv   rz   rx   r2   r0   )"r.   ospathisdirr   listdirjoinloggerwarningr   resolver   r   infoopenrj   loadr4   rJ   r=   
annotationr:   r;   r<   r   _load_dataclassr   __dataclass_fields__anyrV   r6   rK   getattrr2   )rL   r|   ru   rv   rw   rx   ry   rz   r{   model_kwargsr   config_fileerB   fr@   Zconfig_annotationZ_sub_annotationrP   rQ   r\   r0   r0   r1   from_pretrained6  s   ,
$



	 zModelHubMixin.from_pretrainedr   c          
      K   rr   )a  Overwrite this method in subclass to define how to load your model from pretrained.

        Use [`hf_hub_download`] or [`snapshot_download`] to download files from the Hub before loading them. Most
        args taken as input can be directly passed to those 2 methods. If needed, you can add more arguments to this
        method using "model_kwargs". For example [`PyTorchModelHubMixin._from_pretrained`] takes as input a `map_location`
        parameter to set on which device the model should be loaded.

        Check out our [integration guide](../guides/integrations) for more instructions.

        Args:
            model_id (`str`):
                ID of the model to load from the Huggingface Hub (e.g. `bigscience/bloom`).
            revision (`str`, *optional*):
                Revision of the model on the Hub. Can be a branch name, a git tag or any commit id. Defaults to the
                latest commit on `main` branch.
            force_download (`bool`, *optional*, defaults to `False`):
                Whether to force (re-)downloading the model weights and configuration files from the Hub, overriding
                the existing cache.
            resume_download (`bool`, *optional*, defaults to `False`):
                Whether to delete incompletely received files. Will attempt 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'}`).
            token (`str` or `bool`, *optional*):
                The token to use as HTTP bearer authorization for remote files. By default, it will use the token
                cached when running `huggingface-cli login`.
            cache_dir (`str`, `Path`, *optional*):
                Path to the folder where cached files are stored.
            local_files_only (`bool`, *optional*, defaults to `False`):
                If `True`, avoid downloading the file and return the path to the local cached file if it exists.
            model_kwargs:
                Additional keyword arguments passed along to the [`~ModelHubMixin._from_pretrained`] method.
        rs   )
rL   r   r{   ry   ru   rw   rv   rz   rx   r   r0   r0   r1   rK     s   /zModelHubMixin._from_pretrainedz0.23.0api_endpointz/Use `HF_ENDPOINT` environment variable instead.)versionZdeprecated_argsZcustom_messagez!Push model using huggingface_hub.)
rB   commit_messageprivaterx   branch	create_prallow_patternsignore_patternsdelete_patternsr   r   r   r   r   r   r   r   c       
         C   s~   t ||d}|j||ddj}t #}t|| }| j||d |j|d||||||	|
d	W  d   S 1 s8w   Y  dS )a  
        Upload model checkpoint to the Hub.

        Use `allow_patterns` and `ignore_patterns` to precisely filter which files should be pushed to the hub. Use
        `delete_patterns` to delete existing remote files in the same commit. See [`upload_folder`] reference for more
        details.

        Args:
            repo_id (`str`):
                ID of the repository to push to (example: `"username/my-model"`).
            config (`dict` or `DataclassInstance`, *optional*):
                Model configuration specified as a key/value dictionary or a dataclass instance.
            commit_message (`str`, *optional*):
                Message to commit while pushing.
            private (`bool`, *optional*, defaults to `False`):
                Whether the repository created should be private.
            api_endpoint (`str`, *optional*):
                The API endpoint to use when pushing the model to the hub.
            token (`str`, *optional*):
                The token to use as HTTP bearer authorization for remote files. By default, it will use the token
                cached when running `huggingface-cli login`.
            branch (`str`, *optional*):
                The git branch on which to push the model. This defaults to `"main"`.
            create_pr (`boolean`, *optional*):
                Whether or not to create a Pull Request from `branch` with that commit. Defaults to `False`.
            allow_patterns (`List[str]` or `str`, *optional*):
                If provided, only files matching at least one pattern are pushed.
            ignore_patterns (`List[str]` or `str`, *optional*):
                If provided, files matching any of the patterns are not pushed.
            delete_patterns (`List[str]` or `str`, *optional*):
                If provided, remote files matching any of the patterns will be deleted from the repo.

        Returns:
            The url of the commit of your model in the given repository.
        )Zendpointrx   T)r]   r   ra   )rB   model)	r]   Z	repo_typeZfolder_pathr   r{   r   r   r   r   N)r   Zcreate_repor]   r   r   rq   Zupload_folder)rp   r]   rB   r   r   rx   r   r   r   r   r   r   apitmpZ
saved_pathr0   r0   r1   r^     s"   9$zModelHubMixin.push_to_hubc                 O   s"   t jtdi t| jtd}|S )N)Z	card_dataZtemplate_strr0   )r   Zfrom_templater   r   r3   DEFAULT_MODEL_CARD)rp   rZ   r[   Zcardr0   r0   r1   rm   4  s
   z!ModelHubMixin.generate_model_card)r8   r$   )!r+   r,   r-   __doc__r2   r
   r   rF   r/   r&   r   r.   r:   r;   r   boolr	   rD   rR   r   rq   rh   classmethodr   r   r#   r   rK   r   r^   r   rm   __classcell__r0   r0   rM   r1   r$   >   s  
 T
7

>
	
	
0	
Fc                       s   e Zd ZdZdddeee  ddf fddZdeddfd	d
Z	e
ddddedee deeeef  dedee dededeeedf dedefddZe
dededededef
ddZe
dededededef
ddZ  ZS ) PyTorchModelHubMixina  
    Implementation of [`ModelHubMixin`] to provide model Hub upload/download capabilities to PyTorch models. The model
    is set in evaluation mode by default using `model.eval()` (dropout modules are deactivated). To train the model,
    you should first set it back in training mode with `model.train()`.

    Example:

    ```python
    >>> import torch
    >>> import torch.nn as nn
    >>> from huggingface_hub import PyTorchModelHubMixin

    >>> class MyModel(
    ...         nn.Module,
    ...         PyTorchModelHubMixin,
    ...         library_name="keras-nlp",
    ...         repo_url="https://github.com/keras-team/keras-nlp",
    ...         docs_url="https://keras.io/keras_nlp/",
    ...         # ^ optional metadata to generate model card
    ...     ):
    ...     def __init__(self, hidden_size: int = 512, vocab_size: int = 30000, output_size: int = 4):
    ...         super().__init__()
    ...         self.param = nn.Parameter(torch.rand(hidden_size, vocab_size))
    ...         self.linear = nn.Linear(output_size, vocab_size)

    ...     def forward(self, x):
    ...         return self.linear(x + self.param)
    >>> model = MyModel(hidden_size=256)

    # Save model weights to local directory
    >>> model.save_pretrained("my-awesome-model")

    # Push model weights to the Hub
    >>> model.push_to_hub("my-awesome-model")

    # Download and initialize weights from the Hub
    >>> model = MyModel.from_pretrained("username/my-awesome-model")
    >>> model.hidden_size
    256
    ```
    N)r(   r(   r8   c                   s,   |pg }| d ||d< t j|i |S )NZpytorch_model_hub_mixinr(   )rE   rC   rD   )rL   r(   rZ   r[   rM   r0   r1   rD   g  s   
z&PyTorchModelHubMixin.__init_subclass__r_   c                 C   s*   t | dr| jn| }t|t|t  dS )z7Save weights from a Pytorch model to a local directory.moduleN)hasattrr   save_model_as_safetensorr.   r   )rp   r_   Zmodel_to_saver0   r0   r1   rh   m  s   z%PyTorchModelHubMixin._save_pretrainedcpuF)map_locationstrictr   r{   ry   ru   rw   rv   rz   rx   r   r   c       
         K   s   | di |}t j|r td t j|t}| |||	|
S zt|t|||||||d	}| |||	|
W S  tyT   t|t	|||||||d	}| 
|||	|
 Y S w )z<Load Pytorch pretrained weights and return the loaded model.z$Loading weights from local directoryr}   Nr0   )r   r   r   printr   r   _load_as_safetensorr   r   r   _load_as_pickle)rL   r   r{   ry   ru   rw   rv   rz   rx   r   r   r   r   
model_filer0   r0   r1   rK   r  s@   z%PyTorchModelHubMixin._from_pretrainedr   r   c                 C   s.   t j|t |d}|j||d |  |S )N)r   r   )torchr   ZdeviceZload_state_dicteval)rL   r   r   r   r   Z
state_dictr0   r0   r1   r     s   z$PyTorchModelHubMixin._load_as_picklec                 C   s.   t |||d |dkrtd || |S )Nr   r   ak  Loading model weights on other devices than 'cpu' is not supported natively. This means that the model is loaded on 'cpu' first and then copied to the device. This leads to a slower loading time. Support for loading directly on other devices is planned to be added in future releases. See https://github.com/huggingface/huggingface_hub/pull/2086 for more details.)load_model_as_safetensorr   r   to)rL   r   r   r   r   r0   r0   r1   r     s   
z(PyTorchModelHubMixin._load_as_safetensor)r+   r,   r-   r   r
   r	   r.   rD   r   rh   r   r   r   r   rK   r#   r   r   r   r0   r0   rM   r1   r   <  s@    $*	
1 (r   dataclsr    datar8   c                    s     di  fdd|  D S )zhLoad a dataclass instance from a dictionary.

    Fields not expected by the dataclass are ignored.
    c                    s    i | ]\}}| j v r||qS r0   )r   )r?   kvr   r0   r1   rA     s     z#_load_dataclass.<locals>.<dictcomp>Nr0   )rV   )r   r   r0   r   r1   r     s    r   );r:   rj   r   dataclassesr   r   r   pathlibr   typingr   r   r   r	   r
   r   r   r   r   	constantsr   r   r   Zfile_downloadr   Zhf_apir   Zrepocardr   r   utilsr   r   r   r   r   r   r   r   Zutils._deprecationr   Z	_typeshedr    r   Zsafetensors.torchr!   r   r"   r   Z
get_loggerr+   r   r#   r   r&   r$   r   rF   r   r0   r0   r0   r1   <module>   s>    ,(

    