
    +jt                         d dl Z d dlZd dlZd dlmZ d dlmZmZ d dl	Zd dl
Z
d dlZ G d dej                  Z G d dej                  Z G d d	ej                  Z G d
 de          ZdefdZdS )    N)override)ComfyExtensionioc                   L    e Zd Zed             Zeddej        fd            ZdS )TextEncodeQwenImageEditc                 j   t          j        ddt           j                            d          t           j                            ddd          t           j                            dd          t           j                            d	d          gt           j                                        g
          S )Nr   advanced/conditioningclippromptT	multilinedynamic_promptsvaeoptionalimagenode_idcategoryinputsoutputs	r   SchemaClipInputStringVaeImageConditioningOutputclss    7/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_qwen.pydefine_schemaz%TextEncodeQwenImageEdit.define_schema   s    y-,f%%	D$OOUT22w66	 &&((
 
 
 	
    Nreturnc                    d }|g }n|                     dd          }t          d          }t          j        ||j        d         |j        d         z  z            }	t          |j        d         |	z            }
t          |j        d         |	z            }t          j                            ||
|dd          }|                     dd          }|d d d d d d d df         g}|'|	                    |d d d d d d d df                   }|
                    ||          }|                    |          }|t          j        |d	|gid
          }t          j        |          S )N            areadisabled)imagesreference_latentsTappend)movedimintmathsqrtshaperoundcomfyutilscommon_upscaleencodetokenizeencode_from_tokens_schedulednode_helpersconditioning_set_valuesr   
NodeOutput)r"   r
   r   r   r   
ref_latentr/   samplestotalscale_bywidthheightstokensconditionings                  r#   executezTextEncodeQwenImageEdit.execute   st   
=FFmmB**G$$Ey'-*:W]1=M*M!NOOH'-*X566E7=+h677F**7E66:VVAIIa$$EAAAqqq!!!RaRK()F ZZaaaAAArrk(:;;
vf5588@@!'?ObeodpNqz~L}\***r%   )NN__name__
__module____qualname__classmethodr$   r   rA   rK    r%   r#   r   r   
   s[        
 
 [
 + +BM + + + [+ + +r%   r   c                   L    e Zd Zed             Zeddej        fd            ZdS )TextEncodeQwenImageEditPlusc                    t          j        ddt           j                            d          t           j                            ddd          t           j                            dd          t           j                            d	d          t           j                            d
d          t           j                            dd          gt           j                                        g          S )NrS   r	   r
   r   Tr   r   r   image1image2image3r   r   r!   s    r#   r$   z)TextEncodeQwenImageEditPlus.define_schema6   s    y1,f%%	D$OOUT22x$77x$77x$77 &&((
 
 
 	
r%   Nr&   c                    g }|||g}g }	d}
d}t          |          D ]\  }}||                    dd          }t          d          }t          j        ||j        d         |j        d         z  z            }t          |j        d         |z            }t          |j        d         |z            }t          j        	                    |||dd	          }|	
                    |                    dd                     |t          d
          }t          j        ||j        d         |j        d         z  z            }t          |j        d         |z  dz            dz  }t          |j        d         |z  dz            dz  }t          j        	                    |||dd	          }|
                    |                    |                    dd          d d d d d d d df                              |d                    |dz             z  }|                    ||z   |	|
          }|                    |          }t          |          dk    rt!          j        |d|id          }t%          j        |          S )Na  <|im_start|>system
Describe the key features of the input image (color, shape, size, texture, objects, background), then explain how the user's text instruction should alter or modify the image. Generate a new image that meets the user's requirements while maintaining consistency with the original input where appropriate.<|im_end|>
<|im_start|>user
{}<|im_end|>
<|im_start|>assistant
 r(   r)   i @ r+   r,   r-   r.   r*   g       @   z7Picture {}: <|vision_start|><|image_pad|><|vision_end|>)r/   llama_templater   r0   Tr1   )	enumerater3   r4   r5   r6   r7   r8   r9   r:   r;   r2   r<   formatr=   r>   lenr?   r@   r   rA   )r"   r
   r   r   rU   rV   rW   ref_latentsr/   	images_vlr[   image_promptir   rC   rD   rE   rF   rG   rH   rI   rJ   s                         r#   rK   z#TextEncodeQwenImageEditPlus.executeH   sz   &&)	 c!&)) 	h 	hHAu --A..I9UgmA.>qAQ.Q%RSSgmA.9::w}Q/(:;;K..wvvzZZ  1b!1!1222?,,E#y'-2BW]STEU2U)VWWH!'-"2X"="CDDqHE"7=#3h#>#DEEIF227E66S]^^A&&szz!))Ar2B2B111aaaBQB;2O'P'PQQQ Y ` `abefaf g gg|f4YWeff88@@{a'?ObdoNpy}~~~L}\***r%   )NNNNrL   rQ   r%   r#   rS   rS   5   s]        
 
 [
" !+ !+WYWd !+ !+ !+ [!+ !+ !+r%   rS   c                   L    e Zd Zed             Zeddej        fd            ZdS ) EmptyQwenImageLayeredLatentImagec                    t          j        dddt           j                            dddt          j        d          t           j                            dddt          j        d          t           j                            d	d
dt          j        d          t           j                            dddd          gt           j                                        g          S )Nrd   zEmpty Qwen Image Layered Latentzmodel/latent/qwenrF   i     )defaultminmaxsteprG   layersr+   r   r)   
batch_sizei   )rg   rh   ri   )r   display_namer   r   r   )r   r   Intr   nodesMAX_RESOLUTIONLatentr    r!   s    r#   r$   z.EmptyQwenImageLayeredLatentImage.define_scheman   s    y6:(Wcru?SZ\]]Xs@T[]^^XqaU=QXYZZ\1!FF	 	  ""
 
 
 	
r%   r)   r&   c                     t          j        |d|dz   |dz  |dz  gt          j                                                  }t          j        d|i          S )Nrf   r)   rZ   )devicerC   )torchzerosr9   model_managementintermediate_devicer   rA   )r"   rF   rG   rk   rl   latents         r#   rK   z(EmptyQwenImageLayeredLatentImage.execute   sl    j"fqj&A+uPQzR[`[q  \F  \F  \H  \H  I  I  I}i0111r%   N)r)   rL   rQ   r%   r#   rd   rd   m   s[        
 
 [
  2 2R] 2 2 2 [2 2 2r%   rd   c                   L    e Zd Zedeeej                          fd            ZdS )QwenExtensionr&   c                 .   K   t           t          t          gS N)r   rS   rd   )selfs    r#   get_node_listzQwenExtension.get_node_list   s       $',
 	
r%   N)	rM   rN   rO   r   listtyper   	ComfyNoder~   rQ   r%   r#   rz   rz      sE        
T$r|*<%= 
 
 
 X
 
 
r%   rz   r&   c                  "   K   t                      S r|   )rz   rQ   r%   r#   comfy_entrypointr      s      ??r%   )r?   comfy.utilsr9   r5   typing_extensionsr   comfy_api.latestr   r   comfy.model_managementrt   ro   r   r   rS   rd   rz   r   rQ   r%   r#   <module>r      sC            & & & & & & / / / / / / / /      (+ (+ (+ (+ (+bl (+ (+ (+V5+ 5+ 5+ 5+ 5+", 5+ 5+ 5+p2 2 2 2 2r| 2 2 20
 
 
 
 
N 
 
 
      r%   