o
    i`@                     @   s.  d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	m
Z
mZmZmZmZ d dlmZmZmZmZmZmZ dZdZdddd	d
eej dedededeej deej dee dejfdd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jZ"G dd deZ#de#fddZ$dS )     )OptionalN)override)IOComfyExtension)MinimaxFileRetrieveResponseMiniMaxModelMinimaxTaskResultResponseMinimaxVideoGenerationRequestMinimaxVideoGenerationResponseSubjectReferenceItem)ApiEndpointdownload_url_to_video_outputpoll_opsync_opupload_images_to_comfyapivalidate_stringr      )imagesubjectaverage_durationclsprompt_textseedmodelr   r   r   returnc                   s  |d u rt |dd d }|d urt| |ddI d H d }d }|d ur5t| |ddI d H d }	t|	dg}t| tddd	ttt||d ||d d
dI d H }
|
j}|s[t	d|
j
 t| tdd|idtdd |dI d H }|j}|d u rzt	dt| tddt|idtdI d H }|jj}|d u rt	d|  |jjrztt|dddI d H W S  t	y   tt|jjddI d H  Y S w tt|I d H S )Nr   
field_name   
max_imagesr   )r   /proxy/minimax/video_generationPOSTpathmethod)r   promptcallback_urlfirst_frame_imagesubject_referenceprompt_optimizerresponse_modeldataMiniMax generation failed: %/proxy/minimax/query/video_generationtask_idr$   query_paramsc                 S      | j jS Nstatusvaluex r:   ;/mnt/c/Users/fbmor/ComfyUI/comfy_api_nodes/nodes_minimax.py<lambda>H       z$_generate_mm_video.<locals>.<lambda>r,   status_extractorestimated_duration,Request was not successful. Missing file ID./proxy/minimax/files/retrievefile_idr,   3No video was found in the response. Full response: 
      timeoutmax_retries   rJ   )r   r   r   r   r   r
   r	   r   r0   	Exception	base_respr   r   rC   intr   filedownload_url
model_dumpbackup_download_urlr   
NodeOutputr   )r   r   r   r   r   r   r   	image_urlr)   subject_urlresponser0   task_resultrC   file_resultfile_urlr:   r:   r;   _generate_mm_video   sj   

r[   c                
   @   sF   e Zd ZedejfddZe		ddedededej	fd	d
Z
dS )MinimaxTextToVideoNoder   c                 C   s   t jddddt jjddddd	t jjd
ddgdddt jjdddddddddgt j gt jj	t jj
t jjgdt jddd	S )Nr\   zMiniMax Text to Videoapi node/video/MiniMaxzJGenerates videos synchronously based on a prompt, and optional parameters.r   T )Text prompt to guide the video generation	multilinedefaulttooltipr   T2V-01zT2V-01-Director!Model to use for video generationoptionsrb   rc   r   r       r   ,The random seed used for creating the noise.rb   minmaxstepcontrol_after_generaterc   optional{"type":"usd","usd":0.43}expr	node_iddisplay_namecategorydescriptioninputsoutputshiddenis_api_nodeprice_badge)r   SchemaStringInputComboIntVideoOutputHiddenauth_token_comfy_orgapi_key_comfy_org	unique_id
PriceBadger   r:   r:   r;   define_schemac   sL   
z$MinimaxTextToVideoNode.define_schemard   r   r   r   r   c              	      s   t | |||d d tdI d H S N)r   r   r   r   r   r   r[   T2V_AVERAGE_DURATION)r   r   r   r   r:   r:   r;   execute   s   zMinimaxTextToVideoNode.executeN)rd   r   )__name__
__module____qualname__classmethodr   r}   r   strrO   rT   r   r:   r:   r:   r;   r\   b   s    *r\   c                   @   L   e Zd ZedejfddZe		ddejde	de	d	e
dejf
d
dZdS )MinimaxImageToVideoNoder   c                 C   s   t jddddt jjdddt jjdd	d
ddt jjdg ddddt jjdddddd	dd	dgt j gt j	j
t j	jt j	jgd	t jddd	S )Nr   zMiniMax Image to Videor]   UGenerates videos synchronously based on an image and prompt, and optional parameters.r   z/Image to use as first frame of video generationrc   r   Tr^   r_   r`   r   )zI2V-01-DirectorI2V-01zI2V-01-liver   re   rf   r   r   rh   r   ri   rj   rp   rq   rs   )r   r}   Imager   r~   r   r   r   r   r   r   r   r   r   r   r:   r:   r;   r      sT   
z%MinimaxImageToVideoNode.define_schemar   r   r   r   r   r   c              	      s   t | ||||d tdI d H S r   )r[   I2V_AVERAGE_DURATION)r   r   r   r   r   r:   r:   r;   r         zMinimaxImageToVideoNode.executeN)r   r   r   r   r   r   r   r}   r   torchTensorr   rO   rT   r   r:   r:   r:   r;   r      s"    .r   c                   @   r   )MinimaxSubjectToVideoNoder   c                 C   s   t jddddt jjdddt jjdd	d
ddt jjddgdddt jjdddddd	dd	dgt j gt j	j
t j	jt j	jgd	dS )Nr   zMiniMax Subject to Videor]   r   r   z2Image of subject to reference for video generationr   r   Tr^   r_   r`   r   S2V-01re   rf   r   r   rh   r   ri   rj   )rt   ru   rv   rw   rx   ry   rz   r{   )r   r}   r   r   r~   r   r   r   r   r   r   r   r   r   r:   r:   r;   r      sN   
z'MinimaxSubjectToVideoNode.define_schemar   r   r   r   r   r   c              	      s   t | |||d |tdI d H S r   r   )r   r   r   r   r   r:   r:   r;   r     r   z!MinimaxSubjectToVideoNode.executeN)r   r   r   r:   r:   r:   r;   r      s"    +r   c                   @   sd   e Zd ZedejfddZe							dd
edede	e
j dededededejfddZdS )MinimaxHailuoVideoNoder   c                 C   s   t jddddt jjddddd	t jjd
ddddddddt jjddddt jjdddddt jjdddgddddt jjdddgddddgt j	 gt j
jt j
jt j
jgdt jt jddgdd d!d"	S )#Nr   zMiniMax Hailuo Videor]   z^Generates videos from prompt, with optional start frame using the new MiniMax Hailuo-02 model.r   Tr^   z*Text prompt to guide the video generation.r`   r   r   rh   r   ri   rj   r(   z=Optional image to use as the first frame to generate a video.)rc   ro   r*   z:Optimize prompt to improve generation quality when needed.)rb   rc   ro   duration   rF   z*The length of the output video in seconds.)rg   rb   rc   ro   
resolution768P1080PzJThe dimensions of the video display. 1080p is 1920x1080, 768p is 1366x768.)widgetsa  
                (
                  $prices := {
                    "768p": {"6": 0.28, "10": 0.56},
                    "1080p": {"6": 0.49}
                  };
                  $resPrices := $lookup($prices, $lowercase(widgets.resolution));
                  $price := $lookup($resPrices, $string(widgets.duration));
                  {"type":"usd","usd": $price ? $price : 0.43}
                )
                )
