From d8ad3646170df18d850b658279ee0a14273ebee2 Mon Sep 17 00:00:00 2001 From: Takashi Takebayashi Date: Thu, 3 Oct 2024 14:33:37 +0900 Subject: [PATCH 01/19] Fix typo Github -> GitHub --- extensions-builtin/hypertile/hypertile.py | 2 +- html/footer.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions-builtin/hypertile/hypertile.py b/extensions-builtin/hypertile/hypertile.py index 0f40e2d3925..2da71583871 100644 --- a/extensions-builtin/hypertile/hypertile.py +++ b/extensions-builtin/hypertile/hypertile.py @@ -1,7 +1,7 @@ """ Hypertile module for splitting attention layers in SD-1.5 U-Net and SD-1.5 VAE Warn: The patch works well only if the input image has a width and height that are multiples of 128 -Original author: @tfernd Github: https://github.com/tfernd/HyperTile +Original author: @tfernd GitHub: https://github.com/tfernd/HyperTile """ from __future__ import annotations diff --git a/html/footer.html b/html/footer.html index 69b2372c755..2bedddbc19d 100644 --- a/html/footer.html +++ b/html/footer.html @@ -1,7 +1,7 @@
API  •  - Github + GitHub  •  Gradio  •  From deb3803a3ad54d134f66d124c6ce9483ccd9a03a Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:28:21 +0900 Subject: [PATCH 02/19] image embedding data cache (#16556) --- modules/shared_options.py | 1 + .../textual_inversion/textual_inversion.py | 44 +++++++++++++------ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/modules/shared_options.py b/modules/shared_options.py index efede7067f2..ebe59d4da72 100644 --- a/modules/shared_options.py +++ b/modules/shared_options.py @@ -291,6 +291,7 @@ "textual_inversion_print_at_load": OptionInfo(False, "Print a list of Textual Inversion embeddings when loading model"), "textual_inversion_add_hashes_to_infotext": OptionInfo(True, "Add Textual Inversion hashes to infotext"), "sd_hypernetwork": OptionInfo("None", "Add hypernetwork to prompt", gr.Dropdown, lambda: {"choices": ["None", *shared.hypernetworks]}, refresh=shared_items.reload_hypernetworks), + "textual_inversion_image_embedding_data_cache": OptionInfo(False, 'Cache the data of image embeddings').info('potentially increase TI load time at the cost some disk space'), })) options_templates.update(options_section(('ui_prompt_editing', "Prompt editing", "ui"), { diff --git a/modules/textual_inversion/textual_inversion.py b/modules/textual_inversion/textual_inversion.py index dc7833e9394..f209b883450 100644 --- a/modules/textual_inversion/textual_inversion.py +++ b/modules/textual_inversion/textual_inversion.py @@ -12,7 +12,7 @@ import numpy as np from PIL import Image, PngImagePlugin -from modules import shared, devices, sd_hijack, sd_models, images, sd_samplers, sd_hijack_checkpoint, errors, hashes +from modules import shared, devices, sd_hijack, sd_models, images, sd_samplers, sd_hijack_checkpoint, errors, hashes, cache import modules.textual_inversion.dataset from modules.textual_inversion.learn_schedule import LearnRateScheduler @@ -116,6 +116,7 @@ def __init__(self): self.expected_shape = -1 self.embedding_dirs = {} self.previously_displayed_embeddings = () + self.image_embedding_cache = cache.cache('image-embedding') def add_embedding_dir(self, path): self.embedding_dirs[path] = DirWithTextualInversionEmbeddings(path) @@ -154,6 +155,31 @@ def get_expected_shape(self): vec = shared.sd_model.cond_stage_model.encode_embedding_init_text(",", 1) return vec.shape[1] + def read_embedding_from_image(self, path, name): + try: + ondisk_mtime = os.path.getmtime(path) + + if (cache_embedding := self.image_embedding_cache.get(path)) and ondisk_mtime == cache_embedding.get('mtime', 0): + # cache will only be used if the file has not been modified time matches + return cache_embedding.get('data', None), cache_embedding.get('name', None) + + embed_image = Image.open(path) + if hasattr(embed_image, 'text') and 'sd-ti-embedding' in embed_image.text: + data = embedding_from_b64(embed_image.text['sd-ti-embedding']) + name = data.get('name', name) + elif data := extract_image_data_embed(embed_image): + name = data.get('name', name) + + if data is None or shared.opts.textual_inversion_image_embedding_data_cache: + # data of image embeddings only will be cached if the option textual_inversion_image_embedding_data_cache is enabled + # results of images that are not embeddings will allways be cached to reduce unnecessary future disk reads + self.image_embedding_cache[path] = {'data': data, 'name': None if data is None else name, 'mtime': ondisk_mtime} + + return data, name + except Exception: + errors.report(f"Error loading embedding {path}", exc_info=True) + return None, None + def load_from_file(self, path, filename): name, ext = os.path.splitext(filename) ext = ext.upper() @@ -163,17 +189,10 @@ def load_from_file(self, path, filename): if second_ext.upper() == '.PREVIEW': return - embed_image = Image.open(path) - if hasattr(embed_image, 'text') and 'sd-ti-embedding' in embed_image.text: - data = embedding_from_b64(embed_image.text['sd-ti-embedding']) - name = data.get('name', name) - else: - data = extract_image_data_embed(embed_image) - if data: - name = data.get('name', name) - else: - # if data is None, means this is not an embedding, just a preview image - return + data, name = self.read_embedding_from_image(path, name) + if data is None: + return + elif ext in ['.BIN', '.PT']: data = torch.load(path, map_location="cpu") elif ext in ['.SAFETENSORS']: @@ -191,7 +210,6 @@ def load_from_file(self, path, filename): else: print(f"Unable to load Textual inversion embedding due to data issue: '{name}'.") - def load_from_dir(self, embdir): if not os.path.isdir(embdir.path): return From 820fe8d2b556aaded190f21780afcd1fe5318e39 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:30:08 +0900 Subject: [PATCH 03/19] Allow newline in Extra Network activation text (#16428) --- javascript/extraNetworks.js | 2 +- modules/ui_extra_networks.py | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/javascript/extraNetworks.js b/javascript/extraNetworks.js index c5cced97399..8e8adad0bae 100644 --- a/javascript/extraNetworks.js +++ b/javascript/extraNetworks.js @@ -201,7 +201,7 @@ function setupExtraNetworks() { setupExtraNetworksForTab('img2img'); } -var re_extranet = /<([^:^>]+:[^:]+):[\d.]+>(.*)/; +var re_extranet = /<([^:^>]+:[^:]+):[\d.]+>(.*)/s; var re_extranet_g = /<([^:^>]+:[^:]+):[\d.]+>/g; var re_extranet_neg = /\(([^:^>]+:[\d.]+)\)/; diff --git a/modules/ui_extra_networks.py b/modules/ui_extra_networks.py index 6e9ec164552..1f19bd36d12 100644 --- a/modules/ui_extra_networks.py +++ b/modules/ui_extra_networks.py @@ -177,10 +177,8 @@ def add_pages_to_demo(app): app.add_api_route("/sd_extra_networks/get-single-card", get_single_card, methods=["GET"]) -def quote_js(s): - s = s.replace('\\', '\\\\') - s = s.replace('"', '\\"') - return f'"{s}"' +def quote_js(s: str): + return json.dumps(s, ensure_ascii=False) class ExtraNetworksPage: From 0bf36cf2adbf216cbac74c5f9461f7cacf0c6cb2 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:35:46 +0900 Subject: [PATCH 04/19] extra_only / main_ui_only ScriptPostprocessing (#16374) --- modules/scripts_auto_postprocessing.py | 2 +- modules/scripts_postprocessing.py | 10 +++++----- modules/shared_items.py | 8 +++++--- modules/shared_options.py | 6 +++--- scripts/postprocessing_upscale.py | 1 + 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/modules/scripts_auto_postprocessing.py b/modules/scripts_auto_postprocessing.py index d63078de50e..26fd138e736 100644 --- a/modules/scripts_auto_postprocessing.py +++ b/modules/scripts_auto_postprocessing.py @@ -33,7 +33,7 @@ def create_auto_preprocessing_script_data(): for name in shared.opts.postprocessing_enable_in_main_ui: script = next(iter([x for x in scripts.postprocessing_scripts_data if x.script_class.name == name]), None) - if script is None: + if script is None or script.script_class.extra_only: continue constructor = lambda s=script: ScriptPostprocessingForMainUI(s.script_class()) diff --git a/modules/scripts_postprocessing.py b/modules/scripts_postprocessing.py index 4b3b7afda1c..131ba4b970d 100644 --- a/modules/scripts_postprocessing.py +++ b/modules/scripts_postprocessing.py @@ -59,6 +59,10 @@ class ScriptPostprocessing: args_from = None args_to = None + # define if the script should be used only in extras or main UI + extra_only = None + main_ui_only = None + order = 1000 """scripts will be ordred by this value in postprocessing UI""" @@ -119,10 +123,6 @@ def initialize_scripts(self, scripts_data): for script_data in scripts_data: script: ScriptPostprocessing = script_data.script_class() script.filename = script_data.path - - if script.name == "Simple Upscale": - continue - self.scripts.append(script) def create_script_ui(self, script, inputs): @@ -152,7 +152,7 @@ def script_score(name): return len(self.scripts) - filtered_scripts = [script for script in self.scripts if script.name not in scripts_filter_out] + filtered_scripts = [script for script in self.scripts if script.name not in scripts_filter_out and not script.main_ui_only] script_scores = {script.name: (script_score(script.name), script.order, script.name, original_index) for original_index, script in enumerate(filtered_scripts)} return sorted(filtered_scripts, key=lambda x: script_scores[x.name]) diff --git a/modules/shared_items.py b/modules/shared_items.py index 11f10b3f7b1..3aaf0649028 100644 --- a/modules/shared_items.py +++ b/modules/shared_items.py @@ -16,10 +16,12 @@ def dat_models_names(): return [x.name for x in modules.dat_model.get_dat_models(None)] -def postprocessing_scripts(): +def postprocessing_scripts(filter_out_extra_only=False, filter_out_main_ui_only=False): import modules.scripts - - return modules.scripts.scripts_postproc.scripts + return list(filter( + lambda s: (not filter_out_extra_only or not s.extra_only) and (not filter_out_main_ui_only or not s.main_ui_only), + modules.scripts.scripts_postproc.scripts, + )) def sd_vae_items(): diff --git a/modules/shared_options.py b/modules/shared_options.py index ebe59d4da72..78089cbec05 100644 --- a/modules/shared_options.py +++ b/modules/shared_options.py @@ -411,9 +411,9 @@ })) options_templates.update(options_section(('postprocessing', "Postprocessing", "postprocessing"), { - 'postprocessing_enable_in_main_ui': OptionInfo([], "Enable postprocessing operations in txt2img and img2img tabs", ui_components.DropdownMulti, lambda: {"choices": [x.name for x in shared_items.postprocessing_scripts()]}), - 'postprocessing_disable_in_extras': OptionInfo([], "Disable postprocessing operations in extras tab", ui_components.DropdownMulti, lambda: {"choices": [x.name for x in shared_items.postprocessing_scripts()]}), - 'postprocessing_operation_order': OptionInfo([], "Postprocessing operation order", ui_components.DropdownMulti, lambda: {"choices": [x.name for x in shared_items.postprocessing_scripts()]}), + 'postprocessing_enable_in_main_ui': OptionInfo([], "Enable postprocessing operations in txt2img and img2img tabs", ui_components.DropdownMulti, lambda: {"choices": [x.name for x in shared_items.postprocessing_scripts(filter_out_extra_only=True)]}), + 'postprocessing_disable_in_extras': OptionInfo([], "Disable postprocessing operations in extras tab", ui_components.DropdownMulti, lambda: {"choices": [x.name for x in shared_items.postprocessing_scripts(filter_out_main_ui_only=True)]}), + 'postprocessing_operation_order': OptionInfo([], "Postprocessing operation order", ui_components.DropdownMulti, lambda: {"choices": [x.name for x in shared_items.postprocessing_scripts(filter_out_main_ui_only=True)]}), 'upscaling_max_images_in_cache': OptionInfo(5, "Maximum number of images in upscaling cache", gr.Slider, {"minimum": 0, "maximum": 10, "step": 1}), 'postprocessing_existing_caption_action': OptionInfo("Ignore", "Action for existing captions", gr.Radio, {"choices": ["Ignore", "Keep", "Prepend", "Append"]}).info("when generating captions using postprocessing; Ignore = use generated; Keep = use original; Prepend/Append = combine both"), })) diff --git a/scripts/postprocessing_upscale.py b/scripts/postprocessing_upscale.py index 2409fd2073e..47a24b1b784 100644 --- a/scripts/postprocessing_upscale.py +++ b/scripts/postprocessing_upscale.py @@ -169,6 +169,7 @@ def image_changed(self): class ScriptPostprocessingUpscaleSimple(ScriptPostprocessingUpscale): name = "Simple Upscale" order = 900 + main_ui_only = True def ui(self): with FormRow(): From 4ec10bcdc1aa56a46f648205dc857fee5c05c2c1 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:38:55 +0900 Subject: [PATCH 05/19] Fix postprocessing_enable_in_main_ui ScriptPostprocessing elem_id (#16373) --- .../scripts/postprocessing_autosized_crop.py | 12 ++++---- .../scripts/postprocessing_focal_crop.py | 8 ++--- .../scripts/postprocessing_split_oversized.py | 4 +-- modules/scripts_auto_postprocessing.py | 1 + modules/scripts_postprocessing.py | 26 ++++++++++++++++ scripts/postprocessing_codeformer.py | 4 +-- scripts/postprocessing_gfpgan.py | 2 +- scripts/postprocessing_upscale.py | 30 +++++++++---------- 8 files changed, 57 insertions(+), 30 deletions(-) diff --git a/extensions-builtin/postprocessing-for-training/scripts/postprocessing_autosized_crop.py b/extensions-builtin/postprocessing-for-training/scripts/postprocessing_autosized_crop.py index 7e674989814..c8ea7e0e061 100644 --- a/extensions-builtin/postprocessing-for-training/scripts/postprocessing_autosized_crop.py +++ b/extensions-builtin/postprocessing-for-training/scripts/postprocessing_autosized_crop.py @@ -34,14 +34,14 @@ def ui(self): with ui_components.InputAccordion(False, label="Auto-sized crop") as enable: gr.Markdown('Each image is center-cropped with an automatically chosen width and height.') with gr.Row(): - mindim = gr.Slider(minimum=64, maximum=2048, step=8, label="Dimension lower bound", value=384, elem_id="postprocess_multicrop_mindim") - maxdim = gr.Slider(minimum=64, maximum=2048, step=8, label="Dimension upper bound", value=768, elem_id="postprocess_multicrop_maxdim") + mindim = gr.Slider(minimum=64, maximum=2048, step=8, label="Dimension lower bound", value=384, elem_id=self.elem_id_suffix("postprocess_multicrop_mindim")) + maxdim = gr.Slider(minimum=64, maximum=2048, step=8, label="Dimension upper bound", value=768, elem_id=self.elem_id_suffix("postprocess_multicrop_maxdim")) with gr.Row(): - minarea = gr.Slider(minimum=64 * 64, maximum=2048 * 2048, step=1, label="Area lower bound", value=64 * 64, elem_id="postprocess_multicrop_minarea") - maxarea = gr.Slider(minimum=64 * 64, maximum=2048 * 2048, step=1, label="Area upper bound", value=640 * 640, elem_id="postprocess_multicrop_maxarea") + minarea = gr.Slider(minimum=64 * 64, maximum=2048 * 2048, step=1, label="Area lower bound", value=64 * 64, elem_id=self.elem_id_suffix("postprocess_multicrop_minarea")) + maxarea = gr.Slider(minimum=64 * 64, maximum=2048 * 2048, step=1, label="Area upper bound", value=640 * 640, elem_id=self.elem_id_suffix("postprocess_multicrop_maxarea")) with gr.Row(): - objective = gr.Radio(["Maximize area", "Minimize error"], value="Maximize area", label="Resizing objective", elem_id="postprocess_multicrop_objective") - threshold = gr.Slider(minimum=0, maximum=1, step=0.01, label="Error threshold", value=0.1, elem_id="postprocess_multicrop_threshold") + objective = gr.Radio(["Maximize area", "Minimize error"], value="Maximize area", label="Resizing objective", elem_id=self.elem_id_suffix("postprocess_multicrop_objective")) + threshold = gr.Slider(minimum=0, maximum=1, step=0.01, label="Error threshold", value=0.1, elem_id=self.elem_id_suffix("postprocess_multicrop_threshold")) return { "enable": enable, diff --git a/extensions-builtin/postprocessing-for-training/scripts/postprocessing_focal_crop.py b/extensions-builtin/postprocessing-for-training/scripts/postprocessing_focal_crop.py index cff1dbc5470..db835fb6b61 100644 --- a/extensions-builtin/postprocessing-for-training/scripts/postprocessing_focal_crop.py +++ b/extensions-builtin/postprocessing-for-training/scripts/postprocessing_focal_crop.py @@ -11,10 +11,10 @@ class ScriptPostprocessingFocalCrop(scripts_postprocessing.ScriptPostprocessing) def ui(self): with ui_components.InputAccordion(False, label="Auto focal point crop") as enable: - face_weight = gr.Slider(label='Focal point face weight', value=0.9, minimum=0.0, maximum=1.0, step=0.05, elem_id="postprocess_focal_crop_face_weight") - entropy_weight = gr.Slider(label='Focal point entropy weight', value=0.15, minimum=0.0, maximum=1.0, step=0.05, elem_id="postprocess_focal_crop_entropy_weight") - edges_weight = gr.Slider(label='Focal point edges weight', value=0.5, minimum=0.0, maximum=1.0, step=0.05, elem_id="postprocess_focal_crop_edges_weight") - debug = gr.Checkbox(label='Create debug image', elem_id="train_process_focal_crop_debug") + face_weight = gr.Slider(label='Focal point face weight', value=0.9, minimum=0.0, maximum=1.0, step=0.05, elem_id=self.elem_id_suffix("postprocess_focal_crop_face_weight")) + entropy_weight = gr.Slider(label='Focal point entropy weight', value=0.15, minimum=0.0, maximum=1.0, step=0.05, elem_id=self.elem_id_suffix("postprocess_focal_crop_entropy_weight")) + edges_weight = gr.Slider(label='Focal point edges weight', value=0.5, minimum=0.0, maximum=1.0, step=0.05, elem_id=self.elem_id_suffix("postprocess_focal_crop_edges_weight")) + debug = gr.Checkbox(label='Create debug image', elem_id=self.elem_id_suffix("train_process_focal_crop_debug")) return { "enable": enable, diff --git a/extensions-builtin/postprocessing-for-training/scripts/postprocessing_split_oversized.py b/extensions-builtin/postprocessing-for-training/scripts/postprocessing_split_oversized.py index 133e199b838..b569a06e373 100644 --- a/extensions-builtin/postprocessing-for-training/scripts/postprocessing_split_oversized.py +++ b/extensions-builtin/postprocessing-for-training/scripts/postprocessing_split_oversized.py @@ -35,8 +35,8 @@ class ScriptPostprocessingSplitOversized(scripts_postprocessing.ScriptPostproces def ui(self): with ui_components.InputAccordion(False, label="Split oversized images") as enable: with gr.Row(): - split_threshold = gr.Slider(label='Threshold', value=0.5, minimum=0.0, maximum=1.0, step=0.05, elem_id="postprocess_split_threshold") - overlap_ratio = gr.Slider(label='Overlap ratio', value=0.2, minimum=0.0, maximum=0.9, step=0.05, elem_id="postprocess_overlap_ratio") + split_threshold = gr.Slider(label='Threshold', value=0.5, minimum=0.0, maximum=1.0, step=0.05, elem_id=self.elem_id_suffix("postprocess_split_threshold")) + overlap_ratio = gr.Slider(label='Overlap ratio', value=0.2, minimum=0.0, maximum=0.9, step=0.05, elem_id=self.elem_id_suffix("postprocess_overlap_ratio")) return { "enable": enable, diff --git a/modules/scripts_auto_postprocessing.py b/modules/scripts_auto_postprocessing.py index 26fd138e736..cf981625034 100644 --- a/modules/scripts_auto_postprocessing.py +++ b/modules/scripts_auto_postprocessing.py @@ -13,6 +13,7 @@ def show(self, is_img2img): return scripts.AlwaysVisible def ui(self, is_img2img): + self.script.tab_name = '_img2img' if is_img2img else '_txt2img' self.postprocessing_controls = self.script.ui() return self.postprocessing_controls.values() diff --git a/modules/scripts_postprocessing.py b/modules/scripts_postprocessing.py index 131ba4b970d..70270c658d9 100644 --- a/modules/scripts_postprocessing.py +++ b/modules/scripts_postprocessing.py @@ -1,3 +1,4 @@ +import re import dataclasses import os import gradio as gr @@ -101,6 +102,31 @@ def process_firstpass(self, pp: PostprocessedImage, **args): def image_changed(self): pass + tab_name = '' # used by ScriptPostprocessingForMainUI + replace_pattern = re.compile(r'\s') + rm_pattern = re.compile(r'[^a-z_0-9]') + + def elem_id(self, item_id): + """ + Helper function to generate id for a HTML element + constructs final id out of script name and user-supplied item_id + 'script_extras_{self.name.lower()}_{item_id}' + {tab_name} will append to the end of the id if set + tab_name will be set to '_img2img' or '_txt2img' if use by ScriptPostprocessingForMainUI + + Extensions should use this function to generate element IDs + """ + return self.elem_id_suffix(f'extras_{self.name.lower()}_{item_id}') + + def elem_id_suffix(self, base_id): + """ + Append tab_name to the base_id + + Extensions that already have specific there element IDs and wish to keep their IDs the same when possible should use this function + """ + base_id = self.rm_pattern.sub('', self.replace_pattern.sub('_', base_id)) + return f'{base_id}{self.tab_name}' + def wrap_call(func, filename, funcname, *args, default=None, **kwargs): try: diff --git a/scripts/postprocessing_codeformer.py b/scripts/postprocessing_codeformer.py index 53a0cc44cde..f86e99a04db 100644 --- a/scripts/postprocessing_codeformer.py +++ b/scripts/postprocessing_codeformer.py @@ -12,8 +12,8 @@ class ScriptPostprocessingCodeFormer(scripts_postprocessing.ScriptPostprocessing def ui(self): with ui_components.InputAccordion(False, label="CodeFormer") as enable: with gr.Row(): - codeformer_visibility = gr.Slider(minimum=0.0, maximum=1.0, step=0.001, label="Visibility", value=1.0, elem_id="extras_codeformer_visibility") - codeformer_weight = gr.Slider(minimum=0.0, maximum=1.0, step=0.001, label="Weight (0 = maximum effect, 1 = minimum effect)", value=0, elem_id="extras_codeformer_weight") + codeformer_visibility = gr.Slider(minimum=0.0, maximum=1.0, step=0.001, label="Visibility", value=1.0, elem_id=self.elem_id_suffix("extras_codeformer_visibility")) + codeformer_weight = gr.Slider(minimum=0.0, maximum=1.0, step=0.001, label="Weight (0 = maximum effect, 1 = minimum effect)", value=0, elem_id=self.elem_id_suffix("extras_codeformer_weight")) return { "enable": enable, diff --git a/scripts/postprocessing_gfpgan.py b/scripts/postprocessing_gfpgan.py index 57e3623995c..3a130fd6336 100644 --- a/scripts/postprocessing_gfpgan.py +++ b/scripts/postprocessing_gfpgan.py @@ -11,7 +11,7 @@ class ScriptPostprocessingGfpGan(scripts_postprocessing.ScriptPostprocessing): def ui(self): with ui_components.InputAccordion(False, label="GFPGAN") as enable: - gfpgan_visibility = gr.Slider(minimum=0.0, maximum=1.0, step=0.001, label="Visibility", value=1.0, elem_id="extras_gfpgan_visibility") + gfpgan_visibility = gr.Slider(minimum=0.0, maximum=1.0, step=0.001, label="Visibility", value=1.0, elem_id=self.elem_id_suffix("extras_gfpgan_visibility")) return { "enable": enable, diff --git a/scripts/postprocessing_upscale.py b/scripts/postprocessing_upscale.py index 47a24b1b784..838872bf1b9 100644 --- a/scripts/postprocessing_upscale.py +++ b/scripts/postprocessing_upscale.py @@ -30,31 +30,31 @@ class ScriptPostprocessingUpscale(scripts_postprocessing.ScriptPostprocessing): def ui(self): selected_tab = gr.Number(value=0, visible=False) - with InputAccordion(True, label="Upscale", elem_id="extras_upscale") as upscale_enabled: + with InputAccordion(True, label="Upscale", elem_id=self.elem_id_suffix("extras_upscale")) as upscale_enabled: with FormRow(): - extras_upscaler_1 = gr.Dropdown(label='Upscaler 1', elem_id="extras_upscaler_1", choices=[x.name for x in shared.sd_upscalers], value=shared.sd_upscalers[0].name) + extras_upscaler_1 = gr.Dropdown(label='Upscaler 1', elem_id=self.elem_id_suffix("extras_upscaler_1"), choices=[x.name for x in shared.sd_upscalers], value=shared.sd_upscalers[0].name) with FormRow(): - extras_upscaler_2 = gr.Dropdown(label='Upscaler 2', elem_id="extras_upscaler_2", choices=[x.name for x in shared.sd_upscalers], value=shared.sd_upscalers[0].name) - extras_upscaler_2_visibility = gr.Slider(minimum=0.0, maximum=1.0, step=0.001, label="Upscaler 2 visibility", value=0.0, elem_id="extras_upscaler_2_visibility") + extras_upscaler_2 = gr.Dropdown(label='Upscaler 2', elem_id=self.elem_id_suffix("extras_upscaler_2"), choices=[x.name for x in shared.sd_upscalers], value=shared.sd_upscalers[0].name) + extras_upscaler_2_visibility = gr.Slider(minimum=0.0, maximum=1.0, step=0.001, label="Upscaler 2 visibility", value=0.0, elem_id=self.elem_id_suffix("extras_upscaler_2_visibility")) with FormRow(): - with gr.Tabs(elem_id="extras_resize_mode"): - with gr.TabItem('Scale by', elem_id="extras_scale_by_tab") as tab_scale_by: + with gr.Tabs(elem_id=self.elem_id_suffix("extras_resize_mode")): + with gr.TabItem('Scale by', elem_id=self.elem_id_suffix("extras_scale_by_tab")) as tab_scale_by: with gr.Row(): with gr.Column(scale=4): - upscaling_resize = gr.Slider(minimum=1.0, maximum=8.0, step=0.05, label="Resize", value=4, elem_id="extras_upscaling_resize") + upscaling_resize = gr.Slider(minimum=1.0, maximum=8.0, step=0.05, label="Resize", value=4, elem_id=self.elem_id_suffix("extras_upscaling_resize")) with gr.Column(scale=1, min_width=160): - max_side_length = gr.Number(label="Max side length", value=0, elem_id="extras_upscale_max_side_length", tooltip="If any of two sides of the image ends up larger than specified, will downscale it to fit. 0 = no limit.", min_width=160, step=8, minimum=0) + max_side_length = gr.Number(label="Max side length", value=0, elem_id=self.elem_id_suffix("extras_upscale_max_side_length"), tooltip="If any of two sides of the image ends up larger than specified, will downscale it to fit. 0 = no limit.", min_width=160, step=8, minimum=0) - with gr.TabItem('Scale to', elem_id="extras_scale_to_tab") as tab_scale_to: + with gr.TabItem('Scale to', elem_id=self.elem_id_suffix("extras_scale_to_tab")) as tab_scale_to: with FormRow(): - with gr.Column(elem_id="upscaling_column_size", scale=4): - upscaling_resize_w = gr.Slider(minimum=64, maximum=8192, step=8, label="Width", value=512, elem_id="extras_upscaling_resize_w") - upscaling_resize_h = gr.Slider(minimum=64, maximum=8192, step=8, label="Height", value=512, elem_id="extras_upscaling_resize_h") - with gr.Column(elem_id="upscaling_dimensions_row", scale=1, elem_classes="dimensions-tools"): - upscaling_res_switch_btn = ToolButton(value=switch_values_symbol, elem_id="upscaling_res_switch_btn", tooltip="Switch width/height") - upscaling_crop = gr.Checkbox(label='Crop to fit', value=True, elem_id="extras_upscaling_crop") + with gr.Column(elem_id=self.elem_id_suffix("upscaling_column_size"), scale=4): + upscaling_resize_w = gr.Slider(minimum=64, maximum=8192, step=8, label="Width", value=512, elem_id=self.elem_id_suffix("extras_upscaling_resize_w")) + upscaling_resize_h = gr.Slider(minimum=64, maximum=8192, step=8, label="Height", value=512, elem_id=self.elem_id_suffix("extras_upscaling_resize_h")) + with gr.Column(elem_id=self.elem_id_suffix("upscaling_dimensions_row"), scale=1, elem_classes="dimensions-tools"): + upscaling_res_switch_btn = ToolButton(value=switch_values_symbol, elem_id=self.elem_id_suffix("upscaling_res_switch_btn"), tooltip="Switch width/height") + upscaling_crop = gr.Checkbox(label='Crop to fit', value=True, elem_id=self.elem_id_suffix("extras_upscaling_crop")) def on_selected_upscale_method(upscale_method): if not shared.opts.set_scale_by_when_changing_upscaler: From 14c6d6cb3ac774eef1ac969bc5b712e878822871 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:45:45 +0900 Subject: [PATCH 06/19] Disable Hires checkpoint if same as First pass checkpoint (#16269) --- modules/processing.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/modules/processing.py b/modules/processing.py index 7535b56e18c..92c3582cc66 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -1259,7 +1259,10 @@ def init(self, all_prompts, all_seeds, all_subseeds): if self.hr_checkpoint_info is None: raise Exception(f'Could not find checkpoint with name {self.hr_checkpoint_name}') - self.extra_generation_params["Hires checkpoint"] = self.hr_checkpoint_info.short_title + if shared.sd_model.sd_checkpoint_info == self.hr_checkpoint_info: + self.hr_checkpoint_info = None + else: + self.extra_generation_params["Hires checkpoint"] = self.hr_checkpoint_info.short_title if self.hr_sampler_name is not None and self.hr_sampler_name != self.sampler_name: self.extra_generation_params["Hires sampler"] = self.hr_sampler_name From f31faf6f14a648fc698b112293c8c9506db17ea2 Mon Sep 17 00:00:00 2001 From: viking1304 Date: Tue, 29 Oct 2024 15:54:58 +0100 Subject: [PATCH 07/19] pyenv-win compatibility - another approach (#16287) --- webui.bat | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/webui.bat b/webui.bat index 7b162ce27cc..f29a40b36e9 100644 --- a/webui.bat +++ b/webui.bat @@ -4,7 +4,16 @@ if exist webui.settings.bat ( call webui.settings.bat ) -if not defined PYTHON (set PYTHON=python) +if not defined PYTHON ( + for /f "delims=" %%A in ('where python ^| findstr /n . ^| findstr ^^1:') do ( + if /i "%%~xA" == ".exe" ( + set PYTHON=python + ) else ( + set PYTHON=call python + ) + ) +) + if defined GIT (set "GIT_PYTHON_GIT_EXECUTABLE=%GIT%") if not defined VENV_DIR (set "VENV_DIR=%~dp0%venv") From 59481435f40b48b89d3565be9804255cf134d522 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:59:04 +0900 Subject: [PATCH 08/19] addEventListener {passive: false} (#16575) --- extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js | 2 +- javascript/contextMenus.js | 2 +- javascript/resizeHandle.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js index 7807f7f6185..b40c860275b 100644 --- a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js +++ b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js @@ -816,7 +816,7 @@ onUiLoaded(async() => { // Increase or decrease brush size based on scroll direction adjustBrushSize(elemId, e.deltaY); } - }); + }, {passive: false}); // Handle the move event for pan functionality. Updates the panX and panY variables and applies the new transform to the target element. function handleMoveKeyDown(e) { diff --git a/javascript/contextMenus.js b/javascript/contextMenus.js index e01fd67e80e..661343d524b 100644 --- a/javascript/contextMenus.js +++ b/javascript/contextMenus.js @@ -104,7 +104,7 @@ var contextMenuInit = function() { e.preventDefault(); } }); - }); + }, {passive: false}); }); eventListenerApplied = true; diff --git a/javascript/resizeHandle.js b/javascript/resizeHandle.js index 4aeb14b41f3..0283f21bfe1 100644 --- a/javascript/resizeHandle.js +++ b/javascript/resizeHandle.js @@ -124,7 +124,7 @@ } else { R.screenX = evt.changedTouches[0].screenX; } - }); + }, {passive: false}); }); resizeHandle.addEventListener('dblclick', onDoubleClick); From 5206b938efe617e7747472e12360db9096fa6eb0 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:03:21 +0900 Subject: [PATCH 09/19] InputAccordion duplicate elem_id handling (#16381) --- modules/ui_components.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/modules/ui_components.py b/modules/ui_components.py index 9cf67722a3d..3e3ea54bafe 100644 --- a/modules/ui_components.py +++ b/modules/ui_components.py @@ -91,6 +91,7 @@ class InputAccordion(gr.Checkbox): Actually just a hidden checkbox, but creates an accordion that follows and is followed by the state of the checkbox. """ + accordion_id_set = set() global_index = 0 def __init__(self, value, **kwargs): @@ -99,6 +100,18 @@ def __init__(self, value, **kwargs): self.accordion_id = f"input-accordion-{InputAccordion.global_index}" InputAccordion.global_index += 1 + if not InputAccordion.accordion_id_set: + from modules import script_callbacks + script_callbacks.on_script_unloaded(InputAccordion.reset) + + if self.accordion_id in InputAccordion.accordion_id_set: + count = 1 + while (unique_id := f'{self.accordion_id}-{count}') in InputAccordion.accordion_id_set: + count += 1 + self.accordion_id = unique_id + + InputAccordion.accordion_id_set.add(self.accordion_id) + kwargs_checkbox = { **kwargs, "elem_id": f"{self.accordion_id}-checkbox", @@ -143,3 +156,7 @@ def __exit__(self, exc_type, exc_val, exc_tb): def get_block_name(self): return "checkbox" + @classmethod + def reset(cls): + cls.global_index = 0 + cls.accordion_id_set.clear() From ac28cad998a5c6f09d1cd928f68d9f255ed72622 Mon Sep 17 00:00:00 2001 From: catboxanon <122327233+catboxanon@users.noreply.github.com> Date: Tue, 29 Oct 2024 11:49:09 -0400 Subject: [PATCH 10/19] Fix weighting config for SDXL v-pred Fixes a small oversight I made. --- configs/sd_xl_v.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configs/sd_xl_v.yaml b/configs/sd_xl_v.yaml index c755dc74fda..5adbc256c01 100644 --- a/configs/sd_xl_v.yaml +++ b/configs/sd_xl_v.yaml @@ -10,7 +10,7 @@ model: num_idx: 1000 weighting_config: - target: sgm.modules.diffusionmodules.denoiser_weighting.EpsWeighting + target: sgm.modules.diffusionmodules.denoiser_weighting.VWeighting scaling_config: target: sgm.modules.diffusionmodules.denoiser_scaling.VScaling discretization_config: From 533c7b7b7fb13889a25da570bf1369a998a9371b Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 30 Oct 2024 02:13:16 +0900 Subject: [PATCH 11/19] Fix Default system None filter logic (#16309) --- modules/ui_loadsave.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ui_loadsave.py b/modules/ui_loadsave.py index 0cc1ab82af4..95a776d947e 100644 --- a/modules/ui_loadsave.py +++ b/modules/ui_loadsave.py @@ -176,7 +176,7 @@ def iter_changes(self, current_ui_settings, values): if new_value == old_value: continue - if old_value is None and new_value == '' or new_value == []: + if old_value is None and (new_value == '' or new_value == []): continue yield path, old_value, new_value From 28323cf8ee90594d4a31bb58af2da160dfc85086 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 30 Oct 2024 02:18:38 +0900 Subject: [PATCH 12/19] XYZ option to disable grid (#16416) --- scripts/xyz_grid.py | 73 ++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/scripts/xyz_grid.py b/scripts/xyz_grid.py index c60dd6dda2f..5778d5f4c8d 100644 --- a/scripts/xyz_grid.py +++ b/scripts/xyz_grid.py @@ -20,7 +20,7 @@ import modules.sd_vae import re -from modules.ui_components import ToolButton +from modules.ui_components import ToolButton, InputAccordion fill_values_symbol = "\U0001f4d2" # 📒 @@ -285,7 +285,7 @@ def __init__(self, *args, **kwargs): ] -def draw_xyz_grid(p, xs, ys, zs, x_labels, y_labels, z_labels, cell, draw_legend, include_lone_images, include_sub_grids, first_axes_processed, second_axes_processed, margin_size): +def draw_xyz_grid(p, xs, ys, zs, x_labels, y_labels, z_labels, cell, draw_legend, include_lone_images, include_sub_grids, first_axes_processed, second_axes_processed, margin_size, draw_grid): hor_texts = [[images.GridAnnotation(x)] for x in x_labels] ver_texts = [[images.GridAnnotation(y)] for y in y_labels] title_texts = [[images.GridAnnotation(z)] for z in z_labels] @@ -370,29 +370,30 @@ def index(ix, iy, iz): print("Unexpected error: draw_xyz_grid failed to return even a single processed image") return Processed(p, []) - z_count = len(zs) + if draw_grid: + z_count = len(zs) - for i in range(z_count): - start_index = (i * len(xs) * len(ys)) + i - end_index = start_index + len(xs) * len(ys) - grid = images.image_grid(processed_result.images[start_index:end_index], rows=len(ys)) + for i in range(z_count): + start_index = (i * len(xs) * len(ys)) + i + end_index = start_index + len(xs) * len(ys) + grid = images.image_grid(processed_result.images[start_index:end_index], rows=len(ys)) + if draw_legend: + grid_max_w, grid_max_h = map(max, zip(*(img.size for img in processed_result.images[start_index:end_index]))) + grid = images.draw_grid_annotations(grid, grid_max_w, grid_max_h, hor_texts, ver_texts, margin_size) + processed_result.images.insert(i, grid) + processed_result.all_prompts.insert(i, processed_result.all_prompts[start_index]) + processed_result.all_seeds.insert(i, processed_result.all_seeds[start_index]) + processed_result.infotexts.insert(i, processed_result.infotexts[start_index]) + + z_grid = images.image_grid(processed_result.images[:z_count], rows=1) + z_sub_grid_max_w, z_sub_grid_max_h = map(max, zip(*(img.size for img in processed_result.images[:z_count]))) if draw_legend: - grid_max_w, grid_max_h = map(max, zip(*(img.size for img in processed_result.images[start_index:end_index]))) - grid = images.draw_grid_annotations(grid, grid_max_w, grid_max_h, hor_texts, ver_texts, margin_size) - processed_result.images.insert(i, grid) - processed_result.all_prompts.insert(i, processed_result.all_prompts[start_index]) - processed_result.all_seeds.insert(i, processed_result.all_seeds[start_index]) - processed_result.infotexts.insert(i, processed_result.infotexts[start_index]) - - z_grid = images.image_grid(processed_result.images[:z_count], rows=1) - z_sub_grid_max_w, z_sub_grid_max_h = map(max, zip(*(img.size for img in processed_result.images[:z_count]))) - if draw_legend: - z_grid = images.draw_grid_annotations(z_grid, z_sub_grid_max_w, z_sub_grid_max_h, title_texts, [[images.GridAnnotation()]]) - processed_result.images.insert(0, z_grid) - # TODO: Deeper aspects of the program rely on grid info being misaligned between metadata arrays, which is not ideal. - # processed_result.all_prompts.insert(0, processed_result.all_prompts[0]) - # processed_result.all_seeds.insert(0, processed_result.all_seeds[0]) - processed_result.infotexts.insert(0, processed_result.infotexts[0]) + z_grid = images.draw_grid_annotations(z_grid, z_sub_grid_max_w, z_sub_grid_max_h, title_texts, [[images.GridAnnotation()]]) + processed_result.images.insert(0, z_grid) + # TODO: Deeper aspects of the program rely on grid info being misaligned between metadata arrays, which is not ideal. + # processed_result.all_prompts.insert(0, processed_result.all_prompts[0]) + # processed_result.all_seeds.insert(0, processed_result.all_seeds[0]) + processed_result.infotexts.insert(0, processed_result.infotexts[0]) return processed_result @@ -442,7 +443,6 @@ def ui(self, is_img2img): with gr.Row(variant="compact", elem_id="axis_options"): with gr.Column(): - draw_legend = gr.Checkbox(label='Draw legend', value=True, elem_id=self.elem_id("draw_legend")) no_fixed_seeds = gr.Checkbox(label='Keep -1 for seeds', value=False, elem_id=self.elem_id("no_fixed_seeds")) with gr.Row(): vary_seeds_x = gr.Checkbox(label='Vary seeds for X', value=False, min_width=80, elem_id=self.elem_id("vary_seeds_x"), tooltip="Use different seeds for images along X axis.") @@ -450,9 +450,12 @@ def ui(self, is_img2img): vary_seeds_z = gr.Checkbox(label='Vary seeds for Z', value=False, min_width=80, elem_id=self.elem_id("vary_seeds_z"), tooltip="Use different seeds for images along Z axis.") with gr.Column(): include_lone_images = gr.Checkbox(label='Include Sub Images', value=False, elem_id=self.elem_id("include_lone_images")) - include_sub_grids = gr.Checkbox(label='Include Sub Grids', value=False, elem_id=self.elem_id("include_sub_grids")) csv_mode = gr.Checkbox(label='Use text inputs instead of dropdowns', value=False, elem_id=self.elem_id("csv_mode")) - with gr.Column(): + + with InputAccordion(True, label='Draw grid', elem_id=self.elem_id('draw_grid')) as draw_grid: + with gr.Row(): + include_sub_grids = gr.Checkbox(label='Include Sub Grids', value=False, elem_id=self.elem_id("include_sub_grids")) + draw_legend = gr.Checkbox(label='Draw legend', value=True, elem_id=self.elem_id("draw_legend")) margin_size = gr.Slider(label="Grid margins (px)", minimum=0, maximum=500, value=0, step=2, elem_id=self.elem_id("margin_size")) with gr.Row(variant="compact", elem_id="swap_axes"): @@ -534,9 +537,9 @@ def get_dropdown_update_from_params(axis, params): (z_values_dropdown, lambda params: get_dropdown_update_from_params("Z", params)), ) - return [x_type, x_values, x_values_dropdown, y_type, y_values, y_values_dropdown, z_type, z_values, z_values_dropdown, draw_legend, include_lone_images, include_sub_grids, no_fixed_seeds, vary_seeds_x, vary_seeds_y, vary_seeds_z, margin_size, csv_mode] + return [x_type, x_values, x_values_dropdown, y_type, y_values, y_values_dropdown, z_type, z_values, z_values_dropdown, draw_legend, include_lone_images, include_sub_grids, no_fixed_seeds, vary_seeds_x, vary_seeds_y, vary_seeds_z, margin_size, csv_mode, draw_grid] - def run(self, p, x_type, x_values, x_values_dropdown, y_type, y_values, y_values_dropdown, z_type, z_values, z_values_dropdown, draw_legend, include_lone_images, include_sub_grids, no_fixed_seeds, vary_seeds_x, vary_seeds_y, vary_seeds_z, margin_size, csv_mode): + def run(self, p, x_type, x_values, x_values_dropdown, y_type, y_values, y_values_dropdown, z_type, z_values, z_values_dropdown, draw_legend, include_lone_images, include_sub_grids, no_fixed_seeds, vary_seeds_x, vary_seeds_y, vary_seeds_z, margin_size, csv_mode, draw_grid): x_type, y_type, z_type = x_type or 0, y_type or 0, z_type or 0 # if axle type is None set to 0 if not no_fixed_seeds: @@ -781,7 +784,8 @@ def cell(x, y, z, ix, iy, iz): include_sub_grids=include_sub_grids, first_axes_processed=first_axes_processed, second_axes_processed=second_axes_processed, - margin_size=margin_size + margin_size=margin_size, + draw_grid=draw_grid, ) if not processed.images: @@ -790,14 +794,15 @@ def cell(x, y, z, ix, iy, iz): z_count = len(zs) - # Set the grid infotexts to the real ones with extra_generation_params (1 main grid + z_count sub-grids) - processed.infotexts[:1 + z_count] = grid_infotext[:1 + z_count] + if draw_grid: + # Set the grid infotexts to the real ones with extra_generation_params (1 main grid + z_count sub-grids) + processed.infotexts[:1 + z_count] = grid_infotext[:1 + z_count] if not include_lone_images: # Don't need sub-images anymore, drop from list: - processed.images = processed.images[:z_count + 1] + processed.images = processed.images[:z_count + 1] if draw_grid else [] - if opts.grid_save: + if draw_grid and opts.grid_save: # Auto-save main and sub-grids: grid_count = z_count + 1 if z_count > 1 else 1 for g in range(grid_count): @@ -807,7 +812,7 @@ def cell(x, y, z, ix, iy, iz): if not include_sub_grids: # if not include_sub_grids then skip saving after the first grid break - if not include_sub_grids: + if draw_grid and not include_sub_grids: # Done with sub-grids, drop all related information: for _ in range(z_count): del processed.images[1] From e6f36d9cdc42d8008be482d449e85e737f3b989d Mon Sep 17 00:00:00 2001 From: catboxanon <122327233+catboxanon@users.noreply.github.com> Date: Tue, 29 Oct 2024 13:27:32 -0400 Subject: [PATCH 13/19] sd_xl_v.yaml: use_checkpoint = False In accordance with https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/15803 --- configs/sd_xl_v.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configs/sd_xl_v.yaml b/configs/sd_xl_v.yaml index 5adbc256c01..9fbcbbac8b2 100644 --- a/configs/sd_xl_v.yaml +++ b/configs/sd_xl_v.yaml @@ -21,7 +21,7 @@ model: params: adm_in_channels: 2816 num_classes: sequential - use_checkpoint: True + use_checkpoint: False in_channels: 4 out_channels: 4 model_channels: 320 From 91de9194512dbf654999bd58be827434ed5cba90 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 30 Oct 2024 22:34:37 +0900 Subject: [PATCH 14/19] Warn if WebUI is installed under a dot directory (#16584) --- webui.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/webui.py b/webui.py index 2c417168aa6..421e3b8334f 100644 --- a/webui.py +++ b/webui.py @@ -45,6 +45,44 @@ def api_only(): ) +def warning_if_invalid_install_dir(): + """ + Shows a warning if the webui is installed under a path that contains a leading dot in any of its parent directories. + + Gradio '/file=' route will block access to files that have a leading dot in the path segments. + We use this route to serve files such as JavaScript and CSS to the webpage, + if those files are blocked, the webpage will not function properly. + See https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/13292 + + This is a security feature was added to Gradio 3.32.0 and is removed in later versions, + this function replicates Gradio file access blocking logic. + + This check should be removed when it's no longer applicable. + """ + from packaging.version import parse + from pathlib import Path + import gradio + + if parse('3.32.0') <= parse(gradio.__version__) < parse('4'): + + def abspath(path): + """modified from Gradio 3.41.2 gradio.utils.abspath()""" + if path.is_absolute(): + return path + is_symlink = path.is_symlink() or any(parent.is_symlink() for parent in path.parents) + return Path.cwd() / path if (is_symlink or path == path.resolve()) else path.resolve() + + webui_root = Path(__file__).parent + if any(part.startswith(".") for part in abspath(webui_root).parts): + print(f'''{"!"*25} Warning {"!"*25} +WebUI is installed in a directory that has a leading dot (.) in one of its parent directories. +This will prevent WebUI from functioning properly. +Please move the installation to a different directory. +Current path: "{webui_root}" +For more information see: https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/13292 +{"!"*25} Warning {"!"*25}''') + + def webui(): from modules.shared_cmd_options import cmd_opts @@ -53,6 +91,8 @@ def webui(): from modules import shared, ui_tempdir, script_callbacks, ui, progress, ui_extra_networks + warning_if_invalid_install_dir() + while 1: if shared.opts.clean_temp_dir_at_start: ui_tempdir.cleanup_tmpdr() From 1b16c6260819a438d2f7d65110813d227aff9058 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Thu, 31 Oct 2024 02:01:32 +0900 Subject: [PATCH 15/19] use shared.hf_endpoint (#16611) --- modules/dat_model.py | 8 ++++---- modules/models/sd3/sd3_cond.py | 6 +++--- modules/sd_disable_initialization.py | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/dat_model.py b/modules/dat_model.py index 298d160d1e8..e256a5a3282 100644 --- a/modules/dat_model.py +++ b/modules/dat_model.py @@ -1,7 +1,7 @@ import os from modules import modelloader, errors -from modules.shared import cmd_opts, opts +from modules.shared import cmd_opts, opts, hf_endpoint from modules.upscaler import Upscaler, UpscalerData from modules.upscaler_utils import upscale_with_model @@ -71,21 +71,21 @@ def get_dat_models(scaler): return [ UpscalerData( name="DAT x2", - path="https://huggingface.co/w-e-w/DAT/resolve/main/experiments/pretrained_models/DAT/DAT_x2.pth", + path=f"{hf_endpoint}/w-e-w/DAT/resolve/main/experiments/pretrained_models/DAT/DAT_x2.pth", scale=2, upscaler=scaler, sha256='7760aa96e4ee77e29d4f89c3a4486200042e019461fdb8aa286f49aa00b89b51', ), UpscalerData( name="DAT x3", - path="https://huggingface.co/w-e-w/DAT/resolve/main/experiments/pretrained_models/DAT/DAT_x3.pth", + path=f"{hf_endpoint}/w-e-w/DAT/resolve/main/experiments/pretrained_models/DAT/DAT_x3.pth", scale=3, upscaler=scaler, sha256='581973e02c06f90d4eb90acf743ec9604f56f3c2c6f9e1e2c2b38ded1f80d197', ), UpscalerData( name="DAT x4", - path="https://huggingface.co/w-e-w/DAT/resolve/main/experiments/pretrained_models/DAT/DAT_x4.pth", + path=f"{hf_endpoint}/w-e-w/DAT/resolve/main/experiments/pretrained_models/DAT/DAT_x4.pth", scale=4, upscaler=scaler, sha256='391a6ce69899dff5ea3214557e9d585608254579217169faf3d4c353caff049e', diff --git a/modules/models/sd3/sd3_cond.py b/modules/models/sd3/sd3_cond.py index 325c512d594..6a43f569bea 100644 --- a/modules/models/sd3/sd3_cond.py +++ b/modules/models/sd3/sd3_cond.py @@ -24,7 +24,7 @@ def __getitem__(self, key): return self.file.get_tensor(key) -CLIPL_URL = "https://huggingface.co/AUTOMATIC/stable-diffusion-3-medium-text-encoders/resolve/main/clip_l.safetensors" +CLIPL_URL = f"{shared.hf_endpoint}/AUTOMATIC/stable-diffusion-3-medium-text-encoders/resolve/main/clip_l.safetensors" CLIPL_CONFIG = { "hidden_act": "quick_gelu", "hidden_size": 768, @@ -33,7 +33,7 @@ def __getitem__(self, key): "num_hidden_layers": 12, } -CLIPG_URL = "https://huggingface.co/AUTOMATIC/stable-diffusion-3-medium-text-encoders/resolve/main/clip_g.safetensors" +CLIPG_URL = f"{shared.hf_endpoint}/AUTOMATIC/stable-diffusion-3-medium-text-encoders/resolve/main/clip_g.safetensors" CLIPG_CONFIG = { "hidden_act": "gelu", "hidden_size": 1280, @@ -43,7 +43,7 @@ def __getitem__(self, key): "textual_inversion_key": "clip_g", } -T5_URL = "https://huggingface.co/AUTOMATIC/stable-diffusion-3-medium-text-encoders/resolve/main/t5xxl_fp16.safetensors" +T5_URL = f"{shared.hf_endpoint}/AUTOMATIC/stable-diffusion-3-medium-text-encoders/resolve/main/t5xxl_fp16.safetensors" T5_CONFIG = { "d_ff": 10240, "d_model": 4096, diff --git a/modules/sd_disable_initialization.py b/modules/sd_disable_initialization.py index 273a7edd8b4..3750e85e906 100644 --- a/modules/sd_disable_initialization.py +++ b/modules/sd_disable_initialization.py @@ -76,7 +76,7 @@ def transformers_modeling_utils_load_pretrained_model(*args, **kwargs): def transformers_utils_hub_get_file_from_cache(original, url, *args, **kwargs): # this file is always 404, prevent making request - if url == 'https://huggingface.co/openai/clip-vit-large-patch14/resolve/main/added_tokens.json' or url == 'openai/clip-vit-large-patch14' and args[0] == 'added_tokens.json': + if url == f'{shared.hf_endpoint}/openai/clip-vit-large-patch14/resolve/main/added_tokens.json' or url == 'openai/clip-vit-large-patch14' and args[0] == 'added_tokens.json': return None try: From ca3bedbd02d9b9837641b0e58345f63eac52a8cc Mon Sep 17 00:00:00 2001 From: catboxanon <122327233+catboxanon@users.noreply.github.com> Date: Fri, 1 Nov 2024 11:32:52 -0400 Subject: [PATCH 16/19] Honor lossless webp compression option in API --- modules/api/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/api/api.py b/modules/api/api.py index 97ec7514ea1..db2fc648019 100644 --- a/modules/api/api.py +++ b/modules/api/api.py @@ -122,7 +122,7 @@ def encode_pil_to_base64(image): if opts.samples_format.lower() in ("jpg", "jpeg"): image.save(output_bytes, format="JPEG", exif = exif_bytes, quality=opts.jpeg_quality) else: - image.save(output_bytes, format="WEBP", exif = exif_bytes, quality=opts.jpeg_quality) + image.save(output_bytes, format="WEBP", exif = exif_bytes, quality=opts.jpeg_quality, lossless=opts.webp_lossless) else: raise HTTPException(status_code=500, detail="Invalid image format") From d2c9efb1f3ebd53893c9ce98c5a0f36d9d6ac5a9 Mon Sep 17 00:00:00 2001 From: catboxanon <122327233+catboxanon@users.noreply.github.com> Date: Mon, 18 Nov 2024 20:48:36 -0500 Subject: [PATCH 17/19] Bump safetensors to v0.4.5 Resolves #16650 --- requirements_versions.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements_versions.txt b/requirements_versions.txt index 0306ce94fda..389e2af33b0 100644 --- a/requirements_versions.txt +++ b/requirements_versions.txt @@ -22,7 +22,7 @@ protobuf==3.20.0 psutil==5.9.5 pytorch_lightning==1.9.4 resize-right==0.0.2 -safetensors==0.4.2 +safetensors==0.4.5 scikit-image==0.21.0 spandrel==0.3.4 spandrel-extra-arches==0.1.1 From cd869bb7a35e6a3eb09b8fb48873e583f9e2eaf6 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Thu, 21 Nov 2024 02:29:41 +0900 Subject: [PATCH 18/19] fix prompt-bracket-checker miscounting of literal tokens (#16669) --- .../prompt-bracket-checker/javascript/prompt-bracket-checker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js b/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js index 114cf94ccbf..f1f42fe7f0a 100644 --- a/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js +++ b/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js @@ -5,7 +5,7 @@ function checkBrackets(textArea, counterElt) { var counts = {}; - (textArea.value.match(/[(){}[\]]/g) || []).forEach(bracket => { + (textArea.value.match(/(? { counts[bracket] = (counts[bracket] || 0) + 1; }); var errors = []; From 023454b49e6bf2a98804b91a12f84af2f653d128 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Thu, 21 Nov 2024 11:33:59 +0900 Subject: [PATCH 19/19] fix passing of literal backslash (#16671) --- .../javascript/prompt-bracket-checker.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js b/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js index f1f42fe7f0a..a59e584c26d 100644 --- a/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js +++ b/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js @@ -4,11 +4,11 @@ // If there's a mismatch, the keyword counter turns red and if you hover on it, a tooltip tells you what's wrong. function checkBrackets(textArea, counterElt) { - var counts = {}; - (textArea.value.match(/(? { - counts[bracket] = (counts[bracket] || 0) + 1; + const counts = {}; + textArea.value.matchAll(/(? { + counts[bracket[1]] = (counts[bracket[1]] || 0) + 1; }); - var errors = []; + const errors = []; function checkPair(open, close, kind) { if (counts[open] !== counts[close]) {