
    +j                     x   d dl mZ d dlZd dlZd dlmZ d dlm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mZ d dlmZmZmZm Z m!Z!m"Z"m#Z# d d	l$m%Z% 	 	 	 	 dFde&ej'                 dej(        de)dej(        dz  de*de*de+e         fdZ,	 	 	 	 	 dGde&e-         dz  de+e+         dz  de.de)de/ej0        z  f
dZ1 G d d          Z2 G d dej'                  Z3 G d d ej'                  Z4 G d! d"ej'                  Z5 G d# d$e5          Z6 G d% d&e5          Z7 G d' d(e5          Z8 G d) d*ej'                  Z9 G d+ d,ej'                  Z: G d- d.ej'                  Z; G d/ d0ej'                  Z< G d1 d2ej'                  Z= G d3 d4ej'                  Z> G d5 d6ej'                  Z? G d7 d8ej'                  Z@ G d9 d:ej'                  ZA G d; d<ej'                  ZB G d= d>eB          ZC G d? d@ej'                  ZD G dA dBej'                  ZE G dC dDe          ZFdeFfdEZGdS )H    )BytesION)UnidentifiedImageError)override)ProgressBar)IOComfyExtension)RECRAFT_V4_PRO_SIZESRECRAFT_V4_SIZESRecraftColorRecraftColorChainRecraftControlsRecraftCreateStyleRequestRecraftCreateStyleResponseRecraftImageGenerationRequestRecraftImageGenerationResponseRecraftImageSize	RecraftIORecraftStyleRecraftStyleV3get_v3_substyles)ApiEndpointbytesio_to_image_tensordownload_url_as_bytesioresize_mask_to_imagesync_optensor_to_bytesiovalidate_string)SVG      clsimagepathmasktotal_pixelstimeoutreturnc           
        K   dt          ||                                          i}|&t          ||                                          |d<   t          | t          |d          t          |r|nd|dt
          d	           d{V }g }	|j        5|	                    t          |j        j	        |
           d{V            n9|j
        D ]1}
|	                    t          |
j	        |
           d{V            2|	S )zGHandle sending common Recraft file-only request to get back file bytes.r"   )r%   Nr$   POSTr#   methodmultipart/form-data   )endpointresponse_modeldatafilescontent_typemultipart_parsermax_retriesr&   )r   readr   r   r   recraft_multipart_parserr"   appendr   urlr0   )r!   r"   r#   r$   r%   r&   requestr1   responseall_bytesior0   s              =/home/wildlama/comfy/ComfyUI/comfy_api_nodes/nodes_recraft.pyhandle_recraft_file_requestr>   &   sR      'LIIINNPPQE)$\JJJOOQQf$v6665)WWT*1	 	 	 	 	 	 	 	 	H K~!!89KU\!]!]!]]]]]]]^^^^M 	Y 	YD%<TXw%W%W%WWWWWWWXXXX    Fformdata	formatterconverted_to_checkis_listreturn_modec                 f   t           t                    ffd	}|g }||nd}d t          | t                    s. || ||          }|ri S |r| |           giS | |           iS g }	|	g}
|
                    |           |                                 D ]\  }}||n| d| d}t          |t                    r8|	                    t          |||
                                                     ^t          |t                     rTt          |          D ]C\  }}| d}|	                    t          |||
d	                                                     D|	                    | |          f           |d
k    rt          j	                    }t          |	                                          D ]f\  }}t          |t                     r)|D ]%}|
                    |t          |                     &C|
                    |t          |                     g|S t          |	          S )a  
    Formats data such that multipart/form-data will work with aiohttp library when both files and data are present.

    The OpenAI client that Recraft uses has a bizarre way of serializing lists:

    It does NOT keep track of indeces of each list, so for background_color, that must be serialized as:
        'background_color[rgb][]' = [0, 0, 255]
    where the array is assigned to a key that has '[]' at the end, to signal it's an array.

    This has the consequence of nested lists having the exact same key, forcing arrays to merge; all colors inputs fall under the same key:
        if 1 color  -> 'controls[colors][][rgb][]' = [0, 0, 255]
        if 2 colors -> 'controls[colors][][rgb][]' = [0, 0, 255, 255, 0, 0]
        if 3 colors -> 'controls[colors][][rgb][]' = [0, 0, 255, 255, 0, 0, 0, 255, 0]
        etc.
    Whoever made this serialization up at OpenAI added the constraint that lists must be of uniform length on objects of same 'type'.
    c                     |D ]V}|D ]Q}|d         |k    rCt          |d         t                    r(|d                              |                        dS RWdS )Nr   r-   TF)
