
    +j                     X   d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlmZmZmZmZmZm Z m!Z!m"Z"m#Z# d dl$Z$d dl%Z%d dl&m'Z' d d	l(m)Z)  G d
 de*e)          Z+defdZ, G d de	j-                  Z. G d de	j-                  Z/ G d de	j-                  Z0 G d de	j-                  Z1 G d de	j-                  Z2 G d de	j-                  Z3 G d de	j-                  Z4 G d de	j-                  Z5 G d de          Z6d e6fd!Z7dS )"    )cleandoc)Optional)override)ComfyExtensionInputIO)#StabilityUpscaleConservativeRequestStabilityUpscaleCreativeRequestStabilityAsyncResponseStabilityResultsGetResponseStabilityStable3_5RequestStabilityStableUltraRequestStabilityStableUltraResponseStabilityAspectRatioStability_SD3_5_ModelStability_SD3_5_GenerationModeget_stability_style_presetsStabilityTextToAudioRequestStabilityAudioToAudioRequestStabilityAudioInpaintRequestStabilityAudioResponse)	validate_audio_durationvalidate_stringaudio_input_to_mp3bytesio_to_image_tensortensor_to_bytesioaudio_bytes_to_audio_inputsync_oppoll_opApiEndpointN)BytesIO)Enumc                       e Zd ZdZdZdZdS )StabilityPollStatusfinishedin_progressfailedN)__name__
__module____qualname__r%   r&   r'        ?/home/wildlama/comfy/ComfyUI/comfy_api_nodes/nodes_stability.pyr$   r$   )   s        HKFFFr,   r$   xc                 t    | j         | j        t          j        S | j        t          j        S t          j        S N)nameerrorsr$   r'   finish_reasonr%   r&   r.   s    r-   get_async_dummy_statusr5   /   s4    vQX1"))	
	$"++**r,   c                       e Zd ZdZed             Ze	 	 	 ddededed	ed
ee	j
                 dedee         dej        fd            ZdS )StabilityStableImageUltraNodeH
    Generates images synchronously based on prompt and resolution.
    c                 b   t          j        dddt          | j        pd          t           j                            dddd          t           j                            d	t          t          j        d
          t           j                            dt                      dd          t           j
                            dddddt           j        j        dd          t           j                            dd          t           j                            dddddd          t           j                            ddddddd           gt           j                                        gt           j        j        t           j        j        t           j        j        gdt          j        d!"          #	  	        S )$Nr7   zStability AI Stable Image Ultrapartner/image/Stability AI promptTa  What you wish to see in the output image. A strong, descriptive prompt that clearly defineselements, colors, and subjects will lead to better results. To control the weight of a given word use the format `(word:weight)`,where `word` is the word you'd like to control the weight of and `weight`is a value between 0 and 1. For example: `The sky was a crisp (blue:0.3) and (green:0.8)`would convey a sky that was blue and green, but more green than blue.	multilinedefaulttooltipaspect_ratio Aspect ratio of generated image.optionsr?   r@   style_preset*Optional desired style of generated image.rD   r@   advancedseedr          ,The random seed used for creating the noise.r?   minmaxstepdisplay_modecontrol_after_generater@   imageoptionalnegative_promptzhA blurb of text describing what you do not wish to see in the output image. This is an advanced feature.r?   r@   force_inputrU   rH   image_denoise      ?              ?{Gz?gDenoise of input image; 0.0 yields image identical to input, 1.0 is as if no image was provided at all.r?   rN   rO   rP   r@   rU   z{"type":"usd","usd":0.08}expr	node_iddisplay_namecategorydescriptioninputsoutputshiddenis_api_nodeprice_badge)r   Schemar   __doc__Stringr   Combor   	ratio_1_1r   IntNumberDisplaynumberImageFloatOutputHiddenauth_token_comfy_orgapi_key_comfy_org	unique_id
PriceBadgeclss    r-   define_schemaz+StabilityStableImageUltraNode.define_schema<   s   y3:1 !233	"l	   
 
 "00:>	    "799H!	    "!#!1!8+/J  	 	 !    	% G $!!     # F!   ]7r !! 	.	+	#
 4  MI
 I
 I
 I	
