
    +j                        S SK Jr  S SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
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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#  S S	K$J%r%      SBS
\&\RN                     S\RP                  S\)S\RP                  S-  S\*S\*S\+\   4S jjr,     SCS\&\-   S-  S\+\+   S-  S\.S\)S\/\R`                  -  4
S jjr1 " S S5      r2 " S S\RN                  5      r3 " S S\RN                  5      r4 " S S\RN                  5      r5 " S S \55      r6 " S! S"\55      r7 " S# S$\55      r8 " S% S&\RN                  5      r9 " S' S(\RN                  5      r: " S) S*\RN                  5      r; " S+ S,\RN                  5      r< " S- S.\RN                  5      r= " S/ S0\RN                  5      r> " S1 S2\RN                  5      r? " S3 S4\RN                  5      r@ " S5 S6\RN                  5      rA " S7 S8\RN                  5      rB " S9 S:\B5      rC " S; S<\RN                  5      rD " S= S>\RN                  5      rE " S? S@\5      rFS\F4SA jrGg)D    )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                   #    S[        XS9R                  5       0nUb  [        X4S9R                  5       US'   [        U [        USS9[        U(       a  UOSUS[
        SS	9I Sh  vN n/ n	UR                  b7  U	R                  [        UR                  R                  US
9I Sh  vN 5        U	$ UR                   H.  n
U	R                  [        U
R                  US
9I Sh  vN 5        M0     U	$  N NL N7f)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   urlr.   )r   r    r!   r"   r#   r$   requestr/   responseall_bytesior.   s              =/home/wildlama/comfy/ComfyUI/comfy_api_nodes/nodes_recraft.pyhandle_recraft_file_requestr<   &   s      'INNPQE)$JOOQf$v65WT*1	 	H K~~!!89K9KU\!]]^
  MMD%<TXXw%WWX " #	 ^  Xs6   A#C6%C0&?C6%C2&;C6!C4
"C62C64C6	formatterconverted_to_checkis_listreturn_modec                   ^ [         [            4U4S jjnUc  / nUc  UOSnTc  S m[        U [        5      (       d.  U" XU5      nU(       a  0 $ U(       a  UT" U 5      /0$ UT" U 5      0$ / n	U	/n
U
R                  U5        U R	                  5        H  u  pUc  UOU SU S3n[        U[        5      (       a,  U	R                  [        XTU
5      R	                  5       5        MS  [        U[         5      (       aE  [        U5       H4  u  pU S3nU	R                  [        UUTU
SS9R	                  5       5        M6     M  U	R                  UT" U5      45        M     US	:X  a  [        R                  " 5       n[        U	5      R	                  5        H\  u  nn[        U[         5      (       a&  U H  nUR                  U[        U5      5        M      MA  UR                  U[        U5      5        M^     U$ [        U	5      $ )
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                    > U HL  nU HC  nUS   U:X  d  M  [        US   [        5      (       d  M(  US   R                  T" U 5      5            g   MN     g)Nr   r+   TF)
isinstancelistr6   )item
parent_keylists_to_check
check_list
conv_tupler=   s        r;   handle_converted_lists8recraft_multipart_parser.<locals>.handle_converted_listsd   sQ    (J(
a=J.:jmT3R3RqM((49 ) )
     dictc                     U $ N )vs    r;   <lambda>*recraft_multipart_parser.<locals>.<lambda>r   s    arL   []z[]T)r?   formdata)rD   rC   rM   extenditemsr5   	enumerater6   aiohttpFormData	add_fieldstr)r.   rF   r=   r>   r?   r@   rJ   effective_modeadded	converted
next_checkkeyvaluecurrent_keyind
list_valueiter_keyfdkrQ   rE   s     `                  r;   r5   r5   I   s   6 AET
  !$.$6[FN	dD!!&t9KLI4 122IdO,,IJ()jjl
'/c
|1SE5KeT""5e)U_`ffhit$$#,U#3)]"-  ,Z9jbfgmmo $4 k9U+;<= # #O))+DAq!T""DLLCI.  QA' , 		?rL   c                   *    \ rS rSrSrS rS rS rSrg)handle_recraft_image_output   zt
Catch an exception related to receiving SVG data instead of image, when Infinite Style Library style_id is in use.
c                     g rO   rP   selfs    r;   __init__$handle_recraft_image_output.__init__       rL   c                     g rO   rP   rn   s    r;   	__enter__%handle_recraft_image_output.__enter__   rr   rL   c                 4    Ub  U[         L a  [        S5      eg g )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)ro   exc_typeexc_valexc_tbs       r;   __exit__$handle_recraft_image_output.__exit__   s,    H0F$FP  %GrL   rP   N)	__name__
__module____qualname____firstlineno____doc__rp   rt   r{   __static_attributes__rP   rL   r;   rk   rk      s    rL   rk   c                   d    \ rS rSr\S 5       r\SS\S\S\S\S\R                  4
S	 jj5       r
S
rg)RecraftColorRGBNode   c                    [         R                  " SSSS[         R                  R                  SSSSSS	9[         R                  R                  S
SSSSS	9[         R                  R                  SSSSSS	9[         R                  " [
        R                  5      R                  SSS9/[         R                  " [
        R                  5      R                  SS9/S9$ )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_schema!RecraftColorRGBNode.define_schema   s    yy%,,OS!EZ[S!E\]S!E[\		)//*0040P	 		)//*111O
 	
rL   Nr   r   r   r   r%   c                     U(       a  UR                  5       O	[        5       nUR                  [        XU5      5        [        R
                  " U5      $ rO   )cloner   addr   r   
NodeOutput)r   r   r   r   r   s        r;   executeRecraftColorRGBNode.execute   s<    1>++-DUDW,qQ/0}}]++rL   rP   rO   )r}   r~   r   r   classmethodr   intr   r   r   r   r   rP   rL   r;   r   r      sS    
 
" , , , ,<M ,Y[YfYf , ,rL   r   c                   \    \ rS rSr\S 5       r\S	S\S\S\R                  4S jj5       r	Sr
g)
RecraftControlsNode   c                 X   [         R                  " SSSS[         R                  " [        R                  5      R                  SSS9[         R                  " [        R                  5      R                  SSS9/[         R                  " [        R                  5      R                  S	S
9/S9$ )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   !RecraftControlsNode.define_schema   s    yy%+,U		)//*00D0I		)//*001Cd0S
 		),,-44BT4U
 	
rL   Nr   r   r%   c                 <    [         R                  " [        XS95      $ )N)r   r   )r   r   r   )r   r   r   s      r;   r   RecraftControlsNode.execute   s    }}_F^__rL   rP   NN)r}   r~   r   r   r   r   r   r   r   r   r   rP   rL   r;   r   r      sK    
 
 `. `IZ `fhfsfs ` `rL   r   c                   l    \ rS rSr\R
                  r\S 5       r\S\	S\
