modularize ui imports

This commit is contained in:
Vladimir Mandic
2023-12-22 08:07:34 -05:00
parent ec0a08c4db
commit 4b74d3ebf9
4 changed files with 127 additions and 115 deletions
+114 -112
View File
@@ -9,41 +9,32 @@ import gradio.utils
import numpy as np
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, script_callbacks, ui_extensions, deepbooru, extra_networks, ui_common, ui_postprocessing, ui_loadsave, ui_train, ui_models, ui_control, ui_interrogate, modelloader
from modules import timer, shared, theme, sd_models, script_callbacks, modelloader, prompt_parser, ui_common, ui_loadsave, ui_symbols, generation_parameters_copypaste
from modules.ui_components import FormRow, FormGroup, ToolButton, FormHTML
from modules.paths import script_path, data_path
from modules.shared import opts, cmd_opts
from modules.dml import directml_override_opts
from modules import prompt_parser
from modules import timer
import modules.ui_symbols as symbols
import modules.generation_parameters_copypaste as parameters_copypaste
import modules.hypernetworks.ui
import modules.scripts
import modules.shared
import modules.errors
import modules.styles
import modules.extras
import modules.theme
import modules.textual_inversion.ui
import modules.sd_samplers
import modules.hypernetworks.ui
import modules.errors
modules.errors.install()
mimetypes.init()
mimetypes.add_type('application/javascript', '.js')
log = modules.shared.log
log = shared.log
opts = shared.opts
cmd_opts = shared.cmd_opts
ui_system_tabs = None
switch_values_symbol = symbols.switch
detect_image_size_symbol = symbols.detect
paste_symbol = symbols.paste
clear_prompt_symbol = symbols.clear
restore_progress_symbol = symbols.apply
folder_symbol = symbols.folder
extra_networks_symbol = symbols.networks
apply_style_symbol = symbols.apply
save_style_symbol = symbols.save
switch_values_symbol = ui_symbols.switch
detect_image_size_symbol = ui_symbols.detect
paste_symbol = ui_symbols.paste
clear_prompt_symbol = ui_symbols.clear
restore_progress_symbol = ui_symbols.apply
folder_symbol = ui_symbols.folder
extra_networks_symbol = ui_symbols.networks
apply_style_symbol = ui_symbols.apply
save_style_symbol = ui_symbols.save
txt2img_paste_fields = []
img2img_paste_fields = []
txt2img_args = []
@@ -77,16 +68,17 @@ def infotext_to_html(text): # may be referenced by extensions
def send_gradio_gallery_to_image(x):
if len(x) == 0:
return None
return parameters_copypaste.image_from_url_text(x[0])
return generation_parameters_copypaste.image_from_url_text(x[0])
def add_style(name: str, prompt: str, negative_prompt: str):
from modules import styles
if name is None:
return [gr_show() for x in range(4)]
style = modules.styles.Style(name, prompt, negative_prompt)
modules.shared.prompt_styles.styles[style.name] = style
modules.shared.prompt_styles.save_styles(modules.shared.opts.styles_dir)
return [gr.Dropdown.update(visible=True, choices=list(modules.shared.prompt_styles.styles)) for _ in range(2)]
style = styles.Style(name, prompt, negative_prompt)
shared.prompt_styles.styles[style.name] = style
shared.prompt_styles.save_styles(shared.opts.styles_dir)
return [gr.Dropdown.update(visible=True, choices=list(shared.prompt_styles.styles)) for _ in range(2)]
def calc_resolution_hires(width, height, hr_scale, hr_resize_x, hr_resize_y, hr_upscaler):
@@ -109,8 +101,8 @@ def resize_from_to_html(width, height, scale_by):
def apply_styles(prompt, prompt_neg, styles):
prompt = modules.shared.prompt_styles.apply_styles_to_prompt(prompt, styles)
prompt_neg = modules.shared.prompt_styles.apply_negative_styles_to_prompt(prompt_neg, styles)
prompt = shared.prompt_styles.apply_styles_to_prompt(prompt, styles)
prompt_neg = shared.prompt_styles.apply_negative_styles_to_prompt(prompt_neg, styles)
return [gr.Textbox.update(value=prompt), gr.Textbox.update(value=prompt_neg), gr.Dropdown.update(value=[])]
@@ -130,7 +122,7 @@ def process_interrogate(interrogation_function, mode, ii_input_files, ii_input_d
if not os.path.isdir(ii_input_dir):
log.error(f"Interrogate: Input directory not found: {ii_input_dir}")
return [gr.update(), None]
images = modules.shared.listfiles(ii_input_dir)
images = shared.listfiles(ii_input_dir)
if ii_output_dir != "":
os.makedirs(ii_output_dir, exist_ok=True)
else:
@@ -147,11 +139,12 @@ def interrogate(image):
if image is None:
log.error("Interrogate: no image selected")
return gr.update()
prompt = modules.shared.interrogator.interrogate(image.convert("RGB"))
prompt = shared.interrogator.interrogate(image.convert("RGB"))
return gr.update() if prompt is None else prompt
def interrogate_deepbooru(image):
from modules import deepbooru
prompt = deepbooru.model.tag(image)
return gr.update() if prompt is None else prompt
@@ -161,7 +154,7 @@ def create_batch_inputs(tab):
with FormRow(elem_id=f"{tab}_row_batch"):
batch_count = gr.Slider(minimum=1, step=1, label='Batch count', value=1, elem_id=f"{tab}_batch_count")
batch_size = gr.Slider(minimum=1, maximum=32, step=1, label='Batch size', value=1, elem_id=f"{tab}_batch_size")
batch_switch_btn = ToolButton(value=symbols.switch, elem_id=f"{tab}_batch_switch_btn", label="Switch dims")
batch_switch_btn = ToolButton(value=ui_symbols.switch, elem_id=f"{tab}_batch_switch_btn", label="Switch dims")
batch_switch_btn.click(lambda w, h: (h, w), inputs=[batch_count, batch_size], outputs=[batch_count, batch_size], show_progress=False)
return batch_count, batch_size
@@ -170,12 +163,12 @@ def create_seed_inputs(tab, reuse_visible=True):
with gr.Accordion(open=False, label="Seed", elem_id=f"{tab}_seed_group", elem_classes=["small-accordion"]):
with FormRow(elem_id=f"{tab}_seed_row", variant="compact"):
seed = gr.Number(label='Initial seed', value=-1, elem_id=f"{tab}_seed", container=True)
random_seed = ToolButton(symbols.random, elem_id=f"{tab}_random_seed", label='Random seed')
reuse_seed = ToolButton(symbols.reuse, elem_id=f"{tab}_reuse_seed", label='Reuse seed', visible=reuse_visible)
random_seed = ToolButton(ui_symbols.random, elem_id=f"{tab}_random_seed", label='Random seed')
reuse_seed = ToolButton(ui_symbols.reuse, elem_id=f"{tab}_reuse_seed", label='Reuse seed', visible=reuse_visible)
with FormRow(visible=True, elem_id=f"{tab}_subseed_row", variant="compact"):
subseed = gr.Number(label='Variation', value=-1, elem_id=f"{tab}_subseed", container=True)
random_subseed = ToolButton(symbols.random, elem_id=f"{tab}_random_subseed")
reuse_subseed = ToolButton(symbols.reuse, elem_id=f"{tab}_reuse_subseed", visible=reuse_visible)
random_subseed = ToolButton(ui_symbols.random, elem_id=f"{tab}_random_subseed")
reuse_subseed = ToolButton(ui_symbols.reuse, elem_id=f"{tab}_reuse_subseed", visible=reuse_visible)
subseed_strength = gr.Slider(label='Variation strength', value=0.0, minimum=0, maximum=1, step=0.01, elem_id=f"{tab}_subseed_strength")
with FormRow(visible=False):
seed_resize_from_w = gr.Slider(minimum=0, maximum=4096, step=8, label="Resize seed from width", value=0, elem_id=f"{tab}_seed_resize_from_w")
@@ -193,13 +186,13 @@ def create_advanced_inputs(tab):
clip_skip = gr.Slider(label='CLIP skip', value=1, minimum=1, maximum=14, step=1, elem_id=f"{tab}_clip_skip", interactive=True)
with FormRow():
image_cfg_scale = gr.Slider(minimum=0.0, maximum=30.0, step=0.1, label='Secondary CFG scale', value=6.0, elem_id=f"{tab}_image_cfg_scale")
diffusers_guidance_rescale = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, label='Guidance rescale', value=0.7, elem_id=f"{tab}_image_cfg_rescale", visible=modules.shared.backend == modules.shared.Backend.DIFFUSERS)
diffusers_guidance_rescale = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, label='Guidance rescale', value=0.7, elem_id=f"{tab}_image_cfg_rescale", visible=shared.backend == shared.Backend.DIFFUSERS)
with gr.Group():
with FormRow():
full_quality = gr.Checkbox(label='Full quality', value=True, elem_id=f"{tab}_full_quality")
restore_faces = gr.Checkbox(label='Face restore', value=False, visible=len(modules.shared.face_restorers) > 1, elem_id=f"{tab}_restore_faces")
tiling = gr.Checkbox(label='Tiling', value=False, elem_id=f"{tab}_tiling", visible=modules.shared.backend == modules.shared.Backend.ORIGINAL)
with gr.Group(visible=modules.shared.backend == modules.shared.Backend.DIFFUSERS):
restore_faces = gr.Checkbox(label='Face restore', value=False, visible=len(shared.face_restorers) > 1, elem_id=f"{tab}_restore_faces")
tiling = gr.Checkbox(label='Tiling', value=False, elem_id=f"{tab}_tiling", visible=shared.backend == shared.Backend.ORIGINAL)
with gr.Group(visible=shared.backend == shared.Backend.DIFFUSERS):
with FormRow():
hdr_clamp = gr.Checkbox(label='HDR clamp', value=False, elem_id=f"{tab}_hdr_clamp")
hdr_boundary = gr.Slider(minimum=0.0, maximum=10.0, step=0.1, value=4.0, label='Range', elem_id=f"{tab}_hdr_boundary")
@@ -219,10 +212,10 @@ def create_resize_inputs(tab, images, time_selector=False, scale_visible=True):
dummy_component = gr.Number(visible=False, value=0)
with gr.Accordion(open=False, label="Resize", elem_classes=["small-accordion"], elem_id=f"{tab}_resize_group"):
with gr.Row():
resize_mode = gr.Radio(label="Resize mode", elem_id=f"{tab}_resize_mode", choices=modules.shared.resize_modes, type="index", value="None")
resize_mode = gr.Radio(label="Resize mode", elem_id=f"{tab}_resize_mode", choices=shared.resize_modes, type="index", value="None")
resize_time = gr.Radio(label="Resize order", elem_id=f"{tab}_resize_order", choices=['Before', 'After'], value="Before", visible=time_selector)
with gr.Row():
resize_name = gr.Dropdown(label="Resize method", elem_id=f"{tab}_resize_name", choices=[x.name for x in modules.shared.sd_upscalers], value=opts.upscaler_for_img2img)
resize_name = gr.Dropdown(label="Resize method", elem_id=f"{tab}_resize_name", choices=[x.name for x in shared.sd_upscalers], value=opts.upscaler_for_img2img)
create_refresh_button(resize_name, modelloader.load_upscalers, lambda: {"choices": modelloader.load_upscalers()}, 'refresh_upscalers')
with FormRow(visible=True) as _resize_group:
@@ -235,9 +228,9 @@ def create_resize_inputs(tab, images, time_selector=False, scale_visible=True):
with FormRow():
width = gr.Slider(minimum=64, maximum=8192, step=8, label="Width", value=512, elem_id=f"{tab}_width")
height = gr.Slider(minimum=64, maximum=8192, step=8, label="Height", value=512, elem_id=f"{tab}_height")
res_switch_btn = ToolButton(value=symbols.switch, elem_id=f"{tab}_res_switch_btn")
res_switch_btn = ToolButton(value=ui_symbols.switch, elem_id=f"{tab}_res_switch_btn")
res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False)
detect_image_size_btn = ToolButton(value=symbols.detect, elem_id=f"{tab}_detect_image_size_btn")
detect_image_size_btn = ToolButton(value=ui_symbols.detect, elem_id=f"{tab}_detect_image_size_btn")
detect_image_size_btn.click(fn=lambda w, h, _: (w or gr.update(), h or gr.update()), _js="currentImg2imgSourceResolution", inputs=[dummy_component, dummy_component, dummy_component], outputs=[width, height], show_progress=False)
with gr.Tab(label="Resize by") as tab_scale_by:
@@ -293,28 +286,28 @@ def connect_reuse_seed(seed: gr.Number, reuse_seed: gr.Button, generation_info:
def update_token_counter(text, steps):
from modules import extra_networks, sd_hijack
try:
text, _ = extra_networks.parse_prompt(text)
_, prompt_flat_list, _ = prompt_parser.get_multicond_prompt_list([text])
prompt_schedules = prompt_parser.get_learned_conditioning_prompt_schedules(prompt_flat_list, steps)
except Exception:
# a parsing error can happen here during typing, and we don't want to bother the user with
# messages related to it in console
prompt_schedules = [[[steps, text]]]
flat_prompts = reduce(lambda list1, list2: list1+list2, prompt_schedules)
prompts = [prompt_text for step, prompt_text in flat_prompts]
if modules.shared.backend == modules.shared.Backend.ORIGINAL:
if shared.backend == shared.Backend.ORIGINAL:
token_count, max_length = max([sd_hijack.model_hijack.get_prompt_lengths(prompt) for prompt in prompts], key=lambda args: args[0])
elif modules.shared.backend == modules.shared.Backend.DIFFUSERS:
if modules.shared.sd_model is not None and hasattr(modules.shared.sd_model, 'tokenizer'):
tokenizer = modules.shared.sd_model.tokenizer
elif shared.backend == shared.Backend.DIFFUSERS:
if shared.sd_model is not None and hasattr(shared.sd_model, 'tokenizer'):
tokenizer = shared.sd_model.tokenizer
if tokenizer is None:
token_count = 0
max_length = 75
else:
has_bos_token = tokenizer.bos_token_id is not None
has_eos_token = tokenizer.eos_token_id is not None
ids = [modules.shared.sd_model.tokenizer(prompt) for prompt in prompts]
ids = [shared.sd_model.tokenizer(prompt) for prompt in prompts]
if len(ids) > 0 and hasattr(ids[0], 'input_ids'):
ids = [x.input_ids for x in ids]
token_count = max([len(x) for x in ids]) - int(has_bos_token) - int(has_eos_token)
@@ -349,11 +342,11 @@ def create_toprow(is_img2img: bool = False, id_part: str = None):
submit = gr.Button('Generate', elem_id=f"{id_part}_generate", variant='primary')
with gr.Row(elem_id=f"{id_part}_generate_line2"):
interrupt = gr.Button('Stop', elem_id=f"{id_part}_interrupt")
interrupt.click(fn=lambda: modules.shared.state.interrupt(), _js="requestInterrupt", inputs=[], outputs=[])
interrupt.click(fn=lambda: shared.state.interrupt(), _js="requestInterrupt", inputs=[], outputs=[])
skip = gr.Button('Skip', elem_id=f"{id_part}_skip")
skip.click(fn=lambda: modules.shared.state.skip(), inputs=[], outputs=[])
skip.click(fn=lambda: shared.state.skip(), inputs=[], outputs=[])
pause = gr.Button('Pause', elem_id=f"{id_part}_pause")
pause.click(fn=lambda: modules.shared.state.pause(), _js='checkPaused', inputs=[], outputs=[])
pause.click(fn=lambda: shared.state.pause(), _js='checkPaused', inputs=[], outputs=[])
with gr.Row(elem_id=f"{id_part}_tools"):
button_paste = gr.Button(value='Restore', variant='secondary', elem_id=f"{id_part}_paste") # symbols.paste
button_clear = gr.Button(value='Clear', variant='secondary', elem_id=f"{id_part}_clear_prompt_btn") # symbols.clear
@@ -365,13 +358,13 @@ def create_toprow(is_img2img: bool = False, id_part: str = None):
negative_token_counter = gr.HTML(value="<span>0/75</span>", elem_id=f"{id_part}_negative_token_counter", elem_classes=["token-counter"])
negative_token_button = gr.Button(visible=False, elem_id=f"{id_part}_negative_token_button")
with gr.Row(elem_id=f"{id_part}_styles_row"):
styles = gr.Dropdown(label="Styles", elem_id=f"{id_part}_styles", choices=[style.name for style in modules.shared.prompt_styles.styles.values()], value=[], multiselect=True)
_styles_btn_refresh = create_refresh_button(styles, modules.shared.prompt_styles.reload, lambda: {"choices": list(modules.shared.prompt_styles.styles)}, f"{id_part}_styles_refresh")
styles = gr.Dropdown(label="Styles", elem_id=f"{id_part}_styles", choices=[style.name for style in shared.prompt_styles.styles.values()], value=[], multiselect=True)
_styles_btn_refresh = create_refresh_button(styles, shared.prompt_styles.reload, lambda: {"choices": list(shared.prompt_styles.styles)}, f"{id_part}_styles_refresh")
# styles_btn_refresh = ToolButton(symbols.refresh, elem_id=f"{id_part}_styles_refresh", visible=True)
# styles_btn_refresh.click(fn=lambda: gr.update(choices=[style.name for style in modules.shared.prompt_styles.styles.values()]), inputs=[], outputs=[styles])
# styles_btn_refresh.click(fn=lambda: gr.update(choices=[style.name for style in shared.prompt_styles.styles.values()]), inputs=[], outputs=[styles])
styles_btn_select = gr.Button('Select', elem_id=f"{id_part}_styles_select", visible=False)
styles_btn_select.click(_js="applyStyles", fn=parse_style, inputs=[styles], outputs=[styles])
styles_btn_apply = ToolButton(symbols.apply, elem_id=f"{id_part}_extra_apply", visible=False)
styles_btn_apply = ToolButton(ui_symbols.apply, elem_id=f"{id_part}_extra_apply", visible=False)
styles_btn_apply.click(fn=apply_styles, inputs=[prompt, negative_prompt, styles], outputs=[prompt, negative_prompt, styles])
return prompt, styles, negative_prompt, submit, button_interrogate, button_deepbooru, button_paste, button_extra, token_counter, token_button, negative_token_counter, negative_token_button
@@ -383,7 +376,7 @@ def setup_progressbar(*args, **kwargs): # pylint: disable=unused-argument
def apply_setting(key, value):
if value is None:
return gr.update()
if modules.shared.cmd_opts.freeze:
if shared.cmd_opts.freeze:
return gr.update()
# dont allow model to be swapped when model hash exists in prompt
if key == "sd_model_checkpoint" and opts.disable_weights_auto_swap:
@@ -402,7 +395,7 @@ def apply_setting(key, value):
opts.data[key] = valtype(value) if valtype != type(None) else value
if oldval != value and opts.data_labels[key].onchange is not None:
opts.data_labels[key].onchange()
opts.save(modules.shared.config_filename)
opts.save(shared.config_filename)
return getattr(opts, key)
@@ -415,19 +408,19 @@ def create_sampler_and_steps_selection(choices, tabname):
opts.data['schedulers_brownian_noise'] = 'brownian noise' in sampler_options
opts.data['schedulers_discard_penultimate'] = 'discard penultimate sigma' in sampler_options
opts.data['schedulers_sigma'] = sampler_algo
opts.save(modules.shared.config_filename, silent=True)
opts.save(shared.config_filename, silent=True)
def set_sampler_diffuser_options(sampler_options):
opts.data['schedulers_use_karras'] = 'karras' in sampler_options
opts.data['schedulers_use_thresholding'] = 'dynamic thresholding' in sampler_options
opts.data['schedulers_use_loworder'] = 'low order' in sampler_options
opts.data['schedulers_rescale_betas'] = 'rescale beta' in sampler_options
opts.save(modules.shared.config_filename, silent=True)
opts.save(shared.config_filename, silent=True)
with FormRow(elem_classes=['flex-break']):
sampler_index = gr.Dropdown(label='Sampling method', elem_id=f"{tabname}_sampling", choices=[x.name for x in choices], value='Default', type="index")
steps = gr.Slider(minimum=1, maximum=99, step=1, label="Sampling steps", elem_id=f"{tabname}_steps", value=20)
if modules.shared.backend == modules.shared.Backend.ORIGINAL:
if shared.backend == shared.Backend.ORIGINAL:
with FormRow(elem_classes=['flex-break']):
choices = ['brownian noise', 'discard penultimate sigma']
values = []
@@ -453,10 +446,11 @@ def create_sampler_and_steps_selection(choices, tabname):
def create_sampler_inputs(tab):
from modules import sd_samplers
with gr.Accordion(open=False, label="Sampler", elem_id=f"{tab}_sampler", elem_classes=["small-accordion"]):
with FormRow(elem_id=f"{tab}_row_sampler"):
modules.sd_samplers.set_samplers()
steps, sampler_index = create_sampler_and_steps_selection(modules.sd_samplers.samplers, tab)
sd_samplers.set_samplers()
steps, sampler_index = create_sampler_and_steps_selection(sd_samplers.samplers, tab)
return steps, sampler_index
@@ -471,7 +465,7 @@ def create_hires_inputs(tab):
with FormRow(elem_id=f"{tab}_hires_finalres", variant="compact"):
hr_final_resolution = FormHTML(value="", elem_id=f"{tab}_hr_finalres", label="Upscaled resolution", interactive=False)
with FormRow(elem_id=f"{tab}_hires_fix_row1", variant="compact"):
hr_upscaler = gr.Dropdown(label="Upscaler", elem_id=f"{tab}_hr_upscaler", choices=[*modules.shared.latent_upscale_modes, *[x.name for x in modules.shared.sd_upscalers]], value=modules.shared.latent_upscale_default_mode)
hr_upscaler = gr.Dropdown(label="Upscaler", elem_id=f"{tab}_hr_upscaler", choices=[*shared.latent_upscale_modes, *[x.name for x in shared.sd_upscalers]], value=shared.latent_upscale_default_mode)
hr_force = gr.Checkbox(label='Force Hires', value=False, elem_id=f"{tab}_hr_force")
with FormRow(elem_id=f"{tab}_hires_fix_row2", variant="compact"):
hr_second_pass_steps = gr.Slider(minimum=0, maximum=99, step=1, label='Hires steps', elem_id=f"{tab}_steps_alt", value=20)
@@ -479,7 +473,7 @@ def create_hires_inputs(tab):
with FormRow(elem_id=f"{tab}_hires_fix_row3", variant="compact"):
hr_resize_x = gr.Slider(minimum=0, maximum=4096, step=8, label="Resize width to", value=0, elem_id=f"{tab}_hr_resize_x")
hr_resize_y = gr.Slider(minimum=0, maximum=4096, step=8, label="Resize height to", value=0, elem_id=f"{tab}_hr_resize_y")
with FormGroup(visible=modules.shared.backend == modules.shared.Backend.DIFFUSERS):
with FormGroup(visible=shared.backend == shared.Backend.DIFFUSERS):
with FormRow(elem_id=f"{tab}_refiner_row1", variant="compact"):
refiner_start = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, label='Refiner start', value=0.8, elem_id=f"{tab}_refiner_start")
refiner_steps = gr.Slider(minimum=0, maximum=99, step=1, label="Refiner steps", elem_id=f"{tab}_refiner_steps", value=5)
@@ -513,7 +507,7 @@ def create_ui(startup_timer = None):
if startup_timer is None:
timer.startup = timer.Timer()
reload_javascript()
parameters_copypaste.reset()
generation_parameters_copypaste.reset()
import modules.txt2img # pylint: disable=redefined-outer-name
modules.scripts.scripts_current = modules.scripts.scripts_txt2img
@@ -535,7 +529,7 @@ def create_ui(startup_timer = None):
with FormRow():
width = gr.Slider(minimum=64, maximum=4096, step=8, label="Width", value=512, elem_id="txt2img_width")
height = gr.Slider(minimum=64, maximum=4096, step=8, label="Height", value=512, elem_id="txt2img_height")
res_switch_btn = ToolButton(value=symbols.switch, elem_id="txt2img_res_switch_btn", label="Switch dims")
res_switch_btn = ToolButton(value=ui_symbols.switch, elem_id="txt2img_res_switch_btn", label="Switch dims")
res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False)
with FormGroup(elem_classes="settings-accordion"):
@@ -642,9 +636,9 @@ def create_ui(startup_timer = None):
(seed_resize_from_h, "Seed resize from-2"),
*modules.scripts.scripts_txt2img.infotext_fields
]
parameters_copypaste.add_paste_fields("txt2img", None, txt2img_paste_fields, override_settings)
txt2img_bindings = parameters_copypaste.ParamBinding(paste_button=txt2img_paste, tabname="txt2img", source_text_component=txt2img_prompt, source_image_component=None)
parameters_copypaste.register_paste_params_button(txt2img_bindings)
generation_parameters_copypaste.add_paste_fields("txt2img", None, txt2img_paste_fields, override_settings)
txt2img_bindings = generation_parameters_copypaste.ParamBinding(paste_button=txt2img_paste, tabname="txt2img", source_text_component=txt2img_prompt, source_image_component=None)
generation_parameters_copypaste.register_paste_params_button(txt2img_bindings)
txt2img_token_button.click(fn=wrap_queued_call(update_token_counter), inputs=[txt2img_prompt, steps], outputs=[txt2img_token_counter])
txt2img_negative_token_button.click(fn=wrap_queued_call(update_token_counter), inputs=[txt2img_negative_prompt, steps], outputs=[txt2img_negative_token_counter])
@@ -716,16 +710,16 @@ def create_ui(startup_timer = None):
init_mask_inpaint = gr.Image(label="Mask", source="upload", interactive=True, type="pil", elem_id="img_inpaint_mask")
with gr.TabItem('Batch', id='batch', elem_id="img2img_batch_tab") as tab_batch:
hidden = '<br>Disabled when launched with --hide-ui-dir-config.' if modules.shared.cmd_opts.hide_ui_dir_config else ''
hidden = '<br>Disabled when launched with --hide-ui-dir-config.' if shared.cmd_opts.hide_ui_dir_config else ''
gr.HTML(
"<p style='padding-bottom: 1em;' class=\"text-gray-500\">Upload images or process images in a directory" +
"<br>Add inpaint batch mask directory to enable inpaint batch processing"
f"{hidden}</p>"
)
img2img_batch_files = gr.Files(label="Batch Process", interactive=True, elem_id="img2img_image_batch")
img2img_batch_input_dir = gr.Textbox(label="Inpaint batch input directory", **modules.shared.hide_dirs, elem_id="img2img_batch_input_dir")
img2img_batch_output_dir = gr.Textbox(label="Inpaint batch output directory", **modules.shared.hide_dirs, elem_id="img2img_batch_output_dir")
img2img_batch_inpaint_mask_dir = gr.Textbox(label="Inpaint batch mask directory", **modules.shared.hide_dirs, elem_id="img2img_batch_inpaint_mask_dir")
img2img_batch_input_dir = gr.Textbox(label="Inpaint batch input directory", **shared.hide_dirs, elem_id="img2img_batch_input_dir")
img2img_batch_output_dir = gr.Textbox(label="Inpaint batch output directory", **shared.hide_dirs, elem_id="img2img_batch_output_dir")
img2img_batch_inpaint_mask_dir = gr.Textbox(label="Inpaint batch mask directory", **shared.hide_dirs, elem_id="img2img_batch_inpaint_mask_dir")
img2img_tabs = [tab_img2img, tab_sketch, tab_inpaint, tab_inpaint_color, tab_inpaint_upload, tab_batch]
for i, tab in enumerate(img2img_tabs):
@@ -897,32 +891,38 @@ def create_ui(startup_timer = None):
(seed_resize_from_h, "Seed resize from-2"),
*modules.scripts.scripts_img2img.infotext_fields
]
parameters_copypaste.add_paste_fields("img2img", init_img, img2img_paste_fields, override_settings)
parameters_copypaste.add_paste_fields("inpaint", init_img_with_mask, img2img_paste_fields, override_settings)
img2img_bindings = parameters_copypaste.ParamBinding(paste_button=img2img_paste, tabname="img2img", source_text_component=img2img_prompt, source_image_component=None)
parameters_copypaste.register_paste_params_button(img2img_bindings)
generation_parameters_copypaste.add_paste_fields("img2img", init_img, img2img_paste_fields, override_settings)
generation_parameters_copypaste.add_paste_fields("inpaint", init_img_with_mask, img2img_paste_fields, override_settings)
img2img_bindings = generation_parameters_copypaste.ParamBinding(paste_button=img2img_paste, tabname="img2img", source_text_component=img2img_prompt, source_image_component=None)
generation_parameters_copypaste.register_paste_params_button(img2img_bindings)
timer.startup.record("ui-img2img")
modules.scripts.scripts_current = None
with gr.Blocks(analytics_enabled=False) as control_interface:
ui_control.create_ui()
timer.startup.record("ui-control")
if shared.backend == shared.Backend.DIFFUSERS:
with gr.Blocks(analytics_enabled=False) as control_interface:
from modules import ui_control
ui_control.create_ui()
timer.startup.record("ui-control")
with gr.Blocks(analytics_enabled=False) as extras_interface:
from modules import ui_postprocessing
ui_postprocessing.create_ui()
timer.startup.record("ui-extras")
with gr.Blocks(analytics_enabled=False) as train_interface:
from modules import ui_train
ui_train.create_ui([txt2img_prompt, txt2img_negative_prompt, steps, sampler_index, cfg_scale, seed, width, height])
timer.startup.record("ui-train")
with gr.Blocks(analytics_enabled=False) as models_interface:
from modules import ui_models
ui_models.create_ui()
timer.startup.record("ui-models")
with gr.Blocks(analytics_enabled=False) as interrogate_interface:
from modules import ui_interrogate
ui_interrogate.create_ui()
timer.startup.record("ui-interrogate")
@@ -989,7 +989,7 @@ def create_ui(startup_timer = None):
loadsave = ui_loadsave.UiLoadsave(cmd_opts.ui_config)
components = []
component_dict = {}
modules.shared.settings_components = component_dict
shared.settings_components = component_dict
dummy_component1 = gr.Label(visible=False)
script_callbacks.ui_settings_callback()
@@ -1008,17 +1008,17 @@ def create_ui(startup_timer = None):
if cmd_opts.use_directml:
directml_override_opts()
if cmd_opts.use_openvino:
if not modules.shared.opts.cuda_compile:
modules.shared.log.warning("OpenVINO: Enabling Torch Compile")
modules.shared.opts.cuda_compile = True
if modules.shared.opts.cuda_compile_backend != "openvino_fx":
modules.shared.log.warning("OpenVINO: Setting Torch Compiler backend to OpenVINO FX")
modules.shared.opts.cuda_compile_backend = "openvino_fx"
if modules.shared.opts.sd_backend != "diffusers":
modules.shared.log.warning("OpenVINO: Setting backend to Diffusers")
modules.shared.opts.sd_backend = "diffusers"
if not shared.opts.cuda_compile:
shared.log.warning("OpenVINO: Enabling Torch Compile")
shared.opts.cuda_compile = True
if shared.opts.cuda_compile_backend != "openvino_fx":
shared.log.warning("OpenVINO: Setting Torch Compiler backend to OpenVINO FX")
shared.opts.cuda_compile_backend = "openvino_fx"
if shared.opts.sd_backend != "diffusers":
shared.log.warning("OpenVINO: Setting backend to Diffusers")
shared.opts.sd_backend = "diffusers"
try:
opts.save(modules.shared.config_filename)
opts.save(shared.config_filename)
if len(changed) > 0:
log.info(f'Settings: changed={len(changed)} {changed}')
except RuntimeError:
@@ -1033,7 +1033,7 @@ def create_ui(startup_timer = None):
return gr.update(value=getattr(opts, key)), opts.dumpjson()
if cmd_opts.use_directml:
directml_override_opts()
opts.save(modules.shared.config_filename)
opts.save(shared.config_filename)
log.debug(f'Setting changed: key={key}, value={value}')
return get_value_for_setting(key), opts.dumpjson()
@@ -1079,7 +1079,7 @@ def create_ui(startup_timer = None):
current_row = gr.Column(variant='compact')
current_row.__enter__()
previous_section = item.section
if k in quicksettings_names and not modules.shared.cmd_opts.freeze:
if k in quicksettings_names and not shared.cmd_opts.freeze:
quicksettings_list.append((i, k, item))
components.append(dummy_component)
elif section_must_be_skipped:
@@ -1109,7 +1109,7 @@ def create_ui(startup_timer = None):
gr.Markdown(md)
with gr.TabItem("Licenses", id="system_licenses", elem_id="system_tab_licenses"):
gr.HTML(modules.shared.html("licenses.html"), elem_id="licenses", elem_classes="licenses")
gr.HTML(shared.html("licenses.html"), elem_id="licenses", elem_classes="licenses")
create_dirty_indicator("tab_licenses", [], interactive=False)
def unload_sd_weights():
@@ -1137,27 +1137,29 @@ def create_ui(startup_timer = None):
]
interfaces += script_callbacks.ui_tabs_callback()
interfaces += [(settings_interface, "System", "system")]
from modules import ui_extensions
extensions_interface = ui_extensions.create_ui()
interfaces += [(extensions_interface, "Extensions", "extensions")]
timer.startup.record("ui-extensions")
modules.shared.tab_names = []
shared.tab_names = []
for _interface, label, _ifid in interfaces:
modules.shared.tab_names.append(label)
shared.tab_names.append(label)
with gr.Blocks(theme=modules.theme.gradio_theme, analytics_enabled=False, title="SD.Next") as demo:
with gr.Blocks(theme=theme.gradio_theme, analytics_enabled=False, title="SD.Next") as demo:
with gr.Row(elem_id="quicksettings", variant="compact"):
for _i, k, _item in sorted(quicksettings_list, key=lambda x: quicksettings_names.get(x[1], x[0])):
component = create_setting_component(k, is_quicksettings=True)
component_dict[k] = component
parameters_copypaste.connect_paste_params_buttons()
generation_parameters_copypaste.connect_paste_params_buttons()
with gr.Tabs(elem_id="tabs") as tabs:
for interface, label, ifid in interfaces:
if interface is None:
continue
# if label in modules.shared.opts.hidden_tabs or label == '':
# if label in shared.opts.hidden_tabs or label == '':
# continue
with gr.TabItem(label, id=ifid, elem_id=f"tab_{ifid}"):
# log.debug(f'UI render: id={ifid}')
@@ -1180,9 +1182,9 @@ def create_ui(startup_timer = None):
inputs=components,
outputs=[text_settings, result],
)
defaults_submit.click(fn=lambda: modules.shared.restore_defaults(restart=True), _js="restartReload")
restart_submit.click(fn=lambda: modules.shared.restart_server(restart=True), _js="restartReload")
shutdown_submit.click(fn=lambda: modules.shared.restart_server(restart=False), _js="restartReload")
defaults_submit.click(fn=lambda: shared.restore_defaults(restart=True), _js="restartReload")
restart_submit.click(fn=lambda: shared.restart_server(restart=True), _js="restartReload")
shutdown_submit.click(fn=lambda: shared.restart_server(restart=False), _js="restartReload")
for _i, k, _item in quicksettings_list:
component = component_dict[k]
@@ -1298,7 +1300,7 @@ def html_css(is_builtin: bool):
if not os.path.isfile(cssfile):
continue
head += stylesheet(cssfile)
if opts.gradio_theme in modules.theme.list_builtin_themes():
if opts.gradio_theme in theme.list_builtin_themes():
head += stylesheet(os.path.join(script_path, "javascript", f"{opts.gradio_theme}.css"))
if os.path.exists(os.path.join(data_path, "user.css")):
head += stylesheet(os.path.join(data_path, "user.css"))
@@ -1308,13 +1310,13 @@ def html_css(is_builtin: bool):
def reload_javascript():
is_builtin = modules.theme.reload_gradio_theme()
is_builtin = theme.reload_gradio_theme()
head = html_head()
css = html_css(is_builtin)
body = html_body()
def template_response(*args, **kwargs):
res = modules.shared.GradioTemplateResponseOriginal(*args, **kwargs)
res = shared.GradioTemplateResponseOriginal(*args, **kwargs)
res.body = res.body.replace(b'</head>', f'{head}</head>'.encode("utf8"))
res.body = res.body.replace(b'</body>', f'{css}{body}</body>'.encode("utf8"))
res.init_headers()
@@ -1338,5 +1340,5 @@ def setup_ui_api(app):
app.add_api_route("/internal/ping", lambda: {}, methods=["GET"])
if not hasattr(modules.shared, 'GradioTemplateResponseOriginal'):
modules.shared.GradioTemplateResponseOriginal = gradio.routes.templates.TemplateResponse
if not hasattr(shared, 'GradioTemplateResponseOriginal'):
shared.GradioTemplateResponseOriginal = gradio.routes.templates.TemplateResponse