r,   Nr;   rZ   r<   rA   rE   rI   rS   rV   rY   returnc                   K   t          |d           d }|$t          |d                                          }nd }|sd }|dk    rd }d|i}	t          | t	          dd	          t
          t          ||||||
          |	d           d {V }
|
j        dk    rt          d|
j         d          t          j
        |
j                  }t          t          |                    }t          j        |          S )NFstrip_whitespace " total_pixelsNonerS   z3/proxy/stability/v2beta/stable-image/generate/ultraPOSTpathmethod)r<   rV   rA   rI   strengthrE   multipart/form-dataresponse_modeldatafilescontent_typeSUCCESSz&Stable Image Ultra generation failed: .)r   r   readr   r    r   r   r3   	Exceptionbase64	b64decoderS   r   r!   r   
NodeOutput)r}   r<   rA   rE   rI   rS   rV   rY   image_binaryr   response_api
image_datareturned_images                r-   executez%StabilityStableImageUltraNode.execute   sS      	7777,UKKKPPRRLL M 	#"O6!!L \
 %R[abbb7, /)&)   .
 
 
 
 
 
 
 
 
  %22b\E_bbbccc%l&899
01D1DEE}^,,,r,   Nr;   rZ   )r(   r)   r*   rm   classmethodr~   strintr   torchTensorfloatr   r   r   r+   r,   r-   r7   r7   7   s          J
 J
 [J
X  )-!),1- 1-1- 1- 	1-
 1- %1- 1-  1- 
1- 1- 1- [1- 1- 1-r,   r7   c                       e Zd ZdZed             Ze	 	 	 ddededed	ed
edede	e
j                 dede	e         dej        fd            ZdS )StabilityStableImageSD_3_5Noder8   c                    t          j        dddt          | j        pd          t           j                            dddd          t           j                            d	t          
          t           j                            dt          t          j	        d          t           j                            dt                      dd          t           j                            dddddd          t           j                            dddddt           j        j        dd          t           j                            dd          t           j                            d dd!ddd"          t           j                            d#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	g)          d*+          ,	  	        S )-Nr   z'Stability AI Stable Diffusion 3.5 Imager:   r;   r<   TWhat you wish to see in the output image. A strong, descriptive prompt that clearly defines elements, colors, and subjects will lead to better results.r=   modelrD   rA   rB   rC   rE   rF   rG   	cfg_scaleg      @r\   g      $@皙?zsHow strictly the diffusion process adheres to the prompt text (higher values keep your image closer to your prompt)r?   rN   rO   rP   r@   rI   r   rJ   rK   rL   rM   rS   rT   rV   YKeywords of what you do not wish to see in the output image. This is an advanced feature.rW   rY   rZ   r[   r]   r^   r_   )widgetsz
                (
                  $contains(widgets.model,"large")
                    ? {"type":"usd","usd":0.065}
                    : {"type":"usd","usd":0.035}
                )
                )
depends_onra   rb   )r   rl   r   rm   rn   r   ro   r   r   rp   r   ru   rq   rr   rs   rt   rv   rw   rx   ry   rz   r{   PriceBadgeDependsr|   s    r-   r~   z,StabilityStableImageSD_3_5Node.define_schema   s)   y4B1 !233	" v	     1    "00:>	    "799H!	     R    "!#!1!8+/J  	 	 !    	%w $!!     # F!   k>@ !! 	.	+	#
 /	BBB	 	 	[W
 W
 W
 W	
r,   Nr;   rZ   r   r<   rA   rE   rI   r   rS   rV   rY   r   c
                 2  K   t          |d           d }
