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