AUTOMATIC1111 / stable-diffusion-webui-wildcards

Wildcards
428 stars 104 forks source link

[Feature Request] Add support for inline wildcards #3

Open TetteDev opened 1 year ago

TetteDev commented 1 year ago

Add support for these kinds of prompts

a very cute ((__color__ __{cat;dog;monkey;fish}__))

This is meant to look for words in a file called color.txt ofcourse as it does by default, but also when it encounters a "chunk" surrounded by __{ and }__ it should directly pick a word from in-between those

Here is some basic code that allows for this, but it could definitely be improved

def is_inline_wilcard(self, text):
    return "}" in text and "}" in text

def replace_wildcard(self, text):
    if " " in text or len(text) == 0:
        return text

    # very basic but it works
    if self.is_inline_wilcard(text):
        choices = text.replace("{", "").replace("}", "").split(";")
        assert len(choices) > 0
        return random.choice(choices).replace("_", " ")
    else:
        file_dir = os.path.dirname(os.path.realpath("__file__"))
        replacement_file = os.path.join(file_dir, f"scripts\\wildcards\\{text}.txt")
        if os.path.exists(replacement_file):
            with open(replacement_file, encoding="utf8") as f:
                return random.choice(f.read().splitlines())
        else:
            if replacement_file not in warned_about_files:
                print(f"File {replacement_file} not found for the __{text}__ wildcard.", file=sys.stderr)
                warned_about_files[replacement_file] = 1

    return text
adieyal commented 1 year ago

You might want to have a look at the dynamic prompts extension which implements {option1|option2|option3} inline:

https://github.com/adieyal/sd-dynamic-prompts