
    
3jr&                        S SK r S SKrS SKJrJrJrJr  S SKrS SKJ	r	  \ R                  " \5      rSrSrS r  SS\\\\   4   S\\\\R"                  \\4         S	\S
\\   4S jjrS\\R"                  \\4   S
\\	R                     4S jrS\\   S	\S\S
\\   4S jrS\\   S\\	R                     S	\S\S
\\   4
S jr SS\\	R                     S\S
\\   4S jjrS rS	\S
\\   4S jrg)    N)ListOptionalTupleUnion)Imagea{	  You are an expert cinematic director with many award winning movies, When writing prompts based on the user input, focus on detailed, chronological descriptions of actions and scenes.
Include specific movements, appearances, camera angles, and environmental details - all in a single flowing paragraph.
Start directly with the action, and keep descriptions literal and precise.
Think like a cinematographer describing a shot list.
Do not change the user input intent, just enhance it.
Keep within 150 words.
For best results, build your prompts using this structure:
Start with main action in a single sentence
Add specific details about movements and gestures
Describe character/object appearances precisely
Include background and environment details
Specify camera angles and movements
Describe lighting and colors
Note any changes or sudden events
Do not exceed the 150 word limit!
Output the enhanced prompt only.

Examples:
user prompt: A man drives a toyota car.
enhanced prompt: A person is driving a car on a two-lane road, holding the steering wheel with both hands. The person's hands are light-skinned and they are wearing a black long-sleeved shirt. The steering wheel has a Toyota logo in the center and black leather around it. The car's dashboard is visible, showing a speedometer, tachometer, and navigation screen. The road ahead is straight and there are trees and fields visible on either side. The camera is positioned inside the car, providing a view from the driver's perspective. The lighting is natural and overcast, with a slightly cool tone.

user prompt: A young woman is sitting on a chair.
enhanced prompt: A young woman with dark, curly hair and pale skin sits on a chair; she wears a dark, intricately patterned dress with a high collar and long, dark gloves that extend past her elbows; the scene is dimly lit, with light streaming in from a large window behind the characters.

user prompt: Aerial view of a city skyline.
enhanced prompt: The camera pans across a cityscape of tall buildings with a circular building in the center. The camera moves from left to right, showing the tops of the buildings and the circular building in the center. The buildings are various shades of gray and white, and the circular building has a green roof. The camera angle is high, looking down at the city. The lighting is bright, with the sun shining from the upper left, casting shadows from the buildings.
aI  You are an expert cinematic director with many award winning movies, When writing prompts based on the user input, focus on detailed, chronological descriptions of actions and scenes.
Include specific movements, appearances, camera angles, and environmental details - all in a single flowing paragraph.
Start directly with the action, and keep descriptions literal and precise.
Think like a cinematographer describing a shot list.
Keep within 150 words.
For best results, build your prompts using this structure:
Describe the image first and then add the user input. Image description should be in first priority! Align to the image caption if it contradicts the user text input.
Start with main action in a single sentence
Add specific details about movements and gestures
Describe character/object appearances precisely
Include background and environment details
Specify camera angles and movements
Describe lighting and colors
Note any changes or sudden events
Align to the image caption if it contradicts the user text input.
Do not exceed the 150 word limit!
Output the enhanced prompt only.
c                    U R                  5       S:  a  U R                  5       S::  d   eU S-   S-  n U R                  SSS5      n U R                  5       R	                  5       S-  R                  S5      n[        R                  " U5      $ )N      r      uint8)minmaxpermutecpunumpyastyper   	fromarray)tensornumpy_images     S/home/wildlama/comfy/ComfyUI/custom_nodes/ComfyUI-LTXVideo/prompt_enhancer_utils.pytensor_to_pilr   :   s~    ::<2&**,!"333 qjAF ^^Aq!$F ::<%%'#-55g>K ??;''    promptconditioning_itemsmax_new_tokensreturnc           
          [        U[        5      (       a  U/OUnUc  [        UUUU[        5      nU$ US   n[	        U5      n	[        U	5      [        U5      :X  d   S5       e[        U UUUUU	U[        5      nU$ )Nr   z:Number of conditioning frames must match number of prompts)
isinstancestr_generate_t2v_promptT2V_CINEMATIC_PROMPT(_get_first_frames_from_conditioning_itemlen_generate_i2v_promptI2V_CINEMATIC_PROMPT)
image_caption_modelimage_caption_processorprompt_enhancer_modelprompt_enhancer_tokenizerr   r   r   promptsfirst_frame_conditioning_itemfirst_framess
             r   generate_cinematic_promptr.   K   s     %VS11vhvG!&!% 
D N) );1(=%?)
 < C%
 
 	HG	H 
 '#!% 	
 Nr   conditioning_itemc                     U S   n[        UR                  S   5       Vs/ s H  n[        XS S 2SS S 2S S 24   5      PM     sn$ s  snf )Nr   )rangeshaper   )r/   frames_tensoris      r   r#   r#   |   sW     &a(M }**1-..A 	mq!QM23.  s   "Ar+   system_promptc           	          U Vs/ s H  nSUS.SSU 3S./PM     nnU Vs/ s H  nUR                  USSS9PM     nnU" USS	9R                  U R                  5      n	[        XX5      $ s  snf s  snf )
Nsystemrolecontentuseruser_prompt: FTtokenizeadd_generation_promptptreturn_tensors)apply_chat_templatetodevice_generate_and_decode_prompts)
r)   r*   r+   r   r5   pmessagesmtextsmodel_inputss
             r   r!   r!      s     
 A -8-s(;<	
    	 A 	"55T 	6 	
 	 
  -U4HKK$$L (, %s
   A+A0r-   c           
      $   [        XU5      n[        XH5       V	V
s/ s H  u  pSUS.SSU	 SU
 3S./PM     nn	n
U Vs/ s H  nUR                  USSS9PM     nnU" US	S
9R                  UR                  5      n[        X#X5      $ s  sn
n	f s  snf )Nr7   r8   r;   r<   z
image_caption: FTr=   r@   rA   )_generate_image_captionsziprC   rD   rE   rF   )r'   r(   r)   r*   r+   r-   r   r5   image_captionsrG   crH   rI   rJ   rK   s                  r   r%   r%      s     .lN 0
 1DA -8-s:KA3(OP	
 1   	 A 	"55T 	6 	
 	 
  -U4HKK$$L (, %s
   BBimagesc           	         U/[        U5      -  nU" XBSS9R                  U R                  5      n[        R                  " 5          U R                  US   US   SSSS9nS S S 5        UR                  WS	S
9$ ! , (       d  f       N= f)Nr@   rA   	input_idspixel_valuesi   F   )rS   rT   r   	do_sample	num_beamsTskip_special_tokens)r$   rD   rE   torchinference_modegeneratebatch_decode)r'   r(   rQ   r5   image_caption_promptsinputsgenerated_idss          r   rM   rM      s     +Oc&k9$db		#	#$  
			+44[)/ 5 
 
  #//SW/XX 
 	s   A77
Bc                  6    / SQn [         R                  " U 5      $ )z4
Randomly select a scene type to add to the prompt.
)z+The scene is captured in real-life footage.z(The scene is computer-generated imagery.z%The scene appears to be from a movie.z'The scene appears to be from a TV show.z"The scene is captured in a studio.)randomchoice)typess    r   _get_random_scene_typere      s    E ==r   c           	         [         R                  " 5          U R                  " S0 UDSU0D6n[        UR                  U5       VVs/ s H  u  pVU[        U5      S  PM     nnnUR                  USS9nS S S 5        W V	s/ s H  oS[        5        S3-   PM     nn	[        U5        U$ s  snnf ! , (       d  f       NA= fs  sn	f )Nr   TrX    . )	rZ   r[   r\   rN   rS   r$   r]   re   print)
r)   r*   rK   r   outputsrS   
output_idsr`   decoded_promptsrG   s
             r   rF   rF      s     
			'00 

+9

 *-\-C-CW)M
)M%	 s9~'()M 	 
 4@@t A 
 
  ETTOqQ578::OOT	/
	 
 	 Us#   0B.B( B.?B?(B..
B<)N   )z<DETAILED_CAPTION>)loggingrb   typingr   r   r   r   rZ   PILr   	getLogger__name__loggerr"   r&   r   r    Tensorintfloatr.   r#   r!   r%   rM   re   rF   ri   r   r   <module>rx      s     / /  			8	$ 8 ((. KO.
 #tCy.!. !eELL#u,D&E!FG. . 
#Y.bU\\356	%++ #Y 	
  
#Y<"
 #Y" u{{#" " " 
#Y"R .	Y Y 	Y
 
#YY. TW	#Yr   