R                  4S j5       rSrg) RecraftStyleV3RealisticImageNode   c                     [         R                  " SSSS[         R                  R                  S[	        U R
                  5      S9/[         R                  " [        R                  5      R                  SS9/S	9$ )
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   .RecraftStyleV3RealisticImageNode.define_schema   sk    yy2:,Mz3CCDUDU3VW 		)++,333Q
 	
rL   r   r%   c                 f    US:X  a  S n[         R                  " [        U R                  U5      5      $ )NNone)r   r   r   r   )r   r   s     r;   r   (RecraftStyleV3RealisticImageNode.execute   s+    vH}}\#*;*;XFGGrL   rP   N)r}   r~   r   r   r   realistic_imager   r   r   r]   r   r   r   r   rP   rL   r;   r   r      sI    "22M
 
 Hs Hr}} H HrL   r   c                   <    \ rS rSr\R
                  r\S 5       rSr	g)%RecraftStyleV3DigitalIllustrationNode   c                     [         R                  " SSSS[         R                  R                  S[	        U R
                  5      S9/[         R                  " [        R                  5      R                  SS9/S	9$ )
N!RecraftStyleV3DigitalIllustrationz$Recraft Style - Digital Illustrationr   r   r   r   r   r   r   r   r   s    r;   r   3RecraftStyleV3DigitalIllustrationNode.define_schema   sk    yy7?,Mz3CCDUDU3VW 		)++,333Q
 	
rL   rP   N)
r}   r~   r   r   r   digital_illustrationr   r   r   r   rP   rL   r;   r   r      s    "77M
 
rL   r   c                   <    \ rS rSr\R
                  r\S 5       rSr	g)$RecraftStyleV3VectorIllustrationNodei  c                     [         R                  " SSSS[         R                  R                  S[	        U R
                  5      S9/[         R                  " [        R                  5      R                  SS9/S	9$ )
Nr   r   r   r   r   r   r   r   r   r   r   s    r;   r   2RecraftStyleV3VectorIllustrationNode.define_schema  sk    yy::,Mz3CCDUDU3VW 		)++,333Q
 	
rL   rP   N)
r}   r~   r   r   r   vector_illustrationr   r   r   r   rP   rL   r;   r   r     s    "66M
 