t          j        }|2t          |d                                          }
t          j        }d }nd }	|sd }|dk    rd }d|
i}t          | t          dd	          t          t          |||||	||||
	  	        |d           d {V }|j
        dk    rt          d|j
         d          t          j        |j                  }t          t!          |                    }t#          j        |          S )NFr   r   r   r   rS   z1/proxy/stability/v2beta/stable-image/generate/sd3r   r   )	r<   rV   rA   rI   r   rE   r   r   moder   r   r   z.Stable Diffusion 3.5 Image generation failed: r   )r   r   text_to_imager   r   image_to_imager   r    r   r   r3   r   r   r   rS   r   r!   r   r   )r}   r   r<   rA   rE   rI   r   rS   rV   rY   r   r   r   r   r   r   s                   r-   r   z&StabilityStableImageSD_3_5Node.execute  sq      	7777-;,UKKKPPRRL1@DLL M 	#"O6!!L \
 %PY_```7* /)&)#
 
 
 .!
 
 
 
 
 
 
 
 
& %22j\Mgjjjkkk%l&899
01D1DEE}^,,,r,   r   )r(   r)   r*   rm   r   r~   r   r   r   r   r   r   r   r   r   r+   r,   r-   r   r      s          X
 X
 [X
t  )-!),9- 9-9- 9- 	9-
 9- 9- 9- %9- 9-  9- 
9- 9- 9- [9- 9- 9-r,   r   c                   p    e Zd 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 ) StabilityUpscaleConservativeNodeB
    Upscale image with minimal alterations to 4K resolution.
    c                    t          j        dddt          | j        pd          t           j                            d          t           j                            dddd	          t           j                            d
ddddd          t           j                            dddddt           j	        j
        d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        d          	  	        S )Nr   z!Stability AI Upscale Conservativer:   r;   rS   r<   Tr   r=   
creativitygffffff?g?rZ   r]   aControls the likelihood of creating additional details not heavily conditioned by the init image.r   rI   r   rJ   rK   rL   rM   rV   r   rW   z{"type":"usd","usd":0.4}r`   rb   )r   rl   r   rm   rt   r   rn   ru   rq   rr   rs   rv   rw   rx   ry   rz   r{   r|   s    r-   r~   z.StabilityUpscaleConservativeNode.define_schema`  sV   y6<1 !233w''	" v	           "!#!1!8+/J  	 	 	%w $!!    5"H !! 	.	+	#
 3  c4
 4
 4
 4	
r,   r;   rS   r<   r   rI   rV   r   c                   K   t          |d           t          |d                                          }|sd }d|i}t          | t	          dd          t
          t          ||t          |d	          |
          |d           d {V }|j        dk    rt          d|j         d          t          j        |j                  }	t          t          |	                    }
t          j        |
          S )NFr      r   rS   z9/proxy/stability/v2beta/stable-image/upscale/conservativer   r      )r<   rV   r   rI   r   r   r   z2Stability Upscale Conservative generation failed: r   )r   r   r   r   r    r   r	   roundr3   r   r   r   rS   r   r!   r   r   )r}   rS   r<   r   rI   rV   r   r   r   r   r   s              r-   r   z(StabilityUpscaleConservativeNode.execute  s6      	7777(YGGGLLNN 	#"O \
 %Xaghhh74 / A..	   .
 
 
 
 
 
 
 
 
 %22nQ]Qknnnooo%l&899
01D1DEE}^,,,r,   Nr;   r(   r)   r*   rm   r   r~   r   r   r   r   r   r   r   r   r+   r,   r-   r   r   [  s          5
 5
 [5
n   "&- &-|&- &- 	&-
 &- &- 
&- &- &- [&- &- &-r,   r   c                   t    e Zd ZdZed             Ze	 ddej        dede	dede
d	ed
ej        fd            ZdS )StabilityUpscaleCreativeNoder   c                    t          j        dddt          | j        pd          t           j                            d          t           j                            dddd	          t           j                            d
ddddd          t           j                            dt                      dd          t           j
                            dddddt           j        j        d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        d          	  	        S ) Nr   zStability AI Upscale Creativer:   r;   rS   r<   Tr   r=   r   g333333?r   rZ   r]   r   r   rE   rF   rG   rI   r   rJ   rK   rL   rM   rV   r   rW   z{"type":"usd","usd":0.6}r`   rb   )r   rl   r   rm   rt   r   rn   ru   ro   r   rq   rr   rs   rv   rw   rx   ry   rz   r{   r|   s    r-   r~   z*StabilityUpscaleCreativeNode.define_schema  s   y281 !233w''	" v	          "799H!	    "!#!1!8+/J  	 	 	%w $!!    A(T !! 	.	+	#
 3  o:
 :
 :
 :	
r,   r;   rS   r<   r   rE   rI   rV   r   c                 p  K   t          |d           t          |d                                          }|sd }|dk    rd }d|i}t          | t	          dd	          t
          t          ||t          |d
          ||          |d           d {V }	t          | t	          d|	j	                   t          dd            d {V }
