aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorAUTOMATIC1111 <16777216c@gmail.com>2022-10-25 08:40:12 +0300
committerGitHub <noreply@github.com>2022-10-25 08:40:12 +0300
commit16416e42b5d06c7c580e12dab427e29750146604 (patch)
treeebe736abe4098fe25ba8536533b6a7d583124cef /modules
parent734986dde3231416813f827242c111da212b2ccb (diff)
parent77a320f406a76425176b8ca4c034c362b6734713 (diff)
Merge branch 'master' into on-image-saved-callback
Diffstat (limited to 'modules')
-rw-r--r--modules/api/api.py2
-rw-r--r--modules/api/models.py (renamed from modules/api/processing.py)0
-rw-r--r--modules/images.py196
-rw-r--r--modules/images_history.py424
-rw-r--r--modules/processing.py33
-rw-r--r--modules/script_callbacks.py45
-rw-r--r--modules/sd_samplers.py4
-rw-r--r--modules/shared.py21
-rw-r--r--modules/ui.py24
9 files changed, 207 insertions, 542 deletions
diff --git a/modules/api/api.py b/modules/api/api.py
index 3caa83a4..a860a964 100644
--- a/modules/api/api.py
+++ b/modules/api/api.py
@@ -1,4 +1,4 @@
-from modules.api.processing import StableDiffusionTxt2ImgProcessingAPI, StableDiffusionImg2ImgProcessingAPI
+from modules.api.models import StableDiffusionTxt2ImgProcessingAPI, StableDiffusionImg2ImgProcessingAPI
from modules.processing import StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img, process_images
from modules.sd_samplers import all_samplers
from modules.extras import run_pnginfo
diff --git a/modules/api/processing.py b/modules/api/models.py
index f551fa35..f551fa35 100644
--- a/modules/api/processing.py
+++ b/modules/api/models.py
diff --git a/modules/images.py b/modules/images.py
index 01c60f89..0e044af2 100644
--- a/modules/images.py
+++ b/modules/images.py
@@ -1,4 +1,8 @@
import datetime
+import sys
+import traceback
+
+import pytz
import io
import math
import os
@@ -273,10 +277,15 @@ invalid_filename_chars = '<>:"/\\|?*\n'
invalid_filename_prefix = ' '
invalid_filename_postfix = ' .'
re_nonletters = re.compile(r'[\s' + string.punctuation + ']+')
+re_pattern = re.compile(r"([^\[\]]+|\[([^]]+)]|[\[\]]*)")
+re_pattern_arg = re.compile(r"(.*)<([^>]*)>$")
max_filename_part_length = 128
def sanitize_filename_part(text, replace_spaces=True):
+ if text is None:
+ return None
+
if replace_spaces:
text = text.replace(' ', '_')
@@ -286,49 +295,106 @@ def sanitize_filename_part(text, replace_spaces=True):
return text
-def apply_filename_pattern(x, p, seed, prompt):
- max_prompt_words = opts.directories_max_prompt_words
-
- if seed is not None:
- x = x.replace("[seed]", str(seed))
-
- if p is not None:
- x = x.replace("[steps]", str(p.steps))
- x = x.replace("[cfg]", str(p.cfg_scale))
- x = x.replace("[width]", str(p.width))
- x = x.replace("[height]", str(p.height))
- x = x.replace("[styles]", sanitize_filename_part(", ".join([x for x in p.styles if not x == "None"]) or "None", replace_spaces=False))
- x = x.replace("[sampler]", sanitize_filename_part(sd_samplers.samplers[p.sampler_index].name, replace_spaces=False))
-
- x = x.replace("[model_hash]", getattr(p, "sd_model_hash", shared.sd_model.sd_model_hash))
- x = x.replace("[date]", datetime.date.today().isoformat())
- x = x.replace("[datetime]", datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
- x = x.replace("[job_timestamp]", getattr(p, "job_timestamp", shared.state.job_timestamp))
+class FilenameGenerator:
+ replacements = {
+ 'seed': lambda self: self.seed if self.seed is not None else '',
+ 'steps': lambda self: self.p and self.p.steps,
+ 'cfg': lambda self: self.p and self.p.cfg_scale,
+ 'width': lambda self: self.p and self.p.width,
+ 'height': lambda self: self.p and self.p.height,
+ 'styles': lambda self: self.p and sanitize_filename_part(", ".join([style for style in self.p.styles if not style == "None"]) or "None", replace_spaces=False),
+ 'sampler': lambda self: self.p and sanitize_filename_part(sd_samplers.samplers[self.p.sampler_index].name, replace_spaces=False),
+ 'model_hash': lambda self: getattr(self.p, "sd_model_hash", shared.sd_model.sd_model_hash),
+ 'date': lambda self: datetime.datetime.now().strftime('%Y-%m-%d'),
+ 'datetime': lambda self, *args: self.datetime(*args), # accepts formats: [datetime], [datetime<Format>], [datetime<Format><Time Zone>]
+ 'job_timestamp': lambda self: getattr(self.p, "job_timestamp", shared.state.job_timestamp),
+ 'prompt': lambda self: sanitize_filename_part(self.prompt),
+ 'prompt_no_styles': lambda self: self.prompt_no_style(),
+ 'prompt_spaces': lambda self: sanitize_filename_part(self.prompt, replace_spaces=False),
+ 'prompt_words': lambda self: self.prompt_words(),
+ }
+ default_time_format = '%Y%m%d%H%M%S'
+
+ def __init__(self, p, seed, prompt):
+ self.p = p
+ self.seed = seed
+ self.prompt = prompt
+
+ def prompt_no_style(self):
+ if self.p is None or self.prompt is None:
+ return None
+
+ prompt_no_style = self.prompt
+ for style in shared.prompt_styles.get_style_prompts(self.p.styles):
+ if len(style) > 0:
+ for part in style.split("{prompt}"):
+ prompt_no_style = prompt_no_style.replace(part, "").replace(", ,", ",").strip().strip(',')
+
+ prompt_no_style = prompt_no_style.replace(style, "").strip().strip(',').strip()
+
+ return sanitize_filename_part(prompt_no_style, replace_spaces=False)
+
+ def prompt_words(self):
+ words = [x for x in re_nonletters.split(self.prompt or "") if len(x) > 0]
+ if len(words) == 0:
+ words = ["empty"]
+ return sanitize_filename_part(" ".join(words[0:opts.directories_max_prompt_words]), replace_spaces=False)
+
+ def datetime(self, *args):
+ time_datetime = datetime.datetime.now()
+
+ time_format = args[0] if len(args) > 0 else self.default_time_format
+ try:
+ time_zone = pytz.timezone(args[1]) if len(args) > 1 else None
+ except pytz.exceptions.UnknownTimeZoneError as _:
+ time_zone = None
+
+ time_zone_time = time_datetime.astimezone(time_zone)
+ try:
+ formatted_time = time_zone_time.strftime(time_format)
+ except (ValueError, TypeError) as _:
+ formatted_time = time_zone_time.strftime(self.default_time_format)
+
+ return sanitize_filename_part(formatted_time, replace_spaces=False)
+
+ def apply(self, x):
+ res = ''
+
+ for m in re_pattern.finditer(x):
+ text, pattern = m.groups()
+
+ if pattern is None:
+ res += text
+ continue
- # Apply [prompt] at last. Because it may contain any replacement word.^M
- if prompt is not None:
- x = x.replace("[prompt]", sanitize_filename_part(prompt))
- if "[prompt_no_styles]" in x:
- prompt_no_style = prompt
- for style in shared.prompt_styles.get_style_prompts(p.styles):
- if len(style) > 0:
- style_parts = [y for y in style.split("{prompt}")]
- for part in style_parts:
- prompt_no_style = prompt_no_style.replace(part, "").replace(", ,", ",").strip().strip(',')
- prompt_no_style = prompt_no_style.replace(style, "").strip().strip(',').strip()
- x = x.replace("[prompt_no_styles]", sanitize_filename_part(prompt_no_style, replace_spaces=False))
+ pattern_args = []
+ while True:
+ m = re_pattern_arg.match(pattern)
+ if m is None:
+ break
+
+ pattern, arg = m.groups()
+ pattern_args.insert(0, arg)
+
+ fun = self.replacements.get(pattern.lower())
+ if fun is not None:
+ try:
+ replacement = fun(self, *pattern_args)
+ except Exception:
+ replacement = None
+ print(f"Error adding [{pattern}] to filename", file=sys.stderr)
+ print(traceback.format_exc(), file=sys.stderr)
+
+ if replacement is None:
+ res += f'[{pattern}]'
+ else:
+ res += str(replacement)
- x = x.replace("[prompt_spaces]", sanitize_filename_part(prompt, replace_spaces=False))
- if "[prompt_words]" in x:
- words = [x for x in re_nonletters.split(prompt or "") if len(x) > 0]
- if len(words) == 0:
- words = ["empty"]
- x = x.replace("[prompt_words]", sanitize_filename_part(" ".join(words[0:max_prompt_words]), replace_spaces=False))
+ continue
- if cmd_opts.hide_ui_dir_config:
- x = re.sub(r'^[\\/]+|\.{2,}[\\/]+|[\\/]+\.{2,}', '', x)
+ res += f'[{pattern}]'
- return x
+ return res
def get_next_sequence_number(path, basename):
@@ -354,7 +420,7 @@ def get_next_sequence_number(path, basename):
def save_image(image, path, basename, seed=None, prompt=None, extension='png', info=None, short_filename=False, no_prompt=False, grid=False, pnginfo_section_name='parameters', p=None, existing_info=None, forced_filename=None, suffix="", save_to_dirs=None):
- '''Save an image.
+ """Save an image.
Args:
image (`PIL.Image`):
@@ -385,18 +451,8 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i
The full path of the saved imaged.
txt_fullfn (`str` or None):
If a text file is saved for this image, this will be its full path. Otherwise None.
- '''
- if short_filename or prompt is None or seed is None:
- file_decoration = ""
- elif opts.save_to_dirs:
- file_decoration = opts.samples_filename_pattern or "[seed]"
- else:
- file_decoration = opts.samples_filename_pattern or "[seed]-[prompt_spaces]"
-
- if file_decoration != "":
- file_decoration = "-" + file_decoration.lower()
-
- file_decoration = apply_filename_pattern(file_decoration, p, seed, prompt) + suffix
+ """
+ namegen = FilenameGenerator(p, seed, prompt)
if extension == 'png' and opts.enable_pnginfo and info is not None:
pnginfo = PngImagePlugin.PngInfo()
@@ -413,21 +469,37 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i
save_to_dirs = (grid and opts.grid_save_to_dirs) or (not grid and opts.save_to_dirs and not no_prompt)
if save_to_dirs:
- dirname = apply_filename_pattern(opts.directories_filename_pattern or "[prompt_words]", p, seed, prompt).strip('\\ /')
+ dirname = namegen.apply(opts.directories_filename_pattern or "[prompt_words]").lstrip(' ').rstrip('\\ /')
path = os.path.join(path, dirname)
os.makedirs(path, exist_ok=True)
if forced_filename is None:
- basecount = get_next_sequence_number(path, basename)
- fullfn = "a.png"
- fullfn_without_extension = "a"
- for i in range(500):
- fn = f"{basecount + i:05}" if basename == '' else f"{basename}-{basecount + i:04}"
- fullfn = os.path.join(path, f"{fn}{file_decoration}.{extension}")
- fullfn_without_extension = os.path.join(path, f"{fn}{file_decoration}")
- if not os.path.exists(fullfn):
- break
+ if short_filename or seed is None:
+ file_decoration = ""
+ else:
+ file_decoration = opts.samples_filename_pattern or "[seed]"
+
+ add_number = opts.save_images_add_number or file_decoration == ''
+
+ if file_decoration != "" and add_number:
+ file_decoration = "-" + file_decoration
+
+ file_decoration = namegen.apply(file_decoration) + suffix
+
+ if add_number:
+ basecount = get_next_sequence_number(path, basename)
+ fullfn = None
+ fullfn_without_extension = None
+ for i in range(500):
+ fn = f"{basecount + i:05}" if basename == '' else f"{basename}-{basecount + i:04}"
+ fullfn = os.path.join(path, f"{fn}{file_decoration}.{extension}")
+ fullfn_without_extension = os.path.join(path, f"{fn}{file_decoration}")
+ if not os.path.exists(fullfn):
+ break
+ else:
+ fullfn = os.path.join(path, f"{file_decoration}.{extension}")
+ fullfn_without_extension = os.path.join(path, file_decoration)
else:
fullfn = os.path.join(path, f"{forced_filename}.{extension}")
fullfn_without_extension = os.path.join(path, forced_filename)
diff --git a/modules/images_history.py b/modules/images_history.py
deleted file mode 100644
index bc5cf11f..00000000
--- a/modules/images_history.py
+++ /dev/null
@@ -1,424 +0,0 @@
-import os
-import shutil
-import time
-import hashlib
-import gradio
-system_bak_path = "webui_log_and_bak"
-custom_tab_name = "custom fold"
-faverate_tab_name = "favorites"
-tabs_list = ["txt2img", "img2img", "extras", faverate_tab_name]
-def is_valid_date(date):
- try:
- time.strptime(date, "%Y%m%d")
- return True
- except:
- return False
-
-def reduplicative_file_move(src, dst):
- def same_name_file(basename, path):
- name, ext = os.path.splitext(basename)
- f_list = os.listdir(path)
- max_num = 0
- for f in f_list:
- if len(f) <= len(basename):
- continue
- f_ext = f[-len(ext):] if len(ext) > 0 else ""
- if f[:len(name)] == name and f_ext == ext:
- if f[len(name)] == "(" and f[-len(ext)-1] == ")":
- number = f[len(name)+1:-len(ext)-1]
- if number.isdigit():
- if int(number) > max_num:
- max_num = int(number)
- return f"{name}({max_num + 1}){ext}"
- name = os.path.basename(src)
- save_name = os.path.join(dst, name)
- if not os.path.exists(save_name):
- shutil.move(src, dst)
- else:
- name = same_name_file(name, dst)
- shutil.move(src, os.path.join(dst, name))
-
-def traverse_all_files(curr_path, image_list, all_type=False):
- try:
- f_list = os.listdir(curr_path)
- except:
- if all_type or (curr_path[-10:].rfind(".") > 0 and curr_path[-4:] != ".txt" and curr_path[-4:] != ".csv"):
- image_list.append(curr_path)
- return image_list
- for file in f_list:
- file = os.path.join(curr_path, file)
- if (not all_type) and (file[-4:] == ".txt" or file[-4:] == ".csv"):
- pass
- elif os.path.isfile(file) and file[-10:].rfind(".") > 0:
- image_list.append(file)
- else:
- image_list = traverse_all_files(file, image_list)
- return image_list
-
-def auto_sorting(dir_name):
- bak_path = os.path.join(dir_name, system_bak_path)
- if not os.path.exists(bak_path):
- os.mkdir(bak_path)
- log_file = None
- files_list = []
- f_list = os.listdir(dir_name)
- for file in f_list:
- if file == system_bak_path:
- continue
- file_path = os.path.join(dir_name, file)
- if not is_valid_date(file):
- if file[-10:].rfind(".") > 0:
- files_list.append(file_path)
- else:
- files_list = traverse_all_files(file_path, files_list, all_type=True)
-
- for file in files_list:
- date_str = time.strftime("%Y%m%d",time.localtime(os.path.getmtime(file)))
- file_path = os.path.dirname(file)
- hash_path = hashlib.md5(file_path.encode()).hexdigest()
- path = os.path.join(dir_name, date_str, hash_path)
- if not os.path.exists(path):
- os.makedirs(path)
- if log_file is None:
- log_file = open(os.path.join(bak_path,"path_mapping.csv"),"a")
- log_file.write(f"{hash_path},{file_path}\n")
- reduplicative_file_move(file, path)
-
- date_list = []
- f_list = os.listdir(dir_name)
- for f in f_list:
- if is_valid_date(f):
- date_list.append(f)
- elif f == system_bak_path:
- continue
- else:
- try:
- reduplicative_file_move(os.path.join(dir_name, f), bak_path)
- except:
- pass
-
- today = time.strftime("%Y%m%d",time.localtime(time.time()))
- if today not in date_list:
- date_list.append(today)
- return sorted(date_list, reverse=True)
-
-def archive_images(dir_name, date_to):
- filenames = []
- batch_size =int(opts.images_history_num_per_page * opts.images_history_pages_num)
- if batch_size <= 0:
- batch_size = opts.images_history_num_per_page * 6
- 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_bak = date_to
- if False: #opts.images_history_reconstruct_directory:
- date_list = auto_sorting(dir_name)
- for date in date_list:
- if date <= date_to:
- path = os.path.join(dir_name, date)
- if date == today and not os.path.exists(path):
- continue
- filenames = traverse_all_files(path, filenames)
- if len(filenames) > batch_size:
- break
- filenames = sorted(filenames, key=lambda file: -os.path.getmtime(file))
- else:
- filenames = traverse_all_files(dir_name, filenames)
- total_num = len(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_to:None}
- 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) > batch_size:
- date = sort_array[batch_size][2]
- filenames = [x[1] for x in sort_array]
- else:
- date = date_to if len(sort_array) == 0 else sort_array[-1][2]
- filenames = [x[1] for x in sort_array]
- filenames = [x[1] for x in sort_array if x[2]>= date]
- num = len(filenames)
- last_date_from = date_to_bak if num == 0 else time.strftime("%Y%m%d", time.localtime(time.mktime(time.strptime(date, "%Y%m%d")) - 1000))
- date = date[:4] + "/" + date[4:6] + "/" + date[6:8]
- date_to_bak = date_to_bak[:4] + "/" + date_to_bak[4:6] + "/" + date_to_bak[6:8]
- load_info = "<div style='color:#999' align='center'>"
- load_info += f"{total_num} images in this directory. Loaded {num} images during {date} - {date_to_bak}, divided into {int((num + 1) // opts.images_history_num_per_page + 1)} pages"
- load_info += "</div>"
- _, image_list, _, _, visible_num = get_recent_images(1, 0, filenames)
- return (
- date_to,
- load_info,
- filenames,
- 1,
- image_list,
- "",
- "",
- visible_num,
- last_date_from,
- gradio.update(visible=total_num > num)
- )
-
-def delete_image(delete_num, name, filenames, image_index, visible_num):
- if name == "":
- return filenames, delete_num
- else:
- delete_num = int(delete_num)
- visible_num = int(visible_num)
- image_index = int(image_index)
- index = list(filenames).index(name)
- i = 0
- new_file_list = []
- for name in filenames:
- if i >= index and i < index + delete_num:
- if os.path.exists(name):
- if visible_num == image_index:
- new_file_list.append(name)
- i += 1
- continue
- print(f"Delete file {name}")
- os.remove(name)
- visible_num -= 1
- txt_file = os.path.splitext(name)[0] + ".txt"
- if os.path.exists(txt_file):
- os.remove(txt_file)
- else:
- print(f"Not exists file {name}")
- else:
- new_file_list.append(name)
- i += 1
- return new_file_list, 1, visible_num
-
-def save_image(file_name):
- if file_name is not None and os.path.exists(file_name):
- shutil.copy(file_name, opts.outdir_save)
-
-def get_recent_images(page_index, step, filenames):
- 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
- 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 = max_page_index if page_index > max_page_index else page_index
- idx_frm = (page_index - 1) * num_of_imgs_per_page
- image_list = filenames[idx_frm:idx_frm + num_of_imgs_per_page]
- length = len(filenames)
- visible_num = num_of_imgs_per_page if idx_frm + num_of_imgs_per_page <= length else length % num_of_imgs_per_page
- visible_num = num_of_imgs_per_page if visible_num == 0 else visible_num
- return page_index, image_list, "", "", visible_num
-
-def loac_batch_click(date_to):
- if date_to is None:
- return time.strftime("%Y%m%d",time.localtime(time.time())), []
- else:
- return None, []
-def forward_click(last_date_from, date_to_recorder):
- if len(date_to_recorder) == 0:
- return None, []
- if last_date_from == date_to_recorder[-1]:
- date_to_recorder = date_to_recorder[:-1]
- if len(date_to_recorder) == 0:
- return None, []
- return date_to_recorder[-1], date_to_recorder[:-1]
-
-def backward_click(last_date_from, date_to_recorder):
- if last_date_from is None or last_date_from == "":
- return time.strftime("%Y%m%d",time.localtime(time.time())), []
- if len(date_to_recorder) == 0 or last_date_from != date_to_recorder[-1]:
- date_to_recorder.append(last_date_from)
- return last_date_from, date_to_recorder
-
-
-def first_page_click(page_index, filenames):
- return get_recent_images(1, 0, filenames)
-
-def end_page_click(page_index, filenames):
- return get_recent_images(-1, 0, filenames)
-
-def prev_page_click(page_index, filenames):
- return get_recent_images(page_index, -1, filenames)
-
-def next_page_click(page_index, filenames):
- return get_recent_images(page_index, 1, filenames)
-
-def page_index_change(page_index, filenames):
- return get_recent_images(page_index, 0, filenames)
-
-def show_image_info(tabname_box, num, page_index, filenames):
- file = filenames[int(num) + int((page_index - 1) * int(opts.images_history_num_per_page))]
- tm = "<div style='color:#999' align='right'>" + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(os.path.getmtime(file))) + "</div>"
- return file, tm, num, file
-
-def enable_page_buttons():
- return gradio.update(visible=True)
-
-def change_dir(img_dir, date_to):
- warning = None
- try:
- if os.path.exists(img_dir):
- try:
- f = os.listdir(img_dir)
- except:
- warning = f"'{img_dir} is not a directory"
- else:
- warning = "The directory is not exist"
- except:
- warning = "The format of the directory is incorrect"
- if warning is None:
- today = time.strftime("%Y%m%d",time.localtime(time.time()))
- return gradio.update(visible=False), gradio.update(visible=True), None, None if date_to != today else today, gradio.update(visible=True), gradio.update(visible=True)
- else:
- return gradio.update(visible=True), gradio.update(visible=False), warning, date_to, gradio.update(visible=False), gradio.update(visible=False)
-
-def show_images_history(gr, opts, tabname, run_pnginfo, switch_dict):
- custom_dir = False
- if tabname == "txt2img":
- dir_name = opts.outdir_txt2img_samples
- elif tabname == "img2img":
- dir_name = opts.outdir_img2img_samples
- elif tabname == "extras":
- dir_name = opts.outdir_extras_samples
- elif tabname == faverate_tab_name:
- dir_name = opts.outdir_save
- else:
- custom_dir = True
- dir_name = None
-
- if not custom_dir:
- d = dir_name.split("/")
- dir_name = d[0]
- for p in d[1:]:
- dir_name = os.path.join(dir_name, p)
- if not os.path.exists(dir_name):
- os.makedirs(dir_name)
-
- with gr.Column() as page_panel:
- with gr.Row():
- with gr.Column(scale=1, visible=not custom_dir) as load_batch_box:
- load_batch = gr.Button('Load', elem_id=tabname + "_images_history_start", full_width=True)
- with gr.Column(scale=4):
- with gr.Row():
- img_path = gr.Textbox(dir_name, label="Images directory", placeholder="Input images directory", interactive=custom_dir)
- with gr.Row():
- with gr.Column(visible=False, scale=1) as batch_panel:
- with gr.Row():
- forward = gr.Button('Prev batch')
- backward = gr.Button('Next batch')
- with gr.Column(scale=3):
- load_info = gr.HTML(visible=not custom_dir)
- with gr.Row(visible=False) as warning:
- warning_box = gr.Textbox("Message", interactive=False)
-
- with gr.Row(visible=not custom_dir, elem_id=tabname + "_images_history") as main_panel:
- with gr.Column(scale=2):
- with gr.Row(visible=True) as turn_page_buttons:
- #date_to = gr.Dropdown(label="Date to")
- first_page = gr.Button('First Page')
- prev_page = gr.Button('Prev Page')
- page_index = gr.Number(value=1, label="Page Index")
- next_page = gr.Button('Next Page')
- end_page = gr.Button('End Page')
-
- history_gallery = gr.Gallery(show_label=False, elem_id=tabname + "_images_history_gallery").style(grid=opts.images_history_grid_num)
- with gr.Row():
- 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")
-
- with gr.Column():
- with gr.Row():
- with gr.Column():
- img_file_info = gr.Textbox(label="Generate Info", interactive=False, lines=6)
- gr.HTML("<hr>")
- img_file_name = gr.Textbox(value="", label="File Name", interactive=False)
- img_file_time= gr.HTML()
- with gr.Row():
- if tabname != faverate_tab_name:
- save_btn = gr.Button('Collect')
- pnginfo_send_to_txt2img = gr.Button('Send to txt2img')
- pnginfo_send_to_img2img = gr.Button('Send to img2img')
-
-
- # hiden items
- with gr.Row(visible=False):
- renew_page = gr.Button('Refresh page', elem_id=tabname + "_images_history_renew_page")
- batch_date_to = gr.Textbox(label="Date to")
- visible_img_num = gr.Number()
- date_to_recorder = gr.State([])
- last_date_from = gr.Textbox()
- tabname_box = gr.Textbox(tabname)
- image_index = gr.Textbox(value=-1)
- set_index = gr.Button('set_index', elem_id=tabname + "_images_history_set_index")
- filenames = gr.State()
- all_images_list = gr.State()
- hidden = gr.Image(type="pil")
- info1 = gr.Textbox()
- info2 = gr.Textbox()
-
- img_path.submit(change_dir, inputs=[img_path, batch_date_to], outputs=[warning, main_panel, warning_box, batch_date_to, load_batch_box, load_info])
-
- #change batch
- change_date_output = [batch_date_to, load_info, filenames, page_index, history_gallery, img_file_name, img_file_time, visible_img_num, last_date_from, batch_panel]
-
- batch_date_to.change(archive_images, inputs=[img_path, batch_date_to], outputs=change_date_output)
- batch_date_to.change(enable_page_buttons, inputs=None, outputs=[turn_page_buttons])
- batch_date_to.change(fn=None, inputs=[tabname_box], outputs=None, _js="images_history_turnpage")
-
- load_batch.click(loac_batch_click, inputs=[batch_date_to], outputs=[batch_date_to, date_to_recorder])
- forward.click(forward_click, inputs=[last_date_from, date_to_recorder], outputs=[batch_date_to, date_to_recorder])
- backward.click(backward_click, inputs=[last_date_from, date_to_recorder], outputs=[batch_date_to, date_to_recorder])
-
-
- #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(fn=None, _js="images_history_delete", inputs=[delete_num, tabname_box, image_index], outputs=None)
- if tabname != faverate_tab_name:
- save_btn.click(save_image, inputs=[img_file_name], outputs=None)
-
- #turn page
- gallery_inputs = [page_index, filenames]
- gallery_outputs = [page_index, history_gallery, img_file_name, img_file_time, visible_img_num]
- first_page.click(first_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)
- end_page.click(end_page_click, inputs=gallery_inputs, outputs=gallery_outputs)
- page_index.submit(page_index_change, inputs=gallery_inputs, outputs=gallery_outputs)
- renew_page.click(page_index_change, inputs=gallery_inputs, outputs=gallery_outputs)
-
- first_page.click(fn=None, inputs=[tabname_box], outputs=None, _js="images_history_turnpage")
- next_page.click(fn=None, inputs=[tabname_box], outputs=None, _js="images_history_turnpage")
- prev_page.click(fn=None, inputs=[tabname_box], outputs=None, _js="images_history_turnpage")
- end_page.click(fn=None, inputs=[tabname_box], outputs=None, _js="images_history_turnpage")
- page_index.submit(fn=None, inputs=[tabname_box], outputs=None, _js="images_history_turnpage")
- renew_page.click(fn=None, inputs=[tabname_box], outputs=None, _js="images_history_turnpage")
-
- # other funcitons
- set_index.click(show_image_info, _js="images_history_get_current_img", inputs=[tabname_box, image_index, page_index, filenames], outputs=[img_file_name, img_file_time, image_index, hidden])
- 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])
- 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')
-
-
-
-def create_history_tabs(gr, sys_opts, cmp_ops, run_pnginfo, switch_dict):
- global opts;
- 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)
- if cmp_ops.browse_all_images:
- tabs_list.append(custom_tab_name)
- with gr.Blocks(analytics_enabled=False) as images_history:
- with gr.Tabs() as tabs:
- for tab in tabs_list:
- with gr.Tab(tab):
- with gr.Blocks(analytics_enabled=False) :
- show_images_history(gr, opts, tab, run_pnginfo, switch_dict)
- gradio.Checkbox(opts.images_history_preload, elem_id="images_history_preload", visible=False)
- gradio.Textbox(",".join(tabs_list), elem_id="images_history_tabnames_list", visible=False)
-
- return images_history
diff --git a/modules/processing.py b/modules/processing.py
index 2a332514..c61bbfbd 100644
--- a/modules/processing.py
+++ b/modules/processing.py
@@ -46,18 +46,23 @@ def apply_color_correction(correction, image):
return image
-def apply_overlay(overlay_exists, overlay, paste_loc, image):
- if overlay_exists:
- if paste_loc is not None:
- x, y, w, h = paste_loc
- base_image = Image.new('RGBA', (overlay.width, overlay.height))
- image = images.resize_image(1, image, w, h)
- base_image.paste(image, (x, y))
- image = base_image
-
- image = image.convert('RGBA')
- image.alpha_composite(overlay)
- image = image.convert('RGB')
+
+def apply_overlay(image, paste_loc, index, overlays):
+ if overlays is None or index >= len(overlays):
+ return image
+
+ overlay = overlays[index]
+
+ if paste_loc is not None:
+ x, y, w, h = paste_loc
+ base_image = Image.new('RGBA', (overlay.width, overlay.height))
+ image = images.resize_image(1, image, w, h)
+ base_image.paste(image, (x, y))
+ image = base_image
+
+ image = image.convert('RGBA')
+ image.alpha_composite(overlay)
+ image = image.convert('RGB')
return image
@@ -463,11 +468,11 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
if p.color_corrections is not None and i < len(p.color_corrections):
if opts.save and not p.do_not_save_samples and opts.save_images_before_color_correction:
- image_without_cc = apply_overlay(p.overlay_images is not None and i < len(p.overlay_images), p.overlay_images[i], p.paste_to, image)
+ image_without_cc = apply_overlay(image, p.paste_to, i, p.overlay_images)
images.save_image(image_without_cc, p.outpath_samples, "", seeds[i], prompts[i], opts.samples_format, info=infotext(n, i), p=p, suffix="-before-color-correction")
image = apply_color_correction(p.color_corrections[i], image)
- image = apply_overlay(p.overlay_images is not None and i < len(p.overlay_images), p.overlay_images[i], p.paste_to, image)
+ image = apply_overlay(image, p.paste_to, i, p.overlay_images)
if opts.samples_save and not p.do_not_save_samples:
images.save_image(image, p.outpath_samples, "", seeds[i], prompts[i], opts.samples_format, info=infotext(n, i), p=p)
diff --git a/modules/script_callbacks.py b/modules/script_callbacks.py
index 5836e4b9..9933fa38 100644
--- a/modules/script_callbacks.py
+++ b/modules/script_callbacks.py
@@ -1,4 +1,15 @@
+import sys
+import traceback
+from collections import namedtuple
+import inspect
+
+def report_exception(c, job):
+ print(f"Error executing callback {job} for {c.script}", file=sys.stderr)
+ print(traceback.format_exc(), file=sys.stderr)
+
+
+ScriptCallback = namedtuple("ScriptCallback", ["script", "callback"])
callbacks_model_loaded = []
callbacks_ui_tabs = []
callbacks_ui_settings = []
@@ -11,22 +22,38 @@ def clear_callbacks():
def model_loaded_callback(sd_model):
- for callback in callbacks_model_loaded:
- callback(sd_model)
+ for c in callbacks_model_loaded:
+ try:
+ c.callback(sd_model)
+ except Exception:
+ report_exception(c, 'model_loaded_callback')
def ui_tabs_callback():
res = []
-
- for callback in callbacks_ui_tabs:
- res += callback() or []
+
+ for c in callbacks_ui_tabs:
+ try:
+ res += c.callback() or []
+ except Exception:
+ report_exception(c, 'ui_tabs_callback')
return res
def ui_settings_callback():
- for callback in callbacks_ui_settings:
- callback()
+ for c in callbacks_ui_settings:
+ try:
+ c.callback()
+ except Exception:
+ report_exception(c, 'ui_settings_callback')
+
+
+def add_callback(callbacks, fun):
+ stack = [x for x in inspect.stack() if x.filename != __file__]
+ filename = stack[0].filename if len(stack) > 0 else 'unknown file'
+
+ callbacks.append(ScriptCallback(filename, fun))
def image_saved_callback(image, p, fullfn, txt_fullfn):
@@ -36,7 +63,7 @@ def image_saved_callback(image, p, fullfn, txt_fullfn):
def on_model_loaded(callback):
"""register a function to be called when the stable diffusion model is created; the model is
passed as an argument"""
- callbacks_model_loaded.append(callback)
+ add_callback(callbacks_model_loaded, callback)
def on_ui_tabs(callback):
@@ -49,7 +76,7 @@ def on_ui_tabs(callback):
title is tab text displayed to user in the UI
elem_id is HTML id for the tab
"""
- callbacks_ui_tabs.append(callback)
+ add_callback(callbacks_ui_tabs, callback)
def on_ui_settings(callback):
diff --git a/modules/sd_samplers.py b/modules/sd_samplers.py
index 0b408a70..3670b57d 100644
--- a/modules/sd_samplers.py
+++ b/modules/sd_samplers.py
@@ -228,7 +228,7 @@ class VanillaStableDiffusionSampler:
unconditional_conditioning = {"c_concat": [image_conditioning], "c_crossattn": [unconditional_conditioning]}
- samples = self.launch_sampling(steps, lambda: self.sampler.decode(x1, conditioning, t_enc, unconditional_guidance_scale=p.cfg_scale, unconditional_conditioning=unconditional_conditioning))
+ samples = self.launch_sampling(t_enc + 1, lambda: self.sampler.decode(x1, conditioning, t_enc, unconditional_guidance_scale=p.cfg_scale, unconditional_conditioning=unconditional_conditioning))
return samples
@@ -429,7 +429,7 @@ class KDiffusionSampler:
self.model_wrap_cfg.init_latent = x
self.last_latent = x
- samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, xi, extra_args={
+ samples = self.launch_sampling(t_enc + 1, lambda: self.func(self.model_wrap_cfg, xi, extra_args={
'cond': conditioning,
'image_cond': image_conditioning,
'uncond': unconditional_conditioning,
diff --git a/modules/shared.py b/modules/shared.py
index b55371d3..76cbb1bd 100644
--- a/modules/shared.py
+++ b/modules/shared.py
@@ -80,12 +80,13 @@ parser.add_argument('--vae-path', type=str, help='Path to Variational Autoencode
parser.add_argument("--disable-safe-unpickle", action='store_true', help="disable checking pytorch models for malicious code", default=False)
parser.add_argument("--api", action='store_true', help="use api=True to launch the api with the webui")
parser.add_argument("--nowebui", action='store_true', help="use api=True to launch the api instead of the webui")
+parser.add_argument("--ui-debug-mode", action='store_true', help="Don't load model to quickly launch UI")
parser.add_argument("--device-id", type=str, help="Select the default CUDA device to use (export CUDA_VISIBLE_DEVICES=0,1,etc might be needed before)", default=None)
-parser.add_argument("--browse-all-images", action='store_true', help="Allow browsing all images by Image Browser", default=False)
cmd_opts = parser.parse_args()
restricted_opts = [
"samples_filename_pattern",
+ "directories_filename_pattern",
"outdir_samples",
"outdir_txt2img_samples",
"outdir_img2img_samples",
@@ -190,7 +191,8 @@ options_templates = {}
options_templates.update(options_section(('saving-images', "Saving images/grids"), {
"samples_save": OptionInfo(True, "Always save all generated images"),
"samples_format": OptionInfo('png', 'File format for images'),
- "samples_filename_pattern": OptionInfo("", "Images filename pattern"),
+ "samples_filename_pattern": OptionInfo("", "Images filename pattern", component_args=hide_dirs),
+ "save_images_add_number": OptionInfo(True, "Add number to filename when saving", component_args=hide_dirs),
"grid_save": OptionInfo(True, "Always save all generated image grids"),
"grid_format": OptionInfo('png', 'File format for grids'),
@@ -225,8 +227,8 @@ options_templates.update(options_section(('saving-to-dirs', "Saving to a directo
"save_to_dirs": OptionInfo(False, "Save images to a subdirectory"),
"grid_save_to_dirs": OptionInfo(False, "Save grids to a subdirectory"),
"use_save_to_dirs_for_ui": OptionInfo(False, "When using \"Save\" button, save images to a subdirectory"),
- "directories_filename_pattern": OptionInfo("", "Directory name pattern"),
- "directories_max_prompt_words": OptionInfo(8, "Max prompt words for [prompt_words] pattern", gr.Slider, {"minimum": 1, "maximum": 20, "step": 1}),
+ "directories_filename_pattern": OptionInfo("", "Directory name pattern", component_args=hide_dirs),
+ "directories_max_prompt_words": OptionInfo(8, "Max prompt words for [prompt_words] pattern", gr.Slider, {"minimum": 1, "maximum": 20, "step": 1, **hide_dirs}),
}))
options_templates.update(options_section(('upscaling', "Upscaling"), {
@@ -320,15 +322,6 @@ options_templates.update(options_section(('sampler-params', "Sampler parameters"
'eta_noise_seed_delta': OptionInfo(0, "Eta noise seed delta", gr.Number, {"precision": 0}),
}))
-options_templates.update(options_section(('images-history', "Images Browser"), {
- #"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_preload": OptionInfo(False, "Preload images at startup"),
- "images_history_num_per_page": OptionInfo(36, "Number of pictures displayed on each page"),
- "images_history_pages_num": OptionInfo(6, "Minimum number of pages per load "),
- "images_history_grid_num": OptionInfo(6, "Number of grids in each row"),
-
-}))
-
class Options:
data = None
@@ -357,7 +350,7 @@ class Options:
def save(self, filename):
with open(filename, "w", encoding="utf8") as file:
- json.dump(self.data, file)
+ json.dump(self.data, file, indent=4)
def same_type(self, x, y):
if x is None or y is None:
diff --git a/modules/ui.py b/modules/ui.py
index 2311572c..03528968 100644
--- a/modules/ui.py
+++ b/modules/ui.py
@@ -38,7 +38,6 @@ import modules.codeformer_model
import modules.generation_parameters_copypaste
import modules.gfpgan_model
import modules.hypernetworks.ui
-import modules.images_history as img_his
import modules.ldsr_model
import modules.scripts
import modules.shared as shared
@@ -51,12 +50,11 @@ from modules.sd_samplers import samplers, samplers_for_img2img
import modules.textual_inversion.ui
import modules.hypernetworks.ui
-import modules.images_history as img_his
-
-
# this is a fix for Windows users. Without it, javascript files will be served with text/html content-type and the browser will not show any UI
mimetypes.init()
mimetypes.add_type('application/javascript', '.js')
+txt2img_paste_fields = []
+img2img_paste_fields = []
if not cmd_opts.share and not cmd_opts.listen:
@@ -786,6 +784,7 @@ def create_ui(wrap_gradio_gpu_call):
]
)
+ global txt2img_paste_fields
txt2img_paste_fields = [
(txt2img_prompt, "Prompt"),
(txt2img_negative_prompt, "Negative prompt"),
@@ -1056,6 +1055,7 @@ def create_ui(wrap_gradio_gpu_call):
outputs=[prompt, negative_prompt, style1, style2],
)
+ global img2img_paste_fields
img2img_paste_fields = [
(img2img_prompt, "Prompt"),
(img2img_negative_prompt, "Negative prompt"),
@@ -1104,9 +1104,9 @@ def create_ui(wrap_gradio_gpu_call):
upscaling_resize_w = gr.Number(label="Width", value=512, precision=0)
upscaling_resize_h = gr.Number(label="Height", value=512, precision=0)
upscaling_crop = gr.Checkbox(label='Crop to fit', value=True)
-
+
with gr.Group():
- extras_upscaler_1 = gr.Radio(label='Upscaler 1', elem_id="extras_upscaler_1", choices=[x.name for x in shared.sd_upscalers], value=shared.sd_upscalers[0].name, type="index")
+ extras_upscaler_1 = gr.Radio(label='Upscaler 1', elem_id="extras_upscaler_1", choices=[x.name for x in shared.sd_upscalers], value=shared.sd_upscalers[0].name, type="index")
with gr.Group():
extras_upscaler_2 = gr.Radio(label='Upscaler 2', elem_id="extras_upscaler_2", choices=[x.name for x in shared.sd_upscalers], value=shared.sd_upscalers[0].name, type="index")
@@ -1193,15 +1193,7 @@ def create_ui(wrap_gradio_gpu_call):
inputs=[image],
outputs=[html, generation_info, html2],
)
- #images history
- images_history_switch_dict = {
- "fn": modules.generation_parameters_copypaste.connect_paste,
- "t2i": txt2img_paste_fields,
- "i2i": img2img_paste_fields
- }
-
- images_history = img_his.create_history_tabs(gr, opts, cmd_opts, wrap_gradio_call(modules.extras.run_pnginfo), images_history_switch_dict)
-
+
with gr.Blocks() as modelmerger_interface:
with gr.Row().style(equal_height=False):
with gr.Column(variant='panel'):
@@ -1650,7 +1642,6 @@ Requested path was: {f}
(img2img_interface, "img2img", "img2img"),
(extras_interface, "Extras", "extras"),
(pnginfo_interface, "PNG Info", "pnginfo"),
- (images_history, "Image Browser", "images_history"),
(modelmerger_interface, "Checkpoint Merger", "modelmerger"),
(train_interface, "Train", "ti"),
]
@@ -1894,6 +1885,7 @@ def load_javascript(raw_response):
javascript = f'<script>{jsfile.read()}</script>'
scripts_list = modules.scripts.list_scripts("javascript", ".js")
+
for basedir, filename, path in scripts_list:
with open(path, "r", encoding="utf8") as jsfile:
javascript += f"\n<!-- {filename} --><script>{jsfile.read()}</script>"