o
    iQB                     @   s*  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mZ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Zd	eej 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%ej&ej'g ddddZ(G dd deZ)de)fddZ*dS )    N)override)IOComfyExtension)PixverseTextVideoRequestPixverseImageVideoRequestPixverseTransitionVideoRequestPixverseImageUploadResponsePixverseVideoResponse PixverseGenerationStatusResponsePixverseAspectRatioPixverseQualityPixverseDurationPixverseMotionModePixverseStatus
PixverseIOpixverse_templates)ApiEndpointdownload_url_to_video_outputpoll_opsync_optensor_to_bytesiovalidate_string       4   clsimagec                    sN   t | tdddtdt|iddI d H }|jd u r#td|j d|jjS )	Nz/proxy/pixverse/image/uploadPOSTpathmethodr   zmultipart/form-data)response_modelfilescontent_typez'PixVerse image upload request failed: '')r   r   r   r   Resp	ExceptionErrMsgimg_id)r   r   response_upload r*   </mnt/c/Users/fbmor/ComfyUI/comfy_api_nodes/nodes_pixverse.pyupload_image_to_pixverse!   s   


r,   c                   @   s<   e Zd ZdZedejfddZededej	fddZ
dS )	PixverseTemplateNodez8
    Select template for PixVerse Video generation.
    returnc              	   C   s<   t jdddt jjdtt dgt tj	j
ddgdS )	Nr-   zPixVerse Templateapi node/video/PixVersetemplateoptionspixverse_template)display_name)node_idr4   categoryinputsoutputs)r   SchemaComboInputlistr   keysCustomr   TEMPLATEOutputr   r*   r*   r+   define_schema3   s   z"PixverseTemplateNode.define_schemar0   c                 C   s.   t |d }|d u rtd| dt|S )Nz
Template 'z' is not recognized.)r   getr&   r   
NodeOutput)r   r0   template_idr*   r*   r+   execute?   s   
zPixverseTemplateNode.executeN)__name__
__module____qualname____doc__classmethodr   r9   rB   strrD   rF   r*   r*   r*   r+   r-   .   s    r-   c                   @   sV   e Zd ZedejfddZe		ddedededed	ed
ededej	fddZ
dS )PixverseTextToVideoNoder.   c                 C   s   t jddddt jjddddd	t jjd
tdt jjdttjdt jjdtdt jjdt	dt j
jdddddddt jjddddddt tjjddddgt j gt jjt jjt jjgdtd	S )NrM   zPixVerse Text to Videor/   1Generates videos based on prompt and output_size.promptT Prompt for the video generation	multilinedefaulttooltipaspect_ratior1   qualityr2   rT   duration_secondsmotion_modeseedr   Seed for video generation.rT   minmaxcontrol_after_generaterU   negative_prompt?An optional text description of undesired elements on an image.rT   rS   rU   optionalr3   ]An optional template to influence style of generation, created by the PixVerse Template node.rU   re   	r5   r4   r6   descriptionr7   r8   hiddenis_api_nodeprice_badge)r   r9   Stringr;   r:   r   r   res_540pr   r   Intr>   r   r?   Videor@   Hiddenauth_token_comfy_orgapi_key_comfy_org	unique_idPRICE_BADGE_VIDEOrA   r*   r*   r+   rB   H   sr   
-z%PixverseTextToVideoNode.define_schemaNrO   rV   rW   rY   rZ   rb   r3   c	                    s   t |ddd |tjkrtj}tj}n|tjkrtj}t| tdddt	t
||||||r.|nd ||ddI d H }	|	jd u rGtd	|	j d
t| td|	jj dttjgtjtjtjgdd tdI d H }
tt|
jjI d H S )NF   )strip_whitespace
min_lengthz#/proxy/pixverse/video/text/generater   r   )rO   rV   rW   durationrZ   rb   rE   r[   r!   dataPixVerse request failed: 'r$   /proxy/pixverse/video/result/r   c                 S      | j jS Nr%   statusxr*   r*   r+   <lambda>       z1PixverseTextToVideoNode.execute.<locals>.<lambda>r!   completed_statusesfailed_statusesstatus_extractorestimated_duration)r   r   	res_1080pr   normalr   dur_5r   r   r	   r   r%   r&   r'   r   video_idr
   r   
successfulcontents_moderationfaileddeletedAVERAGE_DURATION_T2Vr   rD   r   url)r   rO   rV   rW   rY   rZ   r[   rb   r3   response_apiresponse_pollr*   r*   r+   rF      sJ   




zPixverseTextToVideoNode.executeNN)rG   rH   rI   rK   r   r9   rB   rL   intrD   rF   r*   r*   r*   r+   rM   G   s.    =		
rM   c                   @   sX   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	d
e	de
dejfddZdS )PixverseImageToVideoNoder.   c                 C   s   t jddddt jdt jjdddd	d
t jjdttjdt jjdtdt jjdt	dt j
jdddddddt jjddddddt tjjddddgt j gt jjt jjt jjgdtd	S )Nr   zPixVerse Image to Videor/   rN   r   rO   TrP   rQ   rR   rW   rX   rY   r1   rZ   r[   r   r\   r]   r^   rb   rc   rd   r3   rf   rg   rh   )r   r9   Imager;   rm   r:   r   rn   r   r   ro   r>   r   r?   rp   r@   rq   rr   rs   rt   ru   rA   r*   r*   r+   rB      sl   