|
j        dk    rt          d|
j         d          t          j        |
j                  }t!          t#          |                    }t%          j        |          S )NFr   r   r   r   rS   z5/proxy/stability/v2beta/stable-image/upscale/creativer   r   r   )r<   rV   r   rE   rI   r   r   z /proxy/stability/v2beta/results/)r      c                      t          |           S r0   )r5   r4   s    r-   <lambda>z6StabilityUpscaleCreativeNode.execute.<locals>.<lambda>/  s    '=a'@'@ r,   )r   poll_intervalstatus_extractorr   z.Stability Upscale Creative generation failed: r   )r   r   r   r   r    r   r
   r   r   idr   r3   r   r   r   resultr   r!   r   r   )r}   rS   r<   r   rE   rI   rV   r   r   r   response_pollr   r   s                r-   r   z$StabilityUpscaleCreativeNode.execute  s      	7777(YGGGLLNN 	#"O6!!L \
 %T]cddd10 / A..)   .
 
 
 
 
 
 
 
 
 &QQQRRR6@@
 
 
 
 
 
 
 
 
 &)33k]Mhkkklll%m&:;;
01D1DEE}^,,,r,   Nr   r   r+   r,   r-   r   r     s          ;
 ;
 [;
z   "2- 2-|2- 2- 	2-
 2- 2- 2- 
2- 2- 2- [2- 2- 2-r,   r   c                   \    e Zd ZdZed             Zedej        dej	        fd            Z
dS )StabilityUpscaleFastNodez
    Quickly upscales an image via Stability API call to 4x its original size; intended for upscaling low-quality/compressed images.
    c                 `   t          j        dddt          | j        p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Stability AI Upscale Fastr:   r;   rS   Tz{"type":"usd","usd":0.02}r`   rb   )r   rl   r   rm   rt   r   rv   rw   rx   ry   rz   r{   r|   s    r-   r~   z&StabilityUpscaleFastNode.define_schema@  s    y.41 !233w'' !! 	.	+	#
 4  #
 
 
 	
r,   rS   r   c                   K   t          |d                                          }d|i}t          | t          dd          t          |d           d {V }|j        d	k    rt          d
|j         d          t          j        |j	                  }t          t          |                    }t          j        |          S )Ni   r   rS   z1/proxy/stability/v2beta/stable-image/upscale/fastr   r   r   )r   r   r   r   zStability Upscale Fast failed: r   )r   r   r   r    r   r3   r   r   r   rS   r   r!   r   r   )r}   rS   r   r   r   r   r   s          r-   r   z StabilityUpscaleFastNode.executeX  s      (YGGGLLNN \
 %PY_```7.
 
 
 
 
 
 
 
 
 %22[l>X[[[\\\%l&899
01D1DEE}^,,,r,   N)r(   r)   r*   rm   r   r~   r   r   r   r   r   r+   r,   r-   r   r   ;  sk          
 
 [
. -%, -2= - - - [- - -r,   r   c                   b    e Zd ZdZed             Zededededededej	        fd	            Z
d
S )StabilityTextToAudiozFGenerates high-quality music and sound effects from text descriptions.c                    t          j        ddddt          | j        pd          t           j                            ddg          t           j                            d	d
d          t           j                            ddddddd
          t           j                            dddddt           j        j	        d
dd
	  	        t           j                            ddddddd
d
          gt           j
                                        gt           j        j        t           j        j        t           j        j        gd
t          j        d          
  
        S )Nr   zStability AI Text To Audiopartner/audio/Stability AIAudior;   r   stable-audio-2.5r   r<   Tr>   r?   duration   rK   8Controls the duration in seconds of the generated audio.r_   rI   r   rJ   $The random seed used for generation.r?   rN   rO   rP   rQ   rR   r@   rU   steps      &Controls the number of sampling steps.r?   rN   rO   rP   r@   rU   rH   {"type":"usd","usd":0.2}r`   )
