
    bifc                     
   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
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 dd	lmZ  ej.                  e      Zi d
 ed
e	eee   f   d      d ede	eee   f   d      d ede	eee   f   d      d ede	eee   f   d      d edee   d      d edee   d      d ededd      d ededd      d edee	ej:                  eej:                     f      d      d edee   d       d! ed!ee   d"      d# ed#edd$%      d& ed&ed'd(%      d) ed)eej<                     d*      d+ ed+eej<                     d,      d- ed-ee   d.      d/ ed/ed0d1%      i d2 ed2ee   d3      d4 ed4eej<                     d5      d6 ed6eeeef      d7      d8 ed8eeeef      d9      d: ed:eeeef      d;      d< ed<eeeef      d=      d> ed>eeeef      d?      d@ ed@eeef   dAdB%      dC edCeeef   dAdD%      dE edEedFdG%      dH edHedIdJ%      dK edKedLdM%      dN edNedOdP%      dQ edQeddR      dS edSeddT      dU edUe	eee   f   dLdV%      dW edWe	eee   f   dXdY%       edZe	eee   f   dXd[%       ed\e d]d^%       ed_ee   dd`      daZ!i db edbej<                  ddc      dd eddej<                  de      df edfej<                  ddg      dh edhej<                  di      dj edjeddk      dl edlejD                  dm      dn ednee   do      d4 ed4ej<                  ddp      d) ed)ej<                  ddq      d& ed&edd(      dr edrej<                  dds      dt edtej<                  ddu      dv edvej<                  ddw      dx edxej<                  dy      dz edzej<                  dd{      d| ed|ej<                  d}      d~ ed~ej<                  d       edej<                  d       edeee      d       edeej<                     d       edeej<                     d       ede	eejF                  jF                     eej<                     eejH                     f   dd      dZ%i e!e%Z&ddddddddddddddddddd d dddddddddddddddddddddddddddddZ'dd d dddLdLdddddd]dddidZ(g dZ)dZ*g dZ+ddgdgdbgdZ,e,fdZ- G d de      Z.y)    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                 \    | y|j                         D ]  \  }}|D ]  }|| v s|c c S   y)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        a/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/modular_pipelines/node_utils.pyget_group_namer   O  sK    
 |"3"9"9"; "
J# 	"ID !!	""     c                       e Zd ZdZdZe	 ddedee   fd       Z	e
dfdZddZed	        Zd
 Zd Zed        Zd Zd Zy)ModularNodea  
    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                 D    t        j                  |fd|i|} | |fi |S )Nr   )r   from_pretrained)clsr   r   kwargsblockss        r   r   zModularNode.from_pretrainedk  s9     '66)
=N
RX
 6$V$$r   c                 b   || _         | | j                   j                  j                  }i | _        i }| j                   j                  | j                   j
                  z   }|D ]  }|j                  |j                  d       }|rT|||j                  <   |j                  d|j                        }	|	|j                  k7  r|	| j                  |j                  <   v|j                  t        vr"|j                  st        |j                        s|j                  t        v r#t        |j                     j                         }n%t        |j                        rGt        |j                        }|j                  | j                  vr|| j                  |j                  <   n|j                  $t        |j                        j                         }
nGt        j!                  |j                  d       }|r#t        |j                        j                         nd}
t"        j%                         D ]/  \  }}||
v s|j                         }|j                  |d<   d|d<    n |j                  }|||j                  <    i }| j                   j&                  D ]1  }|j                  |j                  d       }|rT|||j                  <   |j                  d|j                        }	|	|j                  k7  r|	| j                  |j                  <   vd}t(        D ]  }||j                  v sd} n |rt        |j                        rGt        |j                        }|j                  | j                  vrI|| j                  |j                  <   n/|j                  t*        v r|j                  ddd	}n|j                  }|||j                  <   4 i }t-        | j                   t.              rTt1        | j                   j2                  j5                               d
   }| j                   j2                  |   j6                  }n| j                   j6                  }|D ]  }|j                  |j                  d       }|rT|||j                  <   |j                  d|j                        }	|	|j                  k7  r|	| j                  |j                  <   v|j                  t        v r't        |j                     j                         }d|d<   nWt        |j                        }|r4|}|j                  | j                  vr&|| j                  |j                  <   n|j                  }|||j                  <    t9        |      dkD  rt:        j=                  d|        |||||| j                  d} | j>                  di | y )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__zModularNode.__init__w  s   =KK))22E ##dkk&E&EE &	+CJJsxx.E).SXX&#ii9#((*2=D%%chh/xx11#,,~^a^f^fOgxx--*388499;)&sxx0884#4#4427D%%chh/
 ==,"3==1779H044SXXtDHBJs8#5#56<<>PRH,=,C,C,E %(Hj8+ * 1),g+2i(%  HHE%*L"M&	+P KK33 	0DJJtyy$/E.3 +#ii		:$))+3>D%%dii0J9 $))+!%J dii(&tyy199D$5$5538D%%dii000"&))5KX_`		*/TYY'5	08 dkk#;<"4;;#9#9#>#>#@A"EOkk,,_=RRGkk66G 	,CJJsxx.E*/chh'#ii9#((*2=D%%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                 V    | j                   j                  ||      | _        || _        y )N)components_manager
