
    +jX                        d dl Z d dlZd dlmZ d dlmZ d dlmZ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 d dlmZmZmZmZmZmZmZmZmZ  G d dej                  Z  G d	 d
ej                  Z! G d dej                  Z" G d dej                  Z# G d dej                  Z$de	j%        de&e	j'        e	j(        f         fdZ) G d dej                  Z* G d de          Z+de+fdZ,dS )    N)CodecContext)override)IOComfyExtensionInput)
BriaEditImageRequestBriaImageEditResponseBriaRemoveBackgroundRequestBriaRemoveBackgroundResponse BriaRemoveVideoBackgroundRequest!BriaRemoveVideoBackgroundResponseBriaStatusResponseBriaVideoGreenScreenRequest!BriaVideoReplaceBackgroundRequestInputModerationSettings)	ApiEndpointconvert_mask_to_imagedownload_url_to_image_tensordownload_url_to_video_outputpoll_opsync_opupload_image_to_comfyapiupload_video_to_comfyapivalidate_video_durationc                       e Zd Zed             Ze	 ddedej        dedededed	e	d
ede
dej        dz  dej        fd            ZdS )BriaImageEditNodec                 x   t          j        ddddt           j                            ddg          t           j                            d          t           j                            d	d
dd          t           j                            dd
d          t           j                            dd
dd          t           j                            dddddt           j        j        d
          t           j	                            dddddt           j        j        d          t           j                            dddddt           j        j                  t           j
                            d t           j
                            d!g           t           j
                            d"t           j                            d#d$%          t           j                            d&d$%          t           j                            d'd
%          g          gd()          t           j                            d*d+d
,          g
t           j                                        t           j                            d-          gt           j        j        t           j        j        t           j        j        gd
t          j        d./          0	  	        S )1Nr   zBria FIBO Image Editpartner/image/Briaz#Edit images using Bria latest modelmodelFIBO)optionsimagepromptT zInstruction to edit image)	multilinedefaulttooltipnegative_prompt)r%   r&   structured_promptzA string containing the structured edit prompt in JSON format. Use this instead of usual prompt for precise, programmatic control.seed   )r&   minmaxstepdisplay_modecontrol_after_generateguidance_scale      g{Gz?z<Higher value makes the image follow the prompt more closely.)r&   r-   r.   r/   r0   r'   steps2      )r&   r-   r.   r/   r0   
moderationfalsetrueprompt_content_moderationFr&   visual_input_moderationvisual_output_moderationModeration settingsr!   r'   maskz1If omitted, the edit applies to the entire image.)r'   optionaldisplay_namez{"type":"usd","usd":0.04}expr	node_idrD   categorydescriptioninputsoutputshiddenis_api_nodeprice_badge)r   SchemaCombor   ImageStringIntNumberDisplaynumberFloatDynamicComboOptionBooleanMaskOutputHiddenauth_token_comfy_orgapi_key_comfy_org	unique_id
PriceBadgeclss    :/home/wildlama/comfy/ComfyUI/comfy_api_nodes/nodes_bria.pydefine_schemazBriaImageEditNode.define_schema"   s   y'/)=w99w''	"7	     	 1T2NN	'"Z	     "!#!1!8+/    $!#!1!8Z    !#!1!8    %% ..w;;.." "
 0 01LV[ 0 \ \ "
 0 01JTY 0 Z Z "
 0 01KUY 0 Z Z 
 2 &   O!   u?B !!	  .A BB
 	.	+	#
 4  _R
 R
 R
 R	
    Nr   r"   r#   r(   r)   r*   r2   r5   r8   rA   returnc                   K   |s|st          d          d }|
%t          | t          |
          d           d {V }t          | t	          dd          t          |r|nd |r|nd t          | |d           d {V g||r|nd |||||	                    dd	          |	                    d
d	          |	                    dd	                    t                     d {V }t          | t	          d|j	                   d t                     d {V }t          j        t          |j        j                   d {V |j        j                  S )Nz?One of prompt or structured_prompt is required to be non-empty.zUploading mask
wait_labelzproxy/bria/v2/image/editPOSTpathmethodUploading imager;   Fr=   r>   )instructionstructured_instructionimagesrA   r(   r2   r*   model_version	steps_numr;   visual_input_content_moderation visual_output_content_moderationdataresponse_model/proxy/bria/v2/status/rm   c                     | j         S Nstatusrs    rd   <lambda>z+BriaImageEditNode.execute.<locals>.<lambda>       qx rf   status_extractorry   )
ValueErrorr   r   r   r   r   getr   r   
request_idr	   r   
NodeOutputr   result	image_urlr)   )rc   r   r"   r#   r(   r)   r*   r2   r5   r8   rA   mask_urlresponses                rd   executezBriaImageEditNode.executex   s       	`/ 	`^___5c;PQU;V;VcstttttttttH 7GGG%&,6FF$<M'W'8'8SW6sEN_`````````a3B L-#*4..9TV[*\*\0:?XZ_0`0`1;@Z\a1b1b   .#
 
 
 
 
 
 
 
 
& !Kh6IKKLLL//0	
 
 
 
 
 
 
 
 
 }.x/HIIIIIIIIO-
 
 	
