
    1;jid                     x   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  Z> e            j?        Z@dMdee
eAdf         deAfdZB	 	 	 	 	 	 	 	 	 	 dNd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 ZEdMd!ed"eeAeeA         f         defd#ZFdMd$eeA         d%eeA         fd&ZGejH        I                     ejJ        d'ejH        K                     ejJ        d(d)          d*                              ZLejH        K                    eLd+          ZMdOd,eeA         d-eeA         ddfd.ZNejH        K                    ed/          ZOejH        P                    eO          sd ZQnO eReO          5 ZS	  eTeSU                                          ZQn# eV$ r d ZQY nw xY wddd           n# 1 swxY w Y   eQdk     rejH        W                    eM          o eX ejY        eM                    d k    ZZeZre;[                    d0           	  eN             nc# e\$ r[Z]d1K                     ej^        e]j_                            Z`e;a                    d2e` d1e]jb        j:         d3e] d4           Y dZ][]ndZ][]ww xY weQdk     rq	  ejc        ed56            eReOd7          5 ZSeSd                    d8           ddd           n# 1 swxY w Y   n$# e\$ r e;[                    d9e d:           Y nw xY wdMd;eAd<eeA         deAfd=Zee#ddddddddddd>
d?eeAef         d;eAd@eeA         dAeeA         dBeCdCee
         dDeeC         dEeCdeeA         deee
eAf                  dFeeA         d%eeA         fdG            ZfdH Zg	 	 	 	 	 	 	 	 dPdJZh G dK dL          ZidS )Q    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                    dt            dt          j                                        d          dt           }t
          st          r|dz   S t                      r|dt           z  }t                      r|dt           z  }|dt           z  }t                      r|d	t           z  }t          j                            d
