a
    Ad                     @   s\   d dl Z d dlZd dlmZmZ G dd dZG dd dZdddd	ZG d
d dZ	dS )    N)errorssharedc                   @   s   e Zd Zdd ZdS )PostprocessedImagec                 C   s   || _ i | _d S N)imageinfo)selfr    r	   F/var/www/html/stable-diffusion-webui/modules/scripts_postprocessing.py__init__   s    zPostprocessedImage.__init__N)__name__
__module____qualname__r   r	   r	   r	   r
   r      s   r   c                   @   sF   e Zd ZdZdZdZdZdZdZdZ	dd Z
edddZdd	 ZdS )
ScriptPostprocessingNi  c                 C   s   dS )a  
        This function should create gradio UI elements. See https://gradio.app/docs/#components
        The return value should be a dictionary that maps parameter names to components used in processing.
        Values of those components will be passed to process() function.
        Nr	   r   r	   r	   r
   ui   s    zScriptPostprocessing.uippc                 K   s   dS )z
        This function is called to postprocess the image.
        args contains a dictionary with all values returned by components from ui()
        Nr	   )r   r   argsr	   r	   r
   process%   s    zScriptPostprocessing.processc                 C   s   d S r   r	   r   r	   r	   r
   image_changed-   s    z"ScriptPostprocessing.image_changed)r   r   r   filenamecontrols	args_fromargs_toordernamegroupr   r   r   r   r	   r	   r	   r
   r      s   	r   )defaultc             
   O   sV   z| |i |}|W S  t yP } z$t|d| d|  W Y d }~n
d }~0 0 |S )Nzcalling /)	Exceptionr   display)funcr   funcnamer   r   kwargsreser	   r	   r
   	wrap_call3   s    .r'   c                   @   sR   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZedddZ	dd Z
dd ZdS )ScriptPostprocessingRunnerc                 C   s   d | _ d| _d S )NF)scripts
ui_createdr   r	   r	   r
   r   >   s    z#ScriptPostprocessingRunner.__init__c                 C   s<   g | _ |D ],}| }|j|_|jdkr*q
| j | q
d S )NzSimple Upscale)r)   script_classpathr   r   append)r   scripts_datascript_datascriptr	   r	   r
   initialize_scriptsB   s    
z-ScriptPostprocessingRunner.initialize_scriptsc                 C   sf   t ||_t ||_t|j|jd|_|j D ]}tj	
|j|_q0|t|j 7 }t ||_d S )Nr   )lenr   r   r'   r   r   r   valuesosr,   basenamecustom_script_sourcelist)r   r0   inputscontrolr	   r	   r
   create_script_uiN   s    

z+ScriptPostprocessingRunner.create_script_uic                    sd   j d u r dd l}|j j tjjfdd  fddtj D tj fdddS )	Nr   c                    s,   t  D ]\}}|| kr|  S qtjS r   )	enumerater2   r)   )r   iZpossible_match)scripts_orderr   r	   r
   script_scorea   s    
zKScriptPostprocessingRunner.scripts_in_preferred_order.<locals>.script_scorec                    s*   i | ]"\}}|j  |j |j|j |fqS r	   )r   r   ).0original_indexr0   )r>   r	   r
   
<dictcomp>h       zIScriptPostprocessingRunner.scripts_in_preferred_order.<locals>.<dictcomp>c                    s
    | j  S r   )r   )x)script_scoresr	   r
   <lambda>j   rB   zGScriptPostprocessingRunner.scripts_in_preferred_order.<locals>.<lambda>)key)	r)   modules.scriptsr1   postprocessing_scripts_datar   optspostprocessing_operation_orderr;   sorted)r   modulesr	   )r>   rD   r=   r   r
   scripts_in_preferred_orderZ   s    
z5ScriptPostprocessingRunner.scripts_in_preferred_orderc              	   C   sV   g }|   D ]>}t }| || W d    n1 s:0    Y  ||_qd| _|S )NT)rM   grRowr:   r   r*   )r   r8   r0   r   r	   r	   r
   setup_uil   s    
*z#ScriptPostprocessingRunner.setup_uir   c           	      C   sh   |   D ]Z}|jtj_||j|j }i }t|j	 |D ]\\}}}|||< q:|j
|fi | qd S r   )rM   r   r   statejobr   r   zipr   itemsr   )	r   r   r   r0   script_argsprocess_argsr   
_componentvaluer	   r	   r
   runx   s    

zScriptPostprocessingRunner.runc                 C   s   | j s:tjdd |   W d    n1 s00    Y  |  }d gtdd |D  }|D ]D}||jd }|d ur^t|j	D ]\}}||d ||j
| < qq^|S )NF)analytics_enabledc                 S   s   g | ]
}|j qS r	   )r   )r?   rC   r	   r	   r
   
<listcomp>   rB   zBScriptPostprocessingRunner.create_args_for_run.<locals>.<listcomp>)r*   rN   BlocksrP   rM   maxgetr   r;   r   r   )r   Zscripts_argsr)   r   r0   Zscript_args_dictr<   r   r	   r	   r
   create_args_for_run   s    &z.ScriptPostprocessingRunner.create_args_for_runc                 C   s   |   D ]}|  qd S r   )rM   r   )r   r0   r	   r	   r
   r      s    z(ScriptPostprocessingRunner.image_changedN)r   r   r   r   r1   r:   rM   rP   r   rY   r_   r   r	   r	   r	   r
   r(   =   s   r(   )
r4   gradiorN   rL   r   r   r   r   r'   r(   r	   r	   r	   r
   <module>   s   &
