o
    ¦ôjd  ã                   @   sÖ  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
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 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# d d	l$m%Z% d d
l&m'Z' ddl(m)Z) ddl*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 ddl8m9Z9 e9e:ƒZ;ee<ƒj=d Z>eƒ j?Z@dVdee
eAdf deAfdd„ZB										dWdeAdeeA deCdeCdeeA deAdeeA deeA deeeD  d eeC defd!d"„ZEdVd#ed$eeAeeA f defd%d&„ZFdVd'eeA d(eeA fd)d*„ZGejH Ie Jd+ejH Ke Jd,d-¡d.¡¡¡ZLejH KeLd/¡ZMdXd0eeA d1eeA ddfd2d3„ZNejH Ked4¡ZOejH PeO¡�sId ZQn*eReOƒ�ZSzeTeS U¡ ƒZQW n eV�yb   d ZQY nw W d  ƒ n	1 �snw   Y  eQdk �rÃejH WeM¡�o‡eXe YeM¡ƒd kZZeZ�rÃe; [d5¡ zeNƒ  W n- e\�yÂ Z] z d6 Ke ^e]j_¡¡Z`e; ad7e`› d6e]jbj:› d8e]› d9�¡ W Y dZ][]ndZ][]ww eQdk �r z$ejced:d;� eReOd<ƒ�ZSeS dd=¡ W d  ƒ n	1 �sæw   Y  W n e\�yÿ   e; [d>e› d?�¡ Y nw dVd@eAdAeeA deAfdBdC„Zee#dddddddddddDœ
dEeeAef d@eAdFeeA dGeeA dHeCdIee
 dJeeC dKeCdeeA deee
eAf  dLeeA d(eeA fdMdN„ƒZfdOdP„ Zg								QdYdRdS„ZhG dTdU„ dUƒZidS )Zé    N)ÚPath)ÚDictÚListÚOptionalÚUnion)Úuuid4)Ú	ModelCardÚModelCardDataÚcreate_repoÚhf_hub_downloadÚ
model_infoÚsnapshot_downloadÚupload_folder)ÚHF_HUB_CACHEÚHF_HUB_DISABLE_TELEMETRYÚHF_HUB_OFFLINE)ÚREGEX_COMMIT_HASH)ÚEntryNotFoundErrorÚRepositoryNotFoundErrorÚRevisionNotFoundErrorÚis_jinja_availableÚvalidate_hf_hub_args)Úversion)Ú	HTTPErroré   )Ú__version__é   )ÚDEPRECATED_REVISION_ARGSÚHUGGINGFACE_CO_RESOLVE_ENDPOINTÚSAFETENSORS_WEIGHTS_NAMEÚWEIGHTS_NAME)ÚENV_VARS_TRUE_VALUESÚ_flax_versionÚ_jax_versionÚ_onnxruntime_versionÚ_torch_versionÚis_flax_availableÚis_onnx_availableÚis_torch_available)Ú
get_loggerzmodel_card_template.mdÚ
user_agentÚreturnc                 C   sà   dt › dtj ¡ d › dt› �}tstr|d S tƒ r"|dt› �7 }t	ƒ r3|dt
› �7 }|dt› �7 }tƒ r=|d	t› �7 }tj d
d¡ ¡ tv rL|d7 }t| tƒrc|dd dd„ |  ¡ D ƒ¡ 7 }|S t| tƒrn|d|  7 }|S )zF
    Formats a user-agent string with basic info about a request.
    z
diffusers/z	; python/r   z; session_id/z; telemetry/offz; torch/z; jax/z; flax/z; onnxruntime/ZDIFFUSERS_IS_CIÚ z; is_ci/truez; c                 s   s"   � | ]\}}|› d |› �V  qdS )ú/N© )Ú.0ÚkÚvr.   r.   úl/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/diffusers/utils/hub_utils.pyÚ	<genexpr>]   s   €  z"http_user_agent.<locals>.<genexpr>)r   Úsysr   ÚsplitÚ
SESSION_IDr   r   r(   r%   r&   r#   r"   r'   r$   ÚosÚenvironÚgetÚupperr!   Ú
isinstanceÚdictÚjoinÚitemsÚstr)r*   Úuar.   r.   r2   Úhttp_user_agentK   s$    
 
þrA   FÚrepo_id_or_pathÚtokenÚis_pipelineÚfrom_trainingÚmodel_descriptionÚ
base_modelÚpromptÚlicenseÚwidgetÚ	inferencec
                 C   sš   t ƒ stdƒ‚z
