rework #6329 to remove duplicate code and add prevent tab names for showing in ids for scripts that only exist on one tab

This commit is contained in:
AUTOMATIC 2023-01-05 12:08:11 +03:00
parent 997461d3dd
commit f8d0cf6a6e
10 changed files with 10 additions and 53 deletions

View file

@ -1,4 +1,5 @@
import os import os
import re
import sys import sys
import traceback import traceback
from collections import namedtuple from collections import namedtuple
@ -128,6 +129,15 @@ class Script:
"""unused""" """unused"""
return "" return ""
def elem_id(self, item_id):
"""helper function to generate id for a HTML element, constructs final id out of script name, tab and user-supplied item_id"""
need_tabname = self.show(True) == self.show(False)
tabname = ('img2img' if self.is_img2img else 'txt2txt') + "_" if need_tabname else ""
title = re.sub(r'[^a-z_0-9]', '', re.sub(r'\s', '_', self.title().lower()))
return f'script_{tabname}{title}_{item_id}'
current_basedir = paths.script_path current_basedir = paths.script_path

View file

@ -3,18 +3,12 @@ import gradio as gr
from modules.processing import Processed from modules.processing import Processed
from modules.shared import opts, cmd_opts, state from modules.shared import opts, cmd_opts, state
import re
class Script(scripts.Script): class Script(scripts.Script):
def title(self): def title(self):
return "Custom code" return "Custom code"
def elem_id(self, item_id):
gen_elem_id = ('img2img' if self.is_img2img else 'txt2txt') + '_script_' + re.sub(r'\s', '_', self.title().lower()) + '_' + item_id
gen_elem_id = re.sub(r'[^a-z_0-9]', '', gen_elem_id)
return gen_elem_id
def show(self, is_img2img): def show(self, is_img2img):
return cmd_opts.allow_code return cmd_opts.allow_code

View file

@ -16,7 +16,6 @@ import k_diffusion as K
from PIL import Image from PIL import Image
from torch import autocast from torch import autocast
from einops import rearrange, repeat from einops import rearrange, repeat
import re
def find_noise_for_image(p, cond, uncond, cfg_scale, steps): def find_noise_for_image(p, cond, uncond, cfg_scale, steps):
@ -123,11 +122,6 @@ class Script(scripts.Script):
def title(self): def title(self):
return "img2img alternative test" return "img2img alternative test"
def elem_id(self, item_id):
gen_elem_id = ('img2img' if self.is_img2img else 'txt2txt') + '_script_' + re.sub(r'\s', '_', self.title().lower()) + '_' + item_id
gen_elem_id = re.sub(r'[^a-z_0-9]', '', gen_elem_id)
return gen_elem_id
def show(self, is_img2img): def show(self, is_img2img):
return is_img2img return is_img2img

View file

@ -8,18 +8,12 @@ from modules import processing, shared, sd_samplers, images
from modules.processing import Processed from modules.processing import Processed
from modules.sd_samplers import samplers from modules.sd_samplers import samplers
from modules.shared import opts, cmd_opts, state from modules.shared import opts, cmd_opts, state
import re
class Script(scripts.Script): class Script(scripts.Script):
def title(self): def title(self):
return "Loopback" return "Loopback"
def elem_id(self, item_id):
gen_elem_id = ('img2img' if self.is_img2img else 'txt2txt') + '_script_' + re.sub(r'\s', '_', self.title().lower()) + '_' + item_id
gen_elem_id = re.sub(r'[^a-z_0-9]', '', gen_elem_id)
return gen_elem_id
def show(self, is_img2img): def show(self, is_img2img):
return is_img2img return is_img2img

View file

@ -10,7 +10,6 @@ from PIL import Image, ImageDraw
from modules import images, processing, devices from modules import images, processing, devices
from modules.processing import Processed, process_images from modules.processing import Processed, process_images
from modules.shared import opts, cmd_opts, state from modules.shared import opts, cmd_opts, state
import re
# this function is taken from https://github.com/parlance-zz/g-diffuser-bot # this function is taken from https://github.com/parlance-zz/g-diffuser-bot
@ -123,11 +122,6 @@ class Script(scripts.Script):
def title(self): def title(self):
return "Outpainting mk2" return "Outpainting mk2"
def elem_id(self, item_id):
gen_elem_id = ('img2img' if self.is_img2img else 'txt2txt') + '_script_' + re.sub(r'\s', '_', self.title().lower()) + '_' + item_id
gen_elem_id = re.sub(r'[^a-z_0-9]', '', gen_elem_id)
return gen_elem_id
def show(self, is_img2img): def show(self, is_img2img):
return is_img2img return is_img2img

View file