rc   rd   re   essentials_categoryrf   rg   rh   ri   rj   rk   )r   rl   r   rm   ro   r   rn   rq   rr   rs   r   rv   rw   rx   ry   rz   r{   r|   s    r-   r~   z"StabilityTextToAudio.define_schemat  se   y*51 ' !233/0    	D"EEV!    "!#!1!8+/B!  
 
 D!!  	 	5$L !! 	.	+	#
 3  i7
 7
 7
 7	
r,   r   r<   r   rI   r   r   c                 H  K   t          |d           t          |||||          }t          | t          dd          t          |d           d {V }|j        st          d	          t          j        t          t          j        |j                                      S )
N'  
max_length)r<   r   r   rI   r   z:/proxy/stability/v2beta/audio/stable-audio-2/text-to-audior   r   r   )r   r   r   'No audio file was received in response.)r   r   r   r    r   audio
ValueErrorr   r   r   r   r   )r}   r   r<   r   rI   r   payloadr   s           r-   r   zStabilityTextToAudio.execute  s      51111-V5S[bfnsttt$Ybhiii1.
 
 
 
 
 
 
 
 
 ! 	HFGGG}78HI[8\8\]]^^^r,   N)r(   r)   r*   rm   r   r~   r   r   r   r   r   r+   r,   r-   r   r   q  s        PP8
 8
 [8
t _# _s _c _ _UX _]_]j _ _ _ [_ _ _r,   r   c                   t    e Zd ZdZed             Zedededej        de	de	de	d	e
d
ej        fd            ZdS )StabilityAudioToAudioz]Transforms existing audio samples into new high-quality compositions using text instructions.c                 T   t          j        dddt          | j        pd          t           j                            ddg          t           j                            dd	d
          t           j                            dd          t           j                            ddddddd	          t           j                            dddddt           j	        j
        d	dd		  	        t           j                            ddddddd	d	          t           j                            dddddt           j	        j        d d	!          gt           j                                        gt           j        j        t           j        j        t           j        j        gd	t          j        d"#          $	  	        S )%Nr   zStability AI Audio To Audior   r;   r   r   r   r<   Tr   r   -Audio must be between 6 and 190 seconds long.r@   r   r   rK   r   r_   rI   r   rJ   r   r   r   r   r   r   r   r   r]   r\   zUParameter controls how much influence the audio parameter has on the generated audio.)r?   rN   rO   rP   rQ   r@   rU   r   r`   rb   )r   rl   r   rm   ro   r   rn   r   rq   rr   rs   ru   sliderrv   rw   rx   ry   rz   r{   r|   s    r-   r~   z#StabilityAudioToAudio.define_schema  s   y+61 !233/0    	D"EEw0_``V!    "!#!1!8+/B!  
 
 D!!  	 	 !#!1!8s!  	 	K/b !! 	.	+	#
 3  }A
 A
 A
 A	
r,   r   r<   r   r   rI   r   r   r   c                   K   t          |d           t          |dd           t          ||||||          }t          | t	          dd          t
          |d	d
