
    3j_                         S SK r S SKrS SKrS SKrS SKrS SKJr  S SKJ	r	J
r
   " S S\
R                  5      rSS jr " S S\
R                  5      r " S	 S
\
R                  5      r " S S\	5      rS\4S jrg)    N)override)ComfyExtensionioc                   T    \ rS rSr\S 5       r\SS\R                  4S jj5       rSr	g)Kandinsky5ImageToVideo   c                 T   [         R                  " SS[         R                  R                  S5      [         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  SSS[        R                  SS	9[         R
                  R                  S
SS[        R                  SS	9[         R
                  R                  SSS[        R                  SS	9[         R
                  R                  SSSSS9[         R                  R                  SSS9/[         R                  R                  SS9[         R                  R                  SS9[         R                  R                  SSS9[         R                  R                  SSS9/S9$ )Nr   model/conditioning/kandinsky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clss    =/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_kandinsky5.pydefine_schema$Kandinsky5ImageToVideo.define_schema   sI   yy,3%%j1%%j1U#Wcru?S?SZ\]Xs@T@T[]^Xsu?S?SZ[\\1!F}t<	 &&J&?&&J&?		  h@T U		  m  F_   `	
 	
    Nreturnc	                    [         R                  " USUS-
  S-  S-   US-  US-  /[        R                  R	                  5       S9n	0 n
UGb  [        R
                  R                  US U R                  SS5      XESS5      R                  SS5      nUR                  US S 2S S 2S S 2S S	24   5      nXS
'   [         R                  " SSU	R                  S   U	R                  S   U	R                  S   4UR                  UR                  S9nSUS S 2S S 2S UR                  S   S-
  S-  S-   24'   [        R                  " XUS.5      n[        R                  " X+US.5      n0 nXS
'   [        R                   " XX5      $ )Nr   r   r      )devicebilinearcenter   samples   )r8   dtypeg        r   )time_dim_replaceconcat_mask)torchzeroscomfymodel_managementintermediate_deviceutilscommon_upscalemovedimencodeonesshaper8   r@   node_helpersconditioning_set_valuesr   
NodeOutput)r0   r   r   r   r   r   r   r   r   r   cond_latent_outencodedmask
out_latents                 r1   executeKandinsky5ImageToVideo.execute#   s   j"
q/@A.EvQR{TY]^T^_hmh~h~  iS  iS  iU  V"++44[&5I5Q5QRTVW5XZ_isu}~  G  G  HI  KM  NKjjQ1bqb[!9:G)0I&::q!V\\!_fll26FUWHXYbmbtbt  }H  }N  }N  ODADDA=+++A.2q8A===>#;;HswFxyH#;;HswFxyH
 &9}}XMMr4    N
__name__
__module____qualname____firstlineno__classmethodr2   r   rP   rU   __static_attributes__rW   r4   r1   r   r      s9    
 
, Nfhfsfs N Nr4   r   c                    U R                  SSS9nU R                  SSS9n[        R                  " UR                  5       Xb-
  Xc-   5      n[        R                  " UR                  5       Xt-
  Xu-   5      n	X-
  US-   -  n
X-  U-   n
U
$ )N)r   r<   r   T)dimkeepdimg:0yE>)meanstdrC   clamp)source	referenceclump_mean_lowclump_mean_highclump_std_lowclump_std_highsource_mean
source_stdreference_meanreference_std
normalizeds              r1   adaptive_mean_std_normalizationrq   7   s    ++)T+:K	48J[[!1;3OQ\QnoNKK	1KZMhiM &:+<=J+n<Jr4   c                   P    \ rS rSr\S 5       r\S\R                  4S j5       rSr	g)NormalizeVideoLatentStartE   c                 b   [         R                  " SSS[         R                  R                  S5      [         R                  R                  SSS[
        R                  SSS	9[         R                  R                  S
SS[
        R                  SSS	9/[         R                  R                  SS9/S9$ )Nrs   zmodel/conditioningzNormalizes 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/   s    r1   r2   'NormalizeVideoLatentStart.define_schemaF   s    yy/) c		)0!H\H\cd  om  n4aQEL`L`gh  sw  x 		  h 7
 	
r4   r5   c           
      r   US   R                   S   S::  a  [        R                  " U5      $ UR                  5       nUS   R	                  5       nUS S 2S S 2S U24   nUS S 2S S 2X"[        X5R                   S   S-
  5      -   24   n[        Xg5      nXS S 2S S 2S U24'   XTS'   [        R                  " U5      $ )Nr=   r>   r   )rM   r   rP   copycloner   rq   )	r0   r   rv   rw   sr=   first_framesreference_frames_datanormalized_first_framess	            r1   rU   !NormalizeVideoLatentStart.executeV   s    )""1%*==((KKM#))+q!%7&7%778 '1.?RUVkmzmz{|m}~m  SA  AA  /A  )A  !B"A,"f,C1(((())}}Qr4   rW   NrY   rW   r4   r1   rs   rs   E   s3    
 
  "--    r4   rs   c                   P    \ rS rSr\S 5       r\S\R                  4S j5       rSr	g)CLIPTextEncodeKandinsky5f   c                 $   [         R                  " SSS/S[         R                  R                  S5      [         R                  R                  SSSS9[         R                  R                  S	SSS9/[         R
                  R                  5       /S
9$ )Nr   zCLIP Text Encode (Kandinsky 5)zkandinsky promptr
   clipclip_lT)	multilinedynamic_prompts	qwen25_7b)r!   r   search_aliasesr"   r#   r$   )r   r%   Clipr'   Stringr&   r-   r/   s    r1   r2   &CLIPTextEncodeKandinsky5.define_schemag   sz    yy.9./3f%		D$O		tTR &&(
 	
r4   r5   c                     UR                  U5      nUR                  U5      S   US'   [        R                  " UR                  U5      5      $ )Nr   )tokenizer   rP   encode_from_tokens_scheduled)r0   r   r   r   tokenss        r1   rU    CLIPTextEncodeKandinsky5.executex   sB    v&"mmI6{C{}}T>>vFGGr4   rW   NrY   rW   r4   r1   r   r   f   s6    
 
  H H Hr4   r   c                   L    \ rS rSr\S\\\R                        4S j5       r	Sr
g)Kandinsky5Extension   r5   c                 ,   #    [         [        [        /$ 7frX   )r   rs   r   )selfs    r1   get_node_list!Kandinsky5Extension.get_node_list   s      #%$
 	
s   rW   N)rZ   r[   r\   r]   r   listtyper   	ComfyNoder   r_   rW   r4   r1   r   r      s)    
T$r||*<%= 
 
r4   r   r5   c                     #    [        5       $ 7frX   )r   rW   r4   r1   comfy_entrypointr      s       s   )g333333?ffffff?r   g      ?)r*   rN   rC   comfy.model_managementrE   comfy.utilstyping_extensionsr   comfy_api.latestr   r   r   r   rq   rs   r   r   r   rW   r4   r1   <module>r      sn         & /)NR\\ )NX   BHr|| H4
. 
! 3 !r4   