Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid Template overwrite when re-process and add new infotext #790

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 66 additions & 5 deletions sd_dynamic_prompts/dynamic_prompting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from dynamicprompts.generators.promptgenerator import GeneratorException
from dynamicprompts.parser.parse import ParserConfig
from dynamicprompts.wildcards import WildcardManager
from modules.processing import fix_seed
from modules.processing import fix_seed, create_infotext
from modules.shared import opts

from sd_dynamic_prompts import __version__, callbacks
Expand Down Expand Up @@ -517,10 +517,22 @@ def process(

if opts.dp_write_raw_template:
params = p.extra_generation_params
if original_prompt:
params["Template"] = original_prompt
if original_negative_prompt:
params["Negative Template"] = original_negative_prompt

first_time_only = params.get("Dynamic Prompts") is None

params["Dynamic Prompts"] = {
"use_fixed_seed": use_fixed_seed,
"unlink_seed_from_prompt": unlink_seed_from_prompt,
"enable_jinja_templates": enable_jinja_templates,
}

if first_time_only:
def _lazy_params(k1: str, v1, v2):
if k1 not in params and v1 != v2:
params[k1] = v1

_lazy_params("Template", original_prompt, all_prompts[0])
_lazy_params("Negative Template", original_negative_prompt, all_negative_prompts[0])

p.all_prompts = all_prompts
p.all_negative_prompts = all_negative_prompts
Expand All @@ -545,5 +557,54 @@ def process(
original_negative_prompt,
)

def process_batch(selfself, p, *args, **kwargs):
batch_number = kwargs.get('batch_number')
prompts = kwargs.get('prompts')

def _lazy_params(params, k1: str, v1, k2: str):
v2 = params.get(k2)
if v2 is not None and v1 != v2:
params[k1] = v1

_lazy_params(p.extra_generation_params, "Template Generated", prompts[0], "Template")
_lazy_params(p.extra_generation_params, "Negative Template Generated", p.all_negative_prompts[batch_number], "Negative Template")

p.extra_generation_params["Template Seeds"] = kwargs.get('seeds')
p.extra_generation_params["Template Seeds Sub"] = kwargs.get('subseeds')

def postprocess(
self,
p,
res,
*args,
**kwargs
):
index_of_first_image = res.index_of_first_image
infotexts = res.infotexts

if index_of_first_image != 0:
images = res.images

grid = images[index_of_first_image-1]

def _lazy_params(params, k1: str, v1: list, k2: str):
v2 = params.get(k2)
if v2 is not None and v1[0] != v2:
params[k1] = v1

res.extra_generation_params.pop("Template Generated", None)
res.extra_generation_params.pop("Negative Template Generated", None)

_lazy_params(res.extra_generation_params, "Template Generated Grid", res.all_prompts, "Template")
_lazy_params(res.extra_generation_params, "Negative Template Generated Grid", res.all_negative_prompts,
"Negative Template")

text = create_infotext(p, res.all_prompts, res.all_seeds, res.all_subseeds, use_main_prompt=True,
all_negative_prompts=res.all_negative_prompts)
grid.info["parameters"] = text

infotexts.pop(index_of_first_image-1)
infotexts.insert(index_of_first_image-1, text)


callbacks.register_settings() # Settings need to be registered early, see #754.
16 changes: 8 additions & 8 deletions sd_dynamic_prompts/wildcards_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,12 @@ def on_ui_tabs():
<li>Use the wildcard in your script by typing the name of the file or copying the text from the Wildcards file text box.</li>
<li>You can also add your own wildcard files to the {wildcard_manager.path} folder.</li>
</ol>
<style>#sddp-wildcard-tree {{ max-height: max(70vh, 900px); overflow-y: auto !important; }}</style>
"""

with gr.Blocks() as wildcards_tab:
with gr.Row():
with gr.Column():
gr.Textbox(
placeholder="Search in wildcard names...",
elem_id=make_element_id("wildcard-search"),
label="",
)
gr.HTML("Loading...", elem_id=make_element_id("wildcard-tree"))
with gr.Accordion("Help", open=False):
gr.HTML(help_html)
with gr.Accordion("Collection actions", open=False):
Expand All @@ -111,6 +106,12 @@ def on_ui_tabs():
"Delete all wildcards",
elem_id=make_element_id("wildcard-delete-tree-button"),
)
gr.Textbox(
placeholder="Search in wildcard names...",
elem_id=make_element_id("wildcard-search"),
label="",
)
gr.HTML("Loading...", elem_id=make_element_id("wildcard-tree"))
with gr.Column():
gr.Textbox(
"",
Expand All @@ -127,7 +128,7 @@ def on_ui_tabs():
)
save_button = gr.Button(
"Save wildcards",
scale=1,
# scale=1,
elem_id=make_element_id("wildcard-save-button"),
)

Expand Down Expand Up @@ -180,7 +181,6 @@ def on_ui_tabs():
inputs=[overwrite_checkbox, collection_dropdown],
outputs=[server_to_client_message_textbox],
)

return ((wildcards_tab, "Wildcards Manager", "sddp-wildcard-manager"),)


Expand Down