t          |          i           d {V }	|	j        st          d          t          j
        t          t          j        |	j                                      S )Nr   r      r   )r<   r   r   rI   r   r   z;/proxy/stability/v2beta/audio/stable-audio-2/audio-to-audior   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   rI   r   r   r   r   s
             r-   r   zStabilityAudioToAudio.execute  s       	51111q#....E\d
 
 
 %Zcijjj1..u556
 
 
 
 
 
 
 
 
 ! 	HFGGG}78HI[8\8\]]^^^r,   N)r(   r)   r*   rm   r   r~   r   r   r   r   r   r   r   r   r+   r,   r-   r   r     s        ggB
 B
 [B
H __!$_-2[_DG_OR_[^_jo_	_ _ _ [_ _ _r,   r   c                   x    e Zd ZdZed             Zedededej        de	de	de	d	e	d
e	de
j        fd            ZdS )StabilityAudioInpaintzATransforms part of existing audio sample using text instructions.c                 ~   t          j        dddt          | j        pd          t           j                            ddg          t           j                            dd	d
          t           j                            dd          t           j                            ddddddd	          t           j                            dddddt           j	        j
        d	dd		  	        t           j                            ddddddd	d	          t           j                            dddddd	d	          t           j                            d ddddd	d	          gt           j                                        gt           j        j        t           j        j        t           j        j        gd	t          j        d!"          #	  	        S )$Nr   zStability AI Audio Inpaintr   r;   r   r   r   r<   Tr   r   r   r   r   r   rK   r   r_   rI   r   rJ   r   r   r   r   r   r   r   
mask_start   )r?   rN   rO   rP   rU   rH   mask_endr   r`   rb   )r   rl   r   rm   ro   r   rn   r   rq   rr   rs   rv   rw   rx   ry   rz   r{   r|   s    r-   r~   z#StabilityAudioInpaint.define_schema   s   y+51 !233/0    	D"EEw0_``V!    "!#!1!8+/B!  
 
 D!!  	 	  !!    !!   ]7r !! 	.	+	#
 3  MI
 I
 I
 I	
r,   r   r<   r   r   rI   r   r   r   r   c	                   K   t          |d           ||k    rt          d| d| d          t          |dd           t          |||||||          }	t	          | t          d	d
          t          |	ddt          |          i           d {V }
|
j        st          d          t          j
        t          t          j        |
j                                      S )Nr   r   zValue of mask_end(z$) should be greater then mask_start()r   r   )r<   r   r   rI   r   r   r   z4/proxy/stability/v2beta/audio/stable-audio-2/inpaintr   r   r   r   )endpointr   r   r   r   r   )r   r   r   r   r   r    r   r   r   r   r   r   r   r   )r}   r   r<   r   r   rI   r   r   r   r   r   s              r-   r   zStabilityAudioInpaint.executem  s+      	51111z!!m(mm`jmmmnnnq#....!
 
 
 % &\eklll1..u556
 
 
 
 
 
 
 
 
 ! 	HFGGG}78HI[8\8\]]^^^r,   N)r(   r)   r*   rm   r   r~   r   r   r   r   r   r   r   r+   r,   r-   r   r     s        KKJ
 J
 [J
X #_#_ #_ {	#_
 #_ #_ #_ #_ #_ 
#_ #_ #_ [#_ #_ #_r,   r   c                   L    e Zd Zedeeej                          fd            ZdS )StabilityExtensionr   c                 j   K   t           t          t          t          t          t
          t          t          gS r0   )r7   r   r   r   r   r   r   r   )selfs    r-   get_node_listz StabilityExtension.get_node_list  s*       **,($ !!	
 		
r,   N)	r(   r)   r*   r   listtyper   	ComfyNoder  r+   r,   r-   r   r     sE        

T$r|*<%= 

 

 

 X

 

 

r,   r   r   c                  "   K   t                      S r0   )r   r+   r,   r-   comfy_entrypointr    s      r,   )8inspectr   typingr   typing_extensionsr   comfy_api.latestr   r   r   comfy_api_nodes.apis.stabilityr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   comfy_api_nodes.utilr   r   r   r   r   r   r   r   r    r   r   ior!   enumr"   r   r$   r5   r  r7   r   r   r   r   r   r   r   r   r  r+   r,   r-   <module>r     s               & & & & & & 6 6 6 6 6 6 6 6 6 6                                 "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
                  #t   +9 + + + +D- D- D- D- D-BL D- D- D-NZ- Z- Z- Z- Z-R\ Z- Z- Z-zd- d- d- d- d-r| d- d- d-Nv- v- v- v- v-2< v- v- v-r3- 3- 3- 3- 3-r| 3- 3- 3-lK_ K_ K_ K_ K_2< K_ K_ K_\[_ [_ [_ [_ [_BL [_ [_ [_|t_ t_ t_ t_ t_BL t_ t_ t_n
 
 
 
 
 
 
 
  2            r,   