
    3jX                        S SK r S SKrS SKJr  S SKJr  S SKJrJrJ	r	  S SK
JrJrJrJrJrJrJrJrJrJr  S SKJrJrJrJrJrJrJrJrJr   " S S\R>                  5      r  " S	 S
\R>                  5      r! " S S\R>                  5      r" " S S\R>                  5      r# " S S\R>                  5      r$S\	RJ                  S\&\	RN                  \	RP                  4   4S jr) " S S\R>                  5      r* " S S\5      r+S\+4S jr,g)    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                       \ rS rSr\S 5       r\ SS\S\R                  S\S\S\S	\	S
\
S\	S\S\R                  S-  S\R                  4S jj5       rSrg)BriaImageEditNode    c                    [         R                  " SSSS[         R                  R                  SS/S9[         R                  R                  S5      [         R
                  R                  S	S
SSS9[         R
                  R                  SS
SS9[         R
                  R                  SS
SSS9[         R                  R                  SSSSS[         R                  R                  S
S9[         R                  R                  SSSSS[         R                  R                  SS9[         R                  R                  SSSSS[         R                  R                  S9[         R                  R                  S [         R                  R                  S!/ 5      [         R                  R                  S"[         R                  R                  S#S$S%9[         R                  R                  S&S$S%9[         R                  R                  S'S
S%9/5      /S(S)9[         R                  R                  S*S+S
S,9/
[         R                  R                  5       [         R
                  R                  SS-9/[         R                  R                   [         R                  R"                  [         R                  R$                  /S
[         R&                  " S.S/9S09	$ )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/   r0   r1   r(   steps2      )r'   r.   r/   r0   r1   
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_idrE   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_schemaBriaImageEditNode.define_schema"   s~   yy'/)=w9w'		"7	    		 1T2N		'"Z	    "!#!1!1!8!8+/   $!#!1!1!8!8Z   !#!1!1!8!8   %% ..w;.." "

 0 01LV[ 0 \ "

 0 01JTY 0 Z "

 0 01KUY 0 Z
 2 &  O!  u?B !		  .A B
 		..		++		##
 4_R
 R	
    Nr    r#   r$   r)   r*   r+   r3   r6   r9   rB   returnc                   #    U(       d  U(       d  [        S5      eS nU
b  [        U [        U
5      SS9I S h  vN n[        U [	        SSS9[        U(       a  UOS U(       a  UOS [        XSS9I S h  vN /UU(       a  UOS UUUUU	R                  SS	5      U	R                  S
S	5      U	R                  SS	5      S9[        S9I S h  vN n[        U [	        SUR                   3S9S [        S9I S h  vN n[        R                  " [        UR                  R                  5      I S h  vN UR                  R                   5      $  GN N N N[ N&7f)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imagesrB   r)   r3   r+   model_version	steps_numr<   visual_input_content_moderation visual_output_content_moderationdataresponse_model/proxy/bria/v2/status/ro   c                     U R                   $ Nstatusrs    re   <lambda>+BriaImageEditNode.execute.<locals>.<lambda>       qxxrh   status_extractorr{   )
ValueErrorr   r   r   r   r   getr   r   
request_idr	   r   
NodeOutputr   result	image_urlr*   )rd   r    r#   r$   r)   r*   r+   r3   r6   r9   rB   mask_urlresponses                re   executeBriaImageEditNode.executex   sP     /^__5c;PQU;VcsttH 7G%&,F$<M'8SW6sN_``a3B-#*4..9TV[*\0:?XZ_0`1;@Z\a1b .#
 
& !5h6I6I5JKL/0	
 
 }}.x/H/HIIOO--
 	
5 u a
&
 JsX   6EE9E2E3AEE,E3E46E*E+EEEEE r   )__name__
__module____qualname____firstlineno__classmethodrf   strr   rS   intfloatr   r   r   r   __static_attributes__r   rh   re   r   r       s    S
 S
j  $(.
.
 {{.
 	.

 .
 .
 .
 .
 .
 ,.
 kkD .
 
.
 .
rh   r   c            	       p    \ rS rSr\S 5       r\S\R                  S\S\	S\
R                  4S j5       rSrg	)
BriaRemoveImageBackground   c                 
   [         R                  " SSSS[         R                  R                  S5      [         R                  R                  S[         R                  R                  S/ 5      [         R                  R                  S[         R                  R                  S	S
S9[         R                  R                  SSS9/5      /SS9[         R                  R                  SSSS[         R                  R                  SSS9/[         R                  R                  5       /[         R                  R                  [         R                  R                  [         R                  R                  /S[         R                  " SS9S9	$ )Nr   zBria Remove Image Backgroundr   z8Remove the background from an image using Bria RMBG 2.0.r#   r9   r:   r;   r>   Fr=   r?   Tr@   rA   r+   r   r-   _Seed controls whether the node should re-run; results are non-deterministic regardless of seed.r'   r.   r/   r1   r2   r(   z{"type":"usd","usd":0.018}rF   rH   )r   rQ   rS   r   rY   rZ   r[   rU   rV   rW   r]   r^   r_   r`   ra   rb   rc   s    re   rf   'BriaRemoveImageBackground.define_schema   sG   yy/7)Rw'%% ..w;.." "

 0 01JTY 0 Z "

 0 01KUY 0 Z	 2 &  "!#!1!1!8!8+/H  	!6 XX__&'		..		++		##
 5O*
 *	
rh   r#   r9   r+   ri   c                   #    [        U [        SSS9[        [        XSS9I S h  vN SUR	                  SS5      UR	                  SS5      US	9[
        S
9I S h  vN n[        U [        SUR                   3S9S [        S9I S h  vN n[        R                  " [        UR                  R                  5      I S h  vN 5      $  N Nn NC N7f)Nz+/proxy/bria/v2/image/edit/remove_backgroundrm   rn   rq   rk   Fr>   r?   )r#   syncrw   rx   r+   ry   r|   r}   c                     U R                   $ r   r   r   s    re   r   3BriaRemoveImageBackground.execute.<locals>.<lambda>   r   rh   r   )r   r   r
   r   r   r   r   r   r   r   r   r   r   r   )rd   r#   r9   r+   r   s        re   r   !BriaRemoveImageBackground.execute   s      !JSYZ,4SL]^^0:?XZ_0`1;@Z\a1b .
 
 !5h6I6I5JKL/7	
 
 }}#?@Y@Y#ZZ[[ _	

 [sE   "CC4CC,CC
6C=C>	CC
CCr   N)r   r   r   r   r   rf   r   rS   dictr   r   r   r   r   r   rh   re   r   r      s]    +
 +
Z \{{\ \ 	\
 
\ \rh   r   c            	       p    \ rS rSr\S 5       r\S\R                  S\S\	S\
R                  4S j5       rSrg	)
BriaRemoveVideoBackground   c                    [         R                  " SSSS[         R                  R                  S5      [         R                  R                  S/ SQSS	9[         R
                  R                  S
SSS[         R                  R                  SSS9/[         R                  R                  5       /[         R                  R                  [         R                  R                  [         R                  R                  /S[         R                  " SS9S9	$ )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.rA   r+   r   r-   Tr   r   9{"type":"usd","usd":0.0042,"format":{"suffix":"/second"}}rF   rH   r   rQ   Videor   rR   rU   rV   rW   r]   r^   r_   r`   ra   rb   rc   s    re   rf   'BriaRemoveVideoBackground.define_schema   s    yy/7)Iw'& E    "!#!1!1!8!8+/H  	%: XX__&'		..		++		##
 TS,
 ,	
rh   r   r   r+   ri   c                 t  #    [        USS9  [        U [        SSS9[        [	        X5      I S h  vN USUS9[
        S9I S h  vN n[        U [        S	UR                   3S
9S [        S9I S h  vN n[        R                  " [        UR                  R                  5      I S h  vN 5      $  N Nn NC N7f)N      N@max_duration+/proxy/bria/v2/video/edit/remove_backgroundrm   rn   mp4_h264r   r   output_container_and_codecr+   ry   r|   r}   c                     U R                   $ r   r   r   s    re   r   3BriaRemoveVideoBackground.execute.<locals>.<lambda>>  r   rh   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   	video_url)rd   r   r   r+   r   s        re   r   !BriaRemoveVideoBackground.execute(  s      	 D9 JSYZ14S@@!1+5	 .

 

 !5h6I6I5JKL/<	
 
 }}#?@Y@Y#ZZ[[ A	


 [E   -B8B0B8B2,B80B416B8'B6(	B82B84B86B8r   Nr   r   r   r   r   rf   r   r   r   r   r   r   r   r   r   rh   re   r   r      s]    -
 -
^ \{{\ \ 	\
 
\ \rh   r   c            	       p    \ rS rSr\S 5       r\S\R                  S\S\	S\
R                  4S j5       rSrg	)
BriaVideoGreenScreeniD  c                    [         R                  " SSSS[         R                  R                  S5      [         R                  R                  S/ SQSS	9[         R
                  R                  S
SSS[         R                  R                  SSS9/[         R                  R                  5       /[         R                  R                  [         R                  R                  [         R                  R                  /S[         R                  " SS9S9	$ )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).rA   r+   r   r-   Tr   r   r   rF   rH   r   rc   s    re   rf   "BriaVideoGreenScreen.define_schemaF  s    yy*2)aw'!Nc   "!#!1!1!8!8+/H  	& XX__&'		..		++		##
 T?"
 "	
rh   r   r   r+   ri   c                 t  #    [        USS9  [        U [        SSS9[        [	        X5      I S h  vN USUS9[
        S9I S h  vN n[        U [        S	UR                   3S
9S [        S9I S h  vN n[        R                  " [        UR                  R                  5      I S h  vN 5      $  N Nn NC N7f)Nr   r   z&/proxy/bria/v2/video/edit/green_screenrm   rn   r   )r   r   r   r+   ry   r|   r}   c                     U R                   $ r   r   r   s    re   r   .BriaVideoGreenScreen.execute.<locals>.<lambda>  r   rh   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )rd   r   r   r+   r   s        re   r   BriaVideoGreenScreen.executel  s      	 D9 EfU,4S@@'+5	 .

 

 !5h6I6I5JKL/<	
 
 }}#?@Y@Y#ZZ[[ A	


 [r   r   Nr   r   rh   re   r   r   D  s]    #
 #
J \{{\ \ 	\
 
\ \rh   r   c                       \ rS rSr\S 5       r\  SS\R                  S\S\R                  S-  S\R                  S-  S\
R                  4
S	 jj5       rS
rg)BriaVideoReplaceBackgroundi  c                 T   [         R                  " SSSS[         R                  R                  SSS9[         R                  R                  SS	S
S9[         R                  R                  SS	SS9[         R
                  R                  SSSS[         R                  R                  S	SS9/[         R                  R                  5       /[         R                  R                  [         R                  R                  [         R                  R                  /S	[         R                  " SS9S9	$ )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.)rC   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   rF   rH   )r   rQ   r   r   rS   rU   rV   rW   r]   r^   r_   r`   ra   rb   rc   s    re   rf   (BriaVideoReplaceBackground.define_schema  s   yy08)_ w0`a&!Y   &!Y   "!#!1!1!8!8+/H  	2 XX__&'		..		++		##
 TO*
 *	
rh   Nr   r+   r   r   ri   c                 (  #    US L US L :X  a  [        S5      e[        USS9  Ub  [        USS9  [        XSS9I S h  vN nO![        XS S 2S S 2S S 2S S24   SS9I S h  vN n[	        U [        SSS	9[        [        X5      I S h  vN US
US9[        S9I S h  vN n[        U [        SUR                   3S9S [        S9I S h  vN n[        R                  " [        UR                  R                  5      I S h  vN 5      $  N N N Nr NG N7f)NzBProvide either a background image or a background video, not both.r   r   zUploading backgroundrk   r4   z,/proxy/bria/v2/video/edit/replace_backgroundrm   rn   r   )r   background_urlr   r+   ry   r|   r}   c                     U R                   $ r   r   r   s    re   r   4BriaVideoReplaceBackground.execute.<locals>.<lambda>  r   rh   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )rd   r   r+   r   r   r   r   s          re   r   "BriaVideoReplaceBackground.execute  s0     $*:d*BCabbD9'#$44H#;C^t#uuN $<aArrk2?U$ N !KTZ[24S@@-+5	 .

 

 !5h6I6I5JKL/<	
 
 }}#?@Y@Y#ZZ[[/ v A	


 [si   :DD!DD&DD
DD,DD6D=D>	DD
DDDDr   )NN)r   r   r   r   r   rf   r   r   r   rS   r   r   r   r   r   rh   re   r   r     s    +
 +
Z 
 04/3#\{{#\ #\  ++,	#\
  ++,#\ 
#\ #\rh   r   r   ri   c           	      `   / n/ n[         R                  " U R                  5       SS9 nUR                  R                  S   nUR
                  R                  S:X  a  [        R                  " SS5      OSnUR                  U5       H  nUb  UR                  U5      OUR                  5        Hc  n[        R                  " UR                  SS95      R                  5       S	-  nUR                  US
SS24   5        UR                  US   5        Me     M     SSS5        U(       a  [        R                   " U5      O[        R"                  " SSSS5      n	U(       a  S[        R                   " U5      -
  O%[        R"                  " U	R$                  S   SS45      n
X4$ ! , (       d  f       N= f)aP  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@.r4   ).r4   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   rt   rB   s              re   _video_to_images_and_maskr    sa    &(J')L	((*	5""((+<B<P<P<U<UY^<^,%%lC8dhoof-F4;4G'..0V]]_\''(8(8(8(GHNNPSXX!!$sBQBw-0##DL1 ] . 
6 )3U[[$Aq!Q8OF0<C%++l++%++v||\]`bdfNgBhD< 
6	5s   C0F
F-c                   l    \ rS rSr\S 5       r\S\R                  S\S\	R                  4S j5       rSrg)	BriaTransparentVideoBackgroundi  c                    [         R                  " SSSS[         R                  R                  S5      [         R                  R                  SSSS[         R
                  R                  S	S
S9/[         R                  R                  SS9[         R                  R                  SS9/[         R                  R                  [         R                  R                  [         R                  R                  /S	[         R                  " SS9S9	$ )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   rt   rD   rB   r   rF   rH   )r   rQ   r   r   rU   rV   rW   rS   r]   r\   r^   r_   r`   ra   rb   rc   s    re   rf   ,BriaTransparentVideoBackground.define_schema  s    yy4E)) w'"!#!1!1!8!8+/H  	 X6F3
 		..		++		##
 T=!
 !	
rh   r   r+   ri   c                   #    [        USS9  [        U [        SSS9[        [	        X5      I S h  vN SSUS9[
        S	9I S h  vN n[        U [        S
UR                   3S9S [        S9I S h  vN n[        UR                  R                  5      I S h  vN n[        U5      u  pV[        R                  " XV5      $  N N} NR N-7f)Nr   r   r   rm   rn   Transparentwebm_vp9r   ry   r|   r}   c                     U R                   $ r   r   r   s    re   r   8BriaTransparentVideoBackground.execute.<locals>.<lambda>1  r   rh   r   )r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   )rd   r   r+   r   	video_outrt   rB   s          re   r   &BriaTransparentVideoBackground.execute  s      	 D9 JSYZ14S@@!.+5	 .

 

 !5h6I6I5JKL/<	
 
 7x7P7PQQ	0;}}V** A	


 RsE   -CB?CC,C0C1&CC(CCCCr   N)r   r   r   r   r   rf   r   r   r   r   r   r   r   r   rh   re   r  r    sM    "
 "
H +{{+ + 
	+ +rh   r  c                   L    \ rS rSr\S\\\R                        4S j5       r	Sr
g)BriaExtensioni9  ri   c                 J   #    [         [        [        [        [        [
        /$ 7fr   )r   r   r   r   r   r  )selfs    re   get_node_listBriaExtension.get_node_list:  s#      %% &*
 	
s   !#r   N)r   r   r   r   r   listtyper   	ComfyNoder  r   r   rh   re   r  r  9  s)    
T$r||*<%= 
 
rh   r  c                     #    [        5       $ 7fr   )r  r   rh   re   comfy_entrypointr  F  s     ?s   )-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   tuplerS   r\   r  r  r  r  r   rh   re   <module>r     s    	  ! & 6 6  
 
 
G
 G
TI\ I\XK\ K\\A\2<< A\HT\ T\nU[[ U5;;

;R5S ,A+R\\ A+H

N 

 rh   