
    1;jiN                        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m	Z	 ddl
mZmZmZ ddlmZ ddlmZmZ ddlmZmZ ddlmZ d	d
lmZ ddlmZmZmZ  ej        e          ZdZ d Z!d Z"dee#ej$        f         fdZ%d Z&d Z'd Z(d Z)d Z*e	 	 	 	 	 	 	 d$dee#ej$        f         de#deee#ej$        f                  de+dee+         deee#e#f                  deee+e#f                  dee#         d e+fd!            Z,e	 	 	 	 	 	 	 	 d%dee#ej$        f         de#d"ee#         deee#ej$        f                  de+dee+         deee#e#f                  deee+e#f                  dee#         d e+fd#            Z-dS )&z3Utilities to dynamically load objects from the Hub.    N)Path)DictOptionalUnion)request)hf_hub_download
model_info)RevisionNotFoundErrorvalidate_hf_hub_args)version   )__version__   )DIFFUSERS_DYNAMIC_MODULE_NAMEHF_MODULES_CACHEloggingz$diffusers/community-pipelines-mirrorc                      d} t          j        t          j        |                                                     d                                         }t          |d           S )Nz$https://pypi.org/pypi/diffusers/jsonreleasesc                 *    t          j        |           S )N)r   Version)xs    _/root/voice-cloning/.venv/lib/python3.11/site-packages/diffusers/utils/dynamic_modules_utils.py<lambda>z(get_diffusers_versions.<locals>.<lambda>-   s    '/!*<*<     )key)jsonloadsr   urlopenreadkeyssorted)urlr   s     r   get_diffusers_versionsr#   *   sT    
0Cz'/#..335566zBGGIIH( < <====r   c                  0   t           t          j        v rdS t          j                            t                      t	          j        t           d           t          t                     dz  } |                                 s|                                  dS dS )z_
    Creates the cache directory for modules with an init, and adds it to the Python path.
    NTexist_ok__init__.py)	r   syspathappendosmakedirsr   existstouch)	init_paths    r   init_hf_modulesr0   0   s    
 38##HOO$%%%K 40000%&&6I  r   namec                 4   t                       t          t                    | z  }|j                                        st          |j                   t          j        |d           |dz  }|                                s|                                 dS dS )zF
    Creates a dynamic module in the cache directory for modules.
    Tr%   r'   N)	r0   r   r   parentr-   create_dynamic_moduler+   r,   r.   )r1   dynamic_module_pathr/   s      r   r4   r4   ?   s     /0047%,,.. :18999K#d3333#m3I  r   c                 @   t          | dd          5 }|                                }ddd           n# 1 swxY w Y   t          j        d|t          j                  }|t          j        d|t          j                  z  }t          t          |                    S )z
    Get the list of modules that are relatively imported in a module file.

    Args:
        module_file (`str` or `os.PathLike`): The module file to inspect.
    rutf-8encodingNz^\s*import\s+\.(\S+)\s*$flagsz^\s*from\s+\.(\S+)\s+import)openr   refindall	MULTILINElistset)module_filefcontentrelative_importss       r   get_relative_importsrG   N   s     
k3	1	1	1 Q&&((               z"=wbl[[[
#A7RTR^____$%%&&&s   488c                 :   d}| g}g |sg }|D ]$}|                     t          |                     %t          |           j        fd|D             }fd|D             }d |D             }t	          |          dk    }                     |           |S )a)  
    Get the list of all files that are needed for a given module. Note that this function recurses through the relative
    imports (if a imports b and b imports c, it will return module files for b and c).

    Args:
        module_file (`str` or `os.PathLike`): The module file to inspect.
    Fc                 4    g | ]}t          |z            S  )str).0mmodule_paths     r   
<listcomp>z-get_relative_import_files.<locals>.<listcomp>s   s%    FFFQCa00FFFr   c                     g | ]}|v|	S rJ   rJ   )rL   rD   all_relative_importss     r   rO   z-get_relative_import_files.<locals>.<listcomp>t   s$    YYY!1DX;X;XA;X;X;Xr   c                     g | ]}| d S ).pyrJ   )rL   rD   s     r   rO   z-get_relative_import_files.<locals>.<listcomp>u   s    >>>Q)))>>>r   r   )extendrG   r   r3   len)rC   	no_changefiles_to_checknew_importsrD   new_import_filesrQ   rN   s         @@r   get_relative_import_filesrZ   `   s     I!]N  4 	8 	8A3A667777;''.FFFF+FFFYYYY'7YYY>>-=>>>())Q.	##N333  4  r   c                    t          | dd          5 }|                                }ddd           n# 1 swxY w Y   t          j        d|t          j                  }|t          j        d|t          j                  z  }d |D             }t          t          |                    }g }|D ]<}	 t          j        |           # t          $ r |
                    |           Y 9w xY wt          |          d	k    r<t          d
d                    |           dd                    |           d          t          |           S )zi
    Check if the current Python environment contains all the libraries that are imported in a file.
    r7   r8   r9   Nz^\s*import\s+(\S+)\s*$r;   z^\s*from\s+(\S+)\s+importc                 n    g | ]2}|                     d           |                    d           d         3S ).r   )
startswithsplit)rL   imps     r   rO   z!check_imports.<locals>.<listcomp>   s8    OOOS3>>#;N;NOsyy~~a OOOr   r   z\This modeling file requires the following packages that were not found in your environment: , z. Run `pip install  `)r=   r   r>   r?   r@   rA   rB   	importlibimport_moduleImportErrorr*   rU   joinrG   )filenamerD   rE   importsmissing_packagesr`   s         r   check_importsrk   }   s    
hg	.	.	. !&&((               j2G2<PPPGrz6r|TTTTGOOGOOOG 3w<<  G ) )	)#C(((( 	) 	) 	)##C(((((	) q  ]yy)**] ]?BxxHX?Y?Y] ] ]
 
 	

  )))s   4883CC*)C*c                     |                     t          j        j        d          }t	          j        |          }| t          |          S t          ||           S )zY
    Import a module on the cache directory for modules and extract a class from it.
    r]   )replacer+   r)   seprd   re   find_pipeline_classgetattr)
