speed up images history perfect

This commit is contained in:
yfszzx 2022-10-17 15:20:16 +08:00
commit 5b1394bead
6 changed files with 113 additions and 94 deletions

View File

@ -43,7 +43,6 @@ function images_history_get_parent_by_tagname(item, tagname){
var parent = item.parentElement; var parent = item.parentElement;
tagname = tagname.toUpperCase() tagname = tagname.toUpperCase()
while(parent.tagName != tagname){ while(parent.tagName != tagname){
console.log(parent.tagName, tagname)
parent = parent.parentElement; parent = parent.parentElement;
} }
return parent; return parent;
@ -108,7 +107,6 @@ function images_history_delete(del_num, tabname, image_index){
}); });
var img_num = buttons.length / 2; var img_num = buttons.length / 2;
del_num = Math.min(img_num - image_index, del_num) del_num = Math.min(img_num - image_index, del_num)
console.log(del_num, img_num)
if (img_num <= del_num){ if (img_num <= del_num){
setTimeout(function(tabname){ setTimeout(function(tabname){
gradioApp().getElementById(tabname + '_images_history_renew_page').click(); gradioApp().getElementById(tabname + '_images_history_renew_page').click();
@ -133,7 +131,6 @@ function images_history_delete(del_num, tabname, image_index){
} }
function images_history_turnpage(tabname){ function images_history_turnpage(tabname){
console.log("del_button")
gradioApp().getElementById(tabname + '_images_history_del_button').setAttribute('disabled','disabled'); gradioApp().getElementById(tabname + '_images_history_del_button').setAttribute('disabled','disabled');
var buttons = gradioApp().getElementById(tabname + '_images_history').querySelectorAll(".gallery-item"); var buttons = gradioApp().getElementById(tabname + '_images_history').querySelectorAll(".gallery-item");
buttons.forEach(function(elem) { buttons.forEach(function(elem) {
@ -148,8 +145,9 @@ function images_history_enable_del_buttons(){
} }
function images_history_init(){ function images_history_init(){
var load_txt2img_button = gradioApp().getElementById('saved_images_history_start') var loaded = gradioApp().getElementById("images_history_reconstruct_directory")
if (load_txt2img_button){ if (loaded){
var init_status = loaded.querySelector("input").checked
for (var i in images_history_tab_list ){ for (var i in images_history_tab_list ){
tab = images_history_tab_list[i]; tab = images_history_tab_list[i];
gradioApp().getElementById(tab + '_images_history').classList.add("images_history_cantainor"); gradioApp().getElementById(tab + '_images_history').classList.add("images_history_cantainor");
@ -161,25 +159,23 @@ function images_history_init(){
var tabs_box = gradioApp().getElementById("tab_images_history").querySelector("div").querySelector("div").querySelector("div"); var tabs_box = gradioApp().getElementById("tab_images_history").querySelector("div").querySelector("div").querySelector("div");
tabs_box.setAttribute("id", "images_history_tab"); tabs_box.setAttribute("id", "images_history_tab");
var tab_btns = tabs_box.querySelectorAll("button"); var tab_btns = tabs_box.querySelectorAll("button");
for (var i in images_history_tab_list){ for (var i in images_history_tab_list){
var tabname = images_history_tab_list[i] var tabname = images_history_tab_list[i]
tab_btns[i].setAttribute("tabname", tabname); tab_btns[i].setAttribute("tabname", tabname);
if (init_status){
// this refreshes history upon tab switch
// until the history is known to work well, which is not the case now, we do not do this at startup
// -- load page very fast now, so better user experience by automatically activating pages
tab_btns[i].addEventListener('click', images_history_click_tab); tab_btns[i].addEventListener('click', images_history_click_tab);
} }
tabs_box.classList.add(images_history_tab_list[0]); }
if (init_status){
// same as above, at page load-- load very fast now tab_btns[0].click();
load_txt2img_button.click(); }
} else { } else {
setTimeout(images_history_init, 500); setTimeout(images_history_init, 500);
} }
} }
var images_history_tab_list = ["saved", "txt2img", "img2img", "extras"]; var images_history_tab_list = ["txt2img", "img2img", "extras", "saved"];
setTimeout(images_history_init, 500); setTimeout(images_history_init, 500);
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
var mutationObserver = new MutationObserver(function(m){ var mutationObserver = new MutationObserver(function(m){

View File

@ -73,8 +73,10 @@ function check_gallery(id_gallery){
let galleryBtnSelected = gradioApp().querySelector('#'+id_gallery+' .gallery-item.\\!ring-2') let galleryBtnSelected = gradioApp().querySelector('#'+id_gallery+' .gallery-item.\\!ring-2')
if (prevSelectedIndex !== -1 && galleryButtons.length>prevSelectedIndex && !galleryBtnSelected) { if (prevSelectedIndex !== -1 && galleryButtons.length>prevSelectedIndex && !galleryBtnSelected) {
//automatically re-open previously selected index (if exists) //automatically re-open previously selected index (if exists)
activeElement = document.activeElement;
galleryButtons[prevSelectedIndex].click(); galleryButtons[prevSelectedIndex].click();
showGalleryImage(); showGalleryImage();
if(activeElement) activeElement.focus()
} }
}) })
galleryObservers[id_gallery].observe( gallery, { childList:true, subtree:false }) galleryObservers[id_gallery].observe( gallery, { childList:true, subtree:false })

View File

@ -3,10 +3,7 @@ import shutil
import time import time
import hashlib import hashlib
import gradio import gradio
system_bak_path = "webui_log_and_bak" system_bak_path = "webui_log_and_bak"
loads_files_num = 216
num_of_imgs_per_page = 36
def is_valid_date(date): def is_valid_date(date):
try: try:
time.strptime(date, "%Y%m%d") time.strptime(date, "%Y%m%d")
@ -42,7 +39,7 @@ def traverse_all_files(curr_path, image_list, all_type=False):
try: try:
f_list = os.listdir(curr_path) f_list = os.listdir(curr_path)
except: except:
if all_type or curr_path[-10:].rfind(".") > 0 and curr_path[-4:] != ".txt": if all_type or (curr_path[-10:].rfind(".") > 0 and curr_path[-4:] != ".txt" and curr_path[-4:] != ".csv"):
image_list.append(curr_path) image_list.append(curr_path)
return image_list return image_list
for file in f_list: for file in f_list:
@ -73,7 +70,7 @@ def auto_sorting(dir_name):
files_list = traverse_all_files(file_path, files_list, all_type=True) files_list = traverse_all_files(file_path, files_list, all_type=True)
for file in files_list: for file in files_list:
date_str = time.strftime("%Y%m%d",time.localtime(os.path.getctime(file))) date_str = time.strftime("%Y%m%d",time.localtime(os.path.getmtime(file)))
file_path = os.path.dirname(file) file_path = os.path.dirname(file)
hash_path = hashlib.md5(file_path.encode()).hexdigest() hash_path = hashlib.md5(file_path.encode()).hexdigest()
path = os.path.join(dir_name, date_str, hash_path) path = os.path.join(dir_name, date_str, hash_path)
@ -92,33 +89,55 @@ def auto_sorting(dir_name):
elif f == system_bak_path: elif f == system_bak_path:
continue continue
else: else:
try:
reduplicative_file_move(os.path.join(dir_name, f), bak_path) reduplicative_file_move(os.path.join(dir_name, f), bak_path)
except:
pass
today = time.strftime("%Y%m%d",time.localtime(time.time())) today = time.strftime("%Y%m%d",time.localtime(time.time()))
if today not in date_list: if today not in date_list:
date_list.append(today) date_list.append(today)
return sorted(date_list, reverse=True) return sorted(date_list, reverse=True)
def archive_images(dir_name, date_to): def archive_images(dir_name, date_to):
filenames = []
loads_num =int(opts.images_history_num_per_page * opts.images_history_pages_num)
if opts.images_history_reconstruct_directory:
date_list = auto_sorting(dir_name) date_list = auto_sorting(dir_name)
today = time.strftime("%Y%m%d",time.localtime(time.time())) today = time.strftime("%Y%m%d",time.localtime(time.time()))
date_to = today if date_to is None or date_to == "" else date_to date_to = today if date_to is None or date_to == "" else date_to
filenames = []
for date in date_list: for date in date_list:
if date <= date_to: if date <= date_to:
path = os.path.join(dir_name, date) path = os.path.join(dir_name, date)
if date == today and not os.path.exists(path): if date == today and not os.path.exists(path):
continue continue
filenames = traverse_all_files(path, filenames) filenames = traverse_all_files(path, filenames)
if len(filenames) > loads_files_num: if len(filenames) > loads_num:
break break
filenames = sorted(filenames, key=lambda file: -os.path.getctime(file)) filenames = sorted(filenames, key=lambda file: -os.path.getmtime(file))
else:
filenames = traverse_all_files(dir_name, filenames)
tmparray = [(os.path.getmtime(file), file) for file in filenames ]
date_stamp = time.mktime(time.strptime(date_to, "%Y%m%d")) + 86400
filenames = []
date_list = {}
date = time.strftime("%Y%m%d",time.localtime(time.time()))
for t, f in tmparray:
date = time.strftime("%Y%m%d",time.localtime(t))
date_list[date] = None
if t <= date_stamp:
filenames.append((t, f ,date))
date_list = sorted(list(date_list.keys()), reverse=True)
sort_array = sorted(filenames, key=lambda x:-x[0])
if len(sort_array) > loads_num:
date = sort_array[loads_num][2]
filenames = [x[1] for x in sort_array]
else:
date = sort_array[loads_num][2]
filenames = [x[1] for x in sort_array]
filenames = [x[1] for x in sort_array if x[2]>= date]
_, image_list, _, visible_num = get_recent_images(1, 0, filenames) _, image_list, _, visible_num = get_recent_images(1, 0, filenames)
return ( return (
gradio.update(visible=False),
gradio.update(visible=True),
gradio.Dropdown.update(choices=date_list, value=date_to), gradio.Dropdown.update(choices=date_list, value=date_to),
date, date,
filenames, filenames,
@ -127,15 +146,8 @@ def archive_images(dir_name, date_to):
"", "",
visible_num visible_num
) )
def system_init(dir_name):
ret = [x for x in archive_images(dir_name, None)]
ret += [gradio.update(visible=False)]
return ret
def newest_click(dir_name, date_to): def newest_click(dir_name, date_to):
if date_to == "start":
return True, False, "start", None, None, 1, None, ""
else:
return archive_images(dir_name, time.strftime("%Y%m%d",time.localtime(time.time()))) return archive_images(dir_name, time.strftime("%Y%m%d",time.localtime(time.time())))
def delete_image(delete_num, name, filenames, image_index, visible_num): def delete_image(delete_num, name, filenames, image_index, visible_num):
@ -168,8 +180,12 @@ def delete_image(delete_num, name, filenames, image_index, visible_num):
i += 1 i += 1
return new_file_list, 1, visible_num return new_file_list, 1, visible_num
def save_image(file_name):
shutil.copy2(file_name, opts.outdir_save)
def get_recent_images(page_index, step, filenames): def get_recent_images(page_index, step, filenames):
page_index = int(page_index) page_index = int(page_index)
num_of_imgs_per_page = int(opts.images_history_num_per_page)
max_page_index = len(filenames) // num_of_imgs_per_page + 1 max_page_index = len(filenames) // num_of_imgs_per_page + 1
page_index = max_page_index if page_index == -1 else page_index + step page_index = max_page_index if page_index == -1 else page_index + step
page_index = 1 if page_index < 1 else page_index page_index = 1 if page_index < 1 else page_index
@ -197,9 +213,12 @@ def page_index_change(page_index, filenames):
return get_recent_images(page_index, 0, filenames) return get_recent_images(page_index, 0, filenames)
def show_image_info(tabname_box, num, page_index, filenames): def show_image_info(tabname_box, num, page_index, filenames):
file = filenames[int(num) + int((page_index - 1) * num_of_imgs_per_page)] file = filenames[int(num) + int((page_index - 1) * int(opts.images_history_num_per_page))]
return file, num, file return file, num, file
def enable_page_buttons():
return gradio.update(visible=True)
def show_images_history(gr, opts, tabname, run_pnginfo, switch_dict): def show_images_history(gr, opts, tabname, run_pnginfo, switch_dict):
if tabname == "txt2img": if tabname == "txt2img":
dir_name = opts.outdir_txt2img_samples dir_name = opts.outdir_txt2img_samples
@ -209,19 +228,16 @@ def show_images_history(gr, opts, tabname, run_pnginfo, switch_dict):
dir_name = opts.outdir_extras_samples dir_name = opts.outdir_extras_samples
elif tabname == "saved": elif tabname == "saved":
dir_name = opts.outdir_save dir_name = opts.outdir_save
if not os.path.exists(dir_name):
os.makedirs(dir_name)
d = dir_name.split("/") d = dir_name.split("/")
dir_name = d[0] dir_name = d[0]
for p in d[1:]: for p in d[1:]:
dir_name = os.path.join(dir_name, p) dir_name = os.path.join(dir_name, p)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
f_list = os.listdir(dir_name) with gr.Column() as page_panel:
sorted_flag = os.path.exists(os.path.join(dir_name, system_bak_path)) or len(f_list) == 0 with gr.Row(visible=False) as turn_page_buttons:
date_list, date_from, date_to = None, None, None
with gr.Column(visible=sorted_flag) as page_panel:
with gr.Row():
renew_page = gr.Button('Refresh page', elem_id=tabname + "_images_history_renew_page") renew_page = gr.Button('Refresh page', elem_id=tabname + "_images_history_renew_page")
first_page = gr.Button('First Page') first_page = gr.Button('First Page')
prev_page = gr.Button('Prev Page') prev_page = gr.Button('Prev Page')
@ -234,14 +250,17 @@ def show_images_history(gr, opts, tabname, run_pnginfo, switch_dict):
with gr.Row(): with gr.Row():
newest = gr.Button('Reload', elem_id=tabname + "_images_history_start") newest = gr.Button('Reload', elem_id=tabname + "_images_history_start")
date_from = gr.Textbox(label="Date from", interactive=False) date_from = gr.Textbox(label="Date from", interactive=False)
date_to = gr.Dropdown(value="start" if not sorted_flag else None, label="Date to") date_to = gr.Dropdown(label="Date to")
history_gallery = gr.Gallery(show_label=False, elem_id=tabname + "_images_history_gallery").style(grid=6) history_gallery = gr.Gallery(show_label=False, elem_id=tabname + "_images_history_gallery").style(grid=6)
with gr.Row(): with gr.Row():
delete_num = gr.Number(value=1, interactive=True, label="number of images to delete consecutively next") delete_num = gr.Number(value=1, interactive=True, label="number of images to delete consecutively next")
delete = gr.Button('Delete', elem_id=tabname + "_images_history_del_button") delete = gr.Button('Delete', elem_id=tabname + "_images_history_del_button")
with gr.Column(): with gr.Column():
with gr.Row(): with gr.Row():
if tabname != "saved":
save_btn = gr.Button('Save')
pnginfo_send_to_txt2img = gr.Button('Send to txt2img') pnginfo_send_to_txt2img = gr.Button('Send to txt2img')
pnginfo_send_to_img2img = gr.Button('Send to img2img') pnginfo_send_to_img2img = gr.Button('Send to img2img')
with gr.Row(): with gr.Row():
@ -249,6 +268,7 @@ def show_images_history(gr, opts, tabname, run_pnginfo, switch_dict):
img_file_info = gr.Textbox(label="Generate Info", interactive=False) img_file_info = gr.Textbox(label="Generate Info", interactive=False)
img_file_name = gr.Textbox(value="", label="File Name", interactive=False) img_file_name = gr.Textbox(value="", label="File Name", interactive=False)
# hiden items # hiden items
with gr.Row(visible=False): with gr.Row(visible=False):
visible_img_num = gr.Number() visible_img_num = gr.Number()
@ -262,31 +282,25 @@ def show_images_history(gr, opts, tabname, run_pnginfo, switch_dict):
info1 = gr.Textbox() info1 = gr.Textbox()
info2 = gr.Textbox() info2 = gr.Textbox()
with gr.Column(visible=not sorted_flag) as init_warning:
with gr.Row():
warning = gr.Textbox(
label="Waring",
value=f"The system needs to archive the files according to the date. This requires changing the directory structure of the files.If you have doubts about this operation, you can first back up the files in the '{dir_name}' directory"
)
warning.style(height=100, width=50)
with gr.Row():
sorted_button = gr.Button('Confirme')
change_date_output = [init_warning, page_panel, date_to, date_from, filenames, page_index, history_gallery, img_file_name, visible_img_num] #change date
sorted_button.click(system_init, inputs=[img_path], outputs=change_date_output + [sorted_button]) change_date_output = [date_to, date_from, filenames, page_index, history_gallery, img_file_name, visible_img_num]
newest.click(newest_click, inputs=[img_path, date_to], outputs=change_date_output) newest.click(newest_click, inputs=[img_path, date_to], outputs=change_date_output)
date_to.change(archive_images, inputs=[img_path, date_to], outputs=change_date_output) date_to.change(archive_images, inputs=[img_path, date_to], outputs=change_date_output)
date_to.change(fn=None, inputs=[tabname_box], outputs=None, _js="images_history_turnpage")
newest.click(fn=None, inputs=[tabname_box], outputs=None, _js="images_history_turnpage") newest.click(fn=None, inputs=[tabname_box], outputs=None, _js="images_history_turnpage")
date_to.change(fn=None, inputs=[tabname_box], outputs=None, _js="images_history_turnpage")
date_to.change(enable_page_buttons, inputs=None, outputs=[turn_page_buttons])
newest.click(enable_page_buttons, inputs=None, outputs=[turn_page_buttons])
#delete
delete.click(delete_image, inputs=[delete_num, img_file_name, filenames, image_index, visible_img_num], outputs=[filenames, delete_num, visible_img_num]) delete.click(delete_image, inputs=[delete_num, img_file_name, filenames, image_index, visible_img_num], outputs=[filenames, delete_num, visible_img_num])
delete.click(fn=None, _js="images_history_delete", inputs=[delete_num, tabname_box, image_index], outputs=None) delete.click(fn=None, _js="images_history_delete", inputs=[delete_num, tabname_box, image_index], outputs=None)
if tabname != "saved":
save_btn.click(save_image, inputs=[img_file_name], outputs=None)
#turn page
# turn pages
gallery_inputs = [page_index, filenames] gallery_inputs = [page_index, filenames]
gallery_outputs = [page_index, history_gallery, img_file_name, visible_img_num] gallery_outputs = [page_index, history_gallery, img_file_name, visible_img_num]
first_page.click(first_page_click, inputs=gallery_inputs, outputs=gallery_outputs) first_page.click(first_page_click, inputs=gallery_inputs, outputs=gallery_outputs)
next_page.click(next_page_click, inputs=gallery_inputs, outputs=gallery_outputs) next_page.click(next_page_click, inputs=gallery_inputs, outputs=gallery_outputs)
prev_page.click(prev_page_click, inputs=gallery_inputs, outputs=gallery_outputs) prev_page.click(prev_page_click, inputs=gallery_inputs, outputs=gallery_outputs)
@ -305,17 +319,21 @@ def show_images_history(gr, opts, tabname, run_pnginfo, switch_dict):
set_index.click(show_image_info, _js="images_history_get_current_img", inputs=[tabname_box, image_index, page_index, filenames], outputs=[img_file_name, image_index, hidden]) set_index.click(show_image_info, _js="images_history_get_current_img", inputs=[tabname_box, image_index, page_index, filenames], outputs=[img_file_name, image_index, hidden])
img_file_name.change(fn=None, _js="images_history_enable_del_buttons", inputs=None, outputs=None) img_file_name.change(fn=None, _js="images_history_enable_del_buttons", inputs=None, outputs=None)
hidden.change(fn=run_pnginfo, inputs=[hidden], outputs=[info1, img_file_info, info2]) hidden.change(fn=run_pnginfo, inputs=[hidden], outputs=[info1, img_file_info, info2])
switch_dict["fn"](pnginfo_send_to_txt2img, switch_dict["t2i"], img_file_info, 'switch_to_txt2img') switch_dict["fn"](pnginfo_send_to_txt2img, switch_dict["t2i"], img_file_info, 'switch_to_txt2img')
switch_dict["fn"](pnginfo_send_to_img2img, switch_dict["i2i"], img_file_info, 'switch_to_img2img_img2img') switch_dict["fn"](pnginfo_send_to_img2img, switch_dict["i2i"], img_file_info, 'switch_to_img2img_img2img')
def create_history_tabs(gr, sys_opts, run_pnginfo, switch_dict):
global opts;
def create_history_tabs(gr, opts, run_pnginfo, switch_dict): opts = sys_opts
loads_files_num = int(opts.images_history_num_per_page)
num_of_imgs_per_page = int(opts.images_history_num_per_page * opts.images_history_pages_num)
backup_flag = opts.images_history_backup
with gr.Blocks(analytics_enabled=False) as images_history: with gr.Blocks(analytics_enabled=False) as images_history:
with gr.Tabs() as tabs: with gr.Tabs() as tabs:
for tab in ["saved", "txt2img", "img2img", "extras"]: for tab in ["txt2img", "img2img", "extras", "saved"]:
with gr.Tab(tab): with gr.Tab(tab):
with gr.Blocks(analytics_enabled=False) as images_history_img2img: with gr.Blocks(analytics_enabled=False) as images_history_img2img:
show_images_history(gr, opts, tab, run_pnginfo, switch_dict) show_images_history(gr, opts, tab, run_pnginfo, switch_dict)
gradio.Checkbox(opts.images_history_reconstruct_directory, elem_id="images_history_reconstruct_directory") #, visible=False)
return images_history return images_history

View File

@ -334,12 +334,6 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
seed = get_fixed_seed(p.seed) seed = get_fixed_seed(p.seed)
subseed = get_fixed_seed(p.subseed) subseed = get_fixed_seed(p.subseed)
if p.outpath_samples is not None:
os.makedirs(p.outpath_samples, exist_ok=True)
if p.outpath_grids is not None:
os.makedirs(p.outpath_grids, exist_ok=True)
modules.sd_hijack.model_hijack.apply_circular(p.tiling) modules.sd_hijack.model_hijack.apply_circular(p.tiling)
modules.sd_hijack.model_hijack.clear_comments() modules.sd_hijack.model_hijack.clear_comments()

View File

@ -299,6 +299,12 @@ options_templates.update(options_section(('sampler-params', "Sampler parameters"
'eta_noise_seed_delta': OptionInfo(0, "Eta noise seed delta", gr.Number, {"precision": 0}), 'eta_noise_seed_delta': OptionInfo(0, "Eta noise seed delta", gr.Number, {"precision": 0}),
})) }))
options_templates.update(options_section(('images-history', "Images history"), {
"images_history_reconstruct_directory": OptionInfo(False, "Reconstruct output directory structure.This can greatly improve the speed of loading , but will change the original output directory structure"),
"images_history_num_per_page": OptionInfo(36, "Number of pictures displayed on each page"),
"images_history_pages_num": OptionInfo(6, "Maximum number of pages per load "),
}))
class Options: class Options:
data = None data = None

View File

@ -1394,7 +1394,10 @@ def create_ui(wrap_gradio_gpu_call):
component_dict = {} component_dict = {}
def open_folder(f): def open_folder(f):
if not os.path.isdir(f): if not os.path.exists(f):
print(f'Folder "{f}" does not exist. After you create an image, the folder will be created.')
return
elif not os.path.isdir(f):
print(f""" print(f"""
WARNING WARNING
An open_folder request was made with an argument that is not a folder. An open_folder request was made with an argument that is not a folder.