
    bi2Z                        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	Z	ddl
m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 jB                  e"      Z#dZ$ e% ejL                  dd            Z' e	jP                         Z)d Z*d Z+dee,ejZ                  f   fdZ.d Z/d Z0d Z1d Z2d Z3d'dZ4d Z5e	 	 	 	 	 	 d(dee,ejZ                  f   de,deee,ejZ                  f      de6d eee,e,f      d!eee6e,f      d"ee,   d#e6fd$       Z7e	 	 	 	 	 	 	 d)dee,ejZ                  f   de,d%ee,   deee,ejZ                  f      de6d eee,e,f      d!eee6e,f      d"ee,   d#e6fd&       Z8y)*z3Utilities to dynamically load objects from the Hub.    N)Path)
ModuleType)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-mirrorDIFFUSERS_TIMEOUT_REMOTE_CODE   c                      d} t        j                  t        j                  |       j	                               d   j                         }t        |d       S )Nz$https://pypi.org/pypi/diffusers/jsonreleasesc                 ,    t        j                  |       S )N)r   Version)xs    `/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/utils/dynamic_modules_utils.py<lambda>z(get_diffusers_versions.<locals>.<lambda>2   s    '//!*<     )key)jsonloadsr   urlopenreadkeyssorted)urlr   s     r   get_diffusers_versionsr&   /   sC    
0Czz'//#.3356zBGGIH( <==r   c                     t         t        j                  v ryt        j                  j                  t                t	        j
                  t         d       t        t               dz  } | j                         s| j                          yy)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_modulesr3   5   s^    
 388#HHOO$%KK 40%&6I r   namec                    t                t        t              | z  }|j                  j	                         st        |j                         t        j                  |d       |dz  }|j	                         s|j                          yy)zF
    Creates a dynamic module in the cache directory for modules.
    Tr(   r*   N)	r3   r   r   parentr0   create_dynamic_moduler.   r/   r1   )r4   dynamic_module_pathr2   s      r   r7   r7   D   sp     /047%%,,.1889KK#d3#m3I r   c                 .   t        | dd      5 }|j                         }ddd       t        j                  dt        j                        }|t        j                  d|t        j                        z  }t        t        |            S # 1 sw Y   lxY w)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_importsrJ   S   s}     
k3	1 Q&&( zz"=wbll[

#A7RTR^R^__$%&& s   BBc                 l   d}| g}g }|sg }|D ]  }|j                  t        |              t        |       j                  }|D cg c]  }t	        ||z         }}|D cg c]	  }||vs| }}|D cg c]  }| d	 }}t        |      dk(  }|j                  |       |s|S c c}w c c}w c c}w )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.
    F.pyr   )extendrJ   r   r6   strlen)	rF   	no_changefiles_to_checkall_relative_importsnew_importsrG   module_pathmnew_import_filess	            r   get_relative_import_filesrW   e   s     I!]N  	8A3A67	8 ;'..:EFQCa0FF'7Y!1DX;XAYY-=>QCs)>>()Q.	##N3    GY>s   B'!	B,+B,5B1c                    t        | dd      5 }|j                         }ddd       t        j                  dt        j                        }|t        j                  d|t        j                        z  }|D cg c](  }|j                  d      r|j                  d      d	   * }}t        t        |            }g }|D ]  }	 t        j                  |        t        |      d	kD  r0t        d
dj                  |       ddj                  |       d      t        |       S # 1 sw Y   xY wc c}w # t        $ r |j                  |       Y w xY w)zi
    Check if the current Python environment contains all the libraries that are imported in a file.
    r:   r;   r<   Nz^\s*import\s+(\S+)\s*$r>   z^\s*from\s+(\S+)\s+import.r   z\This modeling file requires the following packages that were not found in your environment: , z. Run `pip install  `)r@   r"   rA   rB   rC   
startswithsplitrD   rE   	importlibimport_moduleImportErrorr-   rO   joinrJ   )filenamerG   rH   importsimpmissing_packagess         r   check_importsrg      sF    
hg	. !&&( jj2G2<<PGrzz6r||TTG,3OS3>>#;Nsyy~a OGO 3w< G )	)##C() q jyy)*++>sxxHX?Y>ZZ[]
 	

  ))3  P  	)##C(	)s)   D&;D3D3D8&D08EEc                     t        d      )NzLoading this model requires you to execute custom code contained in the model repository on your local machine. Please set the option `trust_remote_code=True` to permit loading of this model.)