class_namerN   modules      r   get_class_in_modulers      sQ     %%bgk377K$[11F"6***6:&&&r   c                 t   ddl m} t          t          j        | t          j                            }d}|                                D ]n\  }}||j        k    r^t          ||          rN|j	        
                    d          d         dk    r*|&t          d|j         d|j         d	| d
|  d	          |}o|S )z
    Retrieve pipeline class that inherits from `DiffusionPipeline`. Note that there has to be exactly one class
    inheriting from `DiffusionPipeline`.
    r   )DiffusionPipelineNr]   r   	diffusersz#Multiple classes that inherit from z have been found: z, and z). Please make sure to define only one in )	pipelinesru   dictinspect
getmembersisclassitems__name__
issubclass
__module__r_   
ValueError)loaded_moduleru   cls_memberspipeline_classcls_nameclss         r   ro   ro      s   
 .-----w)-IIJJKN$**,, ! !#)2223 122 3$$S))!,;;) ):K:T ) )&/) )7?) )%) ) )  
 !Nr   Fpretrained_model_name_or_pathrC   	cache_dirforce_downloadresume_downloadproxiestokenrevisionlocal_files_onlyc	                 &
   t          |           } t          j                            | |          }	t          j                            |	          r|	}
d}n|                     d          dk    r,t                      }dd                    t          j        d          dd                   z   }|/|dd         |v r|nd	}t          
                    d
| d           n@||v rd| }n6|d	k    r|}n-t          d| dd                    |d	gz              d          	 t          t          d| d|  d||||          }
d}| dz   }n# t          $ r}t          d| d          |d}~wt          $ r# t                              d| d|  d            w xY w	 t          | |||||||          }
t          j                            dd                    |                     d                              }n0# t          $ r# t                              d| d|  d            w xY wt#          |
          }t$          t          j        j        z   |z   }t)          |           t+          t,                    |z  }|dk    s|dk    rt/          j        |