isinstancelistr8   )item
parent_keylists_to_check
check_list
conv_tuplerA   s        r=   handle_converted_listsz8recraft_multipart_parser.<locals>.handle_converted_listsd   sy    ( 	  	 J(    
a=J..:jmT3R3R.qM((4999444  ur?   Ndictc                     | S N )vs    r=   <lambda>z*recraft_multipart_parser.<locals>.<lambda>r   s    a r?   []z[]T)rC   r@   )rH   rG   rO   extenditemsr7   	enumerater8   aiohttpFormData	add_fieldstr)r0   rJ   rA   rB   rC   rD   rN   effective_modeadded	converted
next_checkkeyvaluecurrent_keyind
list_valueiter_keyfdkrS   rI   s     `                  r=   r7   r7   I   s   6 AET
       !$.$6[[FNK	dD!! 	-&&tZ9KLL 	I 	34 122IIdOO,,IJ()))jjll > >
U'/cc
5K5KS5K5K5KeT"" 		>5e[)U_``ffhhiiiit$$ 	>#,U#3#3  Z)---  ,Z9jbfgggmmoo    k99U+;+;<====##OO))++ 	( 	(DAq!T"" ( / /DLLCII..../ QA''''		??r?   c                   $    e Zd ZdZd Zd Zd ZdS )handle_recraft_image_outputz|
    Catch an exception related to receiving SVG data instead of image, when Infinite Style Library style_id is in use.
    c                     d S rQ   rR   selfs    r=   __init__z$handle_recraft_image_output.__init__       r?   c                     d S rQ   rR   rm   s    r=   	__enter__z%handle_recraft_image_output.__enter__   rp   r?   c                 >    ||t           u rt          d          d S d S )NzsReceived output data was not an image; likely an SVG. If you used style_id, make sure it is not a Vector art style.)r   	Exception)rn   exc_typeexc_valexc_tbs       r=   __exit__z$handle_recraft_image_output.__exit__   s9    H0F$F$FP    $F$Fr?   N)__name__
__module____qualname____doc__ro   rr   rx   rR   r?   r=   rk   rk      sK                 r?   rk   c                   \    e Zd Zed             Zed	dededededej        f
d            Z	dS )
RecraftColorRGBNodec                    t          j        ddddt           j                            ddddd	          t           j                            d
dddd	          t           j                            ddddd	          t          j        t
          j                                      dd          gt          j        t
          j                                      d          g          S )NRecraftColorRGBzRecraft Color RGBpartner/image/Recraftz5Create Recraft Color by choosing specific RGB values.rr      zRed value of color.defaultminmaxtooltipgzGreen value of color.bzBlue value of color.recraft_colorToptionaldisplay_namenode_idr   categorydescriptioninputsoutputs)r   SchemaIntInputCustomr   COLOROutputr!   s    r=   define_schemaz!RecraftColorRGBNode.define_schema   s    y%,,OS!EZ[[S!E\]]S!E[\\	)/**0040PP	 	)/**111OO
 
 
 	
r?   Nr   r   r   r   r'   c                     |r|                                 nt                      }|                    t          |||                     t	          j        |          S rQ   )cloner   addr   r   
NodeOutput)r!   r   r   r   r   s        r=   executezRecraftColorRGBNode.execute   sT    1>W++---DUDWDW,q!Q//000}]+++r?   rQ   )
ry   rz   r{   classmethodr   intr   r   r   r   rR   r?   r=   r~   r~      sz        
 
 [
" , , , , ,<M ,Y[Yf , , , [, , ,r?   r~   c                   T    e Zd Zed             Zeddededej        fd            ZdS )RecraftControlsNodec                 `   t          j        ddddt          j        t          j                                      dd          t          j        t          j                                      dd          gt          j        t          j                                      d	
          g          S )Nr   zRecraft Controlsr   z;Create Recraft Controls for customizing Recraft generation.colorsTr   background_colorrecraft_controlsr   r   )r   r   r   r   r   r   CONTROLSr   r   s    r=   r   z!RecraftControlsNode.define_schema   s    y%+,U	)/**00D0II	)/**001Cd0SS
 	),--44BT4UU
 
 
 	
r?   Nr   r   r'   c                 H    t          j        t          ||                    S )N)r   r   )r   r   r   )r!   r   r   s      r=   r   zRecraftControlsNode.execute   s     }_FM]^^^___r?   NN)	ry   rz   r{   r   r   r   r   r   r   rR   r?   r=   r   r      sw        
 
 [
 ` `. `IZ `fhfs ` ` ` [` ` `r?   r   c                   \    e Zd Zej        Zed             Zedede	j
        fd            ZdS ) RecraftStyleV3RealisticImageNodec                     t          j        ddddt           j                            dt	          | j                            gt          j        t          j                  	                    d          g	          S )
NRecraftStyleV3RealisticImageRecraft Style - Realistic Imager   3Select realistic_image style and optional substyle.substyleoptionsrecraft_styler   r   
r   r   Combor   r   RECRAFT_STYLEr   r   STYLEV3r   r   s    r=   r   z.RecraftStyleV3RealisticImageNode.define_schema   su    y2:,Mz3CCDU3V3VWW 	)+,,333QQ
 
 
 	
r?   r   r'   c                 `    |dk    rd }t          j        t          | j        |                    S )NNone)r   r   r   r   )r!   r   s     r=   r   z(RecraftStyleV3RealisticImageNode.execute   s/    vH}\#*;XFFGGGr?   N)ry   rz   r{   r   realistic_imager   r   r   r]   r   r   r   rR   r?   r=   r   r      sm        "2M
 
 [
 Hs Hr} H H H [H H Hr?   r   c                   2    e Zd Zej        Zed             ZdS )%RecraftStyleV3DigitalIllustrationNodec                     t          j        ddddt           j                            dt	          | j                            gt          j        t          j                  	                    d          g	          S )
N!RecraftStyleV3DigitalIllustrationz$Recraft Style - Digital Illustrationr   r   r   r   r   r   r   r   r   s    r=   r   z3RecraftStyleV3DigitalIllustrationNode.define_schema   su    y7?,Mz3CCDU3V3VWW 	)+,,333QQ
 
 
 	
r?   N)ry   rz   r{   r   digital_illustrationr   r   r   rR   r?   r=   r   r      s5        "7M
 
 [
 
 
r?   r   c                   2    e Zd Zej        Zed             ZdS )$RecraftStyleV3VectorIllustrationNodec                     t          j        ddddt           j                            dt	          | j                            gt          j        t          j                  	                    d          g	          S )
Nr   r   r   r   r   r   r   r   r   r   r   s    r=   r   z2RecraftStyleV3VectorIllustrationNode.define_schema  su    y::,Mz3CCDU3V3VWW 	)+,,333QQ
 
 
 	
r?   N)ry   rz   r{   r   vector_illustrationr   r   r   rR   r?   r=   r   r     s5        "6M
 
 [
 
 
r?   r   c                   2    e Zd Zej        Zed             ZdS )RecraftStyleV3LogoRasterNodec                     t          j        ddddt           j                            dt	          | j        d                    gt          j        t          j                  	                    d	
          g          S )NRecraftStyleV3LogoRasterzRecraft Style - Logo Rasterr   r   r   F)include_noner   r   r   r   r   r   s    r=   r   z*RecraftStyleV3LogoRasterNode.define_schema!  s{    y.6,Mz3CCDUdi3j3j3jkk 	)+,,333QQ
 
 
 	
r?   N)ry   rz   r{   r   logo_rasterr   r   r   rR   r?   r=   r   r     s5        ".M
 
 [
 
 
r?   r   c                   N    e Zd Zed             Zededej        fd            ZdS ) RecraftStyleInfiniteStyleLibraryc                     t          j        ddddt           j                            ddd          gt          j        t
          j                                      d	
          g          S )N"RecraftStyleV3InfiniteStyleLibraryz&Recraft Style - Infinite Style Libraryr   zMChoose style based on preexisting UUID from Recraft's Infinite Style Library.style_id z*UUID of style from Infinite Style Library.)r   r   r   r   r   )r   r   Stringr   r   r   r   r   r   s    r=   r   z.RecraftStyleInfiniteStyleLibrary.define_schema2  sl    y8A,g	
B@lmm 	)+,,333QQ
 
 
 	
r?   r   r'   c                 h    |st          d          t          j        t          |                    S )Nz#The style_id input cannot be empty.)r   )rt   r   r   r   )r!   r   s     r=   r   z(RecraftStyleInfiniteStyleLibrary.executeA  s5     	CABBB}\8<<<===r?   N)	ry   rz   r{   r   r   r]   r   r   r   rR   r?   r=   r   r   1  s]        
 
 [
 >s >r} > > > [> > >r?   r   c                   f    e Zd Zed             Zededej        j        dej	        fd            Z
dS )RecraftCreateStyleNodec                    t          j        ddddt           j                            dddgd	          t           j                            d
t           j                            t           j                            d          ddd                    gt           j                            d          gt           j	        j
        t           j	        j        t           j	        j        gdt          j        d          	  	        S )Nr   zRecraft Create Styler   zCreate a custom style from reference images. Upload 1-5 images to use as style references. Total size of all images is limited to 5 MB.styler   r   z'The base style of the generated images.r   r   imagesr"   r-      )prefixr   r   )templater   r   Tz{"type":"usd","usd": 0.04}expr	r   r   r   r   r   r   hiddenis_api_nodeprice_badge)r   r   r   r   AutogrowTemplatePrefixImager   r   Hiddenauth_token_comfy_orgapi_key_comfy_org	unique_id
PriceBadger   s    r=   r   z$RecraftCreateStyleNode.define_schemaI  s   y,/,; .0FGE   
 !![77w//&	 8   "  " 	  j 99 	.	+	#
 5  A#
 #
 #
 #	
r?   r   r   r'   c           	        K   g }d}d}t          t          |                                                    D ]m\  }}t          |dd                                          }|t          |          z  }||k    rt          d          |                    d|dz    |f           nt          | t          d	d
          t          |t          |          dd           d {V }	t          j        |	j                  S )Nr   i  P i  @ z
image/webp)r%   	mime_typez,Total size of all images exceeds 5 MB limit.filer-   z/proxy/recraft/stylesr)   r*   )r   r,   )r.   r/   r1   r0   r2   r4   )rY   rH   valuesr   r6   lenrt   r8   r   r   r   r   r   r   id)
r!   r   r   r1   
total_sizemax_total_sizeiimg
file_bytesr;   s
             r=   r   zRecraftCreateStyleNode.executep  s%      
(V]]__ 5 566 	7 	7FAs*3[T`aaaffhhJ#j//)JN** NOOOLL.Q..*56666  &=fMMM5*777.
 
 
 
 
 
 
 
 
 }X[)))r?   N)ry   rz   r{   r   r   r]   r   r   Typer   r   rR   r?   r=   r   r   H  sq        $
 $
 [$
L **  * 
	* * * [* * *r?   r   c                   j    e Zd Zed             Ze	 	 	 ddedededededed	e	j
        fd
            ZdS )RecraftTextToImageNodec                 ~   t          j        ddddt           j                            dddd	          t           j                            d
d t
          D             t
          j        d          t           j                            ddddd          t           j                            dddddd          t          j        t          j
                                      dd          t           j                            ddddd          t          j        t          j                                      ddd          gt           j                                        gt           j        j        t           j        j        t           j        j        gdt          j        t          j        dg           d!"          #	  	        S )$Nr   zRecraft Text to Imager   z>Generates images synchronously based on prompt and resolution.promptTr    Prompt for the image generation.	multiliner   r   sizec                     g | ]	}|j         
S rR   rc   .0ress     r=   
<listcomp>z8RecraftTextToImageNode.define_schema.<locals>.<listcomp>      CCC3SYCCCr?    The size of the generated image.r   r   r   nr-      !The number of images to generate.r   seedr       `Seed to determine if node should re-run; actual results are nondeterministic regardless of seed.r   r   r   control_after_generater   r   r   negative_prompt?An optional text description of undesired elements on an image.r   force_inputr   r   r   OOptional additional controls over the generation via the Recraft Controls node.r   r   widgets1{"type":"usd","usd": $round(0.04 * widgets.n, 2)}
depends_onr   r   )r   r   r   r   r   r   res_1024x1024r   r   r   r   r   r   r   r   r   r   r   r   PriceBadgeDependsr   s    r=   r   z$RecraftTextToImageNode.define_schema  s   y,0,X	D"NpqqCC2BCCC,:>	    ?    *+/N    	)+,,22?T2RR	% $]!     	),--33&m! 4  A%N !! 	.	+	#
 />>>L  i8
 8
 8
 8	
r?   Nr   r   r
  r   r  r   r'   c                   K   t          |ddd           t          t          j                  }||}d }	|r|                                }	|sd }t          | t          dd          t          t          ||d|||j	        |j
        |j        |			  	        d
           d {V }
g }|
j        D ]}t                      5  t          t          |j        d           d {V           }d d d            n# 1 swxY w Y   t#          |j                  dk     r|                    d          }|                    |           t+          j        t/          j        |d                    S )NFr-     strip_whitespace
min_length
max_length/proxy/recraft/image_generationr)   r*   	recraftv3)	r   r  modelr   r
  r   r   r   controlsr/   r0   r4   r    r5      r   dim)r   r   r   r   create_api_modelr   r   r   r   r   r   r   r0   rk   r   r   r9   r   shape	unsqueezer8   r   r   torchcat)r!   r   r   r
  r  r   r  r   default_stylecontrols_apir;   r   r0   r"   s                 r=   r   zRecraftTextToImageNode.execute  s      	1QUVVVV$^%CDD )M 	?+<<>>L 	#"O >vNNN9. /!#)&/&/%
 
 
 
 
 
 
 
 
 
 
 
" M 	! 	!D,.. g g/6Mdh`d6e6e6e0e0e0e0e0e0effg g g g g g g g g g g g g g g5;!##**MM%    }UYv1555666s   ;*C11C5	8C5	NNN)ry   rz   r{   r   r   r]   r   r   r   r   r   r   rR   r?   r=   r   r     s        9
 9
 [9
v  '+#,0/7 /7/7 /7 	/7 $/7 /7 */7 
/7 /7 /7 [/7 /7 /7r?   r   c                   x    e Zd Zed             Ze	 	 	 ddej        dedede	de
ded	ed
ej        fd            ZdS )RecraftImageToImageNodec                    t          j        ddddt           j                            d          t           j                            dddd	
          t           j                            ddddd          t           j                            dddddd          t           j                            dddddd          t          j        t          j	                                      dd          t           j                            ddddd           t          j        t          j
                                      d!d"d#          gt           j                                        gt           j        j        t           j        j        t           j        j        gdt          j        t          j        dg$          d%&          '	  	        S )(Nr6  zRecraft Image to Imager   z*Modify image based on prompt and strength.r"   r   Tr   r   r   r
  r-   r  r  r   strengthg      ?g        g      ?g{Gz?zDefines the difference with the original image, should lie in [0, 1], where 0 means almost identical, and 1 means miserable similarity.)r   r   r   stepr   r  r   r  r  r  r   r   r  r  r  r   r  r  r  r  r  r   )r   r   r   r   r   r   Floatr   r   r   r   r   r   r   r   r   r   r  r   s    r=   r   z%RecraftImageToImageNode.define_schema  s   y-1,Dw''	D"Npqq?    X    *+/N    	)+,,22?T2RR	% $]!     	),--33&m! 4  I)V !! 	.	+	#
 />>>L  q<
 <
 <
 <	