tj| |d�}
W |
S  ttfyL   |r/tjt|d|	|||d�t|d�}
Y |
S tƒ }|r6dnd}|du rBd	|› d
�}tj||d�}
Y |
S w )aˆ  
    Loads or creates a model card.

    Args:
        repo_id_or_path (`str`):
            The repo id (e.g., "runwayml/stable-diffusion-v1-5") or local path where to look for the model card.
        token (`str`, *optional*):
            Authentication token. Will default to the stored token. See https://huggingface.co/settings/token for more
            details.
        is_pipeline (`bool`):
            Boolean to indicate if we're adding tag to a [`DiffusionPipeline`].
        from_training: (`bool`): Boolean flag to denote if the model card is being created from a training script.
        model_description (`str`, *optional*): Model description to add to the model card. Helpful when using
            `load_or_create_model_card` from a training script.
        base_model (`str`): Base model identifier (e.g., "stabilityai/stable-diffusion-xl-base-1.0"). Useful
            for DreamBooth-like training.
        prompt (`str`, *optional*): Prompt used for training. Useful for DreamBooth-like training.
        license: (`str`, *optional*): License of the output artifact. Helpful when using
            `load_or_create_model_card` from a training script.
        widget (`List[dict]`, *optional*): Widget to accompany a gallery template.
        inference: (`bool`, optional): Whether to turn on inference widget. Helpful when using
            `load_or_create_model_card` from a training script.
    zµModelcard rendering is based on Jinja templates. Please make sure to have `jinja` installed before using `load_or_create_model_card`. To install it, please run `pip install Jinja2`.©rC   Ú	diffusers)rI   Úlibrary_namerK   rG   Zinstance_promptrJ   )Ú	card_dataÚtemplate_pathrF   ÚpipelineÚmodelNu+   This is the model card of a ðŸ§¨ diffusers zS that has been pushed on the Hub. This model card has been automatically generated.)rF   )	r   Ú
ValueErrorr   Úloadr   r   Úfrom_templater	   ÚMODEL_CARD_TEMPLATE_PATH)rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   Ú
model_cardrO   Ú	componentr.   r.   r2   Úload_or_create_model_cardc   s:   #ÿêúöúêrY   rW   Útagsc                 C   s\   | j jdu r
d| j _|dur,t|tƒr|g}| j jdu r g | j _|D ]	}| j j |¡ q"| S )z?Populates the `model_card` with library name and optional tags.NrM   )ÚdatarN   r;   r?   rZ   Úappend)rW   rZ   Útagr.   r.   r2   Úpopulate_model_card©   s   
r^   Úresolved_fileÚ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)r_   r`   rc   r.   r.   r2   Úextract_commit_hash¹   s   rf   ÚHF_HOMEÚXDG_CACHE_HOMEz~/.cacheÚhuggingfacerM   Úold_cache_dirÚnew_cache_dirc              	   C   s¬   |d u rt }| d u rt} t| ƒ ¡ } t|ƒ ¡ }|  d¡D ]6}| ¡ rS| ¡ sS|| | ¡ }|jj	ddd� t
 ||¡ zt
 ||¡ W q tyR   t d¡ Y qw qd S )Nz
**/blobs/*T)ÚparentsÚexist_okz†Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.)r   Úold_diffusers_cacher   Ú
expanduserÚglobÚis_fileÚ
is_symlinkÚrelative_toÚparentÚmkdirr7   ÚreplaceÚsymlinkÚOSErrorÚloggerÚwarning)rj   rk   Zold_blob_pathZnew_blob_pathr.   r.   r2   Ú
move_cacheÑ   s(   ÿÿ€ùr{   zversion_diffusers_cache.txtzçThe cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your existing cached models. This is a one-time operation, you can interrupt it or run it later by calling `diffusers.utils.hub_utils.move_cache()`.Ú
z5There was a problem when trying to move your cache:

z: z”

Please file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole message and we will do our best to help.T)rm   ÚwÚ1z?There was a problem when trying to write in your cache folder (z=). Please, ensure the directory exists and can be written to.Úweights_nameÚvariantc                 C   s>   |d ur|   d¡}|d d… |g |dd …  }d |¡} | S )NÚ.éÿÿÿÿ)r5   r=   )r   r€   Úsplitsr.   r.   r2   Ú_add_variant  s
   

r„   )
Ú	subfolderÚ	cache_dirÚforce_downloadÚproxiesÚresume_downloadÚlocal_files_onlyrC   r*   Úrevisionr`   Úpretrained_model_name_or_pathr…   r†   r‡   rˆ   r‰   rŠ   r‹   c                C   sŠ  t | ƒ} tj | ¡r| S tj | ¡rKtj tj | |¡¡r&tj | |¡}|S |d ur@tj tj | ||¡¡r@tj | ||¡}|S td|› d| › d�ƒ‚|
tv rÂ|tksW|t	krÂt
 t
 t¡j¡t
 d¡krÂz0t| t||
