
    +hfc                     n	   S SK r S SKrS SKrS SKJr  S SKJrJrJrJ	r	  S SK
rS SKrS SKrSSKJr  SSKJr  SSKJrJr  SS	KJr  \R.                  " \5      r0 S
\" S
\	\\\   4   SS9_S\" S\	\\\   4   SS9_S\" S\	\\\   4   SS9_S\" S\	\\\   4   SS9_S\" S\\   SS9_S\" S\\   SS9_S\" S\SSS9_S\" S\SSS9_S\" S\\	\R:                  \\R:                     4      SS9_S\" S\\   S S9_S!\" S!\\   S"S9_S#\" S#\SS$S%9_S&\" S&\S'S(S%9_S)\" S)\\R<                     S*S9_S+\" S+\\R<                     S,S9_S-\" S-\\   S.S9_S/\" S/\S0S1S%9_0 S2\" S2\\   S3S9_S4\" S4\\R<                     S5S9_S6\" S6\\\\4      S7S9_S8\" S8\\\\4      S9S9_S:\" S:\\\\4      S;S9_S<\" S<\\\\4      S=S9_S>\" S>\\\\4      S?S9_S@\" S@\\\4   SASBS%9_SC\" SC\\\4   SASDS%9_SE\" SE\SFSGS%9_SH\" SH\SISJS%9_SK\" SK\SLSMS%9_SN\" SN\SOSPS%9_SQ\" SQ\SSRS9_SS\" SS\SSTS9_SU\" SU\	\\\   4   SLSVS%9_SW\" SW\	\\\   4   SXSYS%9_E\" SZ\	\\\   4   SXS[S%9\" S\\ S]S^S%9\" S_\\   SS`S9Sa.Er!0 Sb\" Sb\R<                  SScS9_Sd\" Sd\R<                  SeS9_Sf\" Sf\R<                  SSgS9_Sh\" Sh\R<                  SiS9_Sj\" Sj\SSkS9_Sl\" Sl\RD                  SmS9_Sn\" Sn\\   SoS9_S4\" S4\R<                  SSpS9_S)\" S)\R<                  SSqS9_S&\" S&\SS(S9_Sr\" Sr\R<                  SSsS9_St\" St\R<                  SSuS9_Sv\" Sv\R<                  SSwS9_Sx\" Sx\R<                  SyS9_Sz\" Sz\R<                  SS{S9_S|\" S|\R<                  S}S9_S~\" S~\R<                  SS9_\" S\R<                  SS9\" S\\\      SS9\" S\\R<                     SS9\" S\\R<                     SS9\" S\	\\RF                  RF                     \\R<                     \\RH                     4   SSS9S.Er%0 \!E\%Er&SSSSS.SSSSS.SSSSSS.SSS S SS.SSSSSSSSS.SSSSSSSSS.SSSS.SSSS.S.r'SS S S.SSLSLS.SSS.SS]S.SS0S.r(/ SQr)Sr*/ SQr+SS/S/Sb/S.r,\,4S jr- " S S\5      r.g)    N)Path)ListOptionalTupleUnion   )ConfigMixin)PipelineImageInput   )ModularPipelineBlocksSequentialPipelineBlocks)
InputParampromptz3The prompt or prompts to guide the image generation)	type_hintdescriptionprompt_2zFThe prompt or prompts to be sent to the tokenizer_2 and text_encoder_2negative_promptz7The prompt or prompts not to guide the image generationnegative_prompt_2z1The negative prompt or prompts for text_encoder_2cross_attention_kwargsz2Kwargs dictionary passed to the AttentionProcessor	clip_skipz-Number of layers to skip in CLIP text encoderimageTz0The image(s) to modify for img2img or inpainting)r   requiredr   
mask_imagez9Mask image for inpainting, white pixels will be repainted	generatorz)Generator(s) for deterministic generationheightz'Height in pixels of the generated imagewidthz&Width in pixels of the generated imagenum_images_per_promptz'Number of images to generate per prompt)r   defaultr   num_inference_steps2   zNumber of denoising steps	timestepsz*Custom timesteps for the denoising processsigmasz'Custom sigmas for the denoising processdenoising_endz<Fraction of denoising process to complete before terminationstrengthg333333?z)How much to transform the reference imagedenoising_startz'Starting point of the denoising processlatentsz0Pre-generated noisy latents for image generationpadding_mask_cropz)Size of margin in crop for image and maskoriginal_sizez8Original size of the image for SDXL's micro-conditioningtarget_sizez)Target size for SDXL's micro-conditioningnegative_original_sizez/Negative conditioning based on image resolutionnegative_target_sizez0Negative conditioning based on target resolutioncrops_coords_top_left)r   r   z2Top-left coordinates for SDXL's micro-conditioningnegative_crops_coords_top_leftz&Negative conditioning crop coordinatesaesthetic_scoreg      @z,Simulates aesthetic score of generated imagenegative_aesthetic_scoreg       @z"Simulates negative aesthetic scoreetag        u   Parameter η in the DDIM paperoutput_typepilz#Output format (pil/tensor/np.array)ip_adapter_imagez!Image(s) to be used as IP adaptercontrol_imagezControlNet input conditioncontrol_guidance_startzWhen ControlNet starts applyingcontrol_guidance_endg      ?zWhen ControlNet stops applyingcontrolnet_conditioning_scalez#Scale factor for ControlNet outputs
guess_modeFz=Enables ControlNet encoder to recognize input without promptscontrol_modez!Control mode for union controlnet)r7   r8   r9   prompt_embedsz.Text embeddings used to guide image generationnegative_prompt_embedszNegative text embeddingspooled_prompt_embedszPooled text embeddingsnegative_pooled_prompt_embedszNegative pooled text embeddings
batch_sizezNumber of promptsdtypez Data type of model tensor inputspreprocess_kwargszKwargs for ImageProcessorz%Initial latents for denoising processzTimesteps for inferencelatent_timestepzInitial noise level timestepimage_latentsz$Latents representing reference imagemaskzMask for inpaintingmasked_image_latentsz#Masked image latents for inpaintingadd_time_idszTime ids for conditioningnegative_add_time_idszNegative time idstimestep_condzTimestep conditioning for LCMnoisezNoise added to image latentscrops_coordszCrop coordinatesip_adapter_embedszImage embeddings for IP-Adapternegative_ip_adapter_embedsz(Negative image embeddings for IP-AdapterimageszGenerated images)rH   rI   rJ   rK   rL   Promptstringz.a bear sitting in a chair drinking a milkshaketextarea)labeltyper   displayzNegative PromptzRdeformed, ugly, wrong proportion, low res, bad anatomy, worst quality, low qualityStepsint   i  )rP   rQ   r   minmaxSeedrandom)rP   rQ   r   rV   rR   Widthtexti      i    
dimensions)rP   rQ   rR   r   rV   rW   stepgroupHeightImagesoutputrP   rQ   rR   Imageinput)r   r   r   seedr   r   rL   r   )rQ   r   rV   float )rQ   r   booleanrQ   )rT   rg   strboolr   )unetvaetext_encoder	tokenizer
controlnettransformerimage_encoderzModular Diffusers)	processorfeature_extractorsafety_checkerrn   ro   )text_encodersrJ   prompt_embeddingsc                 f    U c  gUR                  5        H  u  p#U H  nX@;   d  M
  Us  s  $    M     g)z