r?   Nr"   r   r
  r8  r   r  r   r'   c	                   K   t          |dd           t          t          j                  }	||	}d }
|r|                                }
|sd }t          ||d|t          |d          |j        |j        |j	        |
	  	        }g }|j
        d         }t          |          }t          |          D ]}t          | ||         d|	           d {V }t                      5  |                    t!          j        d
 |D             d                     d d d            n# 1 swxY w Y   |                    d           t'          j        t!          j        |d                    S )NFr   r"  r$  r&     )	r   r  r'  r
  r8  r   r   r   r(  r   z"/proxy/recraft/images/imageToImager"   r#   r:   c                 ,    g | ]}t          |          S rR   r   r  xs     r=   r  z3RecraftImageToImageNode.execute.<locals>.<listcomp>m  !    (W(W(W)@)C)C(W(W(Wr?   r+  r-   )r   r   r   r   r-  r   roundr   r   r   r.  r   ranger>   rk   r8   r0  r1  updater   r   )r!   r"   r   r
  r8  r  r   r  r   r2  r3  r:   r   totalpbarr   	sub_bytess                    r=   r   zRecraftImageToImageNode.execute>  s      	4HHHH$^%CDD )M 	?+<<>>L 	#"O/+8Q''%"+"+!

 

 

 A5!!u 		 		A9Ah9	        I -.. a aei(W(WY(W(W(W]^___```a a a a a a a a a a a a a a aKKNNNN}UYv1555666s   4DD#	&D#	r4  )ry   rz   r{   r   r   r0  Tensorr]   r   floatr   r   r   r   r   rR   r?   r=   r6  r6    s        =
 =
 [=
~  '+#,017 17|17 17 	17
 17 $17 17 *17 
17 17 17 [17 17 17r?   r6  c                   |    e Zd Zed             Ze	 	 ddej        dej        dedede	ded	e
j        fd
            ZdS )RecraftImageInpaintingNodec                    t          j        ddddt           j                            d          t           j                            d          t           j                            ddd	d
          t           j                            ddddd          t           j                            dddddd          t          j        t          j	                                      dd          t           j                            dd	ddd          gt           j        
                                gt           j        j        t           j        j        t           j        j        gdt          j        t          j        dg          d          	  	        S )NrM  zRecraft Image Inpaintingr   z&Modify image based on prompt and mask.r"   r$   r   Tr   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   Maskr   r   r   r   r   r   r   r   r   r   r   r  r   s    r=   r   z(RecraftImageInpaintingNode.define_schemat  so   y03,@w''f%%	D"Npqq?    *+/N    	)+,,22?T2RR	% $]!    +< !! 	.	+	#
 />>>L  W/
 /
 /
 /	
r?   Nr"   r$   r   r
  r   r  r'   c           	        K   t          |dd           t          t          j                  }||}|sd }t	          ||d||j        |j        |j                  }	t          ||dd          }g }
|j	        d         }t          |          }t          |          D ]}t          | ||         |||d	z            d
|	           d {V }t                      5  |
                    t          j        d |D             d                     d d d            n# 1 swxY w Y   |                    d	           t%          j        t          j        |
d                    S )NFr   r<  r&  r   r  r'  r
  r   r   r   T)allow_gradientadd_channel_dimr   r-   z/proxy/recraft/images/inpaint)r"   r$   r#   r:   c                 ,    g | ]}t          |          S rR   r@  rA  s     r=   r  z6RecraftImageInpaintingNode.execute.<locals>.<listcomp>  rC  r?   r+  )r   r   r   r   r   r   r   r   r   r.  r   rE  r>   rk   r8   r0  r1  rF  r   r   )r!   r"   r$   r   r
  r  r   r  r2  r:   r   rG  rH  r   rI  s                  r=   r   z"RecraftImageInpaintingNode.execute  s      	4HHHH$^%CDD )M 	#"O/+%"+"+
 
 
 $D%W[\\\A5!!u 
	 
	A9Ah!a!e)_4        I -.. a aei(W(WY(W(W(W]^___```a a a a a a a a a a a a a a aKKNNNN}UYv1555666s   4DD	D	r   ry   rz   r{   r   r   r0  rJ  r]   r   r   r   r   r   rR   r?   r=   rM  rM  s  s        0
 0
 [0
d  '+#.7 .7|.7 l.7 	.7
 .7 $.7 .7 
.7 .7 .7 [.7 .7 .7r?   rM  c                   h    e Zd Zed             Ze	 	 ddedededededed	ej	        fd
            Z
dS )RecraftTextToVectorNodec                    t          j        ddddt           j                            dddd	          t           j                            d
t          t          j                            t           j                            dd t          D             t          j	        d          t           j
                            ddddd          t           j
                            dddddd          t           j                            ddddd          t          j        t          j                                      ddd          gt           j                                        gt           j        j        t           j        j        t           j        j        gdt          j        t          j        dg           d!"          #	  	        S )$NrW  zRecraft Text to Vectorr   z;Generates SVG synchronously based on prompt and resolution.r   r   r   T)r   r   r   r   r   r   c                     g | ]	}|j         
S rR   r  r  s     r=   r  z9RecraftTextToVectorNode.define_schema.<locals>.<listcomp>  r  r?   r  r	  r
  r-   r  r  r   r  r   r  r  r  r  r  r  r   r  r  r  z1{"type":"usd","usd": $round(0.08 * widgets.n, 2)}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   s    r=   r   z%RecraftTextToVectorNode.define_schema  s   y-1,U	">`lpqqz3CNDf3g3ghhCC2BCCC,:>	    S!Cfgg*+/N    	% $]!     	),--33&m! 4  5B  	.	+	#
 />>>L  ]2
 2
 2
 2	
r?   Nr   r   r   r
  r  r   r'   c                   K   t          |dd           t          t          j        |          }d }	|r|                                }	|sd }t          | t          dd          t          t          ||d|||j	        |j
        |		          d
           d {V }
g }|
j        D ]1}|                    t          |j        d           d {V            2t          j        t#          |                    S )NFr   r<  )r   r%  r)   r*   r&  r   r  r'  r   r
  r   r   r(  r-   r)  r    r5   )r   r   r   r   r-  r   r   r   r   r   r   r0   r8   r   r9   r   r   r   )r!   r   r   r   r
  r  r  r   r   r3  r;   svg_datar0   s                r=   r   zRecraftTextToVectorNode.execute  sC      	4HHHH$^%GRZ[[[ 	?+<<>>L 	#"O >vNNN9. /!#)&/%	 	 	 
 
 
 
 
 
 
 
 
  M 	S 	SDOO"9$(D"Q"Q"QQQQQQQRRRR}S]]+++r?   r   )ry   rz   r{   r   r   r]   r   r   r   r   r   rR   r?   r=   rW  rW    s        3
 3
 [3
j   $,0), ),), ), 	),
 ), ), *), 
), ), ), [), ), ),r?   rW  c                   X    e Zd Zed             Zedej        dej        fd            Z	dS )RecraftVectorizeImageNodec                 ^   t          j        dddddt           j                            d          gt           j                                        gt           j        j        t           j        j        t           j        j	        gdt          j
        t          j                    d	          

  
        S )Nr^  zRecraft Vectorize Imager   Image Toolsz0Generates SVG synchronously from an input image.r"   Tz{"type":"usd","usd": 0.01}r  