ValueError)signumframes     r   _raise_timeout_errorrl      s    
	c r   c                    | |rt         dkD  rd }	 t        j                  t        j                  t              }t        j                  t                | >t        d| d| d      }|j                         dv rd} n|j                         dv rd} | >t        j                  d       	 |Ht        j                  t        j                  |       t        j                  d       n|rt        d d        |r| st        d
| d      | S # t        $ r t        d| d| d	      w xY w# |:t        j                  t        j                  |       t        j                  d       w w xY w)Nr   zThe repository for z contains custom code which must be executed to correctly load the model. You can inspect the repository content at https://hf.co/z.
You can avoid this prompt in future by passing the argument `trust_remote_code=True`.

Do you wish to run the custom code? [y/N] )yesy1T)non0 FzS.
Please pass the argument `trust_remote_code=True` to allow custom code to be run.zLoading z requires you to execute the configuration file in that repo on your local machine. Make sure you have read the code there to avoid malicious use, then set the option `trust_remote_code=True` to remove this error.)	TIME_OUT_REMOTE_CODEsignalSIGALRMrl   alarminputlower	Exceptionri   )trust_remote_code
model_namehas_remote_codeprev_sig_handleranswers        r   resolve_trust_remote_coder      s}    3a7#$#)==AU#V 12'/"-j\ :ccmbn oEFF ||~)::,0)+??,1) (/ Q $/MM&..2BCLLO t,0zl #M M
 	
 -   )* 6__i^j khi  $/MM&..2BCLLO 0s   B D D D##D& &>E$c                    t         j                  j                  |      }|j                  d      r|dd }|j	                  t         j                  j
                  d      }t        t              |z  }t        5  |r4t        j                  j                  |d       t        j                          t        j                  j                  |      }t        j                  j!                  ||      }|3t        j                  j#                  |      }|t        j                  |<   n|}|j$                  j'                  |       ddd       | t)              S t+        |       S # 1 sw Y   "xY w)zY
    Import a module on the cache directory for modules and extract a class from it.
    rL   NrY   )location)r.   r,   normpathendswithreplacesepr   r   _HF_REMOTE_CODE_LOCKr+   modulespopr_   invalidate_cachesgetutilspec_from_file_locationmodule_from_specloaderexec_modulefind_pipeline_classgetattr)
class_namerT   force_reloadr4   rF   cached_modulemodule_specmodules           r   get_class_in_moduler      s    77K(D}}UCRy<<S)D-.<K	 /KKOOD$''').1kkood.Cnn<<TK<X  ^^44[AF &CKK"F&&v./" "6**6:&&)/ /s   8C	E""E+c                 ~   ddl m} t        t        j                  | t        j
                              }d}|j                         D ]t  \  }}||j                  k7  st        ||      s#|j                  j                  d      d   dk7  sE|,t        d|j                   d|j                   d	| d
|  d	      |}v |S )z
    Retrieve pipeline class that inherits from `DiffusionPipeline`. Note that there has to be exactly one class
    inheriting from `DiffusionPipeline`.
    r   )DiffusionPipelineNrY   r   	diffusersz#Multiple classes that inherit from z have been found: z, and z). Please make sure to define only one in )	pipelinesr   dictinspect
getmembersisclassitems__name__
issubclass
__module__r^   ri   )loaded_moduler   cls_memberspipeline_classcls_nameclss         r   r   r      s    
 .w))-IJKN$**, !#)2223 12$$S)!,;) 9:K:T:T9U V&//0xj A%a) 
 !N! r   pretrained_model_name_or_pathrF   	cache_dirforce_downloadproxiestokenrevisionlocal_files_onlyc                 	   t        |       } t        j                  j                  | |      }t        j                  j	                  |      r|}	d}
n| j                  d      dk(  rt               }ddj                  t        j                  d      dd       z   }|%|dd |v r|nd	}t        j                  d
| d       n7||v rd| }n-|d	k(  r|}n%t        d| ddj                  |d	gz          d      	 t        t        d| d|  d||||      }	d}
| dz   }nQ	 t        | ||||||      }	t        j                  j                  ddj                  | j                  d                  }
t#        |	      }t$        t        j                  j&                  z   |
z   }t)        |       t+        t,              |z  }|
dk(  s|
dk(  rt/        j0                  |	||z         |D ]  }t3        |j                  d            dk(  rndj                  |j                  d            }|j                  d      d   }t        j                  j5                  ||z        st        j6                  ||z         | d}t/        j0                  t        j                  j                  | |      ||z          nMt9        | ||      j:                  }||z  }|t        j                  j&                  z   |z   }t)        |       ||z  j5                         st3        |j                  d            dk(  rN|j                  d      d   }t        j                  j5                  ||z        st        j6                  ||z         t/        j0                  |	||z         |D ]i  }t3        |j                  d            dk(  r dj                  |j                  d            }||z  j5                         rTt=        | | d||||||       k t        j                  j                  ||      S # t        $ r}t        d| d      |d}~wt        $ r t        j!                  d| d|  d        w xY w# t        $ r t        j!                  d| d|  d        w xY w)a	  
    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.
        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 (`hf auth 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   vrY   N   r   mainzDefaulting to latest_version: z`custom_revision`: z3 does not exist. Please make sure to choose one of rZ   datasetrL   )repo_id	repo_typerc   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   z--r   )r   r   r   r   r   r   r   r   )rN   r.   r,   rb   isfilecountr&   r   r^   loggerinfori   r	   COMMUNITY_PIPELINES_MIRROR_IDr   EnvironmentErrorerrorrg   r   r   r7   r   r   shutilcopyfilerO   r0   r/   r
   shaget_cached_module_file)r   rF   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   r %((E$F!&C[Q	ww~~()1		&	,	,S	1Q	635sxx(9(9#(>r(BCC )7);?Q)Q~W]HKK8
!DE++8*~HH%hZ 0II0F8;<=Q@ 
	#25#$:Q'D&ESI#-!1$  I7%?K	#2-#-!1$  Wdii8U8[8[\_8`.abI ##78N 3RWW[[@9LN.)*+n<NGyE1 	,n{.JK+ 	xM=&&s+,1 #)<)<S)A B - 3 3C 8 ;ww~~n}&DEKK >?,oS1MOOBGGLL)FVXfivXvw	x !!>Y^_cc (+5'"''++5Cn-,446;$$S)*a/ + 1 1# 6q 9ww~~n}&DEKK >?OO0.;2NO , 	M=&&s+,1 #)<)<S)A B"]2::<&1$oS)'#1#%%5			 77<<44_ % 	"XJ 'P P 	
   	LL0XFcEddefg	    	LL0XFcEddefg	s%   8#P AQ 	Q"P22*Q'Rr   c	           
      @    t        | |||||||      }
t        ||
      S )a  
    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.
        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 (`hf auth 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   )r   r   )r   rF   r   r   r   r   r   r   r   kwargsfinal_modules              r   get_class_from_dynamic_moduler     s6    T *%%)	L z<88r   )F)NFNNNF)NNFNNNF)9__doc__r_   r   r   r.   rA   r   rv   r+   	threadingpathlibr   typesr   typingr   r   r   urllibr   huggingface_hubr	   r
   huggingface_hub.utilsr   r   	packagingr   rt   r   r   r   r   
get_loggerr   r   r   intgetenvru   Lockr   r&   r3   rN   PathLiker7   rJ   rW   rg   rl   r   r   r   boolr   r    r   r   <module>r      sd   :    	 	   
    ( (  7 M   F F 
		H	% !G 9299%DbIJ %y~~' >c2;;&6 7 '$ :*@)X'B6  48 (,(,""m5#(bkk)9#:m5m5 c2;;./0m5 	m5
 d38n%m5 E$)$%m5 smm5 m5 m5`  !%37 (,(,""S9#(bkk)9#:S9S9 S9 c2;;./0	S9
 S9 d38n%S9 E$)$%S9 smS9 S9 S9r   