Spaces:
Runtime error
Runtime error
File size: 4,471 Bytes
2de3774 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
import random
import csv
from os.path import exists
from csv import DictReader
from pathlib import Path
from modules.prompt_expansion import PromptExpansion
from random_prompt import build_dynamic_prompt
DEFAULT_STYLES_FILE = Path("settings/styles.default")
STYLES_FILE = Path("settings/styles.csv")
prompt_expansion = PromptExpansion()
def load_styles():
default_styles = []
styles = []
with open(DEFAULT_STYLES_FILE) as f:
csv_reader = csv.reader(f)
for row in csv_reader:
default_styles.append(row)
if exists(STYLES_FILE):
with open(STYLES_FILE) as f:
csv_reader = csv.reader(f)
for row in csv_reader:
styles.append(row)
# Add any missing default styles
changed = False
for row in default_styles:
if row not in styles:
styles.append(row)
changed = True
if changed:
with open(STYLES_FILE, "w", newline='') as f:
csv_writer = csv.writer(f)
for row in styles:
csv_writer.writerow(row)
with STYLES_FILE.open("r") as f:
reader = DictReader(f)
styles = list(reader)
default_style = {"name": "None", "prompt": "{prompt}", "negative_prompt": ""}
random_style = {
"name": "Style: Pick Random",
"prompt": "{prompt}",
"negative_prompt": "",
}
lora_keywords_style = {
"name": "LoRA keywords",
"prompt": "{prompt} {lora_keywords}",
"negative_prompt": "",
}
flufferizer_style = {
"name": "Flufferizer",
"prompt": "{prompt}",
"negative_prompt": "",
}
hyperprompt_style = {
"name": "Hyperprompt",
"prompt": "{prompt}",
"negative_prompt": "",
}
styles.insert(0, hyperprompt_style)
styles.insert(0, flufferizer_style)
styles.insert(0, lora_keywords_style)
styles.insert(0, random_style)
styles.insert(0, default_style)
return {s["name"]: (s["prompt"], s["negative_prompt"]) for s in styles}
def apply_style(style, prompt, negative_prompt, lora_keywords):
output_prompt = ""
output_negative_prompt = ""
temp_style_prompt = prompt
bFlufferizer = False
bHyperprompt = False
artifylist = []
artifystylelist = []
index = 0
if not style:
return prompt, negative_prompt
while "Style: Pick Random" in style:
style[style.index("Style: Pick Random")] = random.choice(allstyles)
for s in style.copy():
_s = s.upper().strip()
if _s in map(str.upper, ["Flufferizer", "Style: Flufferizer"]):
bFlufferizer = True
del style[style.index(s)]
if _s in map(str.upper, ["Hyperprompt", "Style: Hyperprompt"]):
bHyperprompt = True
del style[style.index(s)]
if _s in map(str.upper, ["LoRA keywords", "Style: LoRA keywords"]):
style[style.index(s)] = "LoRA keywords" # Make sure it has the correct name
if bHyperprompt:
prompt = build_dynamic_prompt.one_button_superprompt(prompt=prompt)
temp_style_prompt = prompt
print("Hypered prompt: " + prompt)
while index < len(style):
if style[index].startswith("Artify"):
artifylist.append(style[index].replace("Artify: ", ""))
artifystylelist.append(style[index])
index += 1
style = [x for x in style if x not in artifystylelist]
for s in style:
p, n = styles.get(s, default_style)
if p is not None:
output_prompt = p + ", "
if n is not None:
output_negative_prompt += n + ", "
temp_style_prompt = output_prompt.replace("{prompt}", temp_style_prompt)
output_prompt = temp_style_prompt.replace(", ,", ", ")
# prep outputprompt for use in Flufferize and Artify
if output_prompt == "":
output_prompt = prompt
for artist in artifylist:
output_prompt = build_dynamic_prompt.artify_prompt(prompt=output_prompt, artists=artist)
if bFlufferizer:
output_prompt = prompt_expansion.expand_prompt(output_prompt)
output_prompt = output_prompt.replace("{lora_keywords}", lora_keywords)
output_negative_prompt += ", " + negative_prompt
return output_prompt, output_negative_prompt
styles = load_styles()
default_style = styles["None"]
allstyles = [x for x in load_styles() if x.startswith("Style")]
allstyles.remove("Style: Pick Random")
|