*z&PixverseImageToVideoNode.define_schemaNr   rO   rW   rY   rZ   rb   r3   c	                    s   t |dd t| |I d H }	|tjkrtj}tj}n|tjkr#tj}t| t	dddt
t|	|||||r5|nd ||ddI d H }
|
jd u rNtd|
j d	t| t	d
|
jj dttjgtjtjtjgdd tdI d H }tt|jjI d H S )NFrw   z"/proxy/pixverse/video/img/generater   r   )r(   rO   rW   ry   rZ   rb   rE   r[   rz   r|   r$   r}   r~   c                 S   r   r   r   r   r*   r*   r+   r   -  r   z2PixverseImageToVideoNode.execute.<locals>.<lambda>r   )r   r,   r   r   r   r   r   r   r   r   r	   r   r%   r&   r'   r   r   r
   r   r   r   r   r   AVERAGE_DURATION_I2Vr   rD   r   r   )r   r   rO   rW   rY   rZ   r[   rb   r3   r(   r   r   r*   r*   r+   rF      sL   




z PixverseImageToVideoNode.executer   rG   rH   rI   rK   r   r9   rB   torchTensorrL   r   rD   rF   r*   r*   r*   r+   r      s.    :		
r   c                   @   sX   e Zd ZedejfddZe	ddejdejde	de	d	e
d
e	de	dejfddZdS )PixverseTransitionVideoNoder.   c                 C   s   t jddddt jdt jdt jjddd	d
dt jjdttjdt jjdtdt jjdt	dt j
jdddddddt jjdd	ddddgt j gt jjt jjt jjgdtd	S )Nr   zPixVerse Transition Videor/   rN   first_frame
last_framerO   TrP   rQ   rR   rW   rX   rY   r1   rZ   r[   r   r\   r]   r^   rb   rc   rd   rh   )r   r9   r   r;   rm   r:   r   rn   r   r   ro   rp   r@   rq   rr   rs   rt   ru   rA   r*   r*   r+   rB   4  sd   


&z)PixverseTransitionVideoNode.define_schemaNr   r   rO   rW   rY   rZ   rb   c	                    s  t |dd t| |I d H }	t| |I d H }
|tjkr#tj}tj}n|tjkr+tj}t| t	dddt
t|	|
|||||r>|nd |ddI d H }|jd u rVtd|j d	t| t	d
|jj dttjgtjtjtjgdd tdI d H }tt|jjI d H S )NFr   z)/proxy/pixverse/video/transition/generater   r   )first_frame_imglast_frame_imgrO   rW   ry   rZ   rb   r[   rz   r|   r$   r}   r~   c                 S   r   r   r   r   r*   r*   r+   r     r   z5PixverseTransitionVideoNode.execute.<locals>.<lambda>r   )r   r,   r   r   r   r   r   r   r   r   r	   r   r%   r&   r'   r   r   r
   r   r   r   r   r   r   r   rD   r   r   )r   r   r   rO   rW   rY   rZ   r[   rb   first_frame_idlast_frame_idr   r   r*   r*   r+   rF   k  sN   




z#PixverseTransitionVideoNode.executer   r   r*   r*   r*   r+   r   3  s,    6
	
r   )rY   rW   rZ   )widgetsa  
    (
      $prices := {
        "5": {
          "1080p": {"normal": 1.2, "fast": 1.2},
          "720p": {"normal": 0.6, "fast": 1.2},
          "540p": {"normal": 0.45, "fast": 0.9},
          "360p": {"normal": 0.45, "fast": 0.9}
        },
        "8": {
          "1080p": {"normal": 1.2, "fast": 1.2},
          "720p": {"normal": 1.2, "fast": 1.2},
          "540p": {"normal": 0.9, "fast": 1.2},
          "360p": {"normal": 0.9, "fast": 1.2}
        }
      };
      $durPrices := $lookup($prices, $string(widgets.duration_seconds));
      $qualityPrices := $lookup($durPrices, widgets.quality);
      $price := $lookup($qualityPrices, widgets.motion_mode);
      {"type":"usd","usd": $price ? $price : 0.9}
    )
    )
depends_onexprc                   @   s(   e Zd Zedeeej  fddZdS )PixVerseExtensionr.   c                    s   t tttgS r   )rM   r   r   r-   )selfr*   r*   r+   get_node_list  s   zPixVerseExtension.get_node_listN)	rG   rH   rI   r   r<   typer   	ComfyNoder   r*   r*   r*   r+   r     s    r   r.   c                      s   t  S r   )r   r*   r*   r*   r+   comfy_entrypoint  s   r   )+r   typing_extensionsr   comfy_api.latestr   r   comfy_api_nodes.apis.pixverser   r   r   r   r	   r
   r   r   r   r   r   r   r   comfy_api_nodes.utilr   r   r   r   r   r   r   r   AVERAGE_DURATION_T2Tr   r   r   r,   r-   rM   r   r   
PriceBadgePriceBadgeDependsru   r   r   r*   r*   r*   r+   <module>   s&    < 	vvs