
    +j*                        U d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	m
Z
mZmZmZ ddlmZmZmZmZmZmZ  G d d          Zd	eej                 d
ej        defdZdZdZdZededediZeeef         e d<   g dZ!dgZ"g dZ#g dZ$ ej%        d          Z&de'fdZ( G d dej                  Z) G d dej                  Z* G d de          Z+de+fd Z,dS )!zKrea image-generation nodes.    N)override)IOComfyExtensionInput)KreaAssetResponseKreaGenerateImageRequestKreaImageStyleReferenceKreaJobKreaMoodboard)ApiEndpointdownload_url_to_image_tensorpoll_opsync_optensor_to_bytesiovalidate_stringc                       e Zd ZdZdS )KreaIOKREA_STYLE_REFN)__name__
__module____qualname__	STYLE_REF     :/home/wildlama/comfy/ComfyUI/comfy_api_nodes/nodes_krea.pyr   r      s         IIIr   r   clsimagereturnc           	         K   t          |dd          }t          | t          dd          t          d|j        |dffgdd	d
           d{V }|j        S )zPUpload an image to Krea's /assets endpoint and return the Krea-hosted image URL.i  @ z	image/png)total_pixels	mime_typez/proxy/krea/assetsPOSTpathmethodfilezmultipart/form-data   zUploading reference)endpointresponse_modelfilescontent_typemax_retries
wait_labelN)r   r   r   r   name	image_url)r   r   img_ioresponses       r   _upload_image_to_krea_assetsr2      s      u;+VVVF"6vFFF(fk:;<*(        H r   zKrea 2 MediumzKrea 2 Medium TurbozKrea 2 Largez-/proxy/krea/generate/image/krea/krea-2/mediumz3/proxy/krea/generate/image/krea/krea-2/medium-turboz,/proxy/krea/generate/image/krea/krea-2/large_MODEL_ENDPOINTS)z1:1z4:3z3:2z16:9z2.35:1z4:5z2:3z9:161K)rawlowmediumhigh)
backloggedqueued	scheduledzM^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$c                     t           j                            dt          d          t           j                            dt          d          t           j                            dt
          dd	          t           j                            d
ddd          t           j                            ddddddd          t          j        t          j
                                      ddd          gS )zUNested inputs shared by Krea 2 Medium, Medium Turbo and Large under the DynamicCombo.aspect_ratiozOutput aspect ratio.optionstooltip
resolutionzResolution scale.
creativityr7   zWPrompt interpretation strength: raw stays closest to the prompt; high is most creative.)r?   defaultr@   moodboard_id zOptional Krea moodboard UUID (e.g. from the Krea website). Leave empty to disable. Only one moodboard is supported per request.T)rC   r@   optionalmoodboard_strengthffffff?g      g      ?皙?z8Moodboard influence; ignored when moodboard_id is empty.)rC   minmaxstepr@   rF   style_referencezNOptional chain of style references (max 10) from Krea 2 Style Reference nodes.rF   r@   )r   Combor   _ASPECT_RATIOS_RESOLUTIONS_CREATIVITY_LEVELSStringFloatCustomr   r   r   r   r   _krea_model_inputsrV   =   s    	"* 	 	
 	

 	 ' 	 	
 	

 	&m	 	 	
 	
 		S 	 	
 	
 	 N 	 	
 	
 		&"##))d 	* 	
 	
C& &r   c            	       f    e Zd Zedej        fd            Zededede	dej
        fd            ZdS )Krea2ImageNoder   c                    t          j        ddddt           j                            dddd	          t           j                            d
t           j                            t          t                                t           j                            t          t                                t           j                            t          t                                gd          t           j
                            d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