depends_onrr   rs   )r   r}   r~   r   r   r   Booleanr   r   r   r   r   r   r   r   PriceBadgeDependsr   r:   r:   r;   r   $  st   

+z$MinimaxHailuoVideoNode.define_schemar   NTr   r   MiniMax-Hailuo-02r   r   r(   r*   r   r   r   c                    s  |d u rt |dd |dkr| dkr|dkrtdd }|d ur/t| |ddI d H d	 }t| td
ddttt||d ||||ddI d H }	|	j	}
|
sVtd|	j
 |dkr\dnd}t| tdd|
idtdd |dI d H }|j}|d u r}tdt| tddt|idtdI d H }|jj}|d u rtd|  |jjrztt|ddd I d H W S  ty   tt|jjd!d"I d H  Y S w tt|I d H S )#Nr   r   r   r   r   zZWhen model is MiniMax-Hailuo-02 and resolution is 1080P, duration is limited to 6 seconds.r   r   r   r!   r"   r#   )r   r&   r'   r(   r*   r   r   r+   r.   r   x      r/   r0   r1   c                 S   r3   r4   r5   r8   r:   r:   r;   r<     r=   z0MinimaxHailuoVideoNode.execute.<locals>.<lambda>r>   rA   rB   rC   rD   rE   rF   rG   rH   rK   rL   )r   upperrM   r   r   r   r
   r	   r   r0   rN   r   r   rC   rO   r   rP   rQ   rR   rS   r   rT   r   )r   r   r   r(   r*   r   r   r   rU   rW   r0   r   rX   rC   rY   rZ   r:   r:   r;   r   m  sn   
zMinimaxHailuoVideoNode.execute)r   NTr   r   r   )r   r   r   r   r   r}   r   r   rO   r   r   r   boolrT   r   r:   r:   r:   r;   r   #  s6    H	r   c                   @   s(   e Zd Zedeeej  fddZdS )MinimaxExtensionr   c                    s   t ttgS r4   )r\   r   r   )selfr:   r:   r;   get_node_list  s
   zMinimaxExtension.get_node_listN)	r   r   r   r   listtyper   	ComfyNoder   r:   r:   r:   r;   r     s    r   c                      s   t  S r4   )r   r:   r:   r:   r;   comfy_entrypoint  s   r   )%typingr   r   typing_extensionsr   comfy_api.latestr   r   comfy_api_nodes.apis.minimaxr   r   r   r	   r
   r   comfy_api_nodes.utilr   r   r   r   r   r   r   r   r   r   r   rO   r   rT   r[   r\   r   r   r   r   r   r:   r:   r:   r;   <module>   sF      			
F>C@ 