diff --git a/.markdownlint.json b/.markdownlint.json new file mode 100644 index 000000000..c9179cd2f --- /dev/null +++ b/.markdownlint.json @@ -0,0 +1,7 @@ +{ + "MD012": false, + "MD013": false, + "MD033": false, + "MD036": false, + "MD041": false +} \ No newline at end of file diff --git a/.pylintrc b/.pylintrc index 1eebcbbdc..d139e777d 100644 --- a/.pylintrc +++ b/.pylintrc @@ -133,6 +133,7 @@ disable=raw-checker-failed, missing-class-docstring, logging-fstring-interpolation, import-outside-toplevel, + consider-iterating-dictionary, enable=c-extension-no-member [METHOD_ARGS] diff --git a/README.md b/README.md index 51295ec42..530d6e097 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ Fork adds extra functionality: - [Dynamic Thresholding](https://github.com/mcmonkeyprojects/sd-dynamic-thresholding) - [Steps Animation](https://github.com/vladmandic/sd-extension-steps-animation) - [Seed Travel](https://github.com/yownas/seed_travel) +- [Model Keyword](https://github.com/mix1009/model-keyword)
diff --git a/extensions-builtin/sd-extension-system-info b/extensions-builtin/sd-extension-system-info index fc50810f5..50e74e11e 160000 --- a/extensions-builtin/sd-extension-system-info +++ b/extensions-builtin/sd-extension-system-info @@ -1 +1 @@ -Subproject commit fc50810f5fbc3be86fad55a3e13327e77730e321 +Subproject commit 50e74e11e32212437cb1b88846f63e92dcb4a92d diff --git a/extensions-builtin/sd-webui-controlnet b/extensions-builtin/sd-webui-controlnet index e5b565e27..d575ccf5d 160000 --- a/extensions-builtin/sd-webui-controlnet +++ b/extensions-builtin/sd-webui-controlnet @@ -1 +1 @@ -Subproject commit e5b565e27f0fdda34d1ccbdbe4d6ab07d3d2fc67 +Subproject commit d575ccf5d63358d637224ead6ae62e944585cf4e diff --git a/modules/call_queue.py b/modules/call_queue.py index 833a0bb2f..9582585d9 100644 --- a/modules/call_queue.py +++ b/modules/call_queue.py @@ -1,7 +1,9 @@ import html import threading import time -import cProfile, pstats, io +import cProfile +import pstats +import io from modules import shared, progress, errors @@ -103,4 +105,3 @@ def wrap_gradio_call(func, extra_outputs=None, add_stats=False): return tuple(res) return f - diff --git a/modules/cmd_args.py b/modules/cmd_args.py index 8bc457060..3a79f53d7 100644 --- a/modules/cmd_args.py +++ b/modules/cmd_args.py @@ -1,6 +1,6 @@ import argparse import os -from modules.paths_internal import models_path, script_path, data_path, extensions_dir, extensions_builtin_dir, sd_default_config, sd_model_file +from modules.paths_internal import data_path, sd_default_config, sd_model_file parser = argparse.ArgumentParser(description="Stable Diffusion", formatter_class=lambda prog: argparse.HelpFormatter(prog,max_help_position=55,indent_increment=2,width=200)) diff --git a/modules/codeformer_model.py b/modules/codeformer_model.py index c33f12773..cbe06ec1e 100644 --- a/modules/codeformer_model.py +++ b/modules/codeformer_model.py @@ -5,7 +5,6 @@ import cv2 import torch import modules.face_restoration -import modules.shared from modules import shared, devices, modelloader, errors from modules.paths import models_path @@ -32,7 +31,6 @@ def setup_model(dirname): try: from torchvision.transforms.functional import normalize from modules.codeformer.codeformer_arch import CodeFormer - from basicsr.utils.download_util import load_file_from_url from basicsr.utils import imwrite, img2tensor, tensor2img from facelib.utils.face_restoration_helper import FaceRestoreHelper from facelib.detection.retinaface import retinaface diff --git a/modules/deepbooru.py b/modules/deepbooru.py index 122fce7f5..1c4554a20 100644 --- a/modules/deepbooru.py +++ b/modules/deepbooru.py @@ -2,7 +2,6 @@ import os import re import torch -from PIL import Image import numpy as np from modules import modelloader, paths, deepbooru_model, devices, images, shared diff --git a/modules/deepbooru_model.py b/modules/deepbooru_model.py index 83d2ff090..c2c77cd25 100644 --- a/modules/deepbooru_model.py +++ b/modules/deepbooru_model.py @@ -675,4 +675,3 @@ class DeepDanbooruModel(nn.Module): self.tags = state_dict.get('tags', []) super(DeepDanbooruModel, self).load_state_dict({k: v for k, v in state_dict.items() if k != 'tags'}) - diff --git a/modules/errors.py b/modules/errors.py index ab6d90f90..ebe2c03c2 100644 --- a/modules/errors.py +++ b/modules/errors.py @@ -44,7 +44,7 @@ def run(code, task): try: code() except Exception as e: - display(task, e) + display(e, task) def exception(): diff --git a/modules/esrgan_model.py b/modules/esrgan_model.py index 9a9c38f1f..bb4c6619b 100644 --- a/modules/esrgan_model.py +++ b/modules/esrgan_model.py @@ -6,7 +6,7 @@ from PIL import Image from basicsr.utils.download_util import load_file_from_url import modules.esrgan_model_arch as arch -from modules import shared, modelloader, images, devices +from modules import modelloader, images, devices from modules.upscaler import Upscaler, UpscalerData from modules.shared import opts @@ -118,7 +118,7 @@ def infer_params(state_dict): nf = state_dict["model.0.weight"].shape[0] in_nc = state_dict["model.0.weight"].shape[1] - out_nc = out_nc + # out_nc = out_nc scale = 2 ** scale2x return in_nc, out_nc, nf, nb, plus, scale diff --git a/modules/esrgan_model_arch.py b/modules/esrgan_model_arch.py index 954c70de5..411d98d38 100644 --- a/modules/esrgan_model_arch.py +++ b/modules/esrgan_model_arch.py @@ -1,8 +1,6 @@ # this file is adapted from https://github.com/victorca25/iNNfer -from collections import OrderedDict import math -import functools import torch import torch.nn as nn import torch.nn.functional as F diff --git a/modules/extensions.py b/modules/extensions.py index ea9d8a937..df5007ff0 100644 --- a/modules/extensions.py +++ b/modules/extensions.py @@ -1,6 +1,4 @@ import os -import sys - import time import git diff --git a/modules/extra_networks.py b/modules/extra_networks.py index 1978673d7..3170bed4a 100644 --- a/modules/extra_networks.py +++ b/modules/extra_networks.py @@ -74,7 +74,7 @@ def activate(p, extra_network_data): try: extra_network.activate(p, extra_network_args) except Exception as e: - errors.display(e, f"activating extra network {extra_network_name} with arguments {extra_network_args}") + errors.display(e, f"Error activating extra network {extra_network_name} with arguments {extra_network_args}") for extra_network_name, extra_network in extra_network_registry.items(): args = extra_network_data.get(extra_network_name, None) @@ -84,14 +84,14 @@ def activate(p, extra_network_data): try: extra_network.activate(p, []) except Exception as e: - errors.display(e, f"activating extra network {extra_network_name}") + errors.display(e, f"Error activating extra network {extra_network_name}") def deactivate(p, extra_network_data): """call deactivate for extra networks in extra_network_data in specified order, then call deactivate for all remaining registered networks""" - for extra_network_name, extra_network_args in extra_network_data.items(): + for extra_network_name, _extra_network_args in extra_network_data.items(): extra_network = extra_network_registry.get(extra_network_name, None) if extra_network is None: continue @@ -99,7 +99,7 @@ def deactivate(p, extra_network_data): try: extra_network.deactivate(p) except Exception as e: - errors.display(e, f"deactivating extra network {extra_network_name}") + errors.display(e, f"Error deactivating extra network {extra_network_name}") for extra_network_name, extra_network in extra_network_registry.items(): args = extra_network_data.get(extra_network_name, None) @@ -109,7 +109,7 @@ def deactivate(p, extra_network_data): try: extra_network.deactivate(p) except Exception as e: - errors.display(e, f"deactivating unmentioned extra network {extra_network_name}") + errors.display(e, f"Error deactivating unmentioned extra network {extra_network_name}") re_extra_net = re.compile(r"<(\w+):([^>]+)>") @@ -144,4 +144,3 @@ def parse_prompts(prompts): res.append(updated_prompt) return res, extra_data - diff --git a/modules/extra_networks_hypernet.py b/modules/extra_networks_hypernet.py index d3a4d7adc..8594670f7 100644 --- a/modules/extra_networks_hypernet.py +++ b/modules/extra_networks_hypernet.py @@ -1,4 +1,4 @@ -from modules import extra_networks, shared, extra_networks +from modules import extra_networks, shared from modules.hypernetworks import hypernetwork diff --git a/modules/generation_parameters_copypaste.py b/modules/generation_parameters_copypaste.py index dad428848..f960b709f 100644 --- a/modules/generation_parameters_copypaste.py +++ b/modules/generation_parameters_copypaste.py @@ -1,15 +1,11 @@ import base64 -import html import io -import math import os import re -from pathlib import Path import gradio as gr from modules.paths import data_path from modules import shared, ui_tempdir, script_callbacks -import tempfile from PIL import Image re_param_code = r'\s*([\w ]+):\s*("(?:\\"[^,]|\\"|\\|[^\"])+"|[^,]*)(?:,|$)' diff --git a/modules/gfpgan_model.py b/modules/gfpgan_model.py index 0b105ef73..9f332a730 100644 --- a/modules/gfpgan_model.py +++ b/modules/gfpgan_model.py @@ -1,5 +1,4 @@ import os -import sys import modules.face_restoration from modules import paths, shared, devices, modelloader, errors diff --git a/modules/hashes.py b/modules/hashes.py index 83272a078..95d60ca8b 100644 --- a/modules/hashes.py +++ b/modules/hashes.py @@ -84,8 +84,3 @@ def sha256(filename, title): dump_cache() return sha256_value - - - - - diff --git a/modules/images.py b/modules/images.py index 046ae37c2..6c4394cfb 100644 --- a/modules/images.py +++ b/modules/images.py @@ -1,5 +1,4 @@ import datetime -import sys import pytz import io diff --git a/modules/img2img.py b/modules/img2img.py index f1ae874b9..640ef671f 100644 --- a/modules/img2img.py +++ b/modules/img2img.py @@ -1,6 +1,4 @@ -import math import os -import sys import numpy as np from PIL import Image, ImageOps, ImageFilter, ImageEnhance, ImageChops diff --git a/modules/interrogate.py b/modules/interrogate.py index 66ae32e85..4624e5a3a 100644 --- a/modules/interrogate.py +++ b/modules/interrogate.py @@ -10,7 +10,6 @@ import torch.hub from torchvision import transforms from torchvision.transforms.functional import InterpolationMode -import modules.shared as shared from modules import devices, paths, shared, lowvram, modelloader, errors blip_image_eval_size = 384 diff --git a/modules/localization.py b/modules/localization.py index 9aed5a971..5b58f9e8c 100644 --- a/modules/localization.py +++ b/modules/localization.py @@ -1,7 +1,6 @@ import json import os import sys -import modules.shared as shared import modules.errors as errors @@ -11,7 +10,7 @@ localizations = {} def list_localizations(dirname): localizations.clear() return localizations - + """ for file in os.listdir(dirname): fn, ext = os.path.splitext(file) if ext.lower() != ".json": @@ -23,7 +22,7 @@ def list_localizations(dirname): for file in scripts.list_scripts("localizations", ".json"): fn, ext = os.path.splitext(file.filename) localizations[fn] = file.path - + """ def localization_js(current_localization_name): fn = localizations.get(current_localization_name, None) diff --git a/modules/mac_specific.py b/modules/mac_specific.py index 6fe8dea07..c8a534d0e 100644 --- a/modules/mac_specific.py +++ b/modules/mac_specific.py @@ -1,6 +1,5 @@ import torch import platform -from modules import paths from modules.sd_hijack_utils import CondFunc from packaging import version diff --git a/modules/paths.py b/modules/paths.py index 4a3073295..a4230c9a7 100644 --- a/modules/paths.py +++ b/modules/paths.py @@ -1,9 +1,16 @@ import os import sys -from modules.paths_internal import models_path, script_path, data_path, extensions_dir, extensions_builtin_dir - -import modules.safe +import modules.paths_internal +data_path = modules.paths_internal.data_path +script_path = modules.paths_internal.script_path +models_path = modules.paths_internal.models_path +sd_configs_path = modules.paths_internal.sd_configs_path +sd_default_config = modules.paths_internal.sd_default_config +sd_model_file = modules.paths_internal.sd_model_file +default_sd_model_file = modules.paths_internal.default_sd_model_file +extensions_dir = modules.paths_internal.extensions_dir +extensions_builtin_dir = modules.paths_internal.extensions_builtin_dir # data_path = cmd_opts_pre.data sys.path.insert(0, script_path) diff --git a/modules/paths_internal.py b/modules/paths_internal.py index 926ec3bbf..dcb8b79d2 100644 --- a/modules/paths_internal.py +++ b/modules/paths_internal.py @@ -4,7 +4,6 @@ import argparse import os script_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) - sd_configs_path = os.path.join(script_path, "configs") sd_default_config = os.path.join(sd_configs_path, "v1-inference.yaml") sd_model_file = os.path.join(script_path, 'model.ckpt') @@ -14,7 +13,6 @@ default_sd_model_file = sd_model_file parser_pre = argparse.ArgumentParser(add_help=False) parser_pre.add_argument("--data-dir", type=str, default=os.path.dirname(os.path.dirname(os.path.realpath(__file__))), help="base path where all user data is stored",) cmd_opts_pre = parser_pre.parse_known_args()[0] - data_path = cmd_opts_pre.data_dir models_path = os.path.join(data_path, "models") diff --git a/modules/processing.py b/modules/processing.py index 90f294311..01d038973 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -2,7 +2,6 @@ import json import math import os import sys -import warnings import torch import numpy as np @@ -10,7 +9,7 @@ from PIL import Image, ImageFilter, ImageOps import random import cv2 from skimage import exposure -from typing import Any, Dict, List, Optional +from typing import Any, Dict, List import modules.sd_hijack from modules import devices, prompt_parser, masking, sd_samplers, lowvram, generation_parameters_copypaste, script_callbacks, extra_networks, sd_vae_approx, scripts diff --git a/modules/progress.py b/modules/progress.py index c69ecf3d1..07a5eba1d 100644 --- a/modules/progress.py +++ b/modules/progress.py @@ -2,7 +2,6 @@ import base64 import io import time -import gradio as gr from pydantic import BaseModel, Field from modules.shared import opts diff --git a/modules/realesrgan_model.py b/modules/realesrgan_model.py index a69f22a74..86007f906 100644 --- a/modules/realesrgan_model.py +++ b/modules/realesrgan_model.py @@ -7,7 +7,6 @@ from basicsr.utils.download_util import load_file_from_url from modules.upscaler import Upscaler, UpscalerData from modules.shared import cmd_opts, opts -import modules.shared as shared import modules.errors as errors diff --git a/modules/safe.py b/modules/safe.py index 775f3699d..9a1133ddc 100644 --- a/modules/safe.py +++ b/modules/safe.py @@ -1,15 +1,13 @@ # this code is adapted from the script contributed by anon from /h/ -import io import pickle import collections -import sys +import zipfile +import re import torch import numpy import _codecs -import zipfile -import re # PyTorch 1.13 and later have _TypedStorage renamed to TypedStorage TypedStorage = torch.storage.TypedStorage if hasattr(torch.storage, 'TypedStorage') else torch.storage._TypedStorage diff --git a/modules/script_callbacks.py b/modules/script_callbacks.py index a339614a8..700911b71 100644 --- a/modules/script_callbacks.py +++ b/modules/script_callbacks.py @@ -1,12 +1,9 @@ -import sys -from collections import namedtuple import inspect -import modules.shared as shared -import modules.errors as errors +from collections import namedtuple from typing import Optional, Dict, Any - from fastapi import FastAPI from gradio import Blocks +import modules.errors as errors def report_exception(e, c, job): @@ -32,22 +29,22 @@ class CFGDenoiserParams: def __init__(self, x, image_cond, sigma, sampling_step, total_sampling_steps, text_cond, text_uncond): self.x = x """Latent image representation in the process of being denoised""" - + self.image_cond = image_cond """Conditioning image""" - + self.sigma = sigma """Current sigma noise step value""" - + self.sampling_step = sampling_step """Current Sampling step number""" - + self.total_sampling_steps = total_sampling_steps """Total number of sampling steps planned""" - + self.text_cond = text_cond """ Encoder hidden states of text conditioning from prompt""" - + self.text_uncond = text_uncond """ Encoder hidden states of text conditioning from negative prompt""" @@ -231,7 +228,7 @@ def add_callback(callbacks, fun): callbacks.append(ScriptCallback(filename, fun)) - + def remove_current_script_callbacks(): stack = [x for x in inspect.stack() if x.filename != __file__] filename = stack[0].filename if len(stack) > 0 else 'unknown file' diff --git a/modules/script_loading.py b/modules/script_loading.py index cbfb214c2..979844125 100644 --- a/modules/script_loading.py +++ b/modules/script_loading.py @@ -1,9 +1,6 @@ import os -import sys -import modules.shared as shared -import modules.errors as errors import importlib.util -from types import ModuleType +import modules.errors as errors def load_module(path): diff --git a/modules/sd_hijack.py b/modules/sd_hijack.py index eb5a01861..9f9b9a1ce 100644 --- a/modules/sd_hijack.py +++ b/modules/sd_hijack.py @@ -1,15 +1,7 @@ +from types import MethodType +from rich import print import torch from torch.nn.functional import silu -from types import MethodType - -import modules.textual_inversion.textual_inversion -from modules import devices, sd_hijack_optimizations, shared, sd_hijack_checkpoint -from modules.hypernetworks import hypernetwork -from modules.shared import cmd_opts, opts -from modules.shared_items import list_crossattention -from modules import sd_hijack_clip, sd_hijack_open_clip, sd_hijack_unet, sd_hijack_xlmr, xlmr -from rich import print - import ldm.modules.attention import ldm.modules.diffusionmodules.model import ldm.modules.diffusionmodules.openaimodel @@ -17,6 +9,12 @@ import ldm.models.diffusion.ddim import ldm.models.diffusion.plms import ldm.modules.encoders.modules +import modules.textual_inversion.textual_inversion +from modules import devices, sd_hijack_optimizations, shared +from modules.hypernetworks import hypernetwork +from modules.shared import opts +from modules import sd_hijack_clip, sd_hijack_open_clip, sd_hijack_unet, sd_hijack_xlmr, xlmr + attention_CrossAttention_forward = ldm.modules.attention.CrossAttention.forward diffusionmodules_model_nonlinearity = ldm.modules.diffusionmodules.model.nonlinearity diffusionmodules_model_AttnBlock_forward = ldm.modules.diffusionmodules.model.AttnBlock.forward @@ -36,7 +34,7 @@ def apply_optimizations(): ldm.modules.diffusionmodules.model.nonlinearity = silu ldm.modules.diffusionmodules.openaimodel.th = sd_hijack_unet.th - + optimization_method = None can_use_sdp = hasattr(torch.nn.functional, "scaled_dot_product_attention") and callable(getattr(torch.nn.functional, "scaled_dot_product_attention")) @@ -97,12 +95,12 @@ def fix_checkpoint(): def weighted_loss(sd_model, pred, target, mean=True): #Calculate the weight normally, but ignore the mean loss = sd_model._old_get_loss(pred, target, mean=False) - + #Check if we have weights available weight = getattr(sd_model, '_custom_loss_weight', None) if weight is not None: loss *= weight - + #Return the loss, as mean if specified return loss.mean() if mean else loss @@ -110,7 +108,7 @@ def weighted_forward(sd_model, x, c, w, *args, **kwargs): try: #Temporarily append weights to a place accessible during loss calc sd_model._custom_loss_weight = w - + #Replace 'get_loss' with a weight-aware one. Otherwise we need to reimplement 'forward' completely #Keep 'get_loss', but don't overwrite the previous old_get_loss if it's already set if not hasattr(sd_model, '_old_get_loss'): @@ -123,9 +121,9 @@ def weighted_forward(sd_model, x, c, w, *args, **kwargs): try: #Delete temporary weights if appended del sd_model._custom_loss_weight - except AttributeError as e: + except AttributeError: pass - + #If we have an old loss function, reset the loss function to the original one if hasattr(sd_model, '_old_get_loss'): sd_model.get_loss = sd_model._old_get_loss @@ -138,7 +136,7 @@ def apply_weighted_forward(sd_model): def undo_weighted_forward(sd_model): try: del sd_model.weighted_forward - except AttributeError as e: + except AttributeError: pass @@ -200,7 +198,7 @@ class StableDiffusionModelHijack: def undo_hijack(self, m): if type(m.cond_stage_model) == xlmr.BertSeriesModelWithTransformation: - m.cond_stage_model = m.cond_stage_model.wrapped + m.cond_stage_model = m.cond_stage_model.wrapped elif type(m.cond_stage_model) == sd_hijack_clip.FrozenCLIPEmbedderWithCustomWords: m.cond_stage_model = m.cond_stage_model.wrapped diff --git a/modules/sd_hijack_inpainting.py b/modules/sd_hijack_inpainting.py index 88444d243..4b23c132d 100644 --- a/modules/sd_hijack_inpainting.py +++ b/modules/sd_hijack_inpainting.py @@ -1,9 +1,5 @@ -import os import torch -from einops import repeat -from omegaconf import ListConfig - import ldm.models.diffusion.ddpm import ldm.models.diffusion.ddim import ldm.models.diffusion.plms diff --git a/modules/sd_hijack_ip2p.py b/modules/sd_hijack_ip2p.py index 3c727d3b7..6b183e5aa 100644 --- a/modules/sd_hijack_ip2p.py +++ b/modules/sd_hijack_ip2p.py @@ -1,8 +1,4 @@ -import collections import os.path -import sys -import gc -import time def should_hijack_ip2p(checkpoint_info): from modules import sd_models_config diff --git a/modules/sd_hijack_open_clip.py b/modules/sd_hijack_open_clip.py index 431b256db..f76fc1f3b 100644 --- a/modules/sd_hijack_open_clip.py +++ b/modules/sd_hijack_open_clip.py @@ -2,7 +2,6 @@ import open_clip.tokenizer import torch from modules import sd_hijack_clip, devices -from modules.shared import opts tokenizer = open_clip.tokenizer._tokenizer diff --git a/modules/sd_hijack_optimizations.py b/modules/sd_hijack_optimizations.py index 4fe1b1eb1..776d91d6c 100644 --- a/modules/sd_hijack_optimizations.py +++ b/modules/sd_hijack_optimizations.py @@ -1,5 +1,4 @@ import math -import sys import psutil import torch @@ -391,63 +390,63 @@ def scaled_dot_product_no_mem_attention_forward(self, x, context=None, mask=None return scaled_dot_product_attention_forward(self, x, context, mask) def cross_attention_attnblock_forward(self, x): - h_ = x - h_ = self.norm(h_) - q1 = self.q(h_) - k1 = self.k(h_) - v = self.v(h_) + h_ = x + h_ = self.norm(h_) + q1 = self.q(h_) + k1 = self.k(h_) + v = self.v(h_) - # compute attention - b, c, h, w = q1.shape + # compute attention + b, c, h, w = q1.shape - q2 = q1.reshape(b, c, h*w) - del q1 + q2 = q1.reshape(b, c, h*w) + del q1 - q = q2.permute(0, 2, 1) # b,hw,c - del q2 + q = q2.permute(0, 2, 1) # b,hw,c + del q2 - k = k1.reshape(b, c, h*w) # b,c,hw - del k1 + k = k1.reshape(b, c, h*w) # b,c,hw + del k1 - h_ = torch.zeros_like(k, device=q.device) + h_ = torch.zeros_like(k, device=q.device) - mem_free_total = get_available_vram() + mem_free_total = get_available_vram() - tensor_size = q.shape[0] * q.shape[1] * k.shape[2] * q.element_size() - mem_required = tensor_size * 2.5 - steps = 1 + tensor_size = q.shape[0] * q.shape[1] * k.shape[2] * q.element_size() + mem_required = tensor_size * 2.5 + steps = 1 - if mem_required > mem_free_total: - steps = 2**(math.ceil(math.log(mem_required / mem_free_total, 2))) + if mem_required > mem_free_total: + steps = 2**(math.ceil(math.log(mem_required / mem_free_total, 2))) - slice_size = q.shape[1] // steps if (q.shape[1] % steps) == 0 else q.shape[1] - for i in range(0, q.shape[1], slice_size): - end = i + slice_size + slice_size = q.shape[1] // steps if (q.shape[1] % steps) == 0 else q.shape[1] + for i in range(0, q.shape[1], slice_size): + end = i + slice_size - w1 = torch.bmm(q[:, i:end], k) # b,hw,hw w[b,i,j]=sum_c q[b,i,c]k[b,c,j] - w2 = w1 * (int(c)**(-0.5)) - del w1 - w3 = torch.nn.functional.softmax(w2, dim=2, dtype=q.dtype) - del w2 + w1 = torch.bmm(q[:, i:end], k) # b,hw,hw w[b,i,j]=sum_c q[b,i,c]k[b,c,j] + w2 = w1 * (int(c)**(-0.5)) + del w1 + w3 = torch.nn.functional.softmax(w2, dim=2, dtype=q.dtype) + del w2 - # attend to values - v1 = v.reshape(b, c, h*w) - w4 = w3.permute(0, 2, 1) # b,hw,hw (first hw of k, second of q) - del w3 + # attend to values + v1 = v.reshape(b, c, h*w) + w4 = w3.permute(0, 2, 1) # b,hw,hw (first hw of k, second of q) + del w3 - h_[:, :, i:end] = torch.bmm(v1, w4) # b, c,hw (hw of q) h_[b,c,j] = sum_i v[b,c,i] w_[b,i,j] - del v1, w4 + h_[:, :, i:end] = torch.bmm(v1, w4) # b, c,hw (hw of q) h_[b,c,j] = sum_i v[b,c,i] w_[b,i,j] + del v1, w4 - h2 = h_.reshape(b, c, h, w) - del h_ + h2 = h_.reshape(b, c, h, w) + del h_ - h3 = self.proj_out(h2) - del h2 + h3 = self.proj_out(h2) + del h2 - h3 += x + h3 += x + + return h3 - return h3 - def xformers_attnblock_forward(self, x): try: h_ = x diff --git a/modules/sd_hijack_xlmr.py b/modules/sd_hijack_xlmr.py index 4ac51c386..28528329b 100644 --- a/modules/sd_hijack_xlmr.py +++ b/modules/sd_hijack_xlmr.py @@ -1,8 +1,6 @@ -import open_clip.tokenizer import torch from modules import sd_hijack_clip, devices -from modules.shared import opts class FrozenXLMREmbedderWithCustomWords(sd_hijack_clip.FrozenCLIPEmbedderWithCustomWords): diff --git a/modules/sd_models.py b/modules/sd_models.py index 97202c8f5..3215b7137 100644 --- a/modules/sd_models.py +++ b/modules/sd_models.py @@ -2,23 +2,21 @@ import collections import os.path import sys import gc -import torch import re -import safetensors.torch -from omegaconf import OmegaConf +import io from os import mkdir from urllib import request +from rich import print, progress # pylint: disable=W0622 +import torch +import safetensors.torch +from omegaconf import OmegaConf import ldm.modules.midas as midas -import io - from ldm.util import instantiate_from_config from modules import paths, shared, modelloader, devices, script_callbacks, sd_vae, sd_disable_initialization, errors, hashes, sd_models_config -from modules.paths import models_path from modules.sd_hijack_inpainting import do_inpainting_hijack from modules.timer import Timer -from rich import print, progress model_dir = "Stable-diffusion" model_path = os.path.abspath(os.path.join(paths.models_path, model_dir)) @@ -57,8 +55,8 @@ class CheckpointInfo: def register(self): checkpoints_list[self.title] = self - for id in self.ids: - checkpoint_alisases[id] = self + for i in self.ids: + checkpoint_alisases[i] = self def calculate_shorthash(self): self.sha256 = hashes.sha256(self.filename, "checkpoint/" + self.name) @@ -79,9 +77,7 @@ class CheckpointInfo: try: # this silences the annoying "Some weights of the model checkpoint were not used when initializing..." message at start. - - from transformers import logging, CLIPModel - + from transformers import logging logging.set_verbosity_error() except Exception: pass @@ -160,7 +156,7 @@ def model_hash(filename): def select_checkpoint(): model_checkpoint = shared.opts.sd_model_checkpoint - + checkpoint_info = checkpoint_alisases.get(model_checkpoint, None) if checkpoint_info is not None: return checkpoint_info @@ -232,7 +228,7 @@ def read_metadata_from_safetensors(filename): if isinstance(v, str) and v[0:1] == '{': try: res[k] = json.loads(v) - except Exception as e: + except Exception: pass return res @@ -264,7 +260,7 @@ def read_state_dict(checkpoint_file): def get_checkpoint_state_dict(checkpoint_info: CheckpointInfo, timer): if checkpoint_info in checkpoints_loaded: # use checkpoint cache - print(f"Loading weights from cache") + print("Loading weights from cache") return checkpoints_loaded[checkpoint_info] res = read_state_dict(checkpoint_info.filename) @@ -368,7 +364,7 @@ def enable_midas_autodownload(): if not os.path.exists(path): if not os.path.exists(midas_path): mkdir(midas_path) - + print(f"Downloading midas model weights for {model_type} to {path}") request.urlretrieve(midas_urls[model_type], path) print(f"{model_type} downloaded") @@ -444,7 +440,7 @@ def load_model(checkpoint_info=None, already_loaded_state_dict=None): try: with sd_disable_initialization.DisableInitialization(disable_clip=clip_is_included_into_sd): sd_model = instantiate_from_config(sd_config.model) - except Exception as e: + except Exception: sd_model = instantiate_from_config(sd_config.model) sd_model.used_config = checkpoint_config @@ -481,7 +477,7 @@ def load_model(checkpoint_info=None, already_loaded_state_dict=None): def reload_model_weights(sd_model=None, info=None): - from modules import lowvram, devices, sd_hijack + from modules import lowvram, sd_hijack checkpoint_info = info or select_checkpoint() if not sd_model: @@ -517,7 +513,7 @@ def reload_model_weights(sd_model=None, info=None): try: load_model_weights(sd_model, checkpoint_info, state_dict, timer) - except Exception as e: + except Exception: print("Failed to load checkpoint, restoring previous") load_model_weights(sd_model, current_checkpoint_info, None, timer) raise @@ -534,8 +530,8 @@ def reload_model_weights(sd_model=None, info=None): print(f"Weights loaded in {timer.summary()}") -def unload_model_weights(sd_model=None, info=None): - from modules import lowvram, devices, sd_hijack +def unload_model_weights(sd_model=None, _info=None): + from modules import sd_hijack timer = Timer() if shared.sd_model: diff --git a/modules/sd_models_config.py b/modules/sd_models_config.py index bdeb42f42..a9c515b14 100644 --- a/modules/sd_models_config.py +++ b/modules/sd_models_config.py @@ -1,24 +1,20 @@ -import re import os import torch -from modules import shared, paths, sd_disable_initialization +from modules import paths, sd_disable_initialization -sd_configs_path = shared.sd_configs_path sd_repo_configs_path = os.path.join(paths.paths['Stable Diffusion'], "configs", "stable-diffusion") - - -config_default = shared.sd_default_config +config_default = paths.sd_default_config config_sd2 = os.path.join(sd_repo_configs_path, "v2-inference.yaml") config_sd2v = os.path.join(sd_repo_configs_path, "v2-inference-v.yaml") config_sd2_inpainting = os.path.join(sd_repo_configs_path, "v2-inpainting-inference.yaml") config_depth_model = os.path.join(sd_repo_configs_path, "v2-midas-inference.yaml") config_unclip = os.path.join(sd_repo_configs_path, "v2-1-stable-unclip-l-inference.yaml") config_unopenclip = os.path.join(sd_repo_configs_path, "v2-1-stable-unclip-h-inference.yaml") -config_inpainting = os.path.join(sd_configs_path, "v1-inpainting-inference.yaml") -config_instruct_pix2pix = os.path.join(sd_configs_path, "instruct-pix2pix.yaml") -config_alt_diffusion = os.path.join(sd_configs_path, "alt-diffusion-inference.yaml") +config_inpainting = os.path.join(paths.sd_configs_path, "v1-inpainting-inference.yaml") +config_instruct_pix2pix = os.path.join(paths.sd_configs_path, "instruct-pix2pix.yaml") +config_alt_diffusion = os.path.join(paths.sd_configs_path, "alt-diffusion-inference.yaml") def is_using_v_parameterization_for_sd2(state_dict): @@ -64,7 +60,7 @@ def is_using_v_parameterization_for_sd2(state_dict): return out < -1 -def guess_model_config_from_state_dict(sd, filename): +def guess_model_config_from_state_dict(sd, _filename): if sd is None: return None sd2_cond_proj_weight = sd.get('cond_stage_model.model.transformer.resblocks.0.attn.in_proj_weight', None) @@ -118,4 +114,3 @@ def find_checkpoint_config_near_filename(info): return config return None - diff --git a/modules/sd_samplers_kdiffusion.py b/modules/sd_samplers_kdiffusion.py index f49cfdf1c..8bc9d1780 100644 --- a/modules/sd_samplers_kdiffusion.py +++ b/modules/sd_samplers_kdiffusion.py @@ -1,7 +1,6 @@ from collections import deque -import torch import inspect -import einops +import torch import k_diffusion.sampling from modules import prompt_parser, devices, sd_samplers_common @@ -94,10 +93,10 @@ class CFGDenoiser(torch.nn.Module): if shared.sd_model.model.conditioning_key == "crossattn-adm": image_uncond = torch.zeros_like(image_cond) - make_condition_dict = lambda c_crossattn, c_adm: {"c_crossattn": c_crossattn, "c_adm": c_adm} + make_condition_dict = lambda c_crossattn, c_adm: {"c_crossattn": c_crossattn, "c_adm": c_adm} else: image_uncond = image_cond - make_condition_dict = lambda c_crossattn, c_concat: {"c_crossattn": c_crossattn, "c_concat": [c_concat]} + make_condition_dict = lambda c_crossattn, c_concat: {"c_crossattn": c_crossattn, "c_concat": [c_concat]} if not is_edit_model: x_in = torch.cat([torch.stack([x[i] for _ in range(n)]) for i, n in enumerate(repeats)] + [x]) @@ -295,7 +294,7 @@ class KDiffusionSampler: sigma_sched = sigmas[steps - t_enc - 1:] xi = x + noise * sigma_sched[0] - + extra_params_kwargs = self.initialize(p) parameters = inspect.signature(self.func).parameters @@ -359,4 +358,3 @@ class KDiffusionSampler: }, disable=False, callback=self.callback_state, **extra_params_kwargs)) return samples - diff --git a/modules/sd_vae.py b/modules/sd_vae.py index 7e7ab9ed6..85f8b3bd6 100644 --- a/modules/sd_vae.py +++ b/modules/sd_vae.py @@ -1,12 +1,9 @@ -import torch -import safetensors.torch import os import collections -from collections import namedtuple -from modules import paths, shared, devices, script_callbacks, sd_models import glob from copy import deepcopy from rich import print +from modules import paths, shared, devices, script_callbacks, sd_models vae_ignore_keys = {"model_ema.decay", "model_ema.num_updates"} vae_dict = {} diff --git a/modules/shared.py b/modules/shared.py index 75ae443ad..6e52ff2e1 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -1,4 +1,3 @@ -import argparse import datetime import json import os @@ -12,18 +11,19 @@ import modules.interrogate import modules.memmon import modules.styles import modules.devices as devices -from modules import script_loading, errors, ui_components, shared_items, cmd_args, errors -from modules.paths_internal import models_path, script_path, data_path, sd_configs_path, sd_default_config, sd_model_file, default_sd_model_file, extensions_dir, extensions_builtin_dir +from modules import script_loading, errors, ui_components, shared_items, cmd_args +from modules.paths_internal import models_path, script_path, data_path, sd_configs_path, sd_default_config, sd_model_file, default_sd_model_file, extensions_dir, extensions_builtin_dir # pylint: disable=W0611 +import modules.paths_internal as paths +from setup import log as setup_log # pylint: disable=E0611 errors.install() demo = None -from setup import log as setup_log # pylint: disable=E0611 log = setup_log parser = cmd_args.parser -script_loading.preload_extensions(extensions_dir, parser) -script_loading.preload_extensions(extensions_builtin_dir, parser) +script_loading.preload_extensions(paths.extensions_dir, parser) +script_loading.preload_extensions(paths.extensions_builtin_dir, parser) if os.environ.get('IGNORE_CMD_ARGS_ERRORS', None) is None: cmd_opts = parser.parse_args() @@ -68,7 +68,7 @@ clip_model = None def reload_hypernetworks(): from modules.hypernetworks import hypernetwork - global hypernetworks + global hypernetworks # pylint: disable=W0603 hypernetworks = hypernetwork.list_hypernetworks(opts.hypernetwork_dir) @@ -154,8 +154,8 @@ class State: if self.current_latent is None: return - import modules.sd_samplers if opts.show_progress_grid: + import modules.sd_samplers # pylint: disable=W0621 self.assign_current_image(modules.sd_samplers.samples_to_image_grid(self.current_latent)) else: self.assign_current_image(modules.sd_samplers.sample_to_image(self.current_latent)) @@ -188,24 +188,24 @@ class OptionInfo: def options_section(section_identifier, options_dict): - for k, v in options_dict.items(): + for _k, v in options_dict.items(): v.section = section_identifier return options_dict def list_checkpoint_tiles(): - import modules.sd_models + import modules.sd_models # pylint: disable=W0621 return modules.sd_models.checkpoint_tiles() def refresh_checkpoints(): - import modules.sd_models + import modules.sd_models # pylint: disable=W0621 return modules.sd_models.list_models() def list_samplers(): - import modules.sd_samplers + import modules.sd_samplers # pylint: disable=W0621 return modules.sd_samplers.all_samplers @@ -245,18 +245,18 @@ options_templates.update(options_section(('sd', "Stable Diffusion"), { })) options_templates.update(options_section(('system-paths', "System Paths"), { - "ckpt_dir": OptionInfo(os.path.join(models_path, 'Stable-diffusion'), "Path to directory with stable diffusion checkpoints"), - "vae_dir": OptionInfo(os.path.join(models_path, 'VAE'), "Path to directory with VAE files"), - "embeddings_dir": OptionInfo(os.path.join(models_path, 'embeddings'), "Embeddings directory for textual inversion"), - "embeddings_templates_dir": OptionInfo(os.path.join(script_path, 'train/templates'), "Embeddings train templates directory"), - "hypernetwork_dir": OptionInfo(os.path.join(models_path, 'hypernetworks'), "Hypernetwork directory"), - "codeformer_models_path": OptionInfo(os.path.join(models_path, 'Codeformer'), "Path to directory with codeformer model file(s)."), - "gfpgan_models_path": OptionInfo(os.path.join(models_path, 'GFPGAN'), "Path to directory with GFPGAN model file(s)"), - "esrgan_models_path": OptionInfo(os.path.join(models_path, 'ESRGAN'), "Path to directory with ESRGAN model file(s)"), - "bsrgan_models_path": OptionInfo(os.path.join(models_path, 'BSRGAN'), "Path to directory with BSRGAN model file(s)"), - "realesrgan_models_path": OptionInfo(os.path.join(models_path, 'RealESRGAN'), "Path to directory with RealESRGAN model file(s)"), - "clip_models_path": OptionInfo(os.path.join(models_path, 'CLIP'), "Path to directory with CLIP model file(s)"), - "lora_dir": OptionInfo(os.path.join(models_path, 'Lora'), "Path to directory with Lora network(s)"), + "ckpt_dir": OptionInfo(os.path.join(paths.models_path, 'Stable-diffusion'), "Path to directory with stable diffusion checkpoints"), + "vae_dir": OptionInfo(os.path.join(paths.models_path, 'VAE'), "Path to directory with VAE files"), + "embeddings_dir": OptionInfo(os.path.join(paths.models_path, 'embeddings'), "Embeddings directory for textual inversion"), + "embeddings_templates_dir": OptionInfo(os.path.join(paths.script_path, 'train/templates'), "Embeddings train templates directory"), + "hypernetwork_dir": OptionInfo(os.path.join(paths.models_path, 'hypernetworks'), "Hypernetwork directory"), + "codeformer_models_path": OptionInfo(os.path.join(paths.models_path, 'Codeformer'), "Path to directory with codeformer model file(s)."), + "gfpgan_models_path": OptionInfo(os.path.join(paths.models_path, 'GFPGAN'), "Path to directory with GFPGAN model file(s)"), + "esrgan_models_path": OptionInfo(os.path.join(paths.models_path, 'ESRGAN'), "Path to directory with ESRGAN model file(s)"), + "bsrgan_models_path": OptionInfo(os.path.join(paths.models_path, 'BSRGAN'), "Path to directory with BSRGAN model file(s)"), + "realesrgan_models_path": OptionInfo(os.path.join(paths.models_path, 'RealESRGAN'), "Path to directory with RealESRGAN model file(s)"), + "clip_models_path": OptionInfo(os.path.join(paths.models_path, 'CLIP'), "Path to directory with CLIP model file(s)"), + "lora_dir": OptionInfo(os.path.join(paths.models_path, 'Lora'), "Path to directory with Lora network(s)"), # "gfpgan_model": OptionInfo("", "GFPGAN model file name"), })) @@ -647,7 +647,7 @@ def listfiles(dirname): def html_path(filename): - return os.path.join(script_path, "html", filename) + return os.path.join(paths.script_path, "html", filename) def html(filename): diff --git a/modules/sub_quadratic_attention.py b/modules/sub_quadratic_attention.py index 055953236..87c18a38d 100644 --- a/modules/sub_quadratic_attention.py +++ b/modules/sub_quadratic_attention.py @@ -11,11 +11,11 @@ # https://arxiv.org/abs/2112.05682v2 from functools import partial +import math +from typing import Optional, NamedTuple, List import torch from torch import Tensor from torch.utils.checkpoint import checkpoint -import math -from typing import Optional, NamedTuple, List def narrow_trunc( @@ -179,7 +179,7 @@ def efficient_dot_product_attention( chunk_idx, min(query_chunk_size, q_tokens) ) - + summarize_chunk: SummarizeChunk = partial(_summarize_chunk, scale=scale) summarize_chunk: SummarizeChunk = partial(checkpoint, summarize_chunk) if use_checkpoint else summarize_chunk compute_query_chunk_attn: ComputeQueryChunkAttn = partial( @@ -201,7 +201,7 @@ def efficient_dot_product_attention( key=key, value=value, ) - + # TODO: maybe we should use torch.empty_like(query) to allocate storage in-advance, # and pass slices to be mutated, instead of torch.cat()ing the returned slices res = torch.cat([ diff --git a/modules/txt2img.py b/modules/txt2img.py index 97ea8c2bf..632a4ead3 100644 --- a/modules/txt2img.py +++ b/modules/txt2img.py @@ -1,11 +1,9 @@ import modules.scripts from modules import sd_samplers from modules.generation_parameters_copypaste import create_override_settings_dict -from modules.processing import StableDiffusionProcessing, Processed, StableDiffusionProcessingTxt2Img, \ - StableDiffusionProcessingImg2Img, process_images -from modules.shared import opts, cmd_opts +from modules.processing import StableDiffusionProcessing, Processed, StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img, process_images +from modules.shared import opts import modules.shared as shared -import modules.processing as processing from modules.ui import plaintext_to_html diff --git a/modules/ui.py b/modules/ui.py index f8a8adfe9..34e283de5 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -1,29 +1,21 @@ -import html import json -import math import mimetypes import os -import platform -import random import sys -import tempfile -import time -from functools import partial, reduce +from functools import reduce import warnings import gradio as gr import gradio.routes import gradio.utils import numpy as np -from PIL import Image, PngImagePlugin +from PIL import Image from modules.call_queue import wrap_gradio_gpu_call, wrap_queued_call, wrap_gradio_call -from modules import sd_hijack, sd_models, localization, script_callbacks, ui_extensions, deepbooru, sd_vae, extra_networks, postprocessing, ui_components, ui_common, ui_postprocessing +from modules import sd_hijack, sd_models, script_callbacks, ui_extensions, deepbooru, sd_vae, extra_networks, ui_common, ui_postprocessing from modules.ui_components import FormRow, FormColumn, FormGroup, ToolButton, FormHTML from modules.paths import script_path, data_path - -from modules.shared import opts, cmd_opts, restricted_opts - +from modules.shared import opts, cmd_opts import modules.codeformer_model import modules.generation_parameters_copypaste as parameters_copypaste import modules.gfpgan_model @@ -34,11 +26,9 @@ import modules.errors as errors import modules.styles import modules.textual_inversion.ui from modules import prompt_parser -from modules.images import save_image from modules.sd_hijack import model_hijack from modules.sd_samplers import samplers, samplers_for_img2img from modules.textual_inversion import textual_inversion -import modules.hypernetworks.ui from modules.generation_parameters_copypaste import image_from_url_text import modules.extras @@ -1069,7 +1059,7 @@ def create_ui(): process_focal_crop_entropy_weight = gr.Slider(label='Focal point entropy weight', value=0.15, minimum=0.0, maximum=1.0, step=0.05, elem_id="train_process_focal_crop_entropy_weight") process_focal_crop_edges_weight = gr.Slider(label='Focal point edges weight', value=0.5, minimum=0.0, maximum=1.0, step=0.05, elem_id="train_process_focal_crop_edges_weight") process_focal_crop_debug = gr.Checkbox(label='Create debug image', elem_id="train_process_focal_crop_debug") - + with gr.Column(visible=False) as process_multicrop_col: gr.Markdown('Each image is center-cropped with an automatically chosen width and height.') with gr.Row(): @@ -1081,7 +1071,7 @@ def create_ui(): with gr.Row(): process_multicrop_objective = gr.Radio(["Maximize area", "Minimize error"], value="Maximize area", label="Resizing objective", elem_id="train_process_multicrop_objective") process_multicrop_threshold = gr.Slider(minimum=0, maximum=1, step=0.01, label="Error threshold", value=0.1, elem_id="train_process_multicrop_threshold") - + with gr.Row(): with gr.Column(scale=3): gr.HTML(value="") @@ -1124,7 +1114,7 @@ def create_ui(): with FormRow(): embedding_learn_rate = gr.Textbox(label='Embedding Learning rate', placeholder="Embedding Learning rate", value="0.005", elem_id="train_embedding_learn_rate") hypernetwork_learn_rate = gr.Textbox(label='Hypernetwork Learning rate', placeholder="Hypernetwork Learning rate", value="0.00001", elem_id="train_hypernetwork_learn_rate") - + with FormRow(): clip_grad_mode = gr.Dropdown(value="disabled", label="Gradient Clipping", choices=["disabled", "value", "norm"]) clip_grad_value = gr.Textbox(placeholder="Gradient clip value", value="0.1", show_label=False) @@ -1454,7 +1444,6 @@ def create_ui(): gr.HTML(shared.html("licenses.html"), elem_id="licenses") gr.Button(value="Show all pages", elem_id="settings_show_all_pages") - def unload_sd_weights(): modules.sd_models.unload_model_weights() @@ -1631,7 +1620,7 @@ def create_ui(): key = path + "/" + field if getattr(obj, 'custom_script_source', None) is not None: - key = 'customscript/' + obj.custom_script_source + '/' + key + key = 'customscript/' + obj.custom_script_source + '/' + key if getattr(obj, 'do_not_save_to_config', False): return diff --git a/modules/ui_common.py b/modules/ui_common.py index 295912a11..312b07094 100644 --- a/modules/ui_common.py +++ b/modules/ui_common.py @@ -3,9 +3,9 @@ import html import os import platform import sys +import subprocess as sp import gradio as gr -import subprocess as sp from modules import call_queue, shared from modules.generation_parameters_copypaste import image_from_url_text @@ -101,7 +101,6 @@ def initial_image(): return [img] def create_output_panel(tabname, outdir): - from modules import shared import modules.generation_parameters_copypaste as parameters_copypaste def open_folder(f): diff --git a/modules/ui_extensions.py b/modules/ui_extensions.py index 74770bc97..0b4bfd301 100644 --- a/modules/ui_extensions.py +++ b/modules/ui_extensions.py @@ -1,15 +1,14 @@ import json import os.path -import sys import time - -import git - -import gradio as gr -import html import shutil import errno +import html +import git +import gradio as gr + +from rich import print from modules import extensions, shared, paths, errors from modules.call_queue import wrap_gradio_gpu_call @@ -48,7 +47,7 @@ def apply_and_restart(disable_list, update_list, disable_all): shared.state.need_restart = True -def check_updates(id_task, disable_list): +def check_updates(_id_task, disable_list): check_access() disabled = json.loads(disable_list) @@ -134,10 +133,10 @@ def install_extension_from_url(dirname, url): if dirname is None or dirname == "": *parts, last_part = url.split('/') last_part = normalize_git_url(last_part) - dirname = last_part target_dir = os.path.join(extensions.extensions_dir, dirname) + print(f'Installing extension: {url} into {target_dir}') assert not os.path.exists(target_dir), f'Extension directory already exists: {target_dir}' normalized_url = normalize_git_url(url) @@ -155,18 +154,14 @@ def install_extension_from_url(dirname, url): os.rename(tmpdir, target_dir) except OSError as err: if err.errno == errno.EXDEV: - # Cross device link, typical in docker or when tmp/ and extensions/ are on different file systems - # Since we can't use a rename, do the slower but more versitile shutil.move() shutil.move(tmpdir, target_dir) else: - # Something else, not enough free space, permissions, etc. rethrow it so that it gets handled. raise err from launch import run_extension_installer run_extension_installer(target_dir) - extensions.list_extensions() - return [extension_table(), html.escape(f"Installed into {target_dir}. Use Installed tab to restart.")] + return [extension_table(), html.escape(f"Installed into {target_dir}")] finally: shutil.rmtree(tmpdir, True) @@ -290,7 +285,7 @@ def create_ui(): import modules.ui with gr.Blocks(analytics_enabled=False) as ui: - with gr.Tabs(elem_id="tabs_extensions") as tabs: + with gr.Tabs(elem_id="tabs_extensions"): with gr.TabItem("Installed"): with gr.Row(elem_id="extensions_installed_top"): @@ -300,14 +295,14 @@ def create_ui(): extensions_disabled_list = gr.Text(elem_id="extensions_disabled_list", visible=False).style(container=False) extensions_update_list = gr.Text(elem_id="extensions_update_list", visible=False).style(container=False) - html = "" + txt = "" if shared.opts.disable_all_extensions != "none": - html = """ + txt = """ "Disable all extensions" was set, change it to "none" to load all extensions again """ - info = gr.HTML(html) + info = gr.HTML(txt) extensions_table = gr.HTML(lambda: extension_table()) apply.click( @@ -335,9 +330,9 @@ def create_ui(): hide_tags = gr.CheckboxGroup(value=["ads", "localization", "installed"], label="Hide extensions with tags", choices=["script", "ads", "localization", "installed"]) sort_column = gr.Radio(value="newest first", label="Order", choices=["newest first", "oldest first", "a-z", "z-a", "internal order", ], type="index") - with gr.Row(): + with gr.Row(): search_extensions_text = gr.Text(label="Search").style(container=False) - + install_result = gr.HTML() available_extensions_table = gr.HTML() diff --git a/modules/ui_extra_networks.py b/modules/ui_extra_networks.py index 25eb464b9..99aaef6bc 100644 --- a/modules/ui_extra_networks.py +++ b/modules/ui_extra_networks.py @@ -1,15 +1,14 @@ +import json +import html import glob import os.path import urllib.parse from pathlib import Path from PIL import PngImagePlugin +import gradio as gr from modules import shared from modules.images import read_info_from_image -import gradio as gr -import json -import html - from modules.generation_parameters_copypaste import image_from_url_text extra_pages = [] @@ -317,4 +316,3 @@ def setup_ui(ui, gallery): inputs=[ui.preview_target_filename, gallery, ui.preview_target_filename], outputs=[*ui.pages] ) - diff --git a/modules/ui_extra_networks_checkpoints.py b/modules/ui_extra_networks_checkpoints.py index 0c175b2d4..2ddf9b70d 100644 --- a/modules/ui_extra_networks_checkpoints.py +++ b/modules/ui_extra_networks_checkpoints.py @@ -28,4 +28,3 @@ class ExtraNetworksPageCheckpoints(ui_extra_networks.ExtraNetworksPage): def allowed_directories_for_previews(self): return [v for v in [shared.opts.ckpt_dir, sd_models.model_path] if v is not None] - diff --git a/modules/ui_extra_networks_hypernets.py b/modules/ui_extra_networks_hypernets.py index 26d0900b0..3deb9cbac 100644 --- a/modules/ui_extra_networks_hypernets.py +++ b/modules/ui_extra_networks_hypernets.py @@ -27,4 +27,3 @@ class ExtraNetworksPageHypernetworks(ui_extra_networks.ExtraNetworksPage): def allowed_directories_for_previews(self): return [shared.opts.hypernetwork_dir] - diff --git a/modules/upscaler.py b/modules/upscaler.py index 89b65df64..0376d256c 100644 --- a/modules/upscaler.py +++ b/modules/upscaler.py @@ -2,11 +2,8 @@ import os from abc import abstractmethod import PIL -import numpy as np -import torch from PIL import Image -import modules.shared from modules import modelloader, shared LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS) @@ -27,13 +24,13 @@ class Upscaler: def __init__(self, create_dirs=False): self.mod_pad_h = None - self.tile_size = modules.shared.opts.ESRGAN_tile - self.tile_pad = modules.shared.opts.ESRGAN_tile_overlap - self.device = modules.shared.device + self.tile_size = shared.opts.ESRGAN_tile + self.tile_pad = shared.opts.ESRGAN_tile_overlap + self.device = shared.device self.img = None self.output = None self.scale = 1 - self.half = not modules.shared.cmd_opts.no_half + self.half = not shared.cmd_opts.no_half self.pre_pad = 0 self.mod_scale = None diff --git a/setup.py b/setup.py index 7eb27ede4..f6a6854ca 100644 --- a/setup.py +++ b/setup.py @@ -319,19 +319,22 @@ def check_version(): except ImportError: return logging.getLogger("urllib3").setLevel(logging.WARNING) - commits = requests.get('https://api.github.com/repos/vladmandic/automatic/branches/master', timeout=10).json() - if commits['commit']['sha'] != commit: - if args.upgrade: - update('.') - ver = git('log -1 --pretty=format:"%h %ad"') - log.info(f'Updated to version: {ver}') - else: - log.info(f'Latest available version: {commits["commit"]["commit"]["author"]["date"]}') - if not args.noupdate: - log.info('Updating Wiki') - update(os.path.join(os.path.dirname(__file__), "wiki")) - update(os.path.join(os.path.dirname(__file__), "wiki", "origin-wiki")) - + commits = None + try: + commits = requests.get('https://api.github.com/repos/vladmandic/automatic/branches/master', timeout=10).json() + if commits['commit']['sha'] != commit: + if args.upgrade: + update('.') + ver = git('log -1 --pretty=format:"%h %ad"') + log.info(f'Updated to version: {ver}') + else: + log.info(f'Latest available version: {commits["commit"]["commit"]["author"]["date"]}') + if not args.noupdate: + log.info('Updating Wiki') + update(os.path.join(os.path.dirname(__file__), "wiki")) + update(os.path.join(os.path.dirname(__file__), "wiki", "origin-wiki")) + except Exception as e: + log.error(f'Failed to check version: {e} {commits}') # check if we can run setup in quick mode def check_timestamp(): diff --git a/ui-config.json b/ui-config.json index db07650e2..f8dfe6b0f 100644 --- a/ui-config.json +++ b/ui-config.json @@ -1013,5 +1013,201 @@ "train/Drop out tags when creating prompts./maximum": 1, "train/Drop out tags when creating prompts./step": 0.1, "train/Choose latent sampling method/visible": true, - "train/Choose latent sampling method/value": "once" + "train/Choose latent sampling method/value": "once", + "customscript/model_keyword.py/txt2img/multiplier/visible": true, + "customscript/model_keyword.py/txt2img/multiplier/value": 0.7, + "customscript/model_keyword.py/txt2img/multiplier/minimum": 0, + "customscript/model_keyword.py/txt2img/multiplier/maximum": 2, + "customscript/model_keyword.py/txt2img/multiplier/step": 0.01, + "txt2img/Keyword(trigger word)/visible": true, + "txt2img/Keyword(trigger word)/value": "", + "txt2img/result/visible": true, + "txt2img/result/value": "", + "customscript/model_keyword.py/img2img/multiplier/visible": true, + "customscript/model_keyword.py/img2img/multiplier/value": 0.7, + "customscript/model_keyword.py/img2img/multiplier/minimum": 0, + "customscript/model_keyword.py/img2img/multiplier/maximum": 2, + "customscript/model_keyword.py/img2img/multiplier/step": 0.01, + "img2img/Keyword(trigger word)/visible": true, + "img2img/Keyword(trigger word)/value": "", + "img2img/result/visible": true, + "img2img/result/value": "", + "customscript/additional_networks.py/txt2img/Enable/visible": true, + "customscript/additional_networks.py/txt2img/Enable/value": false, + "customscript/additional_networks.py/txt2img/Separate UNet/Text Encoder weights/visible": true, + "customscript/additional_networks.py/txt2img/Separate UNet/Text Encoder weights/value": false, + "customscript/additional_networks.py/txt2img/Network module 1/visible": true, + "customscript/additional_networks.py/txt2img/Network module 1/value": "LoRA", + "customscript/additional_networks.py/txt2img/Model 1/visible": true, + "customscript/additional_networks.py/txt2img/Model 1/value": "None", + "txt2img/Weight 1/visible": true, + "txt2img/Weight 1/value": 1.0, + "txt2img/Weight 1/minimum": -1.0, + "txt2img/Weight 1/maximum": 2.0, + "txt2img/Weight 1/step": 0.05, + "customscript/additional_networks.py/txt2img/UNet Weight 1/value": 1.0, + "customscript/additional_networks.py/txt2img/UNet Weight 1/minimum": -1.0, + "customscript/additional_networks.py/txt2img/UNet Weight 1/maximum": 2.0, + "customscript/additional_networks.py/txt2img/UNet Weight 1/step": 0.05, + "customscript/additional_networks.py/txt2img/TEnc Weight 1/value": 1.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 1/minimum": -1.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 1/maximum": 2.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 1/step": 0.05, + "customscript/additional_networks.py/txt2img/Network module 2/visible": true, + "customscript/additional_networks.py/txt2img/Network module 2/value": "LoRA", + "customscript/additional_networks.py/txt2img/Model 2/visible": true, + "customscript/additional_networks.py/txt2img/Model 2/value": "None", + "txt2img/Weight 2/visible": true, + "txt2img/Weight 2/value": 1.0, + "txt2img/Weight 2/minimum": -1.0, + "txt2img/Weight 2/maximum": 2.0, + "txt2img/Weight 2/step": 0.05, + "customscript/additional_networks.py/txt2img/UNet Weight 2/value": 1.0, + "customscript/additional_networks.py/txt2img/UNet Weight 2/minimum": -1.0, + "customscript/additional_networks.py/txt2img/UNet Weight 2/maximum": 2.0, + "customscript/additional_networks.py/txt2img/UNet Weight 2/step": 0.05, + "customscript/additional_networks.py/txt2img/TEnc Weight 2/value": 1.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 2/minimum": -1.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 2/maximum": 2.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 2/step": 0.05, + "customscript/additional_networks.py/txt2img/Network module 3/visible": true, + "customscript/additional_networks.py/txt2img/Network module 3/value": "LoRA", + "customscript/additional_networks.py/txt2img/Model 3/visible": true, + "customscript/additional_networks.py/txt2img/Model 3/value": "None", + "txt2img/Weight 3/visible": true, + "txt2img/Weight 3/value": 1.0, + "txt2img/Weight 3/minimum": -1.0, + "txt2img/Weight 3/maximum": 2.0, + "txt2img/Weight 3/step": 0.05, + "customscript/additional_networks.py/txt2img/UNet Weight 3/value": 1.0, + "customscript/additional_networks.py/txt2img/UNet Weight 3/minimum": -1.0, + "customscript/additional_networks.py/txt2img/UNet Weight 3/maximum": 2.0, + "customscript/additional_networks.py/txt2img/UNet Weight 3/step": 0.05, + "customscript/additional_networks.py/txt2img/TEnc Weight 3/value": 1.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 3/minimum": -1.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 3/maximum": 2.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 3/step": 0.05, + "customscript/additional_networks.py/txt2img/Network module 4/visible": true, + "customscript/additional_networks.py/txt2img/Network module 4/value": "LoRA", + "customscript/additional_networks.py/txt2img/Model 4/visible": true, + "customscript/additional_networks.py/txt2img/Model 4/value": "None", + "txt2img/Weight 4/visible": true, + "txt2img/Weight 4/value": 1.0, + "txt2img/Weight 4/minimum": -1.0, + "txt2img/Weight 4/maximum": 2.0, + "txt2img/Weight 4/step": 0.05, + "customscript/additional_networks.py/txt2img/UNet Weight 4/value": 1.0, + "customscript/additional_networks.py/txt2img/UNet Weight 4/minimum": -1.0, + "customscript/additional_networks.py/txt2img/UNet Weight 4/maximum": 2.0, + "customscript/additional_networks.py/txt2img/UNet Weight 4/step": 0.05, + "customscript/additional_networks.py/txt2img/TEnc Weight 4/value": 1.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 4/minimum": -1.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 4/maximum": 2.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 4/step": 0.05, + "customscript/additional_networks.py/txt2img/Network module 5/visible": true, + "customscript/additional_networks.py/txt2img/Network module 5/value": "LoRA", + "customscript/additional_networks.py/txt2img/Model 5/visible": true, + "customscript/additional_networks.py/txt2img/Model 5/value": "None", + "txt2img/Weight 5/visible": true, + "txt2img/Weight 5/value": 1.0, + "txt2img/Weight 5/minimum": -1.0, + "txt2img/Weight 5/maximum": 2.0, + "txt2img/Weight 5/step": 0.05, + "customscript/additional_networks.py/txt2img/UNet Weight 5/value": 1.0, + "customscript/additional_networks.py/txt2img/UNet Weight 5/minimum": -1.0, + "customscript/additional_networks.py/txt2img/UNet Weight 5/maximum": 2.0, + "customscript/additional_networks.py/txt2img/UNet Weight 5/step": 0.05, + "customscript/additional_networks.py/txt2img/TEnc Weight 5/value": 1.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 5/minimum": -1.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 5/maximum": 2.0, + "customscript/additional_networks.py/txt2img/TEnc Weight 5/step": 0.05, + "customscript/additional_networks.py/img2img/Enable/visible": true, + "customscript/additional_networks.py/img2img/Enable/value": false, + "customscript/additional_networks.py/img2img/Separate UNet/Text Encoder weights/visible": true, + "customscript/additional_networks.py/img2img/Separate UNet/Text Encoder weights/value": false, + "customscript/additional_networks.py/img2img/Network module 1/visible": true, + "customscript/additional_networks.py/img2img/Network module 1/value": "LoRA", + "customscript/additional_networks.py/img2img/Model 1/visible": true, + "customscript/additional_networks.py/img2img/Model 1/value": "None", + "img2img/Weight 1/visible": true, + "img2img/Weight 1/value": 1.0, + "img2img/Weight 1/minimum": -1.0, + "img2img/Weight 1/maximum": 2.0, + "img2img/Weight 1/step": 0.05, + "customscript/additional_networks.py/img2img/UNet Weight 1/value": 1.0, + "customscript/additional_networks.py/img2img/UNet Weight 1/minimum": -1.0, + "customscript/additional_networks.py/img2img/UNet Weight 1/maximum": 2.0, + "customscript/additional_networks.py/img2img/UNet Weight 1/step": 0.05, + "customscript/additional_networks.py/img2img/TEnc Weight 1/value": 1.0, + "customscript/additional_networks.py/img2img/TEnc Weight 1/minimum": -1.0, + "customscript/additional_networks.py/img2img/TEnc Weight 1/maximum": 2.0, + "customscript/additional_networks.py/img2img/TEnc Weight 1/step": 0.05, + "customscript/additional_networks.py/img2img/Network module 2/visible": true, + "customscript/additional_networks.py/img2img/Network module 2/value": "LoRA", + "customscript/additional_networks.py/img2img/Model 2/visible": true, + "customscript/additional_networks.py/img2img/Model 2/value": "None", + "img2img/Weight 2/visible": true, + "img2img/Weight 2/value": 1.0, + "img2img/Weight 2/minimum": -1.0, + "img2img/Weight 2/maximum": 2.0, + "img2img/Weight 2/step": 0.05, + "customscript/additional_networks.py/img2img/UNet Weight 2/value": 1.0, + "customscript/additional_networks.py/img2img/UNet Weight 2/minimum": -1.0, + "customscript/additional_networks.py/img2img/UNet Weight 2/maximum": 2.0, + "customscript/additional_networks.py/img2img/UNet Weight 2/step": 0.05, + "customscript/additional_networks.py/img2img/TEnc Weight 2/value": 1.0, + "customscript/additional_networks.py/img2img/TEnc Weight 2/minimum": -1.0, + "customscript/additional_networks.py/img2img/TEnc Weight 2/maximum": 2.0, + "customscript/additional_networks.py/img2img/TEnc Weight 2/step": 0.05, + "customscript/additional_networks.py/img2img/Network module 3/visible": true, + "customscript/additional_networks.py/img2img/Network module 3/value": "LoRA", + "customscript/additional_networks.py/img2img/Model 3/visible": true, + "customscript/additional_networks.py/img2img/Model 3/value": "None", + "img2img/Weight 3/visible": true, + "img2img/Weight 3/value": 1.0, + "img2img/Weight 3/minimum": -1.0, + "img2img/Weight 3/maximum": 2.0, + "img2img/Weight 3/step": 0.05, + "customscript/additional_networks.py/img2img/UNet Weight 3/value": 1.0, + "customscript/additional_networks.py/img2img/UNet Weight 3/minimum": -1.0, + "customscript/additional_networks.py/img2img/UNet Weight 3/maximum": 2.0, + "customscript/additional_networks.py/img2img/UNet Weight 3/step": 0.05, + "customscript/additional_networks.py/img2img/TEnc Weight 3/value": 1.0, + "customscript/additional_networks.py/img2img/TEnc Weight 3/minimum": -1.0, + "customscript/additional_networks.py/img2img/TEnc Weight 3/maximum": 2.0, + "customscript/additional_networks.py/img2img/TEnc Weight 3/step": 0.05, + "customscript/additional_networks.py/img2img/Network module 4/visible": true, + "customscript/additional_networks.py/img2img/Network module 4/value": "LoRA", + "customscript/additional_networks.py/img2img/Model 4/visible": true, + "customscript/additional_networks.py/img2img/Model 4/value": "None", + "img2img/Weight 4/visible": true, + "img2img/Weight 4/value": 1.0, + "img2img/Weight 4/minimum": -1.0, + "img2img/Weight 4/maximum": 2.0, + "img2img/Weight 4/step": 0.05, + "customscript/additional_networks.py/img2img/UNet Weight 4/value": 1.0, + "customscript/additional_networks.py/img2img/UNet Weight 4/minimum": -1.0, + "customscript/additional_networks.py/img2img/UNet Weight 4/maximum": 2.0, + "customscript/additional_networks.py/img2img/UNet Weight 4/step": 0.05, + "customscript/additional_networks.py/img2img/TEnc Weight 4/value": 1.0, + "customscript/additional_networks.py/img2img/TEnc Weight 4/minimum": -1.0, + "customscript/additional_networks.py/img2img/TEnc Weight 4/maximum": 2.0, + "customscript/additional_networks.py/img2img/TEnc Weight 4/step": 0.05, + "customscript/additional_networks.py/img2img/Network module 5/visible": true, + "customscript/additional_networks.py/img2img/Network module 5/value": "LoRA", + "customscript/additional_networks.py/img2img/Model 5/visible": true, + "customscript/additional_networks.py/img2img/Model 5/value": "None", + "img2img/Weight 5/visible": true, + "img2img/Weight 5/value": 1.0, + "img2img/Weight 5/minimum": -1.0, + "img2img/Weight 5/maximum": 2.0, + "img2img/Weight 5/step": 0.05, + "customscript/additional_networks.py/img2img/UNet Weight 5/value": 1.0, + "customscript/additional_networks.py/img2img/UNet Weight 5/minimum": -1.0, + "customscript/additional_networks.py/img2img/UNet Weight 5/maximum": 2.0, + "customscript/additional_networks.py/img2img/UNet Weight 5/step": 0.05, + "customscript/additional_networks.py/img2img/TEnc Weight 5/value": 1.0, + "customscript/additional_networks.py/img2img/TEnc Weight 5/minimum": -1.0, + "customscript/additional_networks.py/img2img/TEnc Weight 5/maximum": 2.0, + "customscript/additional_networks.py/img2img/TEnc Weight 5/step": 0.05 } \ No newline at end of file diff --git a/webui.py b/webui.py index 19489d5e6..bdbb53000 100644 --- a/webui.py +++ b/webui.py @@ -4,12 +4,10 @@ import signal import re import logging import warnings - from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.gzip import GZipMiddleware from setup import log - -from modules import paths, timer, errors +from modules import timer, errors errors.install() startup_timer = timer.Timer() @@ -35,7 +33,10 @@ if ".dev" in torch.__version__ or "+git" in torch.__version__: torch.__long_version__ = torch.__version__ torch.__version__ = re.search(r'[\d.]+[\d]', torch.__version__).group(0) -from modules import shared, devices, sd_samplers, upscaler, extensions, ui_tempdir, ui_extra_networks +from modules import shared, extensions, ui_tempdir, ui_extra_networks +import modules.devices +import modules.sd_samplers +import modules.upscaler import modules.codeformer_model as codeformer import modules.face_restoration import modules.gfpgan_model as gfpgan