diff options
Diffstat (limited to 'modules/ui.py')
-rw-r--r-- | modules/ui.py | 132 |
1 files changed, 94 insertions, 38 deletions
diff --git a/modules/ui.py b/modules/ui.py index d1aa7793..b6d5dcd8 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -80,7 +80,7 @@ def send_gradio_gallery_to_image(x): return image_from_url_text(x[0])
-def save_files(js_data, images):
+def save_files(js_data, images, index):
import csv
os.makedirs(opts.outdir_save, exist_ok=True)
@@ -88,6 +88,10 @@ def save_files(js_data, images): filenames = []
data = json.loads(js_data)
+
+ if index > -1 and opts.save_selected_only and (index > 0 or not opts.return_grid): # ensures we are looking at a specific non-grid picture, and we have save_selected_only
+ images = [images[index]]
+ data["seed"] += (index - 1 if opts.return_grid else index)
with open(os.path.join(opts.outdir_save, "log.csv"), "a", encoding="utf8", newline='') as file:
at_start = file.tell() == 0
@@ -233,13 +237,20 @@ def add_style(name: str, prompt: str, negative_prompt: str): return [gr_show(), gr_show()]
style = modules.styles.PromptStyle(name, prompt, negative_prompt)
- shared.prompt_styles[style.name] = style
+ shared.prompt_styles.styles[style.name] = style
# Save all loaded prompt styles: this allows us to update the storage format in the future more easily, because we
# reserialize all styles every time we save them
- modules.styles.save_styles(shared.styles_filename, shared.prompt_styles.values())
+ shared.prompt_styles.save_styles(shared.styles_filename)
- update = {"visible": True, "choices": list(shared.prompt_styles), "__type__": "update"}
- return [update, update]
+ update = {"visible": True, "choices": list(shared.prompt_styles.styles), "__type__": "update"}
+ return [update, update, update, update]
+
+
+def apply_styles(prompt, prompt_neg, style1_name, style2_name):
+ prompt = shared.prompt_styles.apply_styles_to_prompt(prompt, [style1_name, style2_name])
+ prompt_neg = shared.prompt_styles.apply_negative_styles_to_prompt(prompt_neg, [style1_name, style2_name])
+
+ return [gr.Textbox.update(value=prompt), gr.Textbox.update(value=prompt_neg), gr.Dropdown.update(value="None"), gr.Dropdown.update(value="None")]
def interrogate(image):
@@ -247,15 +258,46 @@ def interrogate(image): return gr_show(True) if prompt is None else prompt
+
+def create_toprow(is_img2img):
+ with gr.Row(elem_id="toprow"):
+ with gr.Column(scale=4):
+ with gr.Row():
+ with gr.Column(scale=8):
+ with gr.Row():
+ prompt = gr.Textbox(label="Prompt", elem_id="prompt", show_label=False, placeholder="Prompt", lines=2)
+ roll = gr.Button('Roll', elem_id="roll", visible=len(shared.artist_db.artists) > 0)
+
+ with gr.Column(scale=1, elem_id="style_pos_col"):
+ prompt_style = gr.Dropdown(label="Style 1", elem_id="style_index", choices=[k for k, v in shared.prompt_styles.styles.items()], value=next(iter(shared.prompt_styles.styles.keys())), visible=len(shared.prompt_styles.styles) > 1)
+
+ with gr.Row():
+ with gr.Column(scale=8):
+ negative_prompt = gr.Textbox(label="Negative prompt", elem_id="negative_prompt", show_label=False, placeholder="Negative prompt", lines=2)
+
+ with gr.Column(scale=1, elem_id="style_neg_col"):
+ prompt_style2 = gr.Dropdown(label="Style 2", elem_id="style2_index", choices=[k for k, v in shared.prompt_styles.styles.items()], value=next(iter(shared.prompt_styles.styles.keys())), visible=len(shared.prompt_styles.styles) > 1)
+
+ with gr.Column(scale=1):
+ with gr.Row():
+ submit = gr.Button('Generate', elem_id="generate", variant='primary')
+
+ with gr.Row():
+ if is_img2img:
+ interrogate = gr.Button('Interrogate', elem_id="interrogate")
+ else:
+ interrogate = None
+ prompt_style_apply = gr.Button('Apply style', elem_id="style_apply")
+ save_style = gr.Button('Create style', elem_id="style_create")
+
+ check_progress = gr.Button('Check progress', elem_id="check_progress", visible=False)
+
+ return prompt, roll, prompt_style, negative_prompt, prompt_style2, submit, interrogate, prompt_style_apply, save_style, check_progress
+
+
def create_ui(txt2img, img2img, run_extras, run_pnginfo):
with gr.Blocks(analytics_enabled=False) as txt2img_interface:
- with gr.Row(elem_id="toprow"):
- txt2img_prompt = gr.Textbox(label="Prompt", elem_id="txt2img_prompt", show_label=False, placeholder="Prompt", lines=1)
- txt2img_negative_prompt = gr.Textbox(label="Negative prompt", elem_id="txt2img_negative_prompt", show_label=False, placeholder="Negative prompt", lines=1)
- txt2img_prompt_style = gr.Dropdown(label="Style", show_label=False, elem_id="style_index", choices=[k for k, v in shared.prompt_styles.items()], value=next(iter(shared.prompt_styles.keys())), visible=len(shared.prompt_styles) > 1)
- roll = gr.Button('Roll', elem_id="txt2img_roll", visible=len(shared.artist_db.artists) > 0)
- submit = gr.Button('Generate', elem_id="txt2img_generate", variant='primary')
- check_progress = gr.Button('Check progress', elem_id="check_progress", visible=False)
+ txt2img_prompt, roll, txt2img_prompt_style, txt2img_negative_prompt, txt2img_prompt_style2, submit, _, txt2img_prompt_style_apply, txt2img_save_style, check_progress = create_toprow(is_img2img=False)
with gr.Row().style(equal_height=False):
with gr.Column(variant='panel'):
@@ -286,7 +328,6 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): txt2img_preview = gr.Image(elem_id='txt2img_preview', visible=False)
txt2img_gallery = gr.Gallery(label='Output', elem_id='txt2img_gallery').style(grid=4)
-
with gr.Group():
with gr.Row():
save = gr.Button('Save')
@@ -294,7 +335,6 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): send_to_inpaint = gr.Button('Send to inpaint')
send_to_extras = gr.Button('Send to extras')
interrupt = gr.Button('Interrupt')
- txt2img_save_style = gr.Button('Save prompt as style')
progressbar = gr.HTML(elem_id="progressbar")
@@ -302,7 +342,6 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): html_info = gr.HTML()
generation_info = gr.Textbox(visible=False)
-
txt2img_args = dict(
fn=txt2img,
_js="submit",
@@ -310,6 +349,7 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): txt2img_prompt,
txt2img_negative_prompt,
txt2img_prompt_style,
+ txt2img_prompt_style2,
steps,
sampler_index,
restore_faces,
@@ -339,7 +379,6 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): outputs=[progressbar, txt2img_preview, txt2img_preview],
)
-
interrupt.click(
fn=lambda: shared.state.interrupt(),
inputs=[],
@@ -348,9 +387,11 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): save.click(
fn=wrap_gradio_call(save_files),
+ _js = "(x, y, z) => [x, y, selected_gallery_index()]",
inputs=[
generation_info,
txt2img_gallery,
+ html_info
],
outputs=[
html_info,
@@ -370,18 +411,12 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): )
with gr.Blocks(analytics_enabled=False) as img2img_interface:
- with gr.Row(elem_id="toprow"):
- img2img_prompt = gr.Textbox(label="Prompt", elem_id="img2img_prompt", show_label=False, placeholder="Prompt", lines=1)
- img2img_negative_prompt = gr.Textbox(label="Negative prompt", elem_id="img2img_negative_prompt", show_label=False, placeholder="Negative prompt", lines=1)
- img2img_prompt_style = gr.Dropdown(label="Style", show_label=False, elem_id="style_index", choices=[k for k, v in shared.prompt_styles.items()], value=next(iter(shared.prompt_styles.keys())), visible=len(shared.prompt_styles) > 1)
- img2img_interrogate = gr.Button('Interrogate', elem_id="img2img_interrogate", variant='primary')
- submit = gr.Button('Generate', elem_id="img2img_generate", variant='primary')
- check_progress = gr.Button('Check progress', elem_id="check_progress", visible=False)
+ img2img_prompt, roll, img2img_prompt_style, img2img_negative_prompt, img2img_prompt_style2, submit, img2img_interrogate, img2img_prompt_style_apply, img2img_save_style, check_progress = create_toprow(is_img2img=True)
with gr.Row().style(equal_height=False):
with gr.Column(variant='panel'):
with gr.Group():
- switch_mode = gr.Radio(label='Mode', elem_id="img2img_mode", choices=['Redraw whole image', 'Inpaint a part of image', 'Loopback', 'SD upscale'], value='Redraw whole image', type="index", show_label=False)
+ switch_mode = gr.Radio(label='Mode', elem_id="img2img_mode", choices=['Redraw whole image', 'Inpaint a part of image', 'SD upscale'], value='Redraw whole image', type="index", show_label=False)
init_img = gr.Image(label="Image for img2img", source="upload", interactive=True, type="pil")
init_img_with_mask = gr.Image(label="Image for inpainting with mask", elem_id="img2maskimg", source="upload", interactive=True, type="pil", tool="sketch", visible=False, image_mode="RGBA")
init_mask = gr.Image(label="Mask", source="upload", interactive=True, type="pil", visible=False)
@@ -415,7 +450,6 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): with gr.Group():
cfg_scale = gr.Slider(minimum=1.0, maximum=30.0, step=0.5, label='CFG Scale', value=7.0)
denoising_strength = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Denoising strength', value=0.75)
- denoising_strength_change_factor = gr.Slider(minimum=0.9, maximum=1.1, step=0.01, label='Denoising strength change factor', value=1, visible=False)
with gr.Group():
width = gr.Slider(minimum=64, maximum=2048, step=64, label="Width", value=512)
@@ -449,8 +483,7 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): def apply_mode(mode, uploadmask):
is_classic = mode == 0
is_inpaint = mode == 1
- is_loopback = mode == 2
- is_upscale = mode == 3
+ is_upscale = mode == 2
return {
init_img: gr_show(not is_inpaint or (is_inpaint and uploadmask == 1)),
@@ -460,12 +493,10 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): mask_mode: gr_show(is_inpaint),
mask_blur: gr_show(is_inpaint),
inpainting_fill: gr_show(is_inpaint),
- batch_size: gr_show(not is_loopback),
sd_upscale_upscaler_name: gr_show(is_upscale),
sd_upscale_overlap: gr_show(is_upscale),
inpaint_full_res: gr_show(is_inpaint),
inpainting_mask_invert: gr_show(is_inpaint),
- denoising_strength_change_factor: gr_show(is_loopback),
img2img_interrogate: gr_show(not is_inpaint),
}
@@ -480,12 +511,10 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): mask_mode,
mask_blur,
inpainting_fill,
- batch_size,
sd_upscale_upscaler_name,
sd_upscale_overlap,
inpaint_full_res,
inpainting_mask_invert,
- denoising_strength_change_factor,
img2img_interrogate,
]
)
@@ -511,6 +540,7 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): img2img_prompt,
img2img_negative_prompt,
img2img_prompt_style,
+ img2img_prompt_style2,
init_img,
init_img_with_mask,
init_mask,
@@ -526,7 +556,6 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): batch_size,
cfg_scale,
denoising_strength,
- denoising_strength_change_factor,
seed,
subseed, subseed_strength, seed_resize_from_h, seed_resize_from_w,
height,
@@ -568,9 +597,11 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): save.click(
fn=wrap_gradio_call(save_files),
+ _js = "(x, y, z) => [x, y, selected_gallery_index()]",
inputs=[
generation_info,
img2img_gallery,
+ html_info
],
outputs=[
html_info,
@@ -579,22 +610,46 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): ]
)
+ roll.click(
+ fn=roll_artist,
+ inputs=[
+ img2img_prompt,
+ ],
+ outputs=[
+ img2img_prompt,
+ ]
+ )
+
+ prompts = [(txt2img_prompt, txt2img_negative_prompt), (img2img_prompt, img2img_negative_prompt)]
+ style_dropdowns = [(txt2img_prompt_style, txt2img_prompt_style2), (img2img_prompt_style, img2img_prompt_style2)]
+
dummy_component = gr.Label(visible=False)
- for button, (prompt, negative_prompt) in zip([txt2img_save_style, img2img_save_style], [(txt2img_prompt, txt2img_negative_prompt), (img2img_prompt, img2img_negative_prompt)]):
+ for button, (prompt, negative_prompt) in zip([txt2img_save_style, img2img_save_style], prompts):
button.click(
fn=add_style,
_js="ask_for_style_name",
# Have to pass empty dummy component here, because the JavaScript and Python function have to accept
# the same number of parameters, but we only know the style-name after the JavaScript prompt
inputs=[dummy_component, prompt, negative_prompt],
- outputs=[txt2img_prompt_style, img2img_prompt_style],
+ outputs=[txt2img_prompt_style, img2img_prompt_style, txt2img_prompt_style2, img2img_prompt_style2],
+ )
+
+ for button, (prompt, negative_prompt), (style1, style2) in zip([txt2img_prompt_style_apply, img2img_prompt_style_apply], prompts, style_dropdowns):
+ button.click(
+ fn=apply_styles,
+ inputs=[prompt, negative_prompt, style1, style2],
+ outputs=[prompt, negative_prompt, style1, style2],
)
with gr.Blocks(analytics_enabled=False) as extras_interface:
with gr.Row().style(equal_height=False):
with gr.Column(variant='panel'):
- with gr.Group():
- image = gr.Image(label="Source", source="upload", interactive=True, type="pil")
+ with gr.Tabs():
+ with gr.TabItem('Single Image'):
+ image = gr.Image(label="Source", source="upload", interactive=True, type="pil")
+
+ with gr.TabItem('Batch Process'):
+ image_batch = gr.File(label="Batch Process", file_count="multiple", source="upload", interactive=True, type="file")
upscaling_resize = gr.Slider(minimum=1.0, maximum=4.0, step=0.05, label="Resize", value=2)
@@ -615,7 +670,7 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): submit = gr.Button('Generate', elem_id="extras_generate", variant='primary')
with gr.Column(variant='panel'):
- result_image = gr.Image(label="Result")
+ result_images = gr.Gallery(label="Result")
html_info_x = gr.HTML()
html_info = gr.HTML()
@@ -623,6 +678,7 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): fn=run_extras,
inputs=[
image,
+ image_batch,
gfpgan_visibility,
codeformer_visibility,
codeformer_weight,
@@ -632,7 +688,7 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): extras_upscaler_2_visibility,
],
outputs=[
- result_image,
+ result_images,
html_info_x,
html_info,
]
|