dgdg          d          	  	        S )NrX   zKrea 2 Imagepartner/image/Kreau   Generate images via Krea 2 — pick Medium (expressive illustrations) or Large (expressive photorealism). Supports an optional moodboard and up to 10 chained image style references.promptTrE   zText prompt for the image.)	multilinerC   r@   modelzeKrea 2 Medium is best for expressive illustrations; Krea 2 Large is best for expressive photorealism.r>   seedr   iz Random seed for reproducibility.)rC   rJ   rK   control_after_generater@   zmodel.moodboard_idzmodel.style_reference)widgetsinputsa  
                (
                  $rates := {
                    "krea 2 medium turbo": {"text": 0.015, "style": 0.0175, "moodboard": 0.02},
                    "krea 2 medium": {"text": 0.03, "style": 0.035, "moodboard": 0.04},
                    "krea 2 large": {"text": 0.06, "style": 0.065, "moodboard": 0.07}
                  };
                  $r := $lookup($rates, widgets.model);
                  $hasMoodboard := $length($lookup(widgets, "model.moodboard_id")) > 0;
                  $hasStyle := $lookup(inputs, "model.style_reference").connected;
                  $usd := $hasMoodboard ? $r.moodboard : ($hasStyle ? $r.style : $r.text);
                  {"type":"usd","usd": $usd}
                )
                )
depends_onexpr)	node_iddisplay_namecategorydescriptionra   outputshiddenis_api_nodeprice_badge)r   SchemarS   r   DynamicComboOption_MODEL_MEDIUMrV   _MODEL_MEDIUM_TURBO_MODEL_LARGEIntImageOutputHiddenauth_token_comfy_orgapi_key_comfy_org	unique_id
PriceBadgePriceBadgeDependsr   s    r   define_schemazKrea2ImageNode.define_schemaj   ss   y$')8
 	"8	     %%..}>P>R>RSS../BDVDXDXYY..|=O=Q=QRR
H & 	 	 "+/>   #4 X__&&'	.	+	#
 /$&:;34    U>
 >
 >
 >	
r   r[   r]   r^   c                 P  K   t          |dd           |d         }t                              |          }|t          d|          d }|                    d          pd                                }|ret
                              |          st          d|d	          |                    d
          }t          ||dnt          |                    g}|                    d          }	d }
|	r?t          |	          dk    r t          dt          |	           d          d |	D             }
t          | t          |d          t          t          ||d         |d         ||d         ||
                     d {V }t          | t          d|j         d          t          d t                      d {V }|j        r|j        j        st'          d|j         d          t)          |j        j        d                    d {V }t+          j        |          S ) NFr'   )strip_whitespace
min_lengthr]   zUnknown Krea 2 model: rD   rE   z&moodboard_id must be a UUID (received z!); copy it from the Krea website.rG   rH   )idstrengthrM   
   z;Krea 2 accepts at most 10 image_style_references; received .c           	      b    g | ],}t          |d          t          |d                             -S )urlr   r   r   )r	   float).0refs     r   