collection)r   init_pipelinepipeline_components_manager)r   r   r   s      r   setupzModularNode.setup  s'    11EWdn1o#5 r   c                 "    | j                         S N)_convert_to_mellon_config)r   s    r   mellon_configzModularNode.mellon_config  s    --//r   c                    i }| j                   j                  |d<   | j                   j                  |d<   i }| j                   j                  j	                         D ]c  \  }}|| j
                  v r| j
                  |   }n|}t        |t              r||dd}n|}||vr|||<   It        j                  d| d|        e | j                   j                  j	                         D ]c  \  }}|| j
                  v r| j
                  |   }n|}t        |t              r||dd}n|}||vr|||<   It        j                  d| d|        e | j                   j                  j	                         D ]c  \  }	}
|	| j
                  v r| j
                  |	   }n|	}t        |
t              r|
|
dd}n|
}||vr|||<   It        j                  d	|
 d|	        e ||d
<   |S )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   z%ModularNode._convert_to_mellon_config  s   ))W;;//Z
#';;#;#;#A#A#C 	mHi4,,,"//9&)S)&%& "*,*/
;'|K=8`ai`jkl#	m& &*[[%A%A%G%G%I 	q!IzD---"//	:'*c*'&& #*,*/
;'/
|;cdmcnop#	q& $(;;#<#<#B#B#D 	lHi4,,,"//9&)S)&%' "*,*/
;'}YK7_`h_ijk#	l$ $Xr   c                 6   t        |      }t        j                  |j                  d       | j                  | j
                  d}t        |dd      5 }t        j                  ||d       d	d	d	       t        j                  d
|        |S # 1 sw Y   #xY w)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_configzModularNode.save_mellon_configF  s     O	 	I$$t4 !..@Q@QR )S73 	+qIIfa*	+ 	>ykJK	+ 	+s   BBc                     t        |      }|j                         st        d|       t        |dd      5 }t	        j
                  |      }ddd       t        j                  d|        S # 1 sw Y   #xY w)a  
        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_configzModularNode.load_mellon_config`  sw     O	!#&=i[$IJJ)S73 	"qYYq\F	" 	0<=	" 	"s   A00A9c                 Z   i }| j                   j                  j                         D ]  \  }}t        j	                  d|        | j
                  j                  ||      }||v s?t        ||   t              r|||   v r||   j                  |      }n|j                  |      }|s| j                  j                  |d         ||<    i }| j                   j                  j                         D ]  \  }}	t        j	                  d|        | j
                  j                  ||      }
|
|v s?t        ||
   t              r|||
   v r||
   j                  |      }n|j                  |
      }||||<    t        | j                   j                  j                               }|||fS )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_inputszModularNode.process_inputsw  s   %)[[%A%A%G%G%I 		f!IzLL;yk23#0044Y	J6)f%56=)vVfOgBg!"2377	BD!::&67D373K3K3S3STXYcTd3e%i0		f 
#';;#;#;#A#A#C 		/HiLL78*-."//33HhGO&(f_5t<VTcMdAd 155h?C **_5C?+.Jx(		/ #4;;#<#<#A#A#CD *.AAAr   c                      | j                   di |\  }}} | j                  j                  di |  | j                  di |d|i}|S )Nrb   r   )r   r   update_components)r   r   r   r   r   rb   s         r   executezModularNode.execute  sW    =PT=P=P=ZSY=Z::':'''<*;<HH4GHr   r   )r   
__module____qualname____doc__config_nameclassmethodrj   r   rk   r   DEFAULT_CATEGORYr   r   propertyr   r   r   r   r   r   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     ss	     	  / /  
  - 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   