o
    i6                     @   s  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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 dddd	d
dddZddddddZddddddZdhZg dZg dZdZ e d Z!G dd dej"Z#G dd dej"Z$G dd  d eZ%d!e%fd"d#Z&dS )$    N)override)IOComfyExtensionInput)ImageEnhanceTaskCreateRequestInputVideoModelTaskCreateDataResponseTaskCreateResponseTaskStatusPollRequestTaskStatusResponseVideoEnhanceTaskCreateRequest)
ApiEndpointdownload_url_to_image_tensordownload_url_to_video_outputdownscale_image_tensorget_image_dimensionspoll_opsync_opupload_image_to_comfyapiupload_video_to_comfyapivalidate_video_durationportrait_restore_1xportrait_restore_2xgeneral_restore_1xgeneral_restore_2xgeneral_restore_4xultrahd_restore_2xgenerative_1x)zPortrait Restore Model (1x)zPortrait Restore Model (2x)zGeneral Restore Model (1x)zGeneral Restore Model (2x)zGeneral Restore Model (4x)zUltra HD Model (2x)Generative Model (1x)i  i8  i  ip  i  )720p1080p2K/QHD4K/UHD8Ki   i   r   )originalr   r    r!   r"   )r$   r   r    r!   r"   r#   i Hi@B c                   @   s>   e Zd Zedd Zededejdede	de
jf
dd	Zd
S )HitPawGeneralImageEnhancec                 C   s   t jddddt dt jjdddgd	t jd
t jjdg dd	t jj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HitPaw General Image Enhancezapi node/image/HitPawzbUpscale low-resolution images to super-resolution, eliminate artifacts and noise. Maximum output: z megapixels.modelgenerative_portrait
generativeoptionsimageupscale_factor)         auto_downscaleFzEAutomatically downscale input image if output would exceed the limit.)defaulttooltipTwidgetsa  
                (
                  $prices := {
                    "generative_portrait": {"min": 0.02, "max": 0.06},
                    "generative": {"min": 0.05, "max": 0.15}
                  };
                  $price := $lookup($prices, widgets.model);
                  {
                    "type": "range_usd",
                    "min_usd": $price.min,
                    "max_usd": $price.max
                  }
                )
                