Get the group name for a given parameter name, if not part of a group, return None e.g. "prompt_embeds" ->
"text_embeds", "text_encoder" -> "text_encoders", "prompt" -> None
N)items)namegroup_params_keys
group_name
group_keys	group_keys        `/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/modular_pipelines/node_utils.pyget_group_namer   O  s=    
 |"3"9"9";
#I !! $ #<     c                       \ rS rSrSrSr\ SS\S\\	   4S jj5       r
\S4S jrSS	 jr\S
 5       rS rS r\S 5       rS rS rSrg)ModularNodei]  a  
A ModularNode is a base class to build UI nodes using diffusers. Currently only supports Mellon. It is a wrapper
around a ModularPipelineBlocks object.

<Tip warning={true}>

    This is an experimental feature and is likely to change in the future.

</Tip>
znode_config.jsonNpretrained_model_name_or_pathtrust_remote_codec                 F    [         R                  " U4SU0UD6nU " U40 UD6$ )Nr   )r   from_pretrained)clsr   r   kwargsblockss        r   r   ModularNode.from_pretrainedk  s9     '66)
=N
RX
 6$V$$r   c                     Xl         Uc   U R                   R                  R                  n0 U l        0 nU R                   R                  U R                   R
                  -   nU GH3  nUR                  UR                  S 5      nU(       aS  XUR                  '   UR                  SUR                  5      n	XR                  :w  a  XR                  UR                  '   Mz  UR                  [        ;  a-  UR                  (       d  [        UR                  5      (       d  M  UR                  [        ;   a#  [        UR                     R                  5       nGO3[        UR                  5      (       aH  [        UR                  5      nUR                  U R                  ;  a  XR                  UR                  '   OUR                  b$  [        UR                  5      R                  5       n
OL[        R!                  UR                  S 5      nU(       a#  [        UR                  5      R                  5       OSn
["        R%                  5        H0  u  pX;   d  M  UR                  5       nUR                  US'   SUS'     O   UR                  nXUR                  '   GM6     0 nU R                   R&                   GHG  nUR                  UR                  S 5      nU(       aS  XUR                  '   UR                  SUR                  5      n	XR                  :w  a  XR                  UR                  '   Mz  Sn[(         H  nUUR                  ;   d  M  Sn  O   U(       a  M  [        UR                  5      (       aH  [        UR                  5      nUR                  U R                  ;  a  XR                  UR                  '   O1UR                  [*        ;   a  UR                  SSS	.nOUR                  nXUR                  '   GMJ     0 n[-        U R                   [.        5      (       aT  [1        U R                   R2                  R5                  5       5      S
   nU R                   R2                  U   R6                  nOU R                   R6                  nU GH#  nUR                  UR                  S 5      nU(       aU  UUUR                  '   UR                  SUR                  5      n	U	UR                  :w  a  XR                  UR                  '   M|  UR                  [        ;   a'  [        UR                     R                  5       nSUS'   O][        UR                  5      nU(       a5  UnUR                  U R                  ;  a  XR                  UR                  '   OUR                  nUUUR                  '   GM&     [9        U5      S:  a  [:        R=                  SU 35        UUUUUU R                  S.nU R>                  " S0 UD6  g )Nrz   rh   rP   re   rR   FTdiffusers_auto_modelrc   rb   r   zUnused kwargs: )categoryrP   input_paramscomponent_paramsoutput_paramsname_mapping ) r   	__class____name__r   inputsintermediate_inputspoprz   DEFAULT_PARAM_MAPSr   r   copyr   rj   lowerSDXL_PARAM_SCHEMAgetDEFAULT_TYPE_MAPSry   expected_componentsDEFAULT_EXCLUDE_MODEL_KEYSDEFAULT_MODEL_KEYS
isinstancer   list
sub_blockskeysintermediate_outputslenloggerwarningregister_to_config)selfr   r   rP   r   r   r   inpparammellon_nametype_strinp_spectype_key
type_paramr   comp
to_excludeexclude_keyr   last_block_nameoutputsoutr|   register_dicts                           r   __init__ModularNode.__init__w  s   =KK))22E ##dkk&E&EECJJsxx.E).SXX&#ii9((*2=%%chh/xx11#,,~^a^f^fOgOgxx--*388499;))&sxx0884#4#4427%%chh/
 ==,"3==1779H044SXXtDHBJs8#5#56<<>PRH,=,C,C,E(H+ * 1),g+2i( -F  HHE%*"M P KK33DJJtyy$/E.3+#ii		:))+3>%%dii0J9$))+!%J  : dii((&tyy199D$5$5538%%dii000"&))5KX_`		*/TYY'5 48 dkk#;<<"4;;#9#9#>#>#@A"EOkk,,_=RRGkk66GCJJsxx.E*/chh'#ii9#((*2=%%chh/xx--*388499;#+i +CHH5