ƒ|||||||	||
pw|d�}t d|
› d| › d|
› d	|
› d
|
› d|
› d�t¡ |W S    t d|
› d| › d|
› d|
› d| › dt||
ƒ› d| › d| › dt||
ƒ› d�t¡ Y zt| ||||||||	||
pÑ|d�}|W S  tyä   t| › d�ƒ‚ tyô   t|
› d| › d�ƒ‚ t�y   t| › d|› d�ƒ‚ t�y } z
td| › d|› �ƒ‚d }~w t�y0   tdt› d| › d|› d�ƒ‚ t�yD   td| › d | › d!|› �ƒ‚w )"NzError no file named z found in directory r�   z0.22.0)
Úfilenamer†   r‡   rˆ   r‰   rŠ   rC   r*   r…   r‹   zLoading the variant z from z via `revision='zG'` is deprecated. Loading instead from `revision='main'` with `variant=z)`. Loading model variants via `revision='z9'` will be removed in diffusers v1. Please use `variant='z'` instead.zYou are loading the variant z^'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant='z%'` instead. However, it appears that z currently does not have a z file in the 'main' branch of zœ. 
 The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title 'z is missing z0' so that the correct variant file can be added.zó 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 with `token` or log in with `huggingface-cli login`.z• 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/z' for available revisions.ú& does not appear to have a file named z:There was a specific connection error when trying to load z:
úWe couldn't connect to 'zM' to load this model, 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š or 
Checkout your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/diffusers/installation#offline-mode'.zCan't load the model for 'zœ'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'z=' is the correct path to a directory containing a file named )r?   r7   ÚpathÚisfileÚisdirr=   ÚEnvironmentErrorr   r    r   r   Úparser   Úbase_versionr   r„   ÚwarningsÚwarnÚFutureWarningr   r   r   r   rS   r   )rŒ   r   r…   r†   r‡   rˆ   r‰   rŠ   rC   r*   r‹   r`   Ú
model_fileÚerrr.   r.   r2   Ú_get_model_file  sº   ÿÿõ(þFþõÿþÿÿÿ€ÿþÿþýÿÿr›   c                    sN   t j | |¡‰ ‡ fdd„|D ƒ}|D ]}t j |¡s$tˆ › d|› d�ƒ‚qd S )Nc                    s   g | ]	}t j ˆ |¡‘qS r.   )r7   r�   r=   )r/   Úf©Úshards_pathr.   r2   Ú
<listcomp>–  s    z2_check_if_shards_exist_locally.<locals>.<listcomp>rŽ   ú5 which is required according to the checkpoint index.)r7   r�   r=   ÚexistsrS   )Ú	local_dirr…   Úoriginal_shard_filenamesÚshard_filenamesÚ
shard_filer.   r�   r2   Ú_check_if_shards_exist_locally”  s   ÿÿÿr¦   r,   c
                    sˆ  t j |¡std|› d| › d�ƒ‚t|dƒ�}
t |
 ¡ ¡}W d  ƒ n1 s(w   Y  tt	|d  
¡ ƒƒ}|d }t|d  ¡ ƒ|d< |d  ¡ |d< t j | |	¡}t j | ¡ret| |	|d	� | |fS |}d
dg}|sŽt| ƒ}|D ]‰ t‡ fdd„|jD ƒƒ}|s�t|› dˆ › d�ƒ‚qszt| |||||||||d�
}W n ty¶ } ztdt› d| › d�ƒ|‚d}~ww |rÀ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).
    zCan't find a checkpoint index (z) in r�   ÚrNÚ
weight_mapÚmetadataÚall_checkpoint_keys)r…   r£   z*.jsonz*.mdc                 3   s   � | ]}ˆ |j v V  qd S ©N)Ú	rfilename)r/   r0   ©r¥   r.   r2   r3   Ï  s   € z._get_checkpoint_shard_files.<locals>.<genexpr>rŽ   r    )	r†   r‰   rˆ   rŠ   rC   r‹   Úallow_patternsÚignore_patternsr*   r�   z
' to load z?. You should try again after checking your internet connection.)r¢   r…   r£   )r7   r�   r‘   rS   ÚopenÚjsonÚloadsÚreadÚsortedÚsetÚvaluesÚlistÚkeysÚcopyr=   r’   r¦   r   ÚanyÚsiblingsr“   r   r   r   )rŒ   Úindex_filenamer†   rˆ   r‰   rŠ   rC   r*   r‹   r…   rœ   Úindexr£   Úsharded_metadatarž   r®   r¯   Zmodel_files_infoZshard_file_presentZcached_folderÚer.   r­   r2   Ú_get_checkpoint_shard_filesŸ  sh   ÿÿÿÿ
öÿý€ÿÿrÀ   c                   @   s�   e Zd ZdZ			ddeeejf dedee dee de	f
d	d
„Z
						ddedee dee	 dee de	de	dee defdd„ZdS )ÚPushToHubMixinzR
    A Mixin to push a model, scheduler, or pipeline to the Hugging Face Hub.
    NFÚworking_dirÚrepo_idrC   Úcommit_messageÚ	create_prc                 C   sd   |du rd| j jv rd}nd| j jv rd}nd| j j› �}t d|› d|› d	�¡ t|||||d
