
    iU>                     |   d dl mZ ddlmZmZ ddlmZ ddlmZm	Z	m
Z
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mZmZ dd
lmZmZmZmZ  ej:                  e      Z ed e       fd e       fg      Z  G d de      Z! G d de      Z" ed e       fd ed      fg      Z# G d de      Z$ G d de      Z% ed e       fd e       fd e       fg      Z& G d de      Z' ed e       fd e	       fd e       fd e       fg      Z( G d de      Z) G d d e      Z* ed e       fd e       fd e
       fg      Z+ G d! d"e      Z, G d# d$e      Z- G d% d&e      Z. G d' d(e      Z/ G d) d*e      Z0 ed+ e       fd, e       fg      Z1 G d- d.e      Z2 G d/ d0e      Z3 ed1 e       fd+ e       fd, e       fg      Z4 G d2 d3e      Z5 G d4 d5e      Z6 G d6 d7e      Z7 G d8 d9e      Z8 ed: e       fd; e"       fd< e7       fd= e       fg      Z9 ed: e       fd; e%       fd< e8       fd= e       fg      Z: G d> d?e      Z; G d@ dAe;      Z< ed: e       fdB e       fd e       fd e       fd e       fd< e       fd= e       fg      Z= ed: e       fdC e       fdB e2       fd e       fd e	       fd e       fd e       fd< e       fd= e       fg	      Z> ed: e       fdC ed      fdB e5       fd e       fd e       fd e
       fd< e       fd= e       fg      Z?e=e>e9e:e?dDZ@yE)F   )logging   )AutoPipelineBlocksSequentialPipelineBlocks)InsertableDict   )FluxImg2ImgPrepareLatentsStepFluxImg2ImgSetTimestepsStepFluxKontextRoPEInputsStepFluxPrepareLatentsStepFluxRoPEInputsStepFluxSetTimestepsStep)FluxDecodeStep)FluxDenoiseStepFluxKontextDenoiseStep)!FluxKontextProcessImagesInputStepFluxProcessImagesInputStepFluxTextEncoderStepFluxVaeEncoderDynamicStep)FluxInputsDynamicStepFluxKontextInputsDynamicStepFluxKontextSetResolutionStepFluxTextInputStep
preprocessencodec                   f    e Zd ZdZej                         Zej                         Ze	de
fd       Zy)FluxImg2ImgVaeEncoderStepfluxreturnc                      yNz^Vae encoder step that preprocess andencode the image inputs into their latent representations. selfs    y/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/diffusers/modular_pipelines/flux/modular_blocks.pydescriptionz%FluxImg2ImgVaeEncoderStep.description9       o    N)__name__
__module____qualname__
model_nameFluxImg2ImgVaeEncoderBlocksvaluesblock_classeskeysblock_namespropertystrr&   r"   r(   r%   r   r   3   s?    J/668M-224KpS p pr(   r   c                   .    e Zd ZegZdgZdgZed        Zy)FluxAutoVaeEncoderStepimg2imgimagec                      	 y)Na  Vae encoder step that encode the image inputs into their latent representations.
This is an auto pipeline block that works for img2img tasks.
 - `FluxImg2ImgVaeEncoderStep` (img2img) is used when only `image` is provided. - if `image` is not provided, step will be skipped.r"   r#   s    r%   r&   z"FluxAutoVaeEncoderStep.descriptionC       E	
r(   N)	r)   r*   r+   r   r/   r1   block_trigger_inputsr2   r&   r"   r(   r%   r5   r5   >   +    ./M+K#9
 
