o
    i                     @   s  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 de*e)Z+defddZ,G dd de	j-Z.G dd de	j-Z/G dd de	j-Z0G dd de	j-Z1G dd de	j-Z2G dd de	j-Z3G dd de	j-Z4G dd de	j-Z5G dd  d eZ6d!e6fd"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                   @   s   e Zd ZdZdZdZdS )StabilityPollStatusfinishedin_progressfailedN)__name__
__module____qualname__r#   r$   r%    r)   r)   =/mnt/c/Users/fbmor/ComfyUI/comfy_api_nodes/nodes_stability.pyr"   )   s    r"   xc                 C   s0   | j d us
| jd urtjS | jd urtjS tjS N)nameerrorsr"   r%   finish_reasonr#   r$   r+   r)   r)   r*   get_async_dummy_status/   s
   
r1   c                   @   s^   e Zd ZdZed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dZdS )StabilityStableImageUltraNodeH
    Generates images synchronously based on prompt and resolution.
    c                 C   s   t jdddt| jp
dt jjdddddt jjd	ttjd
dt jjdt	 dddt j
jdddddt jjdddt jjdddt jjdddddddt j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!d"d#	S )$Nr2   zStability AI Stable Image Ultraapi node/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.optionsr9   r:   style_preset*Optional desired style of generated image.r>   r:   advancedseedr          ,The random seed used for creating the noise.r9   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.r9   r:   force_inputrO   rB   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.r9   rH   rI   rJ   r:   rO   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clsr)   r)   r*   define_schema<   s   
9z+StabilityStableImageUltraNode.define_schemaNr5   rT   r6   r;   r?   rC   rM   rP   rS   returnc                    s   t |dd d }|d urt|dd }nd }|sd }|dkr"d }d|i}	t| tddd	tt||||||d
|	ddI d H }
|
jdkrMtd|
j dt	
|
j}tt|}t|S )NFstrip_whitespace " total_pixelsNonerM   z3/proxy/stability/v2beta/stable-image/generate/ultraPOSTpathmethod)r6   rP   r;   rC   strengthr?   multipart/form-dataresponse_modeldatafilescontent_typeSUCCESSz&Stable Image Ultra generation failed: .)r   r   readr   r   r   r   r/   	Exceptionbase64	b64decoderM   r   r    r   
NodeOutput)rw   r6   r;   r?   rC   rM   rP   rS   image_binaryr   response_api
image_datareturned_imager)   r)   r*   execute   s@   


z%StabilityStableImageUltraNode.executeNr5   rT   )r&   r'   r(   rg   classmethodrx   strintr   torchTensorfloatr   r   r   r)   r)   r)   r*   r2   7   s2    
L	r2   c                   @   sf   e Zd ZdZed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dZdS )StabilityStableImageSD_3_5Noder3   c                 C   s  t jdddt| jp
dt jjdddddt jjd	td
t jjdttj	ddt jjdt
 dddt jjdddddddt jjdddddt jjdddt jjdddt jjd dd!dddd"t j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t jd	gd)d*d+d,	S )-Nr   z'Stability AI Stable Diffusion 3.5 Imager4   r5   r6   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.r7   modelr>   r;   r<   r=   r?   r@   rA   	cfg_scaleg      @rV   g      $@皙?zsHow strictly the diffusion process adheres to the prompt text (higher values keep your image closer to your prompt)r9   rH   rI   rJ   r:   rC   r   rD   rE   rF   rG   rM   rN   rP   YKeywords of what you do not wish to see in the output image. This is an advanced feature.rQ   rS   rT   rU   rW   rX   rY   )widgetsz
                (
                  $contains(widgets.model,"large")
                    ? {"type":"usd","usd":0.065}
                    : {"type":"usd","usd":0.035}
                )
                )
depends_onr[   r\   )r   rf   r   rg   rh   r   ri   r   r   rj   r   ro   rk   rl   rm   rn   rp   rq   rr   rs   rt   ru   PriceBadgeDependsrv   r)   r)   r*   rx      s   
@z,StabilityStableImageSD_3_5Node.define_schemaNr5   rT   r   r6   r;   r?   rC   r   rM   rP   rS   ry   c
                    s   t |dd d }
tj}|d urt|dd }
tj}d }nd }	|s$d }|dkr*d }d|
i}t| tddd	tt	|||||	||||d
	|ddI d H }|j
dkrXtd|j
 dt|j}tt|}t|S )NFrz   r|   r}   r   rM   z1/proxy/stability/v2beta/stable-image/generate/sd3r   r   )	r6   rP   r;   rC   r   r?   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   r/   r   r   r   rM   r   r    r   r   )rw   r   r6   r;   r?   rC   r   rM   rP   rS   r   r   r   r   r   r   r)   r)   r*   r     sL   