r   r   r   essentials_categoryr   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r  r   s    r=   r   z'RecraftVectorizeImageNode.define_schema>  s    y/2, -Jw''  	.	+	#
 /115  %
 
 
 	
r?   r"   r'   c                 \  K   g }|j         d         }t          |          }t          |          D ]W}t          | ||         d           d {V }|                    t          |                     |                    d           Xt          j        t          j	        |                    S )Nr   z/proxy/recraft/images/vectorizer"   r#   r-   )
r.  r   rE  r>   r8   r   rF  r   r   combine_all)r!   r"   svgsrG  rH  r   rI  s          r=   r   z!RecraftVectorizeImageNode.executeX  s      A5!!u 	 	A9Ah6        I
 KKI'''KKNNNN}S_T22333r?   N
ry   rz   r{   r   r   r0  rJ  r   r   r   rR   r?   r=   r^  r^  =  s_        
 
 [
2 4%, 42= 4 4 4 [4 4 4r?   r^  c                   n    e Zd Zed             Ze	 	 d
dej        dedede	dede
j        fd	            ZdS )RecraftReplaceBackgroundNodec                    t          j        ddddt           j                            d          t           j                            dddd	
          t           j                            ddddd          t           j                            ddddd	d          t          j        t          j                                      dd	          t           j                            ddd	dd	          gt           j        	                                gt           j
        j        t           j
        j        t           j
        j        gd	t          j        d          	  	        S )Nri  zRecraft Replace Backgroundr   z6Replace background on image, based on provided prompt.r"   r   r   r   T)r   r   r   r
  r-   r  r  r   r  r   r  r  r  r   r   r  r  r  z{"type":"usd","usd":0.04}r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s    r=   r   z*RecraftReplaceBackgroundNode.define_schemaj  s@   y25,Pw''	2T^`lpqqS!Cfgg*+/N    	)+,,22?T2RR	% $]!    . !! 	.	+	#
 4  I'
 '
 '
 '	
r?   Nr"   r   r
  r   r  r'   c           	        K   t          t          j                  }||}|sd }t          ||d||j        |j        |j                  }g }	|j        d         }
t          |
          }t          |
          D ]i}t          | ||         d|           d {V }|	                    t          j        d |D             d                     |                    d           jt          j        t          j        |	d                    S )	Nr&  rQ  r   z'/proxy/recraft/images/replaceBackgroundr>  c                 ,    g | ]}t          |          S rR   r@  rA  s     r=   r  z8RecraftReplaceBackgroundNode.execute.<locals>.<listcomp>  !    $S$S$SA%<Q%?%?$S$S$Sr?   r+  r-   )r   r   r   r   r   r   r   r.  r   rE  r>   r8   r0  r1  rF  r   r   )r!   r"   r   r
  r  r   r  r2  r:   r   rG  rH  r   rI  s                 r=   r   z$RecraftReplaceBackgroundNode.execute  s;      %^%CDD )M 	#"O/+%"+"+
 
 
 A5!!u 	 	A9Ah>	        I MM%)$S$S$S$S$SYZ[[[\\\KKNNNN}UYv1555666r?   r   rU  rR   r?   r=   ri  ri  i  s        (
 (
 [(
T  '+#'7 '7|'7 '7 	'7 $'7 '7 
'7 '7 '7 ['7 '7 '7r?   ri  c                   X    e Zd Zed             Zedej        dej        fd            Z	dS )RecraftRemoveBackgroundNodec                 t   t          j        dddddt           j                            d          gt           j                                        t           j                                        gt           j        j        t           j        j        t           j        j	        gdt          j
        d	          

  
        S )Nro  zRecraft Remove Backgroundr   r`  zBRemove background from image, and return processed image and mask.r"   Tz{"type":"usd","usd":0.01}r   ra  )r   r   r   r   r   rO  r   r   r   r   r   r   s    r=   r   z)RecraftRemoveBackgroundNode.define_schema  s    y14, -\w'' !!  
 	.	+	#
 4  '
 
 
 	
r?   r"   r'   c                   K   g }|j         d         }t          |          }t          |          D ]h}t          | ||         d           d {V }|                    t          j        d |D             d                     |                    d           it          j        |d          }|d d d d d d dd f                             d          }t          j
        ||          S )Nr   z&/proxy/recraft/images/removeBackgroundrd  c                 ,    g | ]}t          |          S rR   r@  rA  s     r=   r  z7RecraftRemoveBackgroundNode.execute.<locals>.<listcomp>  rm  r?   r+  r-   )r.  r   rE  r>   r8   r0  r1  rF  squeezer   r   )	r!   r"   r   rG  rH  r   rI  images_tensormasks_tensors	            r=   r   z#RecraftRemoveBackgroundNode.execute  s     A5!!u 	 	A9Ah=        I
 MM%)$S$S$S$S$SYZ[[[\\\KKNNNN	&a000$QQQ111bcc\2::2>>}]L999r?   Nrg  rR   r?   r=   ro  ro    s_        
 
 [
2 :%, :2= : : : [: : :r?   ro  c                   \    e Zd ZdZed             Zedej        dej	        fd            Z
dS )RecraftCrispUpscaleNodez"/proxy/recraft/images/crispUpscalec                 8   t          j        ddddt           j                            d          gt           j                                        gt           j        j        t           j        j        t           j        j        gdt          j	        d          		  	        S )
Nrx  zRecraft Crisp Upscale Imager   u   Upscale image synchronously.
Enhances a given raster image using ‘crisp upscale’ tool, increasing image resolution, making the image sharper and cleaner.r"   Tz{"type":"usd","usd":0.004}r   r   
r   r   r   r   r   r   r   r   r   r   r   s    r=   r   z%RecraftCrispUpscaleNode.define_schema  s    y-6,Q w'' !! 	.	+	#
 5  '
 
 
 	
r?   r"   r'   c                   K   g }|j         d         }t          |          }t          |          D ]m}t          | ||         | j                   d {V }|                    t          j        d |D             d                     |                    d           nt          j
        t          j        |d                    S )Nr   rd  c                 ,    g | ]}t          |          S rR   r@  rA  s     r=   r  z3RecraftCrispUpscaleNode.execute.<locals>.<listcomp>  rm  r?   r+  r-   )r.  r   rE  r>   RECRAFT_PATHr8   r0  r1  rF  r   r   )r!   r"   r   rG  rH  r   rI  s          r=   r   zRecraftCrispUpscaleNode.execute  s      A5!!u 	 	A9Ah%        I
 MM%)$S$S$S$S$SYZ[[[\\\KKNNNN}UYv1555666r?   N)ry   rz   r{   r}  r   r   r0  rJ  r   r   r   rR   r?   r=   rx  rx    sd        7L
 
 [
2 7%, 72= 7 7 7 [7 7 7r?   rx  c                   (    e Zd ZdZed             ZdS )RecraftCreativeUpscaleNodez%/proxy/recraft/images/creativeUpscalec                 8   t          j        ddddt           j                            d          gt           j                                        gt           j        j        t           j        j        t           j        j        gdt          j	        d          		  	        S )
Nr  zRecraft Creative Upscale Imager   u   Upscale image synchronously.
Enhances a given raster image using ‘creative upscale’ tool, boosting resolution with a focus on refining small details and faces.r"   Tz{"type":"usd","usd":0.25}r   r   rz  r   s    r=   r   z(RecraftCreativeUpscaleNode.define_schema   s    y09,T w'' !! 	.	+	#
 4  '
 
 
 	
r?   N)ry   rz   r{   r}  r   r   rR   r?   r=   r  r    s2        :L
 
 [
 
 
r?   r  c                   l    e Zd Zed             Ze	 ddededededededz  d	e	j
        fd
            ZdS )RecraftV4TextToImageNodec                    t          j        ddddt           j                            ddd          t           j                            d	dd
          t           j                            dt           j                            dt           j                            dt          dd          g          t           j                            dt           j                            dt          dd          g          gd          t           j	                            ddddd          t           j	                            dddddd          t          j
        t          j                                      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#$          %	  	        S )&Nr  zRecraft V4 Text to Imager   z3Generates images using Recraft V4 or V4 Pro models.r   T;Prompt for the image generation. Maximum 10,000 characters.r   r   r  r  r'  	recraftv4r   	1024x1024r  r	  recraftv4_pro	2048x2048 The model to use for generation.r   r
  r-   r  r  r   r  r   r  r  r  r   r  r  r  z
                (
                    $prices := {"recraftv4": 0.04, "recraftv4_pro": 0.25};
                    {"type":"usd","usd": $lookup($prices, widgets.model) * widgets.n}
                )
                r  r   )r   r   r   r   DynamicComboOptionr   r
   r	   r   r   r   r   r   r   r   r   r   r   r   r  r   s    r=   r   z&RecraftV4TextToImageNode.define_schema<  s   y.3,M	"Y    
 	%"]    
 %%..' "$*,<,7,N	 !/ !" !"
 
 ..+ "$*,@,7,N	 !/ !" !"
 
0 ?5 &  8 ?    *+/N    	),--33&m! 4  o<| !! 	.	+	#
 /#GGG  WT
 T
 T
 T	
r?   Nr   r  r'  r
  r  r   r'   c                   K   t          |ddd           t          | t          dd          t          t	          ||r|nd |d         |d	         ||r|                                nd 
          d           d {V }g }|j        D ]}	t                      5  t          t          |	j
        d           d {V           }
d d d            n# 1 swxY w Y   t          |
j                  dk     r|
                    d          }
|                    |
           t          j        t#          j        |d                    S )NFr-   '  r!  r%  r)   r*   r'  r   )r   r  r'  r   r
  r(  r)  r    r5   r*  r   r+  )r   r   r   r   r   r-  r0   rk   r   r   r9   r   r.  r/  r8   r   r   r0  r1  )r!   r   r  r'  r
  r  r   r;   r   r0   r"   s              r=   r   z RecraftV4TextToImageNode.execute  s      	1QVWWWW >vNNN9.3B LGn6]@PZ)::<<<VZ   
 
 
 
 
 
 
 
 
 M 	! 	!D,.. g g/6Mdh`d6e6e6e0e0e0e0e0e0effg g g g g g g g g g g g g g g5;!##**MM%    }UYv1555666s   *CC	C	rQ   ry   rz   r{   r   r   r]   rO   r   r   r   r   r   rR   r?   r=   r  r  ;  s        U
 U
 [U
n  487 77 7 	7
 7 7 *D07 
7 7 7 [7 7 7r?   r  c                   l    e Zd Zed             Ze	 ddededededededz  d	e	j
        fd
            ZdS )RecraftV4TextToVectorNodec                    t          j        ddddt           j                            ddd          t           j                            d	dd
          t           j                            dt           j                            dt           j                            dt          dd          g          t           j                            dt           j                            dt          dd          g          gd          t           j	                            ddddd          t           j	                            dddddd          t          j
        t          j                                      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#$          %	  	        S )&Nr  zRecraft V4 Text to Vectorr   z0Generates SVG using Recraft V4 or V4 Pro models.r   Tr  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  r  z
                (
                    $prices := {"recraftv4": 0.08, "recraftv4_pro": 0.30};
                    {"type":"usd","usd": $lookup($prices, widgets.model) * widgets.n}
                )
                r  r   )r   r   r   r   r  r  r   r
   r	   r   r   r   r   r   r   r   r   r   r   r   r  r   s    r=   r   z'RecraftV4TextToVectorNode.define_schema  s   y/4,J	"Y    
 	%"]    
 %%..' "$*,<,7,N	 !/ !" !"
 
 ..+ "$*,@,7,N	 !/ !" !"
 
0 ?5 &  8 ?    *+/N    	),--33&m! 4  o<|  	.	+	#
 /#GGG  WT
 T
 T
 T	
r?   Nr   r  r'  r
  r  r   r'   c                   K   t          |ddd           t          | t          dd          t          t	          ||r|nd |d         |d	         |d
d |r|                                nd           d           d {V }g }|j        D ]1}	|                    t          |	j	        d           d {V            2t          j        t          |                    S )NFr-   r  r!  r%  r)   r*   r'  r   r   r[  r)  r    r5   )r   r   r   r   r   r-  r0   r8   r   r9   r   r   r   )
r!   r   r  r'  r
  r  r   r;   r\  r0   s
             r=   r   z!RecraftV4TextToVectorNode.execute  s(      	1QVWWWW >vNNN9.3B LGn6]+@PZ)::<<<VZ	 	 	 
 
 
 
 
 
 
 
 
  M 	S 	SDOO"9$(D"Q"Q"QQQQQQQRRRR}S]]+++r?   rQ   r  rR   r?   r=   r  r    s        U
 U
 [U
n  48, ,, , 	,
 , , *D0, 
, , , [, , ,r?   r  c                   L    e Zd Zedeeej                          fd            ZdS )RecraftExtensionr'   c                    K   t           t          t          t          t          t
          t          t          t          t          t          t          t          t          t          t          t           t"          gS rQ   )r   r6  rM  rW  r^  ro  ri  rx  r  r   r   r   r   r   r~   r   r  r  rm   s    r=   get_node_listzRecraftExtension.get_node_list2  sH       ##&#%'(#&,1(,"$%%
 	
r?   N)	ry   rz   r{   r   rH   typer   	ComfyNoder  rR   r?   r=   r  r  1  sE        
T$r|*<%= 
 
 
 X
 
 
r?   r  c                  "   K   t                      S rQ   )r  rR   r?   r=   comfy_entrypointr  J  s      r?   )Nr   r    N)NNNFr@   )Hior   rZ   r0  PILr   typing_extensionsr   comfy.utilsr   comfy_api.latestr   r   comfy_api_nodes.apis.recraftr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   comfy_api_nodes.utilr   r   r   r   r   r   r   comfy_extras.nodes_imagesr   r  r  rJ  r]   r   rH   r>   callableboolrO   r[   r7   rk   r~   r   r   r   r   r   r   r   r   r6  rM  rW  r^  ri  ro  rx  r  r  r  r  r  rR   r?   r=   <module>r     s           & & & & & & & & & & & & # # # # # # / / / / / / / /                                                  * ) ) ) ) ) !%#   	bl	 <    ,
	 
     
']       J '+,0!P P H~$P T
T)	P
 P P 
GP P P Pf       &, , , , ,", , , ,4` ` ` ` `", ` ` `,H H H H Hr| H H H2
 
 
 
 
,L 
 
 
&
 
 
 
 
+K 
 
 
&
 
 
 
 
#C 
 
 
&> > > > >r| > > >.B* B* B* B* B*R\ B* B* B*Jm7 m7 m7 m7 m7R\ m7 m7 m7`s7 s7 s7 s7 s7bl s7 s7 s7lc7 c7 c7 c7 c7 c7 c7 c7La, a, a, a, a,bl a, a, a,H)4 )4 )4 )4 )4 )4 )4 )4XT7 T7 T7 T7 T72< T7 T7 T7n,: ,: ,: ,: ,:", ,: ,: ,:^+7 +7 +7 +7 +7bl +7 +7 +7\
 
 
 
 
!8 
 
 
<y7 y7 y7 y7 y7r| y7 y7 y7xw, w, w, w, w, w, w, w,t
 
 
 
 
~ 
 
 
2 0      r?   