r(   r5   argmax)sample_modec                   f    e Zd ZdZej                         Zej                         Ze	de
fd       Zy)FluxKontextVaeEncoderStepflux-kontextr   c                      yr!   r"   r#   s    r%   r&   z%FluxKontextVaeEncoderStep.descriptionZ   r'   r(   N)r)   r*   r+   r,   FluxKontextVaeEncoderBlocksr.   r/   r0   r1   r2   r3   r&   r"   r(   r%   r?   r?   T   s?    J/668M-224KpS p pr(   r?   c                   .    e Zd ZegZdgZdgZed        Zy)FluxKontextAutoVaeEncoderStepr6   r7   c                      	 y)Na  Vae encoder step that encode the image inputs into their latent representations.
This is an auto pipeline block that works for img2img tasks.
 - `FluxKontextVaeEncoderStep` (img2img) is used when only `image` is provided. - if `image` is not provided, step will be skipped.r"   r#   s    r%   r&   z)FluxKontextAutoVaeEncoderStep.descriptiond   r9   r(   N)	r)   r*   r+   r?   r/   r1   r:   r2   r&   r"   r(   r%   rD   rD   _   r;   r(   rD   prepare_latentsset_timestepsprepare_rope_inputsc                   \    e Zd Zej	                         Zej                         Zed        Z	y)FluxBeforeDenoiseStepc                      y)Nz^Before denoise step that prepares the inputs for the denoise step in text-to-image generation.r"   r#   s    r%   r&   z!FluxBeforeDenoiseStep.description|   r'   r(   N)
r)   r*   r+   FluxBeforeDenoiseBlocksr.   r/   r0   r1   r2   r&   r"   r(   r%   rJ   rJ   x   s2    +224M)..0Kp pr(   rJ   prepare_img2img_latentsc                   \    e Zd Zej	                         Zej                         Zed        Z	y)FluxImg2ImgBeforeDenoiseStepc                      y)NzRBefore denoise step that prepare the inputs for the denoise step for img2img task.r"   r#   s    r%   r&   z(FluxImg2ImgBeforeDenoiseStep.description   s    cr(   N)
r)   r*   r+   FluxImg2ImgBeforeDenoiseBlocksr.   r/   r0   r1   r2   r&   r"   r(   r%   rO   rO      s2    299;M0557Kd dr(   rO   c                   8    e Zd ZdZeegZddgZddgZe	d        Z
y)FluxAutoBeforeDenoiseStepr@   r6   
text2imageimage_latentsNc                      	 y)Na  Before denoise step that prepare the inputs for the denoise step.
This is an auto pipeline block that works for text2image.
 - `FluxBeforeDenoiseStep` (text2image) is used.
 - `FluxImg2ImgBeforeDenoiseStep` (img2img) is used when only `image_latents` is provided.
r"   r#   s    r%   r&   z%FluxAutoBeforeDenoiseStep.description       m	
r(   )r)   r*   r+   r,   rO   rJ   r/   r1   r:   r2   r&   r"   r(   r%   rS   rS      s9    J13HIMl+K+T2
 
r(   rS   c                   \    e Zd Zej	                         Zej                         Zed        Z	y)FluxKontextBeforeDenoiseStepc                      	 y)NzlBefore denoise step that prepare the inputs for the denoise step
for img2img/text2img task for Flux Kontext.r"   r#   s    r%   r&   z(FluxKontextBeforeDenoiseStep.description   s    :	
r(   N)
r)   r*   r+   FluxKontextBeforeDenoiseBlocksr.   r/   r0   r1   r2   r&   r"   r(   r%   rY   rY      s0    299;M0557K
 
r(   rY   c                   4    e Zd ZeegZddgZddgZed        Z	y) FluxKontextAutoBeforeDenoiseStepr6   rT   rU   Nc                      	 y)Na  Before denoise step that prepare the inputs for the denoise step.
This is an auto pipeline block that works for text2image.
 - `FluxBeforeDenoiseStep` (text2image) is used.
 - `FluxKontextBeforeDenoiseStep` (img2img) is used when only `image_latents` is provided.
r"   r#   s    r%   r&   z,FluxKontextAutoBeforeDenoiseStep.description   rW   r(   )
r)   r*   r+   rY   rJ   r/   r1   r:   r2   r&   r"   r(   r%   r]   r]      s4    13HIMl+K+T2
 
r(   r]   c                   4    e Zd ZegZdgZdgZedefd       Z	y)FluxAutoDenoiseStepdenoiseNr   c                      	 y)NzDenoise step that iteratively denoise the latents. This is a auto pipeline block that works for text2image and img2img tasks. - `FluxDenoiseStep` (denoise) for text2image and img2img tasks.r"   r#   s    r%   r&   zFluxAutoDenoiseStep.description       O	
r(   )
r)   r*   r+   r   r/   r1   r:   r2   r3   r&   r"   r(   r%   r`   r`      s2    $%M+K 6
S 
 