&Exxt'8'886;))#((3HHE&+M#((#+ . v;?NN_VH56 !( 0* --
 	0-0r   c                 L    U R                   R                  XS9U l        Xl        g )N)components_manager
collection)r   init_pipelinepipeline_components_manager)r   r   r   s      r   setupModularNode.setup  s"    11EW1o#5 r   c                 "    U R                  5       $ N)_convert_to_mellon_config)r   s    r   mellon_configModularNode.mellon_config  s    --//r   c                    0 nU R                   R                  US'   U R                   R                  US'   0 nU R                   R                  R	                  5        Hj  u  p4X0R
                  ;   a  U R
                  U   nOUn[        U[        5      (       a  UUSS.nOUnXR;  a  XbU'   MO  [        R                  SU SU 35        Ml     U R                   R                  R	                  5        Hj  u  pxXpR
                  ;   a  U R
                  U   nOUn[        U[        5      (       a  UUSS.nOUnXR;  a  XbU'   MO  [        R                  SU SU 35        Ml     U R                   R                  R	                  5        Hj  u  pXR
                  ;   a  U R
                  U	   nOU	n[        U
[        5      (       a  U
U
SS.nOU
nXR;  a  XbU'   MO  [        R                  S	U
 SU	 35        Ml     X!S
'   U$ )NrP   r   re   rc   zInput param z( already exists in node_param, skipping zComponent param rb   zOutput param params)configrP   r   r   ry   r   r   rj   r   debugr   r   )r   node
node_paraminp_name	inp_paramr   r   	comp_name
comp_paramout_name	out_params              r   r   %ModularNode._convert_to_mellon_config  s   ))W;;//Z
#';;#;#;#A#A#CH,,,"//9&)S))&%& ",*/;'|K=8`ai`jkl# $D& &*[[%A%A%G%G%I!I---"//	:'*c**'&& #,*/;'/
|;cdmcnop# &J& $(;;#<#<#B#B#DH,,,"//9&)S))&%' ",*/;'}YK7_`h_ijk# $E$ $Xr   c                 0   [        U5      n[        R                  " UR                  SS9  U R                  U R
                  S.n[        USSS9 n[        R                  " X#SS9  S	S	S	5        [        R                  S
U 35        U$ ! , (       d  f       N(= f)z
Save the Mellon configuration to a JSON file.

Args:
    file_path (str or Path): Path where the JSON file will be saved

Returns:
    Path: Path to the saved config file
T)exist_ok)moduler   wutf-8encodingr   )indentNz(Mellon config and name mapping saved to )r   osmakedirsparentr   r   openjsondumpr   info)r   	file_pathr   fs       r   save_mellon_configModularNode.save_mellon_configF  s     O	 	I$$t4 !..@Q@QR )S73qIIf* 4 	>ykJK 43s   B
Bc                    [        U5      nUR                  5       (       d  [        SU 35      e[        USSS9 n[        R
                  " U5      nSSS5        [        R                  SU 35        W$ ! , (       d  f       N(= f)z
Load a Mellon configuration from a JSON file.

Args:
    file_path (str or Path): Path to the JSON file containing Mellon config

Returns:
    dict: The loaded combined configuration containing 'module' and 'name_mapping'
zConfig file not found: rr   r   NzMellon config loaded from )r   existsFileNotFoundErrorr   r   loadr   r   )r   r   r   r   s       r   load_mellon_configModularNode.load_mellon_config`  ss     O	!!#&=i[$IJJ)S73qYYq\F 4 	0<= 43s   A33
Bc                 |   0 nU R                   R                  R                  5        H  u  p4[        R	                  SU 35        U R
                  R                  X35      nXQ;   d  M?  [        X   [        5      (       a  X1U   ;   a  X   R                  U5      nOUR                  U5      nU(       d  M  U R                  R                  US   5      X#'   M     0 nU R                   R                  R                  5        H  u  p[        R	                  SU 35        U R
                  R                  X5      n
X;   d  M?  [        X   [        5      (       a  XU
   ;   a  X   R                  U5      nOUR                  U
5      nUc  M  XU'   M     [        U R                   R                  R                  5       5      nX'U4$ )Nzcomponent: model_idzinput: )r   r   ry   r   r   r   r   r   dictr   r   get_oner   r   r   r   )r   r   params_componentsr   r   mellon_comp_namer   
params_runr   r   mellon_inp_namer   return_output_namess                r   process_inputsModularNode.process_inputsw  s   %)[[%A%A%G%G%I!ILL;yk23#0044YJ)f6==)VfOgBg!377	BD!::&67D4373K3K3S3STXYcTd3e%0 &J 
#';;#;#;#A#A#CHLL78*-."//33HGO(f5t<<TcMdAd 155h?C **_5C?+.x( $D #4;;#<#<#A#A#CD .AAAr   c                     U R                   " S0 UD6u  p#nU R                  R                  " S0 UD6  U R                  " S0 UDSU0D6nU$ )Nrb   r   )r   r   update_components)r   r   r   r   r   rb   s         r   executeModularNode.execute  sO    =A=P=P=ZSY=Z:':''<*;<HH4GHr   )r   r   r   r   r   )r   
__module____qualname____firstlineno____doc__config_nameclassmethodrj   r   rk   r   DEFAULT_CATEGORYr   r   propertyr   r   r   r   r   r   __static_attributes__r   r   r   r   r   ]  s    	 %K -1	%'*	% $D>	% 	% )9 D1L6 0 0?B4  ,B:r   r   )/r   loggingr   pathlibr   typingr   r   r   r   numpynpPILtorchconfiguration_utilsr	   image_processorr
   modular_pipeliner   r   modular_pipeline_utilsr   	getLoggerr   r   rj   r   rT   	GeneratorTensorrg   rk   SDXL_INPUTS_SCHEMAr?   rd   arraySDXL_INTERMEDIATE_INPUTS_SCHEMAr   r   r   r   r  r   DEFAULT_PARAMS_GROUPS_KEYSr   r   r   r   r   <module>r     sq	     	  / /  
  - 0 M . 
		8	$ZjE#tCy.1?tZ 
T#Y'\	Z zT#Y'MZ T#Y'GZ& j 4.H'Z0 x}:i1Z6 Z$F	7ZB *$O	CZN 5$u2G!GHI?OZX jXc]HqrYZZ Z8C=Fno[Z\ Z3Gp]Zb :bFacZh x5CoiZn jHU\\2@ioZt Z5/RuZ@ 
eS>iAZF zXe_BkGZL zXell3AsMZR 5c?+?SZ\ Z5c?+N]Zf :%S/!:HsgZl j 5c?+EmZv J5c?+FwZ@ ZS/H	AZL %j(S/<	'MZX zUCEsYZ^ 
"eSNr!_Zd 
:eucGgheZf :eAfgZl 
$7	mZx Z#5RnyZ~ j tE{*+5	ZJ JtE{*+4	KZV &0'tE{*+9	& S	 $s)dHkoZ x=#Z,,D	=# j ELLF`=# J%,,Sk=# $Z'5<<Mn&=#  *\S4Uhi!=#" Z5;;Dfg#=#$ x~C^%=#* zU\\DFm+=#0 K5<<$\uv1=#2 :tId3=#8 zU\\DNl9=#> Z5<<$Lr?=#D JvRghE=#F J%,,DiG=#L J%,,KfM=#R Z5<<EXS=#X Z5<<UtuY=#Z 5<<Ecd~%*9M[mn#tELL'9Gh #-$u||$>#
 SYY__-tELL/A4>QR&	o=# ~ N)M-LM 
 C	 #g	   	 	  i9 z   
 
 	' 0 p & Q $k2-.)*  ,F |+ |r   