<listcomp>z*Krea2ImageNode.execute.<locals>.<listcomp>   sC     & & &]`'CJs:AWAWXXX& & &r   r"   r#   r=   rA   rB   )r[   r=   rA   r^   rB   
moodboardsimage_style_references)r)   dataz/proxy/krea/jobs/GETc                     | j         S N)status)rs    r   <lambda>z(Krea2ImageNode.execute.<locals>.<lambda>   s    qx r   )r)   status_extractorqueued_statuseszKrea 2 job z" completed without any image URLs.r   )r   r3   get
ValueErrorstrip_UUID_REmatchr   r   lenr   r   r
   r   r   job_id_KREA_QUEUED_STATUSESresulturlsRuntimeErrorr   r   
NodeOutput)r   r[   r]   r^   model_choiceendpoint_pathr   mb_idmb_strengthrM   r   initialjobr   s                 r   executezKrea2ImageNode.execute   s      	1EEEEW~(,,\:: FlFFGGG15
>**0b7799 	q>>%(( v !t%!t!t!tuuu))$899K'5;CV44\abm\n\nooopJ))$566GK 	?##b(( !v_bcr_s_s!v!v!vwww& &ds& & &"  ]6:::")">2 . .%'=  	
 
 
 
 
 
 
 
 
 AAA%PPP"//1
 
 
 
 
 
 
 
 
 z 	] 	][SZ[[[\\\23:?13EFFFFFFFF}U###r   N)r   r   r   classmethodr   rl   r|   strdictintr   r   r   r   r   rX   rX   h   s        ?
bi ?
 ?
 ?
 [?
B 5$5$ 5$ 	5$
 
5$ 5$ 5$ [5$ 5$ 5$r   rX   c            
           e Zd Zedej        fd            Ze	 ddej        de	de
e         dz  dej        fd            ZdS )	Krea2StyleReferenceNoder   c                    t          j        ddddt           j                            dd          t           j                            dd	d
ddd          t          j        t          j                                      ddd          gt          j        t          j                                      d          gt           j	        j
        t           j	        j        t           j	        j        g          S )Nr   zKrea 2 Style ReferencerZ   zAdd an image style reference to a Krea 2 generation. Chain multiple Krea 2 Style Reference nodes (max 10) and feed the final `style_reference` output into Krea 2 Image. Each image is uploaded to ComfyAPI storage and passed as URL.r   z6Reference image whose style influences the generation.)r@   r   g      ?g       g       @rI   z?Reference strength; negative values invert the style influence.)rC   rJ   rK   rL   r@   rM   TzHOptional incoming chain of style references; this node appends one more.rN   )re   )rd   re   rf   rg   ra   rh   ri   )r   rl   rs   r   rT   rU   r   r   rt   ru   rv   rw   rx   r{   s    r   r|   z%Krea2StyleReferenceNode.define_schema   s    y-1)c
 T    ]    	&*++11%!f 2  & Yv/0077EV7WWX	.	+	#;"
 "
 "
 "	
r   Nr   r   rM   c                   K   |rt          |          ng }t          |          dk    rt          d          t          | |           d {V }|                    |t          |          d           t          j        |          S )Nr   zCKrea 2 accepts at most 10 image_style_references in one generation.r   )listr   r   r2   appendr   r   r   )r   r   r   rM   chainr   s         r   r   zKrea2StyleReferenceNode.execute  s       6ELD111"u::bccc0e<<<<<<<<SeHoo>>???}U###r   r   )r   r   r   r   r   rl   r|   r   rs   r   r   r   r   r   r   r   r   r   r      s        #
bi #
 #
 #
 [#
J 
 .2	$ ${$ $ dd*	$
 
$ $ $ [$ $ $r   r   c                   L    e Zd Zedeeej                          fd            ZdS )KreaExtensionr   c                 "   K   t           t          gS r   )rX   r   )selfs    r   get_node_listzKreaExtension.get_node_list  s       #
 	
r   N)	r   r   r   r   r   typer   	ComfyNoder   r   r   r   r   r     sE        
T$r|*<%= 
 
 
 X
 
 
r   r   c                  "   K   t                      S r   )r   r   r   r   comfy_entrypointr   %  s      ??r   )-__doc__retyping_extensionsr   comfy_api.latestr   r   r   comfy_api_nodes.apis.krear   r   r	   r
   r   comfy_api_nodes.utilr   r   r   r   r   r   r   r   r   rs   r   r2   ro   rp   rq   r3   r   __annotations__rP   rQ   rR   r   compiler   r   rV   rX   r   r   r   r   r   r   <module>r      s   " " " 				 & & & & & & 6 6 6 6 6 6 6 6 6 6                            ! ! ! ! ! ! ! !D,> u{ WZ      + BN@$ $sCx.    ONNv555 === 2:fgg(D ( ( ( (Vz$ z$ z$ z$ z$R\ z$ z$ z$z4$ 4$ 4$ 4$ 4$bl 4$ 4$ 4$n
 
 
 
 
N 
 
 
      r   