r(   r`   c                   4    e Zd ZegZdgZdgZedefd       Z	y)FluxKontextAutoDenoiseStepra   Nr   c                      	 y)NzDenoise step that iteratively denoise the latents for Flux Kontext. This is a auto pipeline block that works for text2image and img2img tasks. - `FluxDenoiseStep` (denoise) for text2image and img2img tasks.r"   r#   s    r%   r&   z&FluxKontextAutoDenoiseStep.description   rc   r(   )
r)   r*   r+   r   r/   r1   r:   r2   r3   r&   r"   r(   r%   re   re      s2    +,M+K 6
S 
 
r(   re   c                   .    e Zd ZegZdgZdgZed        Zy)FluxAutoDecodeStepznon-inpaintNc                      y)NzTDecode step that decode the denoised latents into image outputs.
 - `FluxDecodeStep`r"   r#   s    r%   r&   zFluxAutoDecodeStep.description   s    fr(   )	r)   r*   r+   r   r/   r1   r:   r2   r&   r"   r(   r%   rh   rh      s-    #$M /K 6g gr(   rh   text_inputsadditional_inputsc                   `    e Zd ZdZej                         Zej                         Ze	d        Z
y)FluxImg2ImgInputStepr   c                      y)NzHInput step that prepares the inputs for the img2img denoising step. It:
r"   r#   s    r%   r&   z FluxImg2ImgInputStep.description  s    Zr(   N)r)   r*   r+   r,   FluxImg2ImgBlocksr.   r/   r0   r1   r2   r&   r"   r(   r%   rm   rm      s7    J%,,.M#((*KR Rr(   rm   c                   4    e Zd ZeegZddgZddgZed        Z	y)FluxAutoInputStepr6   rT   rU   Nc                      	 y)Nal  Input step that standardize the inputs for the denoising step, e.g. make sure inputs have consistent batch size, and patchified. 
 This is an auto pipeline block that works for text2image/img2img tasks.
 - `FluxImg2ImgInputStep` (img2img) is used when `image_latents` is provided.
 - `FluxTextInputStep` (text2image) is used when `image_latents` are not provided.
r"   r#   s    r%   r&   zFluxAutoInputStep.description  s    e	
r(   )
r)   r*   r+   rm   r   r/   r1   r:   r2   r&   r"   r(   r%   rq   rq   	  s4    )+<=Ml+K+T2
 
r(   rq   set_resolutionc                   `    e Zd ZdZej                         Zej                         Ze	d        Z
y)FluxKontextInputStepr@   c                      	 y)Na  Input step that prepares the inputs for the both text2img and img2img denoising step. It:
 - make sure the text embeddings have consistent batch size as well as the additional inputs (`image_latents`).
 - update height/width based `image_latents`, patchify `image_latents`.r"   r#   s    r%   r&   z FluxKontextInputStep.description(  s    V	
r(   N)r)   r*   r+   r,   FluxKontextBlocksr.   r/   r0   r1   r2   r&   r"   r(   r%   ru   ru   #  s5    J%,,.M#((*K
 
r(   ru   c                   4    e Zd ZeegZddgZddgZed        Z	y)FluxKontextAutoInputStepr6   text2imgrU   Nc                      	 y)Na  Input step that standardize the inputs for the denoising step, e.g. make sure inputs have consistent batch size, and patchified. 
 This is an auto pipeline block that works for text2image/img2img tasks.
 - `FluxKontextInputStep` (img2img) is used when `image_latents` is provided.
 - `FluxKontextInputStep` is also capable of handling text2image task when `image_latent` isn't present.r"   r#   s    r%   r&   z$FluxKontextAutoInputStep.description9  s    y	
r(   )
r)   r*   r+   ru   r   r/   r1   r:   r2   r&   r"   r(   r%   ry   ry   1  s6    )+<=Mj)K+T2 
 
r(   ry   c                   2    e Zd ZdZeeegZg dZe	d        Z
y)FluxCoreDenoiseStepr   inputbefore_denoisera   c                      	 y)Na  Core step that performs the denoising process. 
 - `FluxAutoInputStep` (input) standardizes the inputs for the denoising step.
 - `FluxAutoBeforeDenoiseStep` (before_denoise) prepares the inputs for the denoising step.
 - `FluxAutoDenoiseStep` (denoise) iteratively denoises the latents.
This step supports text-to-image and image-to-image tasks for Flux:
 - for image-to-image generation, you need to provide `image_latents`
 - for text-to-image generation, all you need to provide is prompt embeddings.r"   r#   s    r%   r&   zFluxCoreDenoiseStep.descriptionH      _	
r(   N)r)   r*   r+   r,   rq   rS   r`   r/   r1   r2   r&   r"   r(   r%   r}   r}   C  s,    J&(ACVWM8K	
 	
r(   r}   c                   2    e Zd ZdZeeegZg dZe	d        Z
y)FluxKontextCoreDenoiseStepr@   r~   c                      	 y)Na  Core step that performs the denoising process. 
 - `FluxKontextAutoInputStep` (input) standardizes the inputs for the denoising step.
 - `FluxKontextAutoBeforeDenoiseStep` (before_denoise) prepares the inputs for the denoising step.
 - `FluxKontextAutoDenoiseStep` (denoise) iteratively denoises the latents.
