From df7dbcdcb8a42c05db6495e13ee72688796f6110 Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Sat, 22 Oct 2022 14:29:23 +0300 Subject: [PATCH] first --- README.md | 6 ++++-- scripts/wildcards.py | 33 ++++++++++++++++++++++++++++++++ wildcards/put wildcards here.txt | 0 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 scripts/wildcards.py create mode 100644 wildcards/put wildcards here.txt diff --git a/README.md b/README.md index 6af5cc6..00b9e5c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ -# stable-diffusion-webui-wildcards -Wildcards +# Wildcards +An extension version of a script from https://github.com/jtkelm2/stable-diffusion-webui-1/blob/main/scripts/wildcards.py + +Allows you to use `__name__` syntax in your prompt to get a random line from a file named `name.txt` in the wildcards directory. diff --git a/scripts/wildcards.py b/scripts/wildcards.py new file mode 100644 index 0000000..1ca83d4 --- /dev/null +++ b/scripts/wildcards.py @@ -0,0 +1,33 @@ +import os +import random + +from modules import scripts + + +class WildcardsScript(scripts.Script): + def title(self): + return "Simple wildcards" + + def show(self, is_img2img): + return scripts.AlwaysVisible + + def replace_wildcard(self, text): + if " " in text: + return text + + replacement_file = os.path.join(scripts.basedir(), f"wildcards/{text}.txt") + if os.path.exists(replacement_file): + with open(replacement_file, encoding="utf8") as f: + return random.choice(f.read().splitlines()) + + return text + + def process(self, p): + p.extra_generation_params["Wildcard prompt"] = p.all_prompts[0] + + for i in range(len(p.all_prompts)): + random.seed(p.all_seeds[i]) + + prompt = p.all_prompts[i] + prompt = "".join(self.replace_wildcard(chunk) for chunk in prompt.split("__")) + p.all_prompts[i] = prompt diff --git a/wildcards/put wildcards here.txt b/wildcards/put wildcards here.txt new file mode 100644 index 0000000..e69de29