�S )zB
        Uploads all files in `working_dir` to `repo_id`.
        NÚModelzUpload modelÚ	SchedulerzUpload schedulerzUpload zUploading the files of z to r�   )rÃ   Úfolder_pathrC   rÄ   rÅ   )Ú	__class__Ú__name__ry   Úinfor   )ÚselfrÂ   rÃ   rC   rÄ   rÅ   r.   r.   r2   Ú_upload_folderû  s   
ÿzPushToHubMixin._upload_folderTÚprivateÚsafe_serializationr€   r+   c                 C   s®   t |||dd�j}t||d�}t|ƒ}d|i}	d| jjvr$|	 d|i¡ t ¡ �%}
| j	|
fi |	¤Ž | 
tj |
d¡¡ | j|
||||d�W  d	  ƒ S 1 sPw   Y  d	S )
uû  
        Upload model, scheduler, or pipeline files to the ðŸ¤— Hugging Face Hub.

        Parameters:
            repo_id (`str`):
                The name of the repository you want to push your model, scheduler, or pipeline files to. It should
                contain your organization name when pushing to an organization. `repo_id` can also be a path to a local
                directory.
            commit_message (`str`, *optional*):
                Message to commit while pushing. Default to `"Upload {object}"`.
            private (`bool`, *optional*):
                Whether or not the repository created should be private.
            token (`str`, *optional*):
                The token to use as HTTP bearer authorization for remote files. The token generated when running
                `huggingface-cli login` (stored in `~/.huggingface`).
            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 to the `safetensors` format.
            variant (`str`, *optional*):
                If specified, weights are saved in the format `pytorch_model.<variant>.bin`.

        Examples:

        ```python
        from diffusers import UNet2DConditionModel

        unet = UNet2DConditionModel.from_pretrained("stabilityai/stable-diffusion-2", subfolder="unet")

        # Push the `unet` to your namespace with the name "my-finetuned-unet".
        unet.push_to_hub("my-finetuned-unet")

        # Push the `unet` to an organization with the name "my-finetuned-unet".
        unet.push_to_hub("your-org/my-finetuned-unet")
        ```
        T)rÎ   rC   rm   rL   rÏ   rÇ   r€   z	README.md)rC   rÄ   rÅ   N)r
   rÃ   rY   r^   rÉ   rÊ   ÚupdateÚtempfileÚTemporaryDirectoryÚsave_pretrainedÚsaver7   r�   r=   rÍ   )rÌ   rÃ   rÄ   rÎ   rC   rÅ   rÏ   r€   rW   Zsave_kwargsÚtmpdirr.   r.   r2   Úpush_to_hub  s"   .
û$úzPushToHubMixin.push_to_hub)NNF)NNNFTN)rÊ   Ú
__module__Ú__qualname__Ú__doc__r   r?   r7   ÚPathLiker   ÚboolrÍ   rÖ   r.   r.   r.   r2   rÁ   ö  sN    úþýüû
úøþýüûúùø	÷rÁ   r«   )
NNFFNNNNNN)NN)NNFFNNNr,   )jr±   r7   rb   r4   rÑ   Ú	tracebackr–   Úpathlibr   Útypingr   r   r   r   Úuuidr   Úhuggingface_hubr   r	   r
   r   r   r   r   Zhuggingface_hub.constantsr   r   r   Zhuggingface_hub.file_downloadr   Úhuggingface_hub.utilsr   r   r   r   r   Ú	packagingr   Úrequestsr   r,   r   Ú	constantsr   r   r   r    Úimport_utilsr!   r"   r#   r$   r%   r&   r'   r(   Úloggingr)   rÊ   ry   Ú__file__rt   rV   Úhexr6   r?   rA   rÛ   r<   rY   r^   rf   r�   ro   Úgetenvr=   Úhf_cache_homern   r{   Zcache_version_filer‘   Zcache_versionr°   rœ   Úintr³   rS   r’   ÚlenÚlistdirZold_cache_is_not_emptyrz   Ú	Exceptionr¿   Ú	format_tbÚ__traceback__ÚtraceÚerrorrÉ   ÚmakedirsÚwriter„   r›   r¦   rÀ   rÁ   r.   r.   r.   r2   Ú<module>   s,  $	(
öÿþýüûúùø
	÷
ö
õ$Fÿ 
ÿ€ý
 ÿ
ÿ€þ
ÿ€
ÿÿ	ó
ÿýüûúùø	÷
öõôó{
öW