o
    i                     @   s   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 dejZG dd dejZG dd	 d	ejZG d
d deZdefddZdS )    N)override)ComfyExtensionioc                   @   .   e Zd Zedd ZeddejfddZdS )TextEncodeQwenImageEditc              
   C   sP   t jddt jdt jjddddt jjdddt jjd	ddgt j gd
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cls r!   5/mnt/c/Users/fbmor/ComfyUI/comfy_extras/nodes_qwen.pydefine_schema   s   
z%TextEncodeQwenImageEdit.define_schemaNreturnc                 C   s  d }|d u r	g }nc| dd}td}t||jd |jd   }	t|jd |	 }
t|jd |	 }tj||
|dd}| dd}|d d d d d d d df g}|d url|	|d d d d d d d df }|j
||d}||}|d urtj|d	|gid
d}t|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conditioningr!   r!   r"   execute   s$   "&

zTextEncodeQwenImageEdit.execute)NN__name__
__module____qualname__classmethodr#   r   r>   rH   r!   r!   r!   r"   r   
   s
    
r   c                   @   r   )TextEncodeQwenImageEditPlusc                 C   sl   t jddt jdt jjddddt jjdddt jjd	ddt jjd
ddt jjdddgt j gdS )NrN   r   r   r	   Tr
   r   r   image1image2image3r   r   r   r!   r!   r"   r#   6   s   
	z)TextEncodeQwenImageEditPlus.define_schemaNr$   c                 C   s  g }|||g}g }	d}
d}t |D ]\}}|d ur|dd}td}t||jd |jd   }t|jd | }t|jd | }tj	|||dd	}|	
|dd |d urtd
}t||jd |jd   }t|jd | d d }t|jd | d d }tj	|||dd	}|
||ddd d d d d d d df  |d|d 7 }q|j|| |	|
d}||}t|dkrtj|d|idd}t|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   r-   Tr.   )	enumerater0   r1   r2   r3   r4   r5   r6   r7   r8   r/   r9   formatr:   r;   lenr<   r=   r   r>   )r    r   r	   r   rO   rP   rQ   ref_latentsr,   	images_vlrT   image_promptir   r@   rA   rB   rC   rD   rE   rF   rG   r!   r!   r"   rH   H   s8   
4

z#TextEncodeQwenImageEditPlus.execute)NNNNrI   r!   r!   r!   r"   rN   5   s
    
rN   c                   @   s.   e Zd Zedd ZeddejfddZdS )	 EmptyQwenImageLayeredLatentImagec                 C   sr   t jdddt jjdddtjddt jjdddtjddt jjd	d
dtjdddt jjdddddgt j gdS )Nr\   zEmpty Qwen Image Layered Latentzlatent/qwenrC   i     )defaultminmaxsteprD   layersr(   r   r&   T)r^   r_   r`   ra   advanced
batch_sizei   )r^   r_   r`   )r   display_namer   r   r   )r   r   Intr   nodesMAX_RESOLUTIONLatentr   r   r!   r!   r"   r#   n   s   z.EmptyQwenImageLayeredLatentImage.define_schemar&   r$   c                 C   s8   t j|d|d |d |d gtj d}td|iS )Nr]   r&   rS   )devicer@   )torchzerosr6   model_managementintermediate_devicer   r>   )r    rC   rD   rb   rd   latentr!   r!   r"   rH      s   *z(EmptyQwenImageLayeredLatentImage.executeN)r&   rI   r!   r!   r!   r"   r\   m   s
    
r\   c                   @   s(   e Zd Zedeeej  fddZdS )QwenExtensionr$   c                    s   t ttgS N)r   rN   r\   )selfr!   r!   r"   get_node_list   s
   zQwenExtension.get_node_listN)	rJ   rK   rL   r   listtyper   	ComfyNoders   r!   r!   r!   r"   rp      s    rp   r$   c                      s   t  S rq   )rp   r!   r!   r!   r"   comfy_entrypoint   s   rw   )r<   comfy.utilsr6   r2   typing_extensionsr   comfy_api.latestr   r   comfy.model_managementrk   rg   rv   r   rN   r\   rp   rw   r!   r!   r!   r"   <module>   s    +8