rL   r   c                   <    \ rS rSr\R
                  r\S 5       rSr	g)RecraftStyleV3LogoRasterNodei  c                     [         R                  " SSSS[         R                  R                  S[	        U R
                  SS9S9/[         R                  " [        R                  5      R                  S	S
9/S9$ )NRecraftStyleV3LogoRasterzRecraft Style - Logo Rasterr   r   r   F)include_noner   r   r   r   r   r   s    r;   r   *RecraftStyleV3LogoRasterNode.define_schema!  sn    yy.6,Mz3CCDUDUdi3jk 		)++,333Q
 	
rL   rP   N)
r}   r~   r   r   r   logo_rasterr   r   r   r   rP   rL   r;   r   r     s    "..M
 
rL   r   c                   T    \ rS rSr\S 5       r\S\S\R                  4S j5       r	Sr
g) RecraftStyleInfiniteStyleLibraryi1  c                     [         R                  " SSSS[         R                  R                  SSSS9/[         R                  " [
        R                  5      R                  S	S
9/S9$ )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   .RecraftStyleInfiniteStyleLibrary.define_schema2  sb    yy8A,g		
B@lm 		)++,333Q
 	
rL   r   r%   c                 `    U(       d  [        S5      e[        R                  " [        US95      $ )Nz#The style_id input cannot be empty.)r   )rw   r   r   r   )r   r   s     r;   r   (RecraftStyleInfiniteStyleLibrary.executeA  s%    ABB}}\8<==rL   rP   N)r}   r~   r   r   r   r   r]   r   r   r   r   rP   rL   r;   r   r   1  s:    
 
 >s >r}} > >rL   r   c                       \ rS rSr\S 5       r\S\S\R                  R                  S\R                  4S j5       rSrg)	RecraftCreateStyleNodeiH  c                    [         R                  " SSSS[         R                  R                  SSS/SS	9[         R                  R                  S
[         R                  R                  [         R                  R                  S5      SSSS9S9/[         R                  R                  SS9/[         R                  R                  [         R                  R                  [         R                  R                  /S[         R                  " SS9S9	$ )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   $RecraftCreateStyleNode.define_schemaI  s    yy,/,; .0FGE  
 !![[77w/&	 8  " " 		  j 9 		..		++		##
 5A#
 #	
rL   r   r   r%   c           
        #    / nSnSn[        [        UR                  5       5      5       HU  u  pg[        USSS9R	                  5       nU[        U5      -  nXE:  a  [        S5      eUR                  SUS-    3U45        MW     [        U [        S	S
S9[        U[        US9SSS9I S h  vN n	[        R                  " U	R                  5      $  N$7f)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-   r/   r.   r0   r2   )rY   rD   valuesr   r4   lenrw   r6   r   r   r   r   r   r   id)
r   r   r   r/   
total_sizemax_total_sizeiimg
file_bytesr9   s
             r;   r   RecraftCreateStyleNode.executep  s      
(V]]_ 56FA*3[T`affhJ#j/)J* NOOLLDQ.*56 7 ! &=fM5*7.
 
 }}X[[))
s   B'C)C*%CrP   N)r}   r~   r   r   r   r   r]   r   r   Typer   r   r   rP   rL   r;   r   r   H  sS    $
 $
L **   * 
	* *rL   r   c                   r    \ rS rSr\S 5       r\   SS\S\S\S\S\S	\	S
