o
    i                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
 G dd de
jZdd	d
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                   @   s.   e Zd Zedd ZeddejfddZdS )Kandinsky5ImageToVideoc                 C   s   t jddt jdt jdt j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	t jjdddddt jjdddgt jj	ddt jj	ddt j
j	dddt j
j	dddgdS )Nr   conditioning/video_modelspositivenegativevaewidthi      )defaultminmaxstepheighti   lengthy         
batch_sizei   )r   r   r   start_imageT)optionaldisplay_namelatentzEmpty video latent)r   tooltipcond_latentzWClean encoded start images, used to replace the noisy start of the model output latents)node_idcategoryinputsoutputs)r   SchemaConditioningInputVaeIntnodesMAX_RESOLUTIONImageOutputLatentcls r-   ;/mnt/c/Users/fbmor/ComfyUI/comfy_extras/nodes_kandinsky5.pydefine_schema   s$   


z$Kandinsky5ImageToVideo.define_schemaNreturnc	                 C   s4  t j|d|d d d |d |d gtj d}	i }
|d urtj|d | dd||dddd}||d d d d d d d d	f }||
d
< t j	dd|	j
d |	j
d |	j
d f|j|jd}d|d d d d d |j
d d d d f< t|||d}t|||d}i }|	|d
< t||||
S )Nr   r   r      )devicebilinearcenter   samples   )r2   dtypeg        r   )time_dim_replaceconcat_mask)torchzeroscomfymodel_managementintermediate_deviceutilscommon_upscalemovedimencodeonesshaper2   r:   node_helpersconditioning_set_valuesr   
NodeOutput)r,   r   r   r	   r
   r   r   r   r   r   cond_latent_outencodedmask
out_latentr-   r-   r.   execute#   s   2,&0,zKandinsky5ImageToVideo.executeN__name__
__module____qualname__classmethodr/   r   rJ   rO   r-   r-   r-   r.   r      s
    
r   333333?ffffff?      ?c                 C   sp   | j ddd}| jddd}t|  || || }t| || || }	| | |d  }
|
|	 | }
|
S )N)r   r6   r   T)dimkeepdimg:0yE>)meanstdr=   clamp)source	referenceclump_mean_lowclump_mean_highclump_std_lowclump_std_highsource_mean
source_stdreference_meanreference_std
normalizedr-   r-   r.   adaptive_mean_std_normalization7   s   ri   c                   @   ,   e Zd Zedd ZedejfddZdS )NormalizeVideoLatentStartc                 C   sZ   t jdddt jdt jjdddtjddd	t jjd
ddtjddd	gt jjddgdS )Nrk   r   zNormalizes the initial frames of a video latent to match the mean and standard deviation of subsequent reference frames. Helps reduce differences between the starting frames and the rest of the video.r   start_frame_countr   r   z<Number of latent frames to normalize, counted from the start)r   r   r   r   r   reference_frame_count   zBNumber of latent frames after the start frames to use as referencer   )r   r   descriptionr   r    )r   r!   r*   r#   r%   r&   r'   r)   r+   r-   r-   r.   r/   F   s   
z'NormalizeVideoLatentStart.define_schemar0   c           	   	   C   s   |d j d dkrt|S | }|d  }|d d d d d |f }|d d d d ||t||j d d  f }t||}||d d d d d |f< ||d< t|S )Nr7   r8   r   )rG   r   rJ   copycloner   ri   )	r,   r   rl   rm   sr7   first_framesreference_frames_datanormalized_first_framesr-   r-   r.   rO   V   s   
.

z!NormalizeVideoLatentStart.executeNrQ   r-   r-   r-   r.   rk   E   
    
rk   c                   @   rj   )CLIPTextEncodeKandinsky5c                 C   sH   t jddgdt jdt jjddddt jjddddgt j gd	S )
Nrw   zkandinsky promptz advanced/conditioning/kandinsky5clipclip_lT)	multilinedynamic_prompts	qwen25_7b)r   search_aliasesr   r   r    )r   r!   Clipr#   Stringr"   r)   r+   r-   r-   r.   r/   g   s   
z&CLIPTextEncodeKandinsky5.define_schemar0   c                 C   s,   | |}| |d |d< t||S )Nr|   )tokenizer   rJ   encode_from_tokens_scheduled)r,   rx   ry   r|   tokensr-   r-   r.   rO   w   s   
z CLIPTextEncodeKandinsky5.executeNrQ   r-   r-   r-   r.   rw   f   rv   rw   c                   @   s(   e Zd Zedeeej  fddZdS )Kandinsky5Extensionr0   c                    s   t ttgS rP   )r   rk   rw   )selfr-   r-   r.   get_node_list   s
   z!Kandinsky5Extension.get_node_listN)	rR   rS   rT   r   listtyper   	ComfyNoder   r-   r-   r-   r.   r      s    r   r0   c                      s   t  S rP   )r   r-   r-   r-   r.   comfy_entrypoint   s   r   )rV   rW   rW   rX   )r&   rH   r=   comfy.model_managementr?   comfy.utilstyping_extensionsr   comfy_api.latestr   r   r   r   ri   rk   rw   r   r   r-   r-   r-   r.   <module>   s    
,!	