This step supports text-to-image and image-to-image tasks for Flux:
 - for image-to-image generation, you need to provide `image_latents`
 - for text-to-image generation, all you need to provide is prompt embeddings.r"   r#   s    r%   r&   z&FluxKontextCoreDenoiseStep.descriptionZ  r   r(   N)r)   r*   r+   r,   ry   r]   re   r/   r1   r2   r&   r"   r(   r%   r   r   U  s,    J-/OQklM8K	
 	
r(   r   text_encoderimage_encoderra   decodec                   `    e Zd ZdZej                         Zej                         Ze	d        Z
y)FluxAutoBlocksr   c                      	 y)NzAuto Modular pipeline for text-to-image and image-to-image using Flux.
- for text-to-image generation, all you need to provide is `prompt`
- for image-to-image generation, you need to provide either `image` or `image_latents`r"   r#   s    r%   r&   zFluxAutoBlocks.description  s    g	
r(   N)r)   r*   r+   r,   AUTO_BLOCKSr.   r/   r0   r1   r2   r&   r"   r(   r%   r   r   {  s5    J&&(M""$K
 
r(   r   c                   P    e Zd ZdZej                         Zej                         Zy)FluxKontextAutoBlocksr@   N)	r)   r*   r+   r,   AUTO_BLOCKS_KONTEXTr.   r/   r0   r1   r"   r(   r%   r   r     s#    J'..0M%**,Kr(   r   r   vae_encoder)rT   r6   autoauto_kontextkontextN)Autilsr   modular_pipeliner   r   modular_pipeline_utilsr   r   r	   r
   r   r   r   r   decodersr   ra   r   r   encodersr   r   r   r   inputsr   r   r   r   
get_loggerr)   loggerr-   r   r5   rB   r?   rD   rL   rJ   rQ   rO   rS   r[   rY   r]   r`   re   rh   ro   rm   rq   rw   ru   ry   r}   r   r   r   r   r   TEXT2IMAGE_BLOCKSIMAGE2IMAGE_BLOCKSFLUX_KONTEXT_BLOCKS
ALL_BLOCKSr"   r(   r%   <module>r      s    K 3  % <   
		H	% -.01H>W>Y3Z[ 
p 8 p
/ 
" -5788E^ksEt:uv 
p 8 p
$6 
  )	245	.01	 2 45 p4 p "0	245	578	"$A$CD	 2 45	" d#; d
 2 
$ "0	245	.01	 9 ;<" 	
#; 	

'9 
 
, 
"
!3 
g+ g #&(),?AVAX+YZ 
	R3 	R
* 
" #	79:	)+,	:<= 
3 

1 
$
2 
$
!9 
& 	,./	023	')*	>#$	 %	,./	79:	.01	>#$	 
- 
-N - #	,./	#%&	245	.01	 2 45	O%&	>#$
  $	,./	134	&()	245	578	"$A$CD	 2 45	O%&	>#$
  %	,./	1hGH	&()	245	.01	 9 ;<	*,-	>#$	  $!'"
r(   