a
    Ad.                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
mZ d dlmZ d dlZ	d dlmZ d dlmZ dZdZdd Zdd	d
Zdd Zdd Zdd ZddddZdS )    N)
call_queueshared)image_from_url_text)
ToolButtonu   📂u   🔄c                 C   sj   zJt | } |dk s$|t| d kr2|t fW S t| d | t fW S  ty\   Y n0 |t fS )Nr   	infotexts)jsonloadslengrupdateplaintext_to_html	Exception)generation_info	html_infoZ	img_index r   9/var/www/html/stable-diffusion-webui/modules/ui_common.pyupdate_generation_info   s    
r   c                 C   s<   d dd | dD }|r0d| d| dS d| dS )	Nz<br>
c                 s   s   | ]}t |V  qd S N)htmlescape).0xr   r   r   	<genexpr>!       z$plaintext_to_html.<locals>.<genexpr>
z
<p class='z'>z</p>z<p>)joinsplit)text	classnamecontentr   r   r   r       s    r   c           !      C   s  dd l }g }g }G dd d}t| }||}	tjj}
tjj}tjj}d}d}|dkr~tjjr~||d kr~d}|| g}|}t	j
tjjdd tt	jtjjd	d
ddd4}| dk}||}|r|g d t||D ]\}}t|}||	jk }|rdn||	j }|d |	_tjj||
d|	j| |	j| ||	j| ||	|d
\}}t	j||
}|| || |r|t	j| || q||d |d |d |d |d |d |d |d |d g	 W d    n1 s0    Y  |r|r|	j|d  n|	jd }tj|	||	jd |d}|tjj pDd}t	j|
| d}ddl!m"} ||dj}t#t$|D ]L}t|| dd$} |%|| | &  W d    n1 s0    Y  qW d    n1 s0    Y  |'d| t(j)j*|ddt+d |d  fS )!Nr   c                   @   s   e Zd ZdddZdS )zsave_files.<locals>.MyObjectNc                 S   s*   |d ur&|  D ]\}}t| || qd S r   )itemssetattr)selfdkeyvaluer   r   r   __init__-   s    z%save_files.<locals>.MyObject.__init__)N)__name__
__module____qualname__r&   r   r   r   r   MyObject,   s   r*   Findex_of_first_imageT)exist_okzlog.csvautf8 )encodingnewline)	promptseedwidthheightsamplercfgsstepsfilenamenegative_prompt   )r4   r3   	extensioninfogridpsave_to_dirsr3   r4   r5   r6   sampler_name	cfg_scaler9   r;   z,[datetime]_[[model_name]]_[seed]-[seed_last]z.zip)ZipFilewrb)mode)r%   visiblezSaved: ),csvr   r   r   optsoutdir_saveuse_save_to_dirs_for_uisamples_formatsave_selected_onlyosmakedirsopenpathr   tellwriterwriterow	enumerater   r,   batch_indexmodulesimages
save_image	all_seedsall_promptsr   relpathappendbasenameFilenameGeneratorapplygrid_zip_filename_patternzipfilerD   ranger	   writestrreadinsertr
   Filer   r   )!Zjs_datarY   Zdo_make_zipindexrI   	filenamesZfullfnsr*   datar@   rR   rA   r=   start_indexZonly_onefileat_startrT   Zimage_indexfiledataimageZis_gridifullfn
txt_fullfnr:   Zzip_fileseednamegenzip_filenameZzip_filepathrD   zip_filefr   r   r   
save_files&   s\    

"


6