d                                          t$          v r|dz  }t'          | t(                    r8|dd                    d |                                 D                       z   z  }nt'          | t.                    r|d| z   z  }|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/DIFFUSERS_IS_CI z; is_ci/truez; c              3   *   K   | ]\  }}| d | V  dS )/N ).0kvs      S/root/voice-cloning/.venv/lib/python3.11/site-packages/diffusers/utils/hub_utils.py	<genexpr>z"http_user_agent.<locals>.<genexpr>]   s0      IIda!zzazzIIIIII    )r   sysr   split
SESSION_IDr   r   r)   r&   r'   r$   r#   r(   r%   osenvirongetupperr"   
isinstancedictjoinitemsstr)r+   uas     r6   http_user_agentrF   K   sk    
^k	]	]CK,=,=,?,?,B	]	]Q[	]	]B &> &%%% *
)))) (
%|%%%
'''' 6
53555	z~~',,22448LLL
n*d##  
dTYYIIj6F6F6H6HIIIIIII	J	$	$  
dZIr8   Frepo_id_or_pathtokenis_pipelinefrom_trainingmodel_description
base_modelpromptlicensewidget	inferencec
                 f   t                      st          d          	 t          j        | |          }
n{# t          t
          f$ rg |r0t          j        t          |d|	|||          t          |          }
n2t                      }|rdnd}|d	| d
}t          j        ||          }
Y nw xY w|
S )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.
    zModelcard 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`.rH   	diffusers)rN   library_namerP   rL   instance_promptrO   )	card_datatemplate_pathrK   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.)rK   )	r   
ValueErrorr	   loadr   r   from_templater
   MODEL_CARD_TEMPLATE_PATH)rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   
model_cardrV   	components                r6   load_or_create_model_cardr`   c   s   F  
?
 
 	
a^O5AAA

 78 a a a 	a"0'#!,')$*!   7"3  JJ &I&1>

wI ( %qR[  %q  %q  %q!"0N_```J)a, s   6 A5B.-B.r^   tagsc                     | j         j        d| j         _        |Tt          |t                    r|g}| j         j        g | j         _        |D ]!}| j         j                            |           "| S )z?Populates the `model_card` with library name and optional tags.NrS   )datarT   r@   rD   ra   append)r^   ra   tags      r6   populate_model_cardrf      s|    #+'2
$dC   	6D?'#%JO  	- 	-CO '',,,,r8   resolved_filecommit_hashc                     | ||S t          t          |                                                     } t          j        d|           }|dS |                                d         }t          j        |          r|ndS )zP
    Extracts the commit hash from a resolved filename toward a cache file.
    Nzsnapshots/([^/]+)/r   )rD   r   as_posixresearchgroupsr   match)rg   rh   rl   s      r6   extract_commit_hashro      s~      7]++446677MY,m<<F~t--//!$K+1+>>H;;DHr8   HF_HOMEXDG_CACHE_HOMEz~/.cachehuggingfacerS   old_cache_dirnew_cache_dirc                 B   |t           }| t          } t          |                                           } t          |                                          }|                     d          D ]}|                                r|                                s||                    |           z  }|j        	                    dd           t          j        ||           	 t          j        ||           # t          $ r t                              d           Y w xY wd S )Nz
**/blobs/*T)parentsexist_okzCould 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mkdirr<   replacesymlinkOSErrorloggerwarning)rs   rt   old_blob_pathnew_blob_paths       r6   
move_cacher      s:   $+''2244M''2244M&++L99 
 
  "" 		=+C+C+E+E 		)M,E,Em,T,TTM &&td&CCCJ}m444
=-8888    ]    
 
s   C44$DDzversion_diffusers_cache.txtzThe 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)rw   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|                      d          }|d d         |gz   |dd          z   }d                    |          } | S )N.)r:   rB   )r   r   splitss      r6   _add_variantr     sS    ##C((y(6"##;6xx''r8   )
	subfolder	cache_dirforce_downloadproxiesresume_downloadlocal_files_onlyrH   r+   revisionrh   pretrained_model_name_or_pathr   r   r   r   r   r   r   c                L   t          |           } t          j                            |           r| S t          j                            |           rt          j                            t          j                            | |                    r"t          j                            | |          }|S |at          j                            t          j                            | ||                    r#t          j                            | ||          }|S t          d| d|  d          |
t          v r|t          k    s|t          k    rt          j        t          j        t                    j                  t          j        d          k    r	 t          | t          ||
          |||||||	||
p|          }t!          j        d|
 d|  d|
 d	|
 d
|
 d|
 dt$                     |S #  t!          j        d|
 d|  d|
 d|
 d|  dt          ||
           d|  d|  dt          ||
           dt$                     Y nxY w	 t          | ||||||||	||
p|          }|S # t&          $ r t          |  d          t(          $ r t          |
 d|  d          t*          $ r t          |  d| d          t,          $ r}t          d|  d|           d }~wt.          $ r t          dt0           d|  d| d          t          $ r t          d|  d |  d!|           w xY w)"NzError no file named z found in directory r   z0.22.0)
filenamer   r   r   r   r   rH   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 )rD   r<   pathisfileisdirrB   EnvironmentErrorr   r!   r    r   parser   base_versionr   r   warningswarnFutureWarningr   r   r   r   rZ   r   )r   r   r   r   r   r   r   r   rH   r+   r   rh   
model_fileerrs                 r6   _get_model_filer     s<     %((E$F$F!	w~~344 c,,	4	5	5 a7>>"',,'DlSSTT 	&C\RRJ"rw~~GLL6	<PP(
 (
" &CYP\]]J"i|iiIfiii   000--AY1Y1YgmK88EFF'-X`JaJaaa,1),AA'#1#$3%5)'%4  
  G8  G  G;X  G  Gjr  G  G  |D  G  G  ow  G  G  rz  G  G  G!   "! E8  E  EC`  E  Erz  E  E  [c  E  E  Jg  E  E  DP  Q]  _g  Dh  Dh  E  E  He  E  E  D	a	  E  E  o	{	  |	H
  J
R
  o	S
  o	S
  E  E  E!    4	(-%#- /!1%#!0[  J & 	 	 	"0      % 	 	 	" e e+He e e  
 " 	 	 	"0ggXdggg    	 	 	"tMjttortt    	 	 	"f+J f f:Wf f6Bf f f     	 	 	":-J : :)F: : ,8: :  	s(   AG( (AH>I   A!L#KAL#c                     t           j                            | |          fd|D             }|D ]6}t           j                            |          st	           d| d          7d S )Nc                 P    g | ]"}t           j                            |          #S r2   )r<   r   rB   )r3   fshards_paths     r6   
<listcomp>z2_check_if_shards_exist_locally.<locals>.<listcomp>  s)    VVVrw||K33VVVr8   r   5 which is required according to the checkpoint index.)r<   r   rB   existsrZ   )	local_dirr   original_shard_filenamesshard_filenames
shard_filer   s        @r6   _check_if_shards_exist_locallyr     s    ',,y)44KVVVV=UVVVO%  
w~~j)) 	 > >j > > >  	 r8   r/   c
                 "   t           j                            |          st          d| d|  d          t	          |d          5 }
t          j        |
                                          }ddd           n# 1 swxY w Y   t          t          |d         
                                                    }|d         }t          |d                                                   |d<   |d                                         |d<   t           j                            | |	          }t           j                            |           rt!          | |	|	           | |fS |}d
dg}|sKt#          |           }|D ]9t%          fd|j        D                       }|st)          | d d          :	 t+          | |||||||||
  
        }n.# t,          $ r!}t)          dt.           d|  d          |d}~ww xY w|rt!          ||	|           ||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   *   K   | ]}|j         v V  d S N)	rfilename)r3   r4   r   s     r6   r7   z._get_checkpoint_shard_files.<locals>.<genexpr>  s*      $b$b1Z1;%>$b$b$b$b$b$br8   r   r   )	r   r   r   r   rH   r   allow_patternsignore_patternsr+   r   z
' to load z?. You should try again after checking your internet connection.)r   r   r   )r<   r   r   rZ   openjsonloadsreadsortedsetvalueslistkeyscopyrB   r   r   r   anysiblingsr   r   r   r   )r   index_filenamer   r   r   r   rH   r+   r   r   r   indexr   sharded_metadatar   r   r   model_files_infoshard_file_presentcached_folderer   s                        @r6   _get_checkpoint_shard_filesr     s   , 7>>.)) rp>ppPmpppqqq	nc	"	" %a
16688$$% % % % % % % % % % % % % % %  &c%*=*D*D*F*F&G&GHHZ(.253F3K3K3M3M.N.N*+%*<%8%=%=%?%?\"',,<iHHK 
w}}233 ?&)YYq	
 	
 	
 	
 -.>>> .N(O 	%&CDD2 	 	J!$$b$b$b$bHXHa$b$b$b!b!b% &" B B* B B B  ))+-)+!
 
 
    >'F > >Ro > > >
 
 	  
&9Og	
 	
 	
 	
 ***s*   'A::A>A>1G 
G6G11G6c                       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	Z
	 	 	 	 	 	 ddedee         dee	         dee         de	de	dee         defdZdS )PushToHubMixinzR
    A Mixin to push a model, scheduler, or pipeline to the Hugging Face Hub.
    NFworking_dirrepo_idrH   commit_message	create_prc                     |1d| j         j        v rd}n d| j         j        v rd}nd| j         j         }t                              d| d| d	           t	          |||||
          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_pathrH   r   r   )	__class____name__r   infor   )selfr   r   rH   r   r   s         r6   _upload_folderzPushToHubMixin._upload_folder  s     !$.111!/ 777!3!D4>+B!D!DIkIIwIIIJJJER`lu
 
 
 	
r8   Tprivatesafe_serializationr   r,   c                    t          |||d          j        }t          ||          }t          |          }d|i}	d| j        j        vr|	                    d|i           t          j                    5 }
 | j	        |
fi |	 |
                    t          j                            |
d                     |                     |
||||          cd	d	d	           S # 1 swxY w 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   rH   rw   rR   r   r   r   z	README.md)rH   r   r   N)r   r   r`   rf   r   r   updatetempfileTemporaryDirectorysave_pretrainedsaver<   r   rB   r   )r   r   r   r   rH   r   r   r   r^   save_kwargstmpdirs              r6   push_to_hubzPushToHubMixin.push_to_hub  sK   \ gwedSSS[ /weDDD
(44
 ,-?@dn555	73444(** 	f D 77;777 OOBGLL==>>>&&-# '  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   5ACC!$C!)NNF)NNNFTN)r   
__module____qualname____doc__r   rD   r<   PathLiker   boolr   r   r2   r8   r6   r   r     s           $(,
 
3+,
 
 }	

 !
 
 
 
 
6 )-"&##'!%E EE !E $	E
 }E E !E #E 
E E E E E Er8   r   r   )
NNFFNNNNNN)NN)NNFFNNNr/   )jr   r<   rk   r9   r   	tracebackr   pathlibr   typingr   r   r   r   uuidr   huggingface_hubr	   r
   r   r   r   r   r   huggingface_hub.constantsr   r   r   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   r   __file__r~   r]   hexr;   rD   rF   r   rA   r`   rf   ro   r   ry   getenvrB   hf_cache_homerx   r   cache_version_filer   cache_versionr   r   intr   rZ   r   lenlistdirold_cache_is_not_emptyr   	Exceptionr   	format_tb__traceback__traceerrorr   makedirswriter   r   r   r   r   r2   r8   r6   <module>r     s1	  "  				 				 



             . . . . . . . . . . . .                        ] \ \ \ \ \ \ \ \ \ ; ; ; ; ; ;                                          	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	        
H		4>>03KK UWW[
 dCo 6 #    2  '+ !#' $C CCC=C C 	C
  }C C SMC c]C T$Z C ~C C C C CL I U3S	>5J V_     I Ix} I8C= I I I I$ ""BIiibi0@*&M&M}]]^^  gll=+>>  hsm 8C= \`    , W\\,0MNN 	w~~()) MM	 	!	! Q	CMMMM 	 	 	MMM	               1W]]+>??lCC

SfHgHgDhDhklDl I	
 	
 	

	JLLLL 	 	 	II1i1!/BBCCELL;% ; ;STS^Sg ; ;kl ; ; ;       	 1
L40000T$c** 	aGGCLLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
 
 
:l : : :	
 	
 	
 	
 	

 s Xc] c     
  $# "&*"-1"!%s s s#(d#3s s }	s
 }s s d^s d^s s C=s tSy)*s sms #s s s sv   
T+ T+ T+ T+nb b b b b b b b b bs   I!H?>I?I	II		III8
K L#ALL#-M9 M-!M9 -M11M9 4M15M9 9NN