rf   r}   )__name__
__module____qualname__classmethodre   strr   rR   intfloatr   r   r   r    rf   rd   r   r       s        S
 S
 [S
j  $(.
 .
.
 {.
 	.

 .
 .
 .
 .
 .
 ,.
 kD .
 
.
 .
 .
 [.
 .
 .
rf   r   c            	       `    e Zd Zed             Zedej        dedede	j
        fd            ZdS )BriaRemoveImageBackgroundc                    t          j        ddddt           j                            d          t           j                            dt           j                            dg           t           j                            dt           j                            d	d
          t           j                            dd          g          gd          t           j                            ddddt           j        j	        dd          gt           j        
                                gt           j        j        t           j        j        t           j        j        gdt          j        d          	  	        S )Nr   zBria Remove Image Backgroundr   z8Remove the background from an image using Bria RMBG 2.0.r"   r8   r9   r:   r=   Fr<   r>   Tr?   r@   r*   r   r,   _Seed controls whether the node should re-run; results are non-deterministic regardless of seed.r&   r-   r.   r0   r1   r'   z{"type":"usd","usd":0.018}rE   rG   )r   rP   rR   r   rX   rY   rZ   rT   rU   rV   r\   r]   r^   r_   r`   ra   rb   s    rd   re   z'BriaRemoveImageBackground.define_schema   sP   y/7)Rw''%% ..w;;.." "
 0 01JTY 0 Z Z "
 0 01KUY 0 Z Z 	 2 &   "!#!1!8+/H  	 	!6 X__&&'	.	+	#
 5  O*
 *
 *
 *	
rf   r"   r8   r*   rg   c                   K   t          | t          dd          t          t          | |d           d {V d|                    dd          |                    dd          |	          t
          
           d {V }t          | t          d|j                   d t                     d {V }t          j
        t          |j        j                   d {V           S )Nz+/proxy/bria/v2/image/edit/remove_backgroundrk   rl   ro   ri   Fr=   r>   )r"   syncru   rv   r*   rw   rz   r{   c                     | j         S r}   r~   r   s    rd   r   z3BriaRemoveImageBackground.execute.<locals>.<lambda>   r   rf   r   )r   r   r
   r   r   r   r   r   r   r   r   r   r   r   )rc   r"   r8   r*   r   s        rd   r   z!BriaRemoveImageBackground.execute   sC      !JSYZZZ,4S%L]^^^^^^^^^0:?XZ_0`0`1;@Z\a1b1b   .
 
 
 
 
 
 
 
 
 !Kh6IKKLLL//7	
 
 
 
 
 
 
 
 
 }#?@Y#Z#ZZZZZZZ[[[rf   N)r   r   r   r   re   r   rR   dictr   r   r   r   r   rf   rd   r   r      s        +
 +
 [+
Z \{\ \ 	\
 
\ \ \ [\ \ \rf   r   c            	       `    e Zd Zed             Zedej        dedede	j
        fd            ZdS )BriaRemoveVideoBackgroundc                    t          j        ddddt           j                            d          t           j                            dg dd	          t           j                            d
dddt           j        j        dd          gt           j                                        gt           j	        j
        t           j	        j        t           j	        j        gdt          j        d          	  	        S )Nr   zBria Remove Video Backgroundpartner/video/Briaz/Remove the background from a video using Bria. videobackground_color)
BlackWhiteGrayRedGreenBlueYellowCyanMagentaOrangez&Background color for the output video.r@   r*   r   r,   Tr   r   7{"type":"usd","usd":0.14,"format":{"suffix":"/second"}}rE   rG   r   rP   Videor   rQ   rT   rU   rV   r\   r]   r^   r_   r`   ra   rb   s    rd   re   z'BriaRemoveVideoBackground.define_schema   s    y/7)Iw''&   E     "!#!1!8+/H  	 	%: X__&&'	.	+	#
 R  S,
 ,
 ,
 ,	
rf   r   r   r*   rg   c           
        K   t          |d           t          | t          dd          t          t	          | |           d {V |d|          t
                     d {V }t          | t          d	|j         
          d t                     d {V }t          j
        t          |j        j                   d {V           S )N      N@max_duration+/proxy/bria/v2/video/edit/remove_backgroundrk   rl   mp4_h264r   r   output_container_and_codecr*   rw   rz   r{   c                     | j         S r}   r~   r   s    rd   r   z3BriaRemoveVideoBackground.execute.<locals>.<lambda>>  r   rf   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   	video_url)rc   r   r   r*   r   s        rd   r   z!BriaRemoveVideoBackground.execute(  s-      	 D9999 JSYZZZ14S%@@@@@@@@!1+5	   .

 

 

 

 

 

 

 

 

 !Kh6IKKLLL//<	
 
 
 
 
 
 
 
 
 }#?@Y#Z#ZZZZZZZ[[[rf   Nr   r   r   r   re   r   r   r   r   r   r   r   r   rf   rd   r   r      s        -
 -
 [-
^ \{\ \ 	\
 
\ \ \ [\ \ \rf   r   c            	       `    e Zd Zed             Zedej        dedede	j
        fd            ZdS )BriaVideoGreenScreenc                    t          j        ddddt           j                            d          t           j                            dg dd	          t           j                            d
dddt           j        j        dd          gt           j                                        gt           j	        j
        t           j	        j        t           j	        j        gdt          j        d          	  	        S )Nr   zBria Video Green Screenr   zGReplace a video's background with a solid chroma-key screen using Bria.r   green_shade)broadcast_greenchroma_greenblue_screenzSolid chroma-key shade applied behind the foreground: broadcast_green (#00B140), chroma_green (#00FF00), or blue_screen (#0000FF).r@   r*   r   r,   Tr   r   r   rE   rG   r   rb   s    rd   re   z"BriaVideoGreenScreen.define_schemaF  s    y*2)aw''!NNNc    "!#!1!8+/H  	 	& X__&&'	.	+	#
 R  ?"
 "
 "
 "	
rf   r   r   r*   rg   c           
        K   t          |d           t          | t          dd          t          t	          | |           d {V |d|          t
                     d {V }t          | t          d	|j         
          d t                     d {V }t          j
        t          |j        j                   d {V           S )Nr   r   z&/proxy/bria/v2/video/edit/green_screenrk   rl   r   )r   r   r   r*   rw   rz   r{   c                     | j         S r}   r~   r   s    rd   r   z.BriaVideoGreenScreen.execute.<locals>.<lambda>  r   rf   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )rc   r   r   r*   r   s        rd   r   zBriaVideoGreenScreen.executel  s,      	 D9999 EfUUU,4S%@@@@@@@@'+5	   .

 

 

 

 

 

 

 

 

 !Kh6IKKLLL//<	
 
 
 
 
 
 
 
 
 }#?@Y#Z#ZZZZZZZ[[[rf   Nr   r   rf   rd   r   r   D  s        #
 #
 [#
J \{\ \ 	\
 
\ \ \ [\ \ \rf   r   c                       e Zd Zed             Ze	 	 d	dej        dedej        dz  dej        dz  de	j
        f
d            ZdS )
BriaVideoReplaceBackgroundc                 (   t          j        ddddt           j                            dd          t           j                            dd	d
          t           j                            dd	d          t           j                            ddddt           j        j        d	d          gt           j                                        gt           j	        j
        t           j	        j        t           j	        j        gd	t          j        d          	  	        S )Nr   zBria Video Replace Backgroundr   zReplace a video's background with a supplied image or video using Bria. The output keeps the foreground's resolution and frame rate; a background with a different aspect ratio is stretched to fit, so match it for undistorted results.r   z.Foreground video whose background is replaced.)r'   background_imageTzwBackground image to composite behind the foreground. Provide either a background image or a background video, not both.)rB   r'   background_videozwBackground video to composite behind the foreground. Provide either a background image or a background video, not both.r*   r   r,   r   r   r   rE   rG   )r   rP   r   r   rR   rT   rU   rV   r\   r]   r^   r_   r`   ra   rb   s    rd   re   z(BriaVideoReplaceBackground.define_schema  s   y08)_ w0`aa&!Y    &!Y    "!#!1!8+/H  	 	2 X__&&'	.	+	#
 R  O*
 *
 *
 *	
rf   Nr   r*   r   r   rg   c           
      R  K   |d u |d u k    rt          d          t          |d           |*t          |d           t          | |d           d {V }nt          | |d           d {V }t	          | t          dd          t          t          | |           d {V |d	|
          t                     d {V }t          | t          d|j	                   d t                     d {V }t          j        t          |j        j                   d {V           S )NzBProvide either a background image or a background video, not both.r   r   zUploading backgroundri   z,/proxy/bria/v2/video/edit/replace_backgroundrk   rl   r   )r   background_urlr   r*   rw   rz   r{   c                     | j         S r}   r~   r   s    rd   r   z4BriaVideoReplaceBackground.execute.<locals>.<lambda>  r   rf   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )rc   r   r*   r   r   r   r   s          rd   r   z"BriaVideoReplaceBackground.execute  s      $*:d*BCCabbbD9999'#$44HHHH#;CAQ^t#u#u#uuuuuuuNN#;CAQ^t#u#u#uuuuuuuN KTZ[[[24S%@@@@@@@@-+5	   .

 

 

 

 

 

 

 

 

 !Kh6IKKLLL//<	
 
 
 
 
 
 
 
 
 }#?@Y#Z#ZZZZZZZ[[[rf   )NN)r   r   r   r   re   r   r   r   rR   r   r   r   r   rf   rd   r   r     s        +
 +
 [+
Z 
 04/3 \  \{ \  \  +,	 \
  +, \ 
 \  \  \ [ \  \  \rf   r   r   rg   c           	      P   g }g }t          j        |                                 d          5 }|j        j        d         }|j        j        dk    rt          j        dd          nd}|	                    |          D ]}||
                    |          n|
                                D ]y}t          j        |                    d                                                    d	z  }|                    |d
ddf                    |                    |d                    z	 ddd           n# 1 swxY w Y   |rt          j        |          nt          j        dddd          }	|rdt          j        |          z
  n!t          j        |	j        d         ddf          }
|	|
fS )a`  Decode a transparent webm (VP9 + alpha) into image frames and an alpha mask.

    VP9 keeps its alpha in a side layer that PyAV's default vp9 decoder drops, so the frames
    are decoded with libvpx-vp9. Returns RGB images [B,H,W,3] in 0..1 and a mask [B,H,W]
    following the Load Image convention (1 = transparent) for compositing or Save WEBM.
    r   )moder   vp9z
libvpx-vp9Nrgba)formatg     o@.r3   ).r3   g      ?@   )avopenget_stream_sourcestreamsr   codec_contextnamer   createdemuxdecodetorch
from_numpy
to_ndarrayr   appendstackzerosshape)r   
rgb_framesalpha_frames	containerstreamdecoderpacketframer   rr   rA   s              rd   _video_to_images_and_maskr     s    &(J')L	((**	5	5	5 2"(+<B<P<UY^<^<^,%lC888dhoof-- 	2 	2F4;4G'..000V]]__ 2 2'(8(8(8(G(GHHNNPPSXX!!$sBQBw-000##DL11112	22 2 2 2 2 2 2 2 2 2 2 2 2 2 2 )3OU[$$$Aq!Q8O8OF0<hC%+l++++%+v|\]`bdfNgBhBhD4<s   C7D11D58D5c                   \    e Zd Zed             Zedej        dedej	        fd            Z
dS )BriaTransparentVideoBackgroundc                    t          j        ddddt           j                            d          t           j                            ddddt           j        j        d	d
          gt           j                            d          t           j	                            d          gt           j
        j        t           j
        j        t           j
        j        gd	t          j        d          	  	        S )Nr   z*Bria Remove Video Background (Transparent)r   zRemove the background from a video using Bria and return the cut-out frames plus an alpha mask. Connect both to a compositing node, or feed them to Save WEBM to write a transparent video.r   r*   r   r,   Tr   r   rr   rC   rA   r   rE   rG   )r   rP   r   r   rT   rU   rV   rR   r\   r[   r]   r^   r_   r`   ra   rb   s    rd   re   z,BriaTransparentVideoBackground.define_schema  s    y4E)) w''"!#!1!8+/H  	 	 X66F33
 	.	+	#
 R  =!
 !
 !
 !	
rf   r   r*   rg   c           
        K   t          |d           t          | t          dd          t          t	          | |           d {V dd|          t
          	           d {V }t          | t          d
|j                   d t                     d {V }t          |j
        j                   d {V }t          |          \  }}t          j        ||          S )Nr   r   r   rk   rl   Transparentwebm_vp9r   rw   rz   r{   c                     | j         S r}   r~   r   s    rd   r   z8BriaTransparentVideoBackground.execute.<locals>.<lambda>.  r   rf   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )rc   r   r*   r   	video_outrr   rA   s          rd   r   z&BriaTransparentVideoBackground.execute  sD      	 D9999 JSYZZZ14S%@@@@@@@@!.+5	   .

 

 

 

 

 

 

 

 

 !Kh6IKKLLL//<	
 
 
 
 
 
 
 
 
 7x7PQQQQQQQQ	0;;}VT***rf   N)r   r   r   r   re   r   r   r   r   r   r   r   rf   rd   r   r     sn        "
 "
 ["
H +{+ + 
	+ + + [+ + +rf   r   c                   L    e Zd Zedeeej                          fd            ZdS )BriaExtensionrg   c                 F   K   t           t          t          t          t          gS r}   )r   r   r   r   r   )selfs    rd   get_node_listzBriaExtension.get_node_list7  s!       %% *
 	
rf   N)	r   r   r   r   listtyper   	ComfyNoder   r   rf   rd   r   r   6  sE        
T$r|*<%= 
 
 
 X
 
 
rf   r   c                  "   K   t                      S r}   )r   r   rf   rd   comfy_entrypointr   C  s      ??rf   )-r   r   av.codecr   typing_extensionsr   comfy_api.latestr   r   r   comfy_api_nodes.apis.briar   r	   r
   r   r   r   r   r   r   r   comfy_api_nodes.utilr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   tuplerR   r[   r   r   r   r   r   rf   rd   <module>r     s   				  ! ! ! ! ! ! & & & & & & 6 6 6 6 6 6 6 6 6 6                       
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
G
 G
 G
 G
 G
 G
 G
 G
TI\ I\ I\ I\ I\ I\ I\ I\XK\ K\ K\ K\ K\ K\ K\ K\\A\ A\ A\ A\ A\2< A\ A\ A\HQ\ Q\ Q\ Q\ Q\ Q\ Q\ Q\hU[ U5;
;R5S    ,A+ A+ A+ A+ A+R\ A+ A+ A+H

 

 

 

 

N 

 

 

      rf   