fix typos, add comment

This commit is contained in:
Kohya S
2023-06-15 20:37:01 +09:00
parent 624fbadea2
commit f2989b36c2

View File

@@ -2180,12 +2180,14 @@ def handle_dynamic_prompt_variants(prompt, repeat_count):
enumerating = False enumerating = False
replacers = [] replacers = []
for found in founds: for found in founds:
# if "e$$" is found, enumerate all variants
found_enumerating = found.group(2) is not None found_enumerating = found.group(2) is not None
enumerating = enumerating or found_enumerating enumerating = enumerating or found_enumerating
separater = ", " if found.group(6) is None else found.group(6) separator = ", " if found.group(6) is None else found.group(6)
variants = found.group(7).split("|") variants = found.group(7).split("|")
# parse count range
count_range = found.group(4) count_range = found.group(4)
if count_range is None: if count_range is None:
count_range = [1, 1] count_range = [1, 1]
@@ -2206,7 +2208,7 @@ def handle_dynamic_prompt_variants(prompt, repeat_count):
count_range[1] = len(variants) count_range[1] = len(variants)
if found_enumerating: if found_enumerating:
# make all combinations # make function to enumerate all combinations
def make_replacer_enum(vari, cr, sep): def make_replacer_enum(vari, cr, sep):
def replacer(): def replacer():
values = [] values = []
@@ -2217,9 +2219,9 @@ def handle_dynamic_prompt_variants(prompt, repeat_count):
return replacer return replacer
replacers.append(make_replacer_enum(variants, count_range, separater)) replacers.append(make_replacer_enum(variants, count_range, separator))
else: else:
# make random combinations # make function to choose random combinations
def make_replacer_single(vari, cr, sep): def make_replacer_single(vari, cr, sep):
def replacer(): def replacer():
count = random.randint(cr[0], cr[1]) count = random.randint(cr[0], cr[1])
@@ -2228,10 +2230,11 @@ def handle_dynamic_prompt_variants(prompt, repeat_count):
return replacer return replacer
replacers.append(make_replacer_single(variants, count_range, separater)) replacers.append(make_replacer_single(variants, count_range, separator))
# make each prompt # make each prompt
if not enumerating: if not enumerating:
# if not enumerating, repeat the prompt, replace each variant randomly
prompts = [] prompts = []
for _ in range(repeat_count): for _ in range(repeat_count):
current = prompt current = prompt
@@ -2239,16 +2242,21 @@ def handle_dynamic_prompt_variants(prompt, repeat_count):
current = current.replace(found.group(0), replacer()[0]) current = current.replace(found.group(0), replacer()[0])
prompts.append(current) prompts.append(current)
else: else:
# if enumerating, iterate all combinations for previous prompts
prompts = [prompt] prompts = [prompt]
for found, replacer in zip(founds, replacers): for found, replacer in zip(founds, replacers):
if found.group(2) is not None: # enumerating if found.group(2) is not None:
# make all combinations for existing prompts
new_prompts = [] new_prompts = []
for current in prompts: for current in prompts:
replecements = replacer() replecements = replacer()
for replecement in replecements: for replecement in replecements:
new_prompts.append(current.replace(found.group(0), replecement)) new_prompts.append(current.replace(found.group(0), replecement))
prompts = new_prompts prompts = new_prompts
for found, replacer in zip(founds, replacers): for found, replacer in zip(founds, replacers):
# make random selection for existing prompts
if found.group(2) is None: if found.group(2) is None:
for i in range(len(prompts)): for i in range(len(prompts)):
prompts[i] = prompts[i].replace(found.group(0), replacer()[0]) prompts[i] = prompts[i].replace(found.group(0), replacer()[0])
@@ -3166,7 +3174,8 @@ def main(args):
else: else:
raw_prompt = prompt_list[prompt_index] raw_prompt = prompt_list[prompt_index]
# sd-dynamic-prompts like variants: count is 1 or images_per_prompt or arbitrary # sd-dynamic-prompts like variants:
# count is 1 (not dynamic) or images_per_prompt (no enumeration) or arbitrary (enumeration)
raw_prompts = handle_dynamic_prompt_variants(raw_prompt, args.images_per_prompt) raw_prompts = handle_dynamic_prompt_variants(raw_prompt, args.images_per_prompt)
# repeat prompt # repeat prompt