@ -7,18 +7,12 @@ from PIL import Image, ImageDraw
from modules import images, processing, devices from modules import images, processing, devices
from modules.processing import Processed, process_images from modules.processing import Processed, process_images
from modules.shared import opts, cmd_opts, state from modules.shared import opts, cmd_opts, state
import re
class Script(scripts.Script): class Script(scripts.Script):
def title(self): def title(self):
return "Poor man's outpainting" return "Poor man's outpainting"
def elem_id(self, item_id):
gen_elem_id = ('img2img' if self.is_img2img else 'txt2txt') + '_script_' + re.sub(r'\s', '_', self.title().lower()) + '_' + item_id
gen_elem_id = re.sub(r'[^a-z_0-9]', '', gen_elem_id)
return gen_elem_id
def show(self, is_img2img): def show(self, is_img2img):
return is_img2img return is_img2img

View file

@ -10,7 +10,6 @@ from modules import images
from modules.processing import process_images, Processed from modules.processing import process_images, Processed
from modules.shared import opts, cmd_opts, state from modules.shared import opts, cmd_opts, state
import modules.sd_samplers import modules.sd_samplers
import re
def draw_xy_grid(xs, ys, x_label, y_label, cell): def draw_xy_grid(xs, ys, x_label, y_label, cell):
@ -45,11 +44,6 @@ class Script(scripts.Script):
def title(self): def title(self):
return "Prompt matrix" return "Prompt matrix"
def elem_id(self, item_id):
gen_elem_id = ('img2img' if self.is_img2img else 'txt2txt') + '_script_' + re.sub(r'\s', '_', self.title().lower()) + '_' + item_id
gen_elem_id = re.sub(r'[^a-z_0-9]', '', gen_elem_id)
return gen_elem_id
def ui(self, is_img2img): def ui(self, is_img2img):
put_at_start = gr.Checkbox(label='Put variable parts at start of prompt', value=False, elem_id=self.elem_id("put_at_start")) put_at_start = gr.Checkbox(label='Put variable parts at start of prompt', value=False, elem_id=self.elem_id("put_at_start"))
different_seeds = gr.Checkbox(label='Use different seed for each picture', value=False, elem_id=self.elem_id("different_seeds")) different_seeds = gr.Checkbox(label='Use different seed for each picture', value=False, elem_id=self.elem_id("different_seeds"))

View file

@ -13,7 +13,6 @@ from modules import sd_samplers
from modules.processing import Processed, process_images from modules.processing import Processed, process_images
from PIL import Image from PIL import Image
from modules.shared import opts, cmd_opts, state from modules.shared import opts, cmd_opts, state
import re
def process_string_tag(tag): def process_string_tag(tag):
@ -112,11 +111,6 @@ class Script(scripts.Script):
def title(self): def title(self):
return "Prompts from file or textbox" return "Prompts from file or textbox"
def elem_id(self, item_id):
gen_elem_id = ('img2img' if self.is_img2img else 'txt2txt') + '_script_' + re.sub(r'\s', '_', self.title().lower()) + '_' + item_id
gen_elem_id = re.sub(r'[^a-z_0-9]', '', gen_elem_id)
return gen_elem_id
def ui(self, is_img2img): def ui(self, is_img2img):
checkbox_iterate = gr.Checkbox(label="Iterate seed every line", value=False, elem_id=self.elem_id("checkbox_iterate")) checkbox_iterate = gr.Checkbox(label="Iterate seed every line", value=False, elem_id=self.elem_id("checkbox_iterate"))
checkbox_iterate_batch = gr.Checkbox(label="Use same random seed for all lines", value=False, elem_id=self.elem_id("checkbox_iterate_batch")) checkbox_iterate_batch = gr.Checkbox(label="Use same random seed for all lines", value=False, elem_id=self.elem_id("checkbox_iterate_batch"))

View file

@ -7,18 +7,12 @@ from PIL import Image
from modules import processing, shared, sd_samplers, images, devices from modules import processing, shared, sd_samplers, images, devices
from modules.processing import Processed from modules.processing import Processed
from modules.shared import opts, cmd_opts, state from modules.shared import opts, cmd_opts, state
import re
class Script(scripts.Script): class Script(scripts.Script):
def title(self): def title(self):
return "SD upscale" return "SD upscale"
def elem_id(self, item_id):
gen_elem_id = ('img2img' if self.is_img2img else 'txt2txt') + '_script_' + re.sub(r'\s', '_', self.title().lower()) + '_' + item_id
gen_elem_id = re.sub(r'[^a-z_0-9]', '', gen_elem_id)
return gen_elem_id
def show(self, is_img2img): def show(self, is_img2img):
return is_img2img return is_img2img

View file

@ -290,11 +290,6 @@ class Script(scripts.Script):
def title(self): def title(self):
return "X/Y plot" return "X/Y plot"
def elem_id(self, item_id):
gen_elem_id = ('img2img' if self.is_img2img else 'txt2txt') + '_script_' + re.sub(r'\s', '_', self.title().lower()) + '_' + item_id
gen_elem_id = re.sub(r'[^a-z_0-9]', '', gen_elem_id)
return gen_elem_id
def ui(self, is_img2img): def ui(self, is_img2img):
current_axis_options = [x for x in axis_options if type(x) == AxisOption or type(x) == AxisOptionImg2Img and is_img2img] current_axis_options = [x for x in axis_options if type(x) == AxisOption or type(x) == AxisOptionImg2Img and is_img2img]