||z             |D ]}t3          |                    d                    dk    r|d                    |                    d                    }|                    d          d         }t          j                            ||z            st          j        ||z             | d}t/          j        t          j                            | |          ||z             npt9          | ||          j        }||z  }|t          j        j        z   |z   }t)          |           ||z                                  st3          |                    d                    dk    rT|                    d          d         }t          j                            ||z            st          j        ||z             t/          j        |
||z             |D ]}t3          |                    d                    dk    r(d                    |                    d                    }||z                                  st=          | | d|||||||	  	         t          j                            ||          S )at
  
    Prepares Downloads a module from a local folder or a distant repo and returns its path inside the cached
    Transformers module.

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

            - a string, the *model id* of a pretrained model configuration hosted inside a model repo on
              huggingface.co. Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced
              under a user or organization name, like `dbmdz/bert-base-german-cased`.
            - a path to a *directory* containing a configuration file saved using the
              [`~PreTrainedTokenizer.save_pretrained`] method, e.g., `./my_model_directory/`.

        module_file (`str`):
            The name of the module file containing the class to look for.
        cache_dir (`str` or `os.PathLike`, *optional*):
            Path to a directory in which a downloaded pretrained model configuration should be cached if the standard
            cache should not be used.
        force_download (`bool`, *optional*, defaults to `False`):
            Whether or not to force to (re-)download the configuration files and override the cached versions if they
            exist. resume_download:
                Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v1
                of Diffusers.
        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 `transformers-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.

    <Tip>

    You may pass a token in `token` if you are not logged in (`huggingface-cli login`) and want to use private or
    [gated models](https://huggingface.co/docs/hub/models-gated#gated-models).

    </Tip>

    Returns:
        `str`: The path to the module inside the cache.
    local/r   vr]   N   r   mainzDefaulting to latest_version: z`custom_revision`: z3 does not exist. Please make sure to choose one of ra   datasetrS   )repo_id	repo_typerh   r   r   r   r   gitz
Revision 'a  ' not found in the community pipelines mirror. Check available revisions on https://huggingface.co/datasets/diffusers/community-pipelines-mirror/tree/main. If you don't find the revision you are looking for, please open an issue on https://github.com/huggingface/diffusers/issues.zCould not locate the z inside )r   r   r   r   r   r   z--r   )r   r   r   r   r   r   r   r   r   )rK   r+   r)   rg   isfilecountr#   r   r_   loggerinfor   r   COMMUNITY_PIPELINES_MIRROR_IDr
   EnvironmentErrorerrorrk   r   rn   r4   r   r   shutilcopyrU   r-   r,   r	   shaget_cached_module_file)r   rC   r   r   r   r   r   r   r   module_file_or_urlresolved_module_file	submoduleavailable_versionslatest_versionemodules_neededfull_submodulesubmodule_pathmodule_neededmodule_foldercommit_hashs                        r   r   r      s   x %((E$F$F!&C[QQ	w~~()) ;1			&	,	,S	1	1Q	6	6355sxx(9#(>(>rr(BCCC )7);?Q)Q)Q~~W]HKKDDDDEEEE+++%8~~HHHH@h @ @II0F8;<<@ @ @  
	#25#$II'DIII#-!1$ $ $  I7%?KK$ 	 	 	"PX P P P  	
   	 	 	LLfffFcfffggg		#2-#- /!1	$ 	$ 	$  Wdii8U8[8[\_8`8`.a.abbII 	 	 	LLfffFcfffggg	
 ##788N 3RW[@9LN.)))*++n<NGyE11 	(.;*FGGG+ 	t 	tM=&&s++,,11 #)<)<S)A)A B B - 3 3C 8 8 ;w~~n}&DEE @K >???,111MK%BMRRTberTrssss	t !!>Y^___c (+5'"'+5Cn---,4466 	L;$$S))**a// + 1 1# 6 6q 9w~~n}&DEE @K >???K,n{.JKKK , 	 	M=&&s++,,11 #)<)<S)A)A B B"]2::<< &1$)))'#1$3#%%5
 
 
 
 7<<444s%   !(E
 

FE((0FAG: :-H'rq   c
                 z    t          | ||||||||		  	        }t          ||                    dd                    S )ay  
    Extracts a class from a module file, present in the local folder or repository of a model.

    <Tip warning={true}>

    Calling this function will execute the code in the module file found locally or downloaded from the Hub. It should
    therefore only be called on trusted repos.

    </Tip>

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

            - a string, the *model id* of a pretrained model configuration hosted inside a model repo on
              huggingface.co. Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced
              under a user or organization name, like `dbmdz/bert-base-german-cased`.
            - a path to a *directory* containing a configuration file saved using the
              [`~PreTrainedTokenizer.save_pretrained`] method, e.g., `./my_model_directory/`.

        module_file (`str`):
            The name of the module file containing the class to look for.
        class_name (`str`):
            The name of the class to import in the module.
        cache_dir (`str` or `os.PathLike`, *optional*):
            Path to a directory in which a downloaded pretrained model configuration should be cached if the standard
            cache should not be used.
        force_download (`bool`, *optional*, defaults to `False`):
            Whether or not to force to (re-)download the configuration files and override the cached versions if they
            exist.
        resume_download:
            Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v1 of
            Diffusers.
        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 `transformers-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.

    <Tip>

    You may pass a token in `token` if you are not logged in (`huggingface-cli login`) and want to use private or
    [gated models](https://huggingface.co/docs/hub/models-gated#gated-models).

    </Tip>

    Returns:
        `type`: The class, dynamically imported from the module.

    Examples:

    ```python
    # Download module `modeling.py` from huggingface.co and cache then extract the class `MyBertModel` from this
    # module.
    cls = get_class_from_dynamic_module("sgugger/my-bert-model", "modeling.py", "MyBertModel")
    ```r   rS    )r   rs   rm   )r   rC   rq   r   r   r   r   r   r   r   kwargsfinal_modules               r   get_class_from_dynamic_moduler   z  sV    \ *%%')
 
 
L z<+?+?r+J+JKKKr   )NFNNNNF)NNFNNNNF).__doc__rd   ry   r   r+   r>   r   r(   pathlibr   typingr   r   r   urllibr   huggingface_hubr   r	   huggingface_hub.utilsr
   r   	packagingr   r   r   r   r   r   
get_loggerr}   r   r   r#   r0   rK   PathLiker4   rG   rZ   rk   rs   ro   boolr   r   rJ   r   r   <module>r      s   : 9       				 				  



       ( ( ( ( ( ( ( ( ( (       7 7 7 7 7 7 7 7 M M M M M M M M             F F F F F F F F F F 
	H	%	% !G > > >  c2;&6 7    ' ' '$     :* * *@	' 	' 	'  6  48 &*(,(,""r5 r5#(bk)9#:r5r5 c2;./0r5 	r5
 d^r5 d38n%r5 E$)$%r5 smr5 r5 r5 r5 r5j  !%37 &*(,(,""XL XL#(bk)9#:XLXL XL c2;./0	XL
 XL d^XL d38n%XL E$)$%XL smXL XL XL XL XL XL XLr   