depends_onexpr	node_iddisplay_namecategorydescriptioninputsoutputshiddenis_api_nodeprice_badge)r   SchemaMAX_MP_GENERATIVECombor   ImageBooleanOutputHiddenauth_token_comfy_orgapi_key_comfy_org	unique_id
PriceBadgePriceBadgeDepends)cls rO   :/mnt/c/Users/fbmor/ComfyUI/comfy_api_nodes/nodes_hitpaw.pydefine_schemaE   s8   
z'HitPawGeneralImageEnhance.define_schemar&   r+   r,   r0   returnc                    s  t |\}}|}|| | | }|tkry|r[|| }	d}
t}dD ],}||kr(q!|	| | }|tkr8|}
d } nt|t }|dkrM|}
t||  } nq!|d urXt||d}|
}n|| }|| }td| d| d|dd	tdd
t dt| tdddt	t
| d| dt| |d dI d H ddddI d H }|jdkrtd|j d|j |jjd  t| tdddt|jjdtdd  fdddddI d H }tt|jjI d H S ) Nr-   )r/   r.   r-   g       @)total_pixelszOutput size (xz = ,z) pixels) exceeds maximum allowed size of z	 pixels (zRMP). Enable auto_downscale or use a smaller input image or a lower upscale factor.z /proxy/hitpaw/api/photo-enhancerPOSTpathmethod_)
model_nameimg_urlCreating taskTask createdresponse_modeldata
wait_labelfinal_label_on_success   Task creation failed with code :   /proxy/hitpaw/api/task-statusjob_idc                 S      | j jS Nra   statusrT   rO   rO   rP   <lambda>       z3HitPawGeneralImageEnhance.execute.<locals>.<lambda>c                        S rl   rO   ro   request_pricerO   rP   rp                $@i  ra   r`   status_extractorprice_extractorpoll_intervalmax_poll_attempts)r   MAX_PIXELS_GENERATIVEmathsqrtr   
ValueErrorrC   r   r   r	   r   r   codemessagera   consume_coinsr   r   rj   r   r   
NodeOutputr   res_url)rN   r&   r+   r,   r0   heightwidthrequested_scaleoutput_pixelsinput_pixelsscalemax_input_pixels	candidatescale_output_pixelsdownscale_ratiooutput_widthoutput_heightinitial_resfinal_responserO   rs   rP   executes   sx   




z!HitPawGeneralImageEnhance.executeN)__name__
__module____qualname__classmethodrQ   strr   rE   intboolr   r   r   rO   rO   rO   rP   r%   D   s    
-r%   c                   @   s6   e Zd Zedd Zededejdej	fddZ
dS )	HitPawVideoEnhancec                 C   s   g }t D ]}|tv rt}nt}|tj|tjj	d|dg qtj
ddddtjj	d|dtj	dgtj gtjjtjjtjjgd	tjtjdd
gdddd	S )N
resolutionr)   r   zHitPaw Video Enhancezapi node/video/HitPawzvUpscale low-resolution videos to high resolution, eliminate artifacts and noise. Prices shown are per second of video.r&   videoTzmodel.resolutionr3   aY  
                (
                  $m := $lookup(widgets, "model");
                  $res := $lookup(widgets, "model.resolution");
                  $standard_model_prices := {
                    "original": {"min": 0.01, "max": 0.198},
                    "720p": {"min": 0.01, "max": 0.06},
                    "1080p": {"min": 0.015, "max": 0.09},
                    "2k/qhd": {"min": 0.02, "max": 0.117},
                    "4k/uhd": {"min": 0.025, "max": 0.152},
                    "8k": {"min": 0.033, "max": 0.198}
                  };
                  $ultra_hd_model_prices := {
                    "original": {"min": 0.015, "max": 0.264},
                    "720p": {"min": 0.015, "max": 0.092},
                    "1080p": {"min": 0.02, "max": 0.12},
                    "2k/qhd": {"min": 0.026, "max": 0.156},
                    "4k/uhd": {"min": 0.034, "max": 0.203},
                    "8k": {"min": 0.044, "max": 0.264}
                  };
                  $generative_model_prices := {
                    "original": {"min": 0.015, "max": 0.338},
                    "720p": {"min": 0.008, "max": 0.090},
                    "1080p": {"min": 0.05, "max": 0.15},
                    "2k/qhd": {"min": 0.038, "max": 0.225},
                    "4k/uhd": {"min": 0.056, "max": 0.338}
                  };
                  $prices := $contains($m, "ultra hd") ? $ultra_hd_model_prices :
                             $contains($m, "generative") ? $generative_model_prices :
                             $standard_model_prices;
                  $price := $lookup($prices, $res);
                  {
                    "type": "range_usd",
                    "min_usd": $price.min,
                    "max_usd": $price.max,
                    "format": {"approximate": true, "suffix": "/second"}
                  }
                )
                r5   r8   )VIDEO_MODELS_MODELS_MAPLIMITED_RESOLUTION_MODELSRESOLUTIONS_LIMITEDRESOLUTIONS_FULLappendr   DynamicComboOptionrD   r   rB   VideorG   rH   rI   rJ   rK   rL   rM   )rN   model_optionsr[   resolutionsrO   rO   rP   rQ      s>   
z HitPawVideoEnhance.define_schemar&   r   rR   c                    s  t |ddd |d }| \}}|dkr|}|}nY||kr@t| }||k r;td| d| d| d	| d| d
|}|}n4t||}	t| }||	k r[td| d| d|	 d||krj|}t|||  }n
|}t|||  }t| tdddt	t
t| |I d H ||g||gt|d  ddddI d H }
|
jjd  |
jdkrtd|
j d|
j t| tdddt|
jjdtdd  fddddd I d H }tt|jjI d H S )!Ng      ?i  )min_durationmax_durationr   r$   zSelected resolution z (rT   z#) is smaller than the input video (z3). Please select a higher resolution or 'original'.z8p) is smaller than the input video's shorter dimension (z4p). Please select a higher resolution or 'original'.z /proxy/hitpaw/api/video-enhancerrV   rW   r&   )	video_urlr   original_resolutionr[   r]   r^   r_   rg   rd   re   rf   rh   ri   c                 S   rk   rl   rm   ro   rO   rO   rP   rp   D  rq   z,HitPawVideoEnhance.execute.<locals>.<lambda>c                    rr   rl   rO   ro   rs   rO   rP   rp   E  ru   rv   i@  rw   )r   get_dimensionsRESOLUTION_SQUARE_MAPr   minRESOLUTION_TARGET_MAPr   r   r   r	   r   r   r   ra   r   r   r   r   r
   rj   r   r   r   r   r   )rN   r&   r   r   	src_width
src_heightr   r   target_sizemin_dimensionr   r   rO   rs   rP   r   	  sv   






zHitPawVideoEnhance.executeN)r   r   r   r   rQ   r   r   r   r   r   r   rO   rO   rO   rP   r      s    
Mr   c                   @   s(   e Zd Zedeeej  fddZdS )HitPawExtensionrR   c                    s
   t tgS rl   )r%   r   )selfrO   rO   rP   get_node_listM  s   zHitPawExtension.get_node_listN)	r   r   r   r   listtyper   	ComfyNoder   rO   rO   rO   rP   r   L  s    r   rR   c                      s   t  S rl   )r   rO   rO   rO   rP   comfy_entrypointU  s   r   )'r}   typing_extensionsr   comfy_api.latestr   r   r   comfy_api_nodes.apis.hitpawr   r   r   r	   r
   r   r   comfy_api_nodes.utilr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r|   rC   r   r%   r   r   r   rO   rO   rO   rP   <module>   sF    $0	
	v 	