z&StabilityStableImageSD_3_5Node.executer   )r&   r'   r(   rg   r   rx   r   r   r   r   r   r   r   r   r   r)   r)   r)   r*   r      s:    
Z		
r   c                   @   sJ   e Zd ZdZedd Ze	ddejdede	de
d	ed
ejfddZdS ) StabilityUpscaleConservativeNodeB
    Upscale image with minimal alterations to 4K resolution.
    c                 C   s   t jdddt| jp
dt jdt jjddddd	t jjd
ddddddt jjdddddt j	j
dddt j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dd	S )Nr   z!Stability AI Upscale Conservativer4   r5   rM   r6   Tr   r7   
creativitygffffff?g?rT   rW   aControls the likelihood of creating additional details not heavily conditioned by the init image.r   rC   r   rD   rE   rF   rG   rP   r   rQ   {"type":"usd","usd":0.25}rZ   r\   )r   rf   r   rg   rn   r   rh   ro   rk   rl   rm   rp   rq   rr   rs   rt   ru   rv   r)   r)   r*   rx   `  sd   

$z.StabilityUpscaleConservativeNode.define_schemar5   rM   r6   r   rC   rP   ry   c              
      s   t |dd t|dd }|sd }d|i}t| tdddtt||t|d	|d
|ddI d H }|jdkr?t	d|j dt
|j}	tt|	}
t|
S )NFrz      r}   rM   z9/proxy/stability/v2beta/stable-image/upscale/conservativer   r      )r6   rP   r   rC   r   r   r   z2Stability Upscale Conservative generation failed: r   )r   r   r   r   r   r   r   roundr/   r   r   r   rM   r   r    r   r   )rw   rM   r6   r   rC   rP   r   r   r   r   r   r)   r)   r*   r     s2   	


z(StabilityUpscaleConservativeNode.executeNr5   r&   r'   r(   rg   r   rx   r   r   r   r   r   r   r   r   r)   r)   r)   r*   r   [  s&    
7r   c                   @   sN   e Zd ZdZedd Ze	ddejdede	ded	e
d
edejfddZdS )StabilityUpscaleCreativeNoder   c                 C   s   t jdddt| jp
dt jdt jjddddd	t jjd
ddddddt jjdt	 dddt j
jdddddt jjdddt j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dd	S ) Nr   zStability AI Upscale Creativer4   r5   rM   r6   Tr   r7   r   g333333?r   rT   rW   r   r   r?   r@   rA   rC   r   rD   rE   rF   rG   rP   r   rQ   r   rZ   r\   )r   rf   r   rg   rn   r   rh   ro   ri   r   rk   rl   rm   rp   rq   rr   rs   rt   ru   rv   r)   r)   r*   rx     sp   

*z*StabilityUpscaleCreativeNode.define_schemar5   rM   r6   r   r?   rC   rP   ry   c                    s   t |dd t|dd }|sd }|dkrd }d|i}t| tddd	tt||t|d
||d|ddI d H }	t| td|	j	 dt
ddd dI d H }
|
jdkr[td|
j dt|
j}tt|}t|S )NFrz   r   r}   r   rM   z5/proxy/stability/v2beta/stable-image/upscale/creativer   r   r   )r6   rP   r   r?   rC   r   r   z /proxy/stability/v2beta/results/)r      c                 S   s   t | S r,   )r1   r0   r)   r)   r*   <lambda>/  s    z6StabilityUpscaleCreativeNode.execute.<locals>.<lambda>)r   poll_intervalstatus_extractorr   z.Stability Upscale Creative generation failed: r   )r   r   r   r   r   r
   r	   r   r   idr   r/   r   r   r   resultr   r    r   r   )rw   rM   r6   r   r?   rC   rP   r   r   r   response_pollr   r   r)   r)   r*   r     sF   



z$StabilityUpscaleCreativeNode.executeNr   r   r)   r)   r)   r*   r     s*    
=r   c                   @   s6   e Zd ZdZedd Zedejdej	fd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                 C   sR   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dd		S )
Nr   zStability AI Upscale Fastr4   r5   rM   Tz{"type":"usd","usd":0.01}rZ   r\   )r   rf   r   rg   rn   r   rp   rq   rr   rs   rt   ru   rv   r)   r)   r*   rx   @  s$   
z&StabilityUpscaleFastNode.define_schemarM   ry   c                    sx   t |dd }d|i}t| tdddt|ddI d H }|jd	kr+td
|j dt|j	}t
t|}t|S )Ni   r}   rM   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   r/   r   r   r   rM   r   r    r   r   )rw   rM   r   r   r   r   r   r)   r)   r*   r   X  s    


z StabilityUpscaleFastNode.executeN)r&   r'   r(   rg   r   rx   r   r   r   r   r   r)   r)   r)   r*   r   ;  s    
r   c                   @   sD   e Zd ZdZedd Zedededededed	ej	fd
dZ
dS )StabilityTextToAudiozFGenerates high-quality music and sound effects from text descriptions.c                 C   s   t jddddt| jpdt jjddgdt jjd	d
ddt jjddddddd
dt jjdddddt jj	d
dd
d	t jjd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dd
S )Nr   zStability AI Text To Audioapi node/audio/Stability AIAudior5   r   stable-audio-2.5r   r6   Tr8   r9   duration   rE   8Controls the duration in seconds of the generated audio.rY   rC   r   rD   $The random seed used for generation.r9   rH   rI   rJ   rK   rL   r:   rO   steps      &Controls the number of sampling steps.r9   rH   rI   rJ   r:   rO   rB   {"type":"usd","usd":0.2}rZ   )
r]   r^   r_   essentials_categoryr`   ra   rb   rc   rd   re   )r   rf   r   rg   ri   r   rh   rk   rl   rm   r   rp   rq   rr   rs   rt   ru   rv   r)   r)   r*   rx   t  sj   	&z"StabilityTextToAudio.define_schemar   r6   r   rC   r   ry   c                    sd   t |dd t|||||d}t| tdddt|ddI d H }|js'td	tt	t
|jS )
N'  
max_length)r6   r   r   rC   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   )rw   r   r6   r   rC   r   payloadr   r)   r)   r*   r     s   
zStabilityTextToAudio.executeN)r&   r'   r(   rg   r   rx   r   r   r   r   r   r)   r)   r)   r*   r   q  s    
:*r   c                   @   sN   e Zd ZdZedd Zedededejde	de	d	e	d
e
dejfddZdS )StabilityAudioToAudioz]Transforms existing audio samples into new high-quality compositions using text instructions.c                 C   s   t jdddt| jp
dt jjddgdt jjdd	dd
t jjdddt jjddddddd	dt jjdddddt j	j
d	dd	d	t jjddddddd	d	dt jjdddddt j	jd d	d!gt j gt jjt jjt jjgd	t jd"d#d$	S )%Nr   zStability AI Audio To Audior   r5   r   r   r   r6   Tr   r   -Audio must be between 6 and 190 seconds long.r:   r   r   rE   r   rY   rC   r   rD   r   r   r   r   r   r   r   r   rW   rV   zUParameter controls how much influence the audio parameter has on the generated audio.)r9   rH   rI   rJ   rK   r:   rO   r   rZ   r\   )r   rf   r   rg   ri   r   rh   r   rk   rl   rm   ro   sliderrp   rq   rr   rs   rt   ru   rv   r)   r)   r*   rx     s~   	
1z#StabilityAudioToAudio.define_schemar   r6   r   r   rC   r   r   ry   c           
   	      s|   t |dd t|dd t||||||d}t| tdddt|d	d
t|idI d H }	|	js3tdt	
tt|	jS )Nr   r      r   )r6   r   r   rC   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   )
rw   r   r6   r   r   rC   r   r   r   r   r)   r)   r*   r     s"   

zStabilityAudioToAudio.executeN)r&   r'   r(   rg   r   rx   r   r   r   r   r   r   r   r   r)   r)   r)   r*   r     s,    
Dr   c                   @   sR   e Zd ZdZed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dZdS )StabilityAudioInpaintzATransforms part of existing audio sample using text instructions.c                 C   s   t jdddt| jp
dt jjddgdt jjdd	dd
t jjdddt jjddddddd	dt jjdddddt j	j
d	dd	d	t jjddddddd	d	dt jjdddddd	d	dt j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!d"d#	S )$Nr   zStability AI Audio Inpaintr   r5   r   r   r   r6   Tr   r   r   r   r   r   rE   r   rY   rC   r   rD   r   r   r   r   r   r   r   
mask_start   )r9   rH   rI   rJ   rO   rB   mask_endr   rZ   r\   )r   rf   r   rg   ri   r   rh   r   rk   rl   rm   rp   rq   rr   rs   rt   ru   rv   r)   r)   r*   rx      s   	
	9z#StabilityAudioInpaint.define_schemar   r6   r   r   rC   r   r   r   ry   c	              	      s   t |dd ||krtd| d| dt|dd t|||||||d}	t| td	d
dt|	ddt|idI d H }
|
jsCtdt	
tt|
jS )Nr   r   zValue of mask_end(z$) should be greater then mask_start()r   r   )r6   r   r   rC   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   )rw   r   r6   r   r   rC   r   r   r   r   r   r)   r)   r*   r   m  s2   	

zStabilityAudioInpaint.executeN)r&   r'   r(   rg   r   rx   r   r   r   r   r   r   r   r)   r)   r)   r*   r     s0    
L	
r   c                   @   s(   e Zd Zedeeej  fddZdS )StabilityExtensionry   c                    s   t tttttttgS r,   )r2   r   r   r   r   r   r   r   )selfr)   r)   r*   get_node_list  s   z StabilityExtension.get_node_listN)	r&   r'   r(   r   listtyper   	ComfyNoder   r)   r)   r)   r*   r     s    r   ry   c                      s   t  S r,   )r   r)   r)   r)   r*   comfy_entrypoint  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"   r1   r   r2   r   r   r   r   r   r   r   r   r   r)   r)   r)   r*   <module>   s0    D,  gy6N^w