\
R                  4S jj5       rSrg)RecraftTextToImageNodei  c                    [         R                  " SSSS[         R                  R                  SSSSS	9[         R                  R                  S
[
         Vs/ s H  oR                  PM     sn[
        R                  SS9[         R                  R                  SSSSSS9[         R                  R                  SSSSSSS9[         R                  " [        R                  5      R                  SSS9[         R                  R                  SSSSSS9[         R                  " [        R                  5      R                  SSSS9/[         R                  R                  5       /[         R                  R                   [         R                  R"                  [         R                  R$                  /S[         R&                  " [         R(                  " S/S9S S!9S"9	$ s  snf )#Nr  zRecraft Text to Imager   z>Generates images synchronously based on prompt and resolution.promptTr    Prompt for the image generation.	multiliner   r   size 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   rc   res_1024x1024r   r   r   r   r   r   r   r   r   r   r   r  PriceBadgeDependsr   ress     r;   r   $RecraftTextToImageNode.define_schema  s   yy,0,X		D"Npq2BC2B3YY2BC,::>	   ?   *+/N   		)++,22?T2R		% $]!    		),,-33&m! 4 A%N ! 		..		++		##
 //>Li8
 8	
 Ds   GNr  r  r  r   r"  r   r%   c                   #    [        USSSS9  [        [        R                  5      nUc  UnS n	U(       a  UR	                  5       n	U(       d  S n[        U [        SSS9[        [        UUSUUUR                  UR                  UR                  U	S	9	SS
9I S h  vN n
/ nU
R                   Hv  n[        5          [        [        UR                   SS9I S h  vN 5      nS S S 5        [#        WR$                  5      S:  a  UR'                  S5      nUR)                  U5        Mx     [*        R,                  " [.        R0                  " USS95      $  N Nz! , (       d  f       Nz= f7f)NFr+     strip_whitespace
min_length
max_length/proxy/recraft/image_generationr'   r(   	recraftv3)	r  r"  modelr  r  r   r   r   controlsr-   r.   r2      r3      r   dim)r   r   r   r   create_api_modelr   r   r   r   r   r   r   r.   rk   r   r   r7   r  shape	unsqueezer6   r   r   torchcat)r   r  r  r  r  r   r"  r   default_stylecontrols_apir9   r   r.   r    s                 r;   r   RecraftTextToImageNode.execute  s=     	1QUV$^%C%CD )M+<<>L"O >vN9. /!#))&//&//%
 
 
" MMD,./6Mdhh`d6e0ef /5;;!#*MM%  " }}UYYv15663
( 1f /.s=   BE#EE#8EEEA1E#E
E 	E#rP   NNN)r}   r~   r   r   r   r   r]   r   r   r   r   r   r   r   rP   rL   r;   r  r    s|    9
 9
v  '+#,0/7/7 /7 	/7 $/7 /7 */7 
/7 /7rL   r  c                       \ rS rSr\S 5       r\   SS\R                  S\S\	S\
S\S	\S
\S\R                  4S jj5       rSrg)RecraftImageToImageNodei  c                    [         R                  " SSSS[         R                  R                  S5      [         R                  R                  SSSS	S
9[         R
                  R                  SSSSSS9[         R                  R                  SSSSSSS9[         R
                  R                  SSSSSSS9[         R                  " [        R                  5      R                  SSS9[         R                  R                  SSSSSS 9[         R                  " [        R                  5      R                  S!S"SS#9/[         R                  R                  5       /[         R                  R                  [         R                  R                  [         R                  R                  /S[         R                   " [         R"                  " S/S$9S%S&9S'9	$ )(NrK  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   %RecraftImageToImageNode.define_schema  s   yy-1,Dw'		D"Npq?   X   *+/N   		)++,22?T2R		% $]!    		),,-33&m! 4 I)V ! 		..		++		##
 //>Lq<
 <	
rL   Nr    r  r  rM  r   r"  r   r%   c	                   #    [        USSS9  [        [        R                  5      n	Uc  U	nS n
U(       a  UR	                  5       n
U(       d  S n[        UUSU[        US5      UR                  UR                  UR                  U
S9	n/ nUR                  S   n[        U5      n[        U5       H{  n[        U X   SUS	9I S h  vN n[        5          UR                  [         R"                  " U Vs/ s H  n[%        U5      PM     snSS
95        S S S 5        UR'                  S5        M}     [(        R*                  " [         R"                  " USS
95      $  Ns  snf ! , (       d  f       NS= f7f)NFr3  r5  r7  r9     )	r  r"  r:  r  rM  r   r   r   r;  r   z"/proxy/recraft/images/imageToImager    r!   r8   r?  r+   )r   r   r   r   rA  r   roundr   r   r   rB  r   ranger<   rk   r6   rD  rE  r   updater   r   )r   r    r  r  rM  r  r   r"  r   rF  rG  r8   r   totalpbarr  	sub_bytesxs                     r;   r   RecraftImageToImageNode.execute>  sG     	4H$^%C%CD )M+<<>L"O/+8Q'%%"++"++!

 A5!uA9h9	 I -.eiiY(WY)@)CY(W]^_` /KKN  }}UYYv1566 )X /.s=   C E.EE. E1E
EAE.E
E+	'E.rP   rI  )r}   r~   r   r   r   r   rD  Tensorr]   r   floatr   r   r   r   r   r   rP   rL   r;   rK  rK    s    =
 =
~  '+#,017||17 17 	17
 17 $17 17 *17 
17 17rL   rK  c                       \ rS rSr\S 5       r\  SS\R                  S\R                  S\S\	S\
S	\S
\R                  4S jj5       rSrg)RecraftImageInpaintingNodeis  c                 0   [         R                  " SSSS[         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  SSS	S
S9[         R                  R                  SSSSSS9[         R                  R                  SSSSSSS9[         R                  " [        R                  5      R                  SSS9[         R
                  R                  SS	SSSS9/[         R                  R                  5       /[         R                  R                  [         R                  R                  [         R                  R                  /S[         R                  " [         R                   " S/S9SS9S9	$ )Nr`  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   (RecraftImageInpaintingNode.define_schemat  s]   yy03,@w'f%		D"Npq?   *+/N   		)++,22?T2R		% $]!   +< ! 		..		++		##
 //>LW/
 /	
rL   Nr    r"   r  r  r   r"  r%   c                   #    [        USSS9  [        [        R                  5      nUc  UnU(       d  S n[	        UUSUUR
                  UR                  UR                  S9n	[        X!SSS9n/ n
UR                  S   n[        U5      n[        U5       H  n[        U X   X-US	-    S
U	S9I S h  vN n[        5          U
R                  [        R                   " U Vs/ s H  n[#        U5      PM     snSS95        S S S 5        UR%                  S	5        M     [&        R(                  " [        R                   " U
SS95      $  Ns  snf ! , (       d  f       NS= f7f)NFr3  rR  r9  r  r"  r:  r  r   r   r   T)allow_gradientadd_channel_dimr   r+   z/proxy/recraft/images/inpaint)r    r"   r!   r8   r?  )r   r   r   r   r   r   r   r   r   rB  r   rV  r<   rk   r6   rD  rE  r   rW  r   r   )r   r    r"   r  r  r  r   r"  rF  r8   r   rX  rY  r  rZ  r[  s                   r;   r   "RecraftImageInpaintingNode.execute  s>     	4H$^%C%CD )M"O/+%%"++"++
 $DW[\A5!uA9ha!e_4 I -.eiiY(WY)@)CY(W]^_` /KKN  }}UYYv1566 )X /.s=   B,E.E/E= E	E2
E	<AEE		
E	ErP   r   r}   r~   r   r   r   r   rD  r]  r]   r   r   r   r   r   r   rP   rL   r;   r`  r`  s  s    0
 0
d  '+#.7||.7 ll.7 	.7
 .7 $.7 .7 
.7 .7rL   r`  c                   p    \ rS rSr\S 5       r\  SS\S\S\S\S\S	\S
\	R                  4S jj5       rSrg)RecraftTextToVectorNodei  c                    [         R                  " SSSS[         R                  R                  SSSSS	9[         R                  R                  S
[        [        R                  5      S9[         R                  R                  S[         Vs/ s H  oR                  PM     sn[        R                  SS9[         R                  R                  SSSSSS9[         R                  R                  SSSSSSS9[         R                  R                  SSSSSS9[         R                  " [        R                  5      R                  SSSS9/[         R                  R!                  5       /[         R"                  R$                  [         R"                  R&                  [         R"                  R(                  /S[         R*                  " [         R,                  " S/S9S S!9S"9	$ s  snf )#Nrk  zRecraft Text to Vectorr   z;Generates SVG synchronously based on prompt and resolution.r  r   r  T)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(  z1{"type":"usd","usd": $round(0.08 * widgets.n, 2)}r+  r   )r   r   r   r   r   r   r   r   r   rc   r-  r   r   r   r   r   r   r   r   r   r   r  r.  r/  s     r;   r   %RecraftTextToVectorNode.define_schema  s   yy-1,U		">`lpqz3CNDfDf3gh2BC2B3YY2BC,::>	   S!Cfg*+/N   		% $]!    		),,-33&m! 4 5B  		..		++		##
 //>L]2
 2	
 Ds   
GNr  r   r  r  r"  r   r%   c                   #    [        USSS9  [        [        R                  US9nS n	U(       a  UR	                  5       n	U(       d  S n[        U [        SSS9[        [        UUSUUUR                  UR                  U	S	9S
S9I S h  vN n
/ nU
R                   H.  nUR                  [        UR                  SS9I S h  vN 5        M0     [        R                   " [#        U5      5      $  Nc N-7f)NFr3  rR  )r   r8  r'   r(   r9  r  r"  r:  r  r  r   r   r;  r+   r<  r=  r3   )r   r   r   r   rA  r   r   r   r   r   r   r.   r6   r   r7   r   r   r   )r   r  r   r  r  r  r"  r   r   rG  r9   svg_datar.   s                r;   r   RecraftTextToVectorNode.execute  s      	4H$^%G%GRZ[+<<>L"O >vN9. /!#))&//%	 
 
  MMDOO"9$((D"QQR " }}S]++)
$ Rs$   BC.C*7C.>C,
?,C.,C.rP   r   )r}   r~   r   r   r   r   r]   r   r   r   r   r   r   rP   rL   r;   rk  rk    sy    3
 3
j   $,0),), ), 	),
 ), ), *), 
), ),rL   rk  c                   h    \ rS rSr\S 5       r\S\R                  S\R                  4S j5       r
Srg)RecraftVectorizeImageNodei=  c                    [         R                  " SSSSS[         R                  R                  S5      /[         R                  R                  5       /[         R                  R                  [         R                  R                  [         R                  R                  /S[         R                  " [         R                  " 5       SS	9S
9
$ )Nrs  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   'RecraftVectorizeImageNode.define_schema>  s    yy/2, -Jw'  		..		++		##
 //15%
 	
rL   r    r%   c                 >  #    / nUR                   S   n[        U5      n[        U5       HC  n[        U X   SS9I S h  vN nUR	                  [        U5      5        UR                  S5        ME     [        R                  " [
        R                  " U5      5      $  N]7f)Nr   z/proxy/recraft/images/vectorizer    r!   r+   )
rB  r   rV  r<   r6   r   rW  r   r   combine_all)r   r    svgsrX  rY  r  rZ  s          r;   r   !RecraftVectorizeImageNode.executeX  s     A5!uA9h6 I
 KKI'KKN  }}S__T233s   ;BBABrP   Nr}   r~   r   r   r   r   rD  r]  r   r   r   r   rP   rL   r;   rs  rs  =  s>    
 
2 4%,, 42== 4 4rL   rs  c                       \ rS rSr\S 5       r\  SS\R                  S\S\	S\
S\S	\R                  4S
 jj5       rSrg)RecraftReplaceBackgroundNodeii  c                    [         R                  " SSSS[         R                  R                  S5      [         R                  R                  SSSS	S
9[         R
                  R                  SSSSSS9[         R
                  R                  SSSSS	SS9[         R                  " [        R                  5      R                  SS	S9[         R                  R                  SSS	SS	S9/[         R                  R                  5       /[         R                  R                  [         R                  R                  [         R                  R                  /S	[         R                  " SS9S9	$ )Nr  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   *RecraftReplaceBackgroundNode.define_schemaj  s4   yy25,Pw'		2T^`lpqS!Cfg*+/N   		)++,22?T2R		% $]!   . ! 		..		++		##
 4I'
 '	
rL   Nr    r  r  r   r"  r%   c                 8  #    [        [        R                  5      nUc  UnU(       d  S n[        UUSUUR                  UR
                  UR                  S9n/ n	UR                  S   n
[        U
5      n[        U
5       Hh  n[        U X   SUS9I S h  vN nU	R                  [        R                  " U Vs/ s H  n[        U5      PM     snSS95        UR                  S5        Mj     [         R"                  " [        R                  " U	SS95      $  Ns  snf 7f)Nr9  re  r   z'/proxy/recraft/images/replaceBackgroundrT  r?  r+   )r   r   r   r   r   r   r   rB  r   rV  r<   r6   rD  rE  r   rW  r   r   )r   r    r  r  r  r   r"  rF  r8   r   rX  rY  r  rZ  r[  s                  r;   r   $RecraftReplaceBackgroundNode.execute  s     %^%C%CD )M"O/+%%"++"++
 A5!uA9h>	 I MM%))$SA%<Q%?$SYZ[\KKN  }}UYYv1566 %Ts%   BDD#D6DA	DDrP   r   ri  rP   rL   r;   r  r  i  ss    (
 (
T  '+#'7||'7 '7 	'7 $'7 '7 
'7 '7rL   r  c                   h    \ rS rSr\S 5       r\S\R                  S\R                  4S j5       r
Srg)RecraftRemoveBackgroundNodei  c                    [         R                  " SSSSS[         R                  R                  S5      /[         R                  R	                  5       [         R
                  R	                  5       /[         R                  R                  [         R                  R                  [         R                  R                  /S[         R                  " SS	9S
9
$ )Nr  zRecraft Remove Backgroundr   ru  zBRemove background from image, and return processed image and mask.r    Tz{"type":"usd","usd":0.01}r   rv  )r   r   r   r   r   rb  r   r   r   r   r  r   s    r;   r   )RecraftRemoveBackgroundNode.define_schema  s    yy14, -\w' ! 
 		..		++		##
 4'
 	
rL   r    r%   c                   #    / nUR                   S   n[        U5      n[        U5       Hg  n[        U X   SS9I S h  vN nUR	                  [
        R                  " U Vs/ s H  n[        U5      PM     snSS95        UR                  S5        Mi     [
        R                  " USS9nUS S 2S S 2S S 2SS 24   R                  S5      n	[        R                  " X5      $  Ns  snf 7f)Nr   z&/proxy/recraft/images/removeBackgroundrz  r?  r+   )rB  r   rV  r<   r6   rD  rE  r   rW  squeezer   r   )
r   r    r   rX  rY  r  rZ  r[  images_tensormasks_tensors
             r;   r   #RecraftRemoveBackgroundNode.execute  s     A5!uA9h= I
 MM%))$SA%<Q%?$SYZ[\KKN  		&a0$Q1bc\2::2>}}]99
 %Ts"   ;C'C #C'!C"6A+C'"C'rP   Nr~  rP   rL   r;   r  r    s>    
 
2 :%,, :2== : :rL   r  c                   l    \ rS rSrSr\S 5       r\S\R                  S\	R                  4S j5       rSrg)	RecraftCrispUpscaleNodei  z"/proxy/recraft/images/crispUpscalec                 j   [         R                  " SSSS[         R                  R                  S5      /[         R                  R	                  5       /[         R
                  R                  [         R
                  R                  [         R
                  R                  /S[         R                  " SS9S	9	$ )
Nr  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   %RecraftCrispUpscaleNode.define_schema  s    yy-6,Q w' ! 		..		++		##
 5'
 	
rL   r    r%   c                   #    / nUR                   S   n[        U5      n[        U5       Hq  n[        U X   U R                  S9I S h  vN nUR                  [        R                  " U Vs/ s H  n[        U5      PM     snSS95        UR                  S5        Ms     [        R                  " [        R                  " USS95      $  Ns  snf 7f)Nr   rz  r?  r+   )rB  r   rV  r<   RECRAFT_PATHr6   rD  rE  r   rW  r   r   )r   r    r   rX  rY  r  rZ  r[  s           r;   r   RecraftCrispUpscaleNode.execute  s     A5!uA9h%% I
 MM%))$SA%<Q%?$SYZ[\KKN  }}UYYv1566
 %Ts%   ACC#C+C
 A	C
CrP   N)r}   r~   r   r   r  r   r   rD  r]  r   r   r   r   rP   rL   r;   r  r    sC    7L
 
2 7%,, 72== 7 7rL   r  c                   (    \ rS rSrSr\S 5       rSrg)RecraftCreativeUpscaleNodei  z%/proxy/recraft/images/creativeUpscalec                 j   [         R                  " SSSS[         R                  R                  S5      /[         R                  R	                  5       /[         R
                  R                  [         R
                  R                  [         R
                  R                  /S[         R                  " SS9S	9	$ )
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   r  r   s    r;   r   (RecraftCreativeUpscaleNode.define_schema   s    yy09,T w' ! 		..		++		##
 4'
 	
rL   rP   N)r}   r~   r   r   r  r   r   r   rP   rL   r;   r  r    s    :L
 
rL   r  c                   t    \ rS rSr\S 5       r\ SS\S\S\S\S\S	\	S-  S
\
R                  4S jj5       rSrg)RecraftV4TextToImageNodei;  c                    [         R                  " SSSS[         R                  R                  SSSS9[         R                  R                  S	SS
S9[         R                  R                  S[         R                  R                  S[         R                  R                  S[        SSS9/5      [         R                  R                  S[         R                  R                  S[        SSS9/5      /SS9[         R                  R                  SSSSSS9[         R                  R                  SSSSSSS9[         R                  " [        R                  5      R                  SS SS!9/[         R                  R                  5       /[         R                  R                   [         R                  R"                  [         R                  R$                  /S[         R&                  " [         R(                  " SS/S"9S#S$9S%9	$ )&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   &RecraftV4TextToImageNode.define_schema<  s   yy.3,M		"Y   
 		%"]   
 %%..' "$*,<,7,N	 !/ !"
 ..+ "$*,@,7,N	 !/ !"
0 ?5 & 8 ?   *+/N   		),,-33&m! 4 o<| ! 		..		++		##
 //#GWT
 T	
rL   Nr  r"  r:  r  r  r   r%   c                 T  #    [        USSSS9  [        U [        SSS9[        [	        UU(       a  UOS US   US	   UU(       a  UR                  5       OS S
9SS9I S h  vN n/ nUR                   Hv  n	[        5          [        [        U	R                  SS9I S h  vN 5      n
S S S 5        [        W
R                  5      S:  a  U
R                  S5      n
UR                  U
5        Mx     [        R                   " ["        R$                  " USS95      $  N Nz! , (       d  f       Nz= f7f)NFr+   '  r4  r8  r'   r(   r:  r  )r  r"  r:  r  r  r;  r<  r=  r3   r>  r   r?  )r   r   r   r   r   rA  r.   rk   r   r   r7   r  rB  rC  r6   r   r   rD  rE  )r   r  r"  r:  r  r  r   r9   r   r.   r    s              r;   r    RecraftV4TextToImageNode.execute  s     	1QVW >vN9.3BGn6]@P)::<VZ 
 
 MMD,./6Mdhh`d6e0ef /5;;!#*MM%  " }}UYYv1566+
" 1f /.s=   AD(DD(=DDD#A1D(D
D%	!D(rP   rO   r}   r~   r   r   r   r   r]   rM   r   r   r   r   r   r   rP   rL   r;   r  r  ;  s}    U
 U
n  4877 7 	7
 7 7 *D07 
7 7rL   r  c                   t    \ rS rSr\S 5       r\ SS\S\S\S\S\S	\	S-  S
\
R                  4S jj5       rSrg)RecraftV4TextToVectorNodei  c                    [         R                  " SSSS[         R                  R                  SSSS9[         R                  R                  S	SS
S9[         R                  R                  S[         R                  R                  S[         R                  R                  S[        SSS9/5      [         R                  R                  S[         R                  R                  S[        SSS9/5      /SS9[         R                  R                  SSSSSS9[         R                  R                  SSSSSSS9[         R                  " [        R                  5      R                  SS SS!9/[         R                  R                  5       /[         R                  R                   [         R                  R"                  [         R                  R$                  /S[         R&                  " [         R(                  " SS/S"9S#S$9S%9	$ )&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   'RecraftV4TextToVectorNode.define_schema  s   yy/4,J		"Y   
 		%"]   
 %%..' "$*,<,7,N	 !/ !"
 ..+ "$*,@,7,N	 !/ !"
0 ?5 & 8 ?   *+/N   		),,-33&m! 4 o<|  		..		++		##
 //#GWT
 T	
rL   Nr  r"  r:  r  r  r   r%   c                   #    [        USSSS9  [        U [        SSS9[        [	        UU(       a  UOS US   US	   US
S U(       a  UR                  5       OS S9SS9I S h  vN n/ nUR                   H.  n	UR                  [        U	R                  SS9I S h  vN 5        M0     [        R                  " [        U5      5      $  Nc N-7f)NFr+   r  r4  r8  r'   r(   r:  r  r   ro  r<  r=  r3   )r   r   r   r   r   rA  r.   r6   r   r7   r   r   r   )
r   r  r"  r:  r  r  r   r9   rp  r.   s
             r;   r   !RecraftV4TextToVectorNode.execute  s      	1QVW >vN9.3BGn6]+@P)::<VZ	 
 
  MMDOO"9$((D"QQR "}}S]++'
$ Rs$   ACC 7CC
,CCrP   rO   r  rP   rL   r;   r  r    s}    U
 U
n  48,, , 	,
 , , *D0, 
, ,rL   r  c                   L    \ rS rSr\S\\\R                        4S j5       r	Sr
g)RecraftExtensioni1  r%   c                    #    [         [        [        [        [        [
        [        [        [        [        [        [        [        [        [        [        [         ["        /$ 7frO   )r  rK  r`  rk  rs  r  r  r  r  r   r   r   r   r   r   r   r  r  rn   s    r;   get_node_listRecraftExtension.get_node_list2  sG      ##&#%'(#&,1(,"$%%
 	
s   AArP   N)r}   r~   r   r   r   rD   typer   	ComfyNoder  r   rP   rL   r;   r  r  1  s)    
T$r||*<%= 
 
rL   r  c                     #    [        5       $ 7frO   )r  rP   rL   r;   comfy_entrypointr  J  s     s   )Ni   r=  N)NNNFrV   )Hior   rZ   rD  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  r]  r]   r   rD   r<   callableboolrM   r[   r5   rk   r   r   r   r   r   r   r   r   r  rK  r`  rk  rs  r  r  r  r  r  r  r  r  rP   rL   r;   <module>r     s      & & # /       * !%# 	bll	 <<    ,,
	 
     
'] J '+,0!P H~$P T
T)	P
 P P 
GPf &,",, ,4`",, `,Hr|| H2
,L 
&
+K 
&
#C 
&>r|| >.B*R\\ B*Jm7R\\ m7`s7bll s7lc7 c7La,bll a,H)4 )4XT72<< T7n,:",, ,:^+7bll +7\
!8 
<y7r|| y7xw, w,t
~ 
2 0 rL   