`Xrx   c                    s  dd  t jd|  ddp t j|  dd4 t jdd	|  d
ddtjjpRd d}W d    n1 sl0    Y  d }t   t jd|  dd tt	|  dtj
j dd}| dkrtdd|  dtjj dd}tdd|  dtjj dd}td|  dd dtd!|  d"d#dtd$|  d%d&dd'}W d    n1 sT0    Y  |j fd(d)g g d* | dkrt jd d+d	d	d	d,|  d-}t   t jd.|  d/d}	t jd0|  d1d}
t jd	d2|  d3}| d4ks| d5kr*t jd	|  d6d3}|jtd7||	|	g|	|	gd	d8 |jttd9|||	|	g||
gd	d8 |jttd:|||	|	g||
gd; W d    n1 s0    Y  n8t jd<|  d}t jd.|  d/d}	t jd0|  d}
g }| d4krtjjj}n| d5krtjjj}| D ]2\}}ttj||| d4kr&d4nd ||d= q|| dkrH|n||	|
fW  d    W  d    S 1 sv0    Y  W d    n1 s0    Y  d S )>Nc                 S   s   t j| s td|  d d S t j| sFtd|  dtjd d S tjj	st j
| }t dkrrt | nHt dkrtd|g n,d	t jv rtd
|g ntd|g d S )NzFolder "zH" does not exist. After you create an image, the folder will be created.z
WARNING
An open_folder request was made with an argument that is not a folder.
This could be an error or a malicious attempt to run code on your computer.
Requested path was: r   )rm   WindowsDarwinrQ   zmicrosoft-standard-WSL2zwsl-openzxdg-open)rO   rR   existsprintisdirsysstderrr   cmd_optshide_ui_dir_confignormpathplatformsystem	startfilespPopenunamerelease)rw   rR   r   r   r   open_folderm   s&    z(create_output_panel.<locals>.open_folderpanel_results)variantelem_idZ_gallery_container)r   OutputFZ_gallery   T)label
show_labelr   columnspreviewr6   Zimage_buttons_zimage-buttons)r   elem_classesZ_open_folderzOpen images output directory.)r   rH   tooltipextrasu   💾Zsave_z)Save the image to a dedicated directory (z).)r   r   u   🗃️Z	save_zip_z7Save zip archive with images to a dedicated directory ()u   🖼️Z_send_to_img2imgz4Send image and generation parameters to img2img tab.u   🎨️Z_send_to_inpaintz<Send image and generation parameters to img2img inpaint tab.u   📐Z_send_to_extrasz3Send image and generation parameters to extras tab.)img2imginpaintr   c                      s    t jjpS r   )r   rJ   outdir_samplesr   r   outdirr   r   <lambda>   r   z%create_output_panel.<locals>.<lambda>fninputsoutputsmultipleZdownload_files_)
file_countinteractiver   rH   r   Z
html_info_infotextZ	html_log_zhtml-logZgeneration_info_)rH   r   txt2imgr   Z_generation_info_buttonz<function(x, y, z){ return [x, y, selected_gallery_index()] })r   _jsr   r   show_progressz7(x, y, z, w) => [x, y, false, selected_gallery_index()]z6(x, y, z, w) => [x, y, true, selected_gallery_index()])r   r   r   r   Zhtml_info_x_)paste_buttontabnamesource_tabnamesource_image_componentpaste_field_names)r
   ColumnGroupGalleryr   rJ   gallery_heightRowr   folder_symbolr   r   rK   clickrh   HTMLTextboxButtonr   r   wrap_gradio_callrx   rX   scriptsscripts_txt2imgr   scripts_img2imgr    parameters_copypasteregister_paste_params_buttonParamBinding)r   r   Zresult_galleryr   Zopen_folder_buttonsaveZsave_zipbuttonsZdownload_filesr   html_logZgeneration_info_buttonZhtml_info_xr   Zpaste_tabnamer   r   r   r   create_output_panelk   s    B
  &

(


r   c                    sz   t | tr| n| g d } D ]}t|dd }|d ur q:q fdd}tt||r^| dndd}|j|g  d |S )Nr   c                     sr     t r n   D ]\} }D ]}t|| | q,q tdkr^ fddD S tjf i  pli S )Nr<   c                    s    g | ]}t jf i  pi qS r   r
   r   )r   _argsr   r   
<listcomp>   r   z:create_refresh_button.<locals>.refresh.<locals>.<listcomp>)callabler    r!   r	   r
   r   )kvcompZrefresh_componentsrefresh_methodrefreshed_argsr   r   refresh   s    z&create_refresh_button.<locals>.refreshz	: refreshZRefresh)r%   r   r   r   )
isinstancelistgetattrr   refresh_symbolr   )Zrefresh_componentr   r   r   r   r   r   Zrefresh_buttonr   r   r   create_refresh_button   s    
r   )button_closec                C   sF   d|_ | jdd g |gdjdd|j d d |rB|jdd	d dS )
zSets up the UI so that the dialog (gr.Box) is invisible, and is only shown when buttons_show is clicked, in a fullscreen modal window.Fc                   S   s   t jddS )NT)rH   r   r   r   r   r   r     r   zsetup_dialog.<locals>.<lambda>r   Nzfunction(){ popupId('z'); })r   r   Z
closePopup)rH   r   thenr   )Zbutton_showZdialogr   r   r   r   setup_dialog   s    r   )N)r   r   rO   r   r~   gradior
   
subprocessr   rX   r   r   'modules.generation_parameters_copypaster   modules.imagesmodules.ui_componentsr   generation_parameters_copypaster   r   r   r   r   rx   r   r   r   r   r   r   r   <module>   s&   
Ex