a
    Ad                     @   sj   d dl Z d dlZd dlmZmZmZ d dlmZ d dl	m
Z
 G dd dejZejejejddd	ZdS )
    N)scriptsuierrors)cmd_opts)
ToolButtonc                   @   s<   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dS )
ScriptSeedseedFc                 C   s   d | _ d | _d | _d S N)r   
reuse_seedreuse_subseedself r   G/var/www/html/stable-diffusion-webui/modules/processing_scripts/seed.py__init__   s    zScriptSeed.__init__c                 C   s   dS )NSeedr   r   r   r   r   title   s    zScriptSeed.titlec                 C   s   t jS r	   )r   AlwaysVisible)r   
is_img2imgr   r   r   show   s    zScriptSeed.showc           	         s  t jdd tjr6t jddddd_nt jddddd	d
_tt	j
ddd}tt	jddd t jdddd}W d    n1 s0    Y  t jddd}t jddl t jdddd	dtt	j
dd}tt	jddt jddd	ddd d!}W d    n1 sP0    Y  t jd"dH t jd	d#d$d%d	d&d'}t jd	d#d$d(d	d)d'}W d    n1 s0    Y  W d    n1 s0    Y  |jd d*d d+ dg g d, |jd d*d d+ dg g d, |jd-d. d|g|gd/ jdf|d0d. fdf|d1f|d2f|d3fg_j fd4d.d5j d jfd6d.d5j d j||||fS )7NZseed_row)elem_idr    r   d   )labelvaluer   	min_widthr   )r   r   r   r   	precisionrandom_seedzRandom seed)r   r   r
   z
Reuse seedExtraZsubseed_showF)r   r   r   seed_extras)visibler   Zsubseed_rowVariation seedsubseed)r   r   r   r   random_subseedr   zVariation strengthg           g{Gz?subseed_strength)r   r   minimummaximumstepr   Zseed_resize_from_rowi      zResize seed from widthseed_resize_from_w)r'   r(   r)   r   r   r   zResize seed from heightseed_resize_from_hzfunction(){setRandomSeed('z')}fn_jsshow_progressinputsoutputsc                 S   s   t j| dS )N)r!   )grupdatexr   r   r   <lambda>3       zScriptSeed.ui.<locals>.<lambda>)r0   r1   r2   c                 S   s   d| v pd| v S )Nr"   Seed resize from-1r   )dr   r   r   r7   7   r8   zVariation seed strengthr9   zSeed resize from-2c                    s   t j | jdS )NF)connect_reuse_seedr   	componentr5   )r
   r   r   r   r7   >   r8   generation_info_c                    s   t  | jdS )NT)r;   r<   r5   )r   r#   r   r   r7   ?   r8   )r3   Rowr   r   use_textbox_seedTextboxr   Numberr   r   random_symbolreuse_symbolCheckboxGroupSliderclickchangeinfotext_fieldson_after_componenttabname)	r   r   r   seed_checkboxr    r$   r&   r+   r,   r   )r
   r   r   r#   r   r      s:    4<\""
	  zScriptSeed.uic                 C   sB   ||_ |r|dkr||_||_|r>|dkr>|dkr>||_||_d S )Nr   )r   r#   r&   r+   r,   )r   pr   rL   r#   r&   r+   r,   r   r   r   setupC   s    zScriptSeed.setupN)
__name__
__module____qualname__sectioncreate_groupr   r   r   r   rN   r   r   r   r   r   
   s   *r   )r   r
   generation_infoc                    s2   t d fdd}|j|dd|| g| | gd dS )a   Connects a 'reuse (sub)seed' button's click event so that it copies last used
        (sub)seed value from generation info the to the seed field. If copying subseed and subseed strength
        was 0, i.e. no variation seed was used, it copies the normal seed value instead.)gen_info_stringc                    s   d}zt | }||dd8 } rl|dddkrl|ddg}|d|  kr\t|k rdn n|nd }n6|ddg}|d|  krt|k rn n|nd }W n* t jjy   | rtd|   Y n0 |t	 gS )Nr   index_of_first_imager   r&   all_subseeds	all_seedsz$Error parsing JSON generation info: )
jsonloadsgetlendecoderJSONDecodeErrorr   reportr3   r4   )rU   indexresZgen_inforW   rX   
is_subseedr   r   	copy_seedU   s    
*,z%connect_reuse_seed.<locals>.copy_seedz'(x, y) => [x, selected_gallery_index()]Fr-   N)strrG   )r   r
   rT   rc   rd   r   rb   r   r;   P   s    r;   )rY   gradior3   modulesr   r   r   Zmodules.sharedr   Zmodules.ui_componentsr   ScriptBuiltinUIr   rA   Buttonr@   r;   r   r   r   r   <module>   s   F