
    iU                     <   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	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 ddl m!Z!  ejD                  e#      Z$dZ% e& ejN                  dd            Z( ejR                         Z*d Z+d Z,dee-ej\                  f   fdZ/d Z0d Z1d Z2d Z3d)dZ4d Z5e	 	 	 	 	 	 	 	 d*dee-ej\                  f   de-dee-   deee-ej\                  f      d e6d!eee-e-f      d"eee6e-f      d#ee-   d$e6d%ee-   fd&       Z7e	 	 	 	 	 	 	 	 	 d+dee-ej\                  f   de-dee-   d'ee-   deee-ej\                  f      d e6d!eee-e-f      d"eee6e-f      d#ee-   d$e6d%ee-   f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logging)DIFFUSERS_DISABLE_REMOTE_CODEz$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    o/home/obispo/Crisostomo_bridge/mision_env/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_modulesr4   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)	r4   r   r   parentr1   create_dynamic_moduler/   r0   r2   )r5   dynamic_module_pathr3   s      r   r8   r8   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_importsrK   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   )extendrK   r   r7   strlen)	rG   	no_changefiles_to_checkall_relative_importsnew_importsrH   module_pathmnew_import_filess	            r   get_relative_import_filesrX   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  r:t        j                  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   zaThis modeling file might require the following packages that were not found in your environment: , z. Run `pip install  `)rA   r#   rB   rC   rD   
startswithsplitrE   rF   	importlibimport_moduleImportErrorr.   rP   loggerwarningjoinrK   )filenamerH   rI   importsimpmissing_packagess         r   check_importsrj      sE    
hg	. !&&( jj2G2<<PGrzz6r||TTG,3OS3>>#;Nsyy~a OGO 3w< G )	)##C() q oyy)*++>sxxHX?Y>ZZ[]	

  ))3  P  	)##C(	)s)   D0;D=D=E0D:EEc                     | xr t          } t         rt        j                  d       |r | sd| d}|t         rdndz  }t        |      |r| rt        j                  d| d       | S )NzDownloading remote code is disabled globally via the DIFFUSERS_DISABLE_REMOTE_CODE environment variable. Ignoring `trust_remote_code`.zThe repository for z contains custom code. zhDownloading remote code is disabled globally via the DIFFUSERS_DISABLE_REMOTE_CODE environment variable.zCPass `trust_remote_code=True` to allow loading remote code modules.z6`trust_remote_code` is enabled. Downloading code from z9. Please ensure you trust the contents of this repository)r   rc   rd   
ValueError)trust_remote_code
model_namehas_remote_code	error_msgs       r   resolve_trust_remote_coderq      s    )O2O.O$ U	
 0)*5LM	, wV	
	
 ##	.DZL  QJ  K	
 r   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.
    rM   NrZ   )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_namerU   force_reloadr5   rG   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   )DiffusionPipelineNrZ   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_   rl   )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_pathrG   	subfolder	cache_dirforce_downloadproxiestokenrevisionlocal_files_only	local_dirc
                 	   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   }nT	 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          nOt9        | ||      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 ]k  }t3        |j                  d            dk(  r dj                  |j                  d            }||z  j5                         rTt=        | | d||||||||	
       m 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).

    Returns:
        `str`: The path to the module inside the cache.
    local/r   vrZ   N   r   mainzDefaulting to latest_version: z`custom_revision`: z3 does not exist. Please make sure to choose one of r[   datasetrM   )repo_id	repo_typerf   r   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   r   r   z--r   )r   r   r   r   r   r   r   r   r   r   )rO   r/   r-   re   isfilecountr'   r   r_   rc   inforl   r	   COMMUNITY_PIPELINES_MIRROR_IDr   EnvironmentErrorerrorrj   r   rx   r8   r   r   shutilcopyfilerP   r1   r0   r
   shaget_cached_module_file)r   rG   r   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   n %((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4i % 	"XJ 'P P 	
   	LL0XFcEddefg	&   	LL0XFcEddefg	s%   8$P AQ% 	Q"(P88*Q"%'Rr   c                 D    t        | ||||||||	|

      }t        ||      S )a  
    Extracts a class from a module file, present in the local folder or repository of a model.

    > [!WARNING] > 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.

    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).

    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   rG   r   r   r   r   r   r   r   r   r   final_modules               r   get_class_from_dynamic_moduler     s<    F *%%)L z<88r   )F)NNFNNNFN)	NNNFNNNFN)9__doc__r`   r   r    r/   rB   r   r,   	threadingpathlibr   typesr   typingr   r   r   urllibr   huggingface_hubr	   r
   huggingface_hub.utilsr   r   	packagingr    r   r   r   r   	constantsr   
get_loggerr   rc   r   intgetenvTIME_OUT_REMOTE_CODELockry   r'   r4   rO   PathLiker8   rK   rX   rj   rq   r   r   boolr   r    r   r   <module>r      s   :    	 	  
    ( (  7 M   F F 4 
		H	% !G 9299%DbIJ %y~~' >c2;;&6 7 '$ :*@0'B6   $37 (,(,""#q5#(bkk)9#:q5q5 }q5 c2;;./0	q5
 q5 d38n%q5 E$)$%q5 smq5 q5 }q5 q5h   $ $37 (,(,""#N9#(bkk)9#:N9N9 }N9 	N9
 c2;;./0N9 N9 d38n%N9 E$)$%N9 smN9 N9 }N9 N9r   