2022-09-09 14:16:02 -06:00
|
|
|
import csv
|
2022-09-11 08:35:12 -06:00
|
|
|
import os
|
2022-09-09 14:16:02 -06:00
|
|
|
import os.path
|
2023-06-04 01:56:48 -06:00
|
|
|
import re
|
2022-09-11 08:35:12 -06:00
|
|
|
import typing
|
|
|
|
import shutil
|
2022-09-09 14:16:02 -06:00
|
|
|
|
|
|
|
|
2022-09-11 08:35:12 -06:00
|
|
|
class PromptStyle(typing.NamedTuple):
|
|
|
|
name: str
|
|
|
|
prompt: str
|
|
|
|
negative_prompt: str
|
2022-09-09 14:16:02 -06:00
|
|
|
|
|
|
|
|
2022-09-14 08:56:21 -06:00
|
|
|
def merge_prompts(style_prompt: str, prompt: str) -> str:
|
|
|
|
if "{prompt}" in style_prompt:
|
|
|
|
res = style_prompt.replace("{prompt}", prompt)
|
|
|
|
else:
|
|
|
|
parts = filter(None, (prompt.strip(), style_prompt.strip()))
|
|
|
|
res = ", ".join(parts)
|
2022-09-09 14:16:02 -06:00
|
|
|
|
2022-09-14 08:56:21 -06:00
|
|
|
return res
|
2022-09-09 14:16:02 -06:00
|
|
|
|
|
|
|
|
2022-09-14 08:56:21 -06:00
|
|
|
def apply_styles_to_prompt(prompt, styles):
|
|
|
|
for style in styles:
|
|
|
|
prompt = merge_prompts(style, prompt)
|
2022-09-09 15:51:07 -06:00
|
|
|
|
2022-09-14 08:56:21 -06:00
|
|
|
return prompt
|
2022-09-09 14:16:02 -06:00
|
|
|
|
|
|
|
|
2023-06-04 01:56:48 -06:00
|
|
|
re_spaces = re.compile(" +")
|
|
|
|
|
|
|
|
|
|
|
|
def extract_style_text_from_prompt(style_text, prompt):
|
|
|
|
stripped_prompt = re.sub(re_spaces, " ", prompt.strip())
|
|
|
|
stripped_style_text = re.sub(re_spaces, " ", style_text.strip())
|
|
|
|
if "{prompt}" in stripped_style_text:
|
|
|
|
left, right = stripped_style_text.split("{prompt}", 2)
|
|
|
|
if stripped_prompt.startswith(left) and stripped_prompt.endswith(right):
|
|
|
|
prompt = stripped_prompt[len(left):len(stripped_prompt)-len(right)]
|
|
|
|
return True, prompt
|
|
|
|
else:
|
|
|
|
if stripped_prompt.endswith(stripped_style_text):
|
|
|
|
prompt = stripped_prompt[:len(stripped_prompt)-len(stripped_style_text)]
|
|
|
|
|
|
|
|
if prompt.endswith(', '):
|
|
|
|
prompt = prompt[:-2]
|
|
|
|
|
|
|
|
return True, prompt
|
|
|
|
|
|
|
|
return False, prompt
|
|
|
|
|
|
|
|
|
|
|
|
def extract_style_from_prompts(style: PromptStyle, prompt, negative_prompt):
|
|
|
|
if not style.prompt and not style.negative_prompt:
|
|
|
|
return False, prompt, negative_prompt
|
|
|
|
|
|
|
|
match_positive, extracted_positive = extract_style_text_from_prompt(style.prompt, prompt)
|
|
|
|
if not match_positive:
|
|
|
|
return False, prompt, negative_prompt
|
|
|
|
|
|
|
|
match_negative, extracted_negative = extract_style_text_from_prompt(style.negative_prompt, negative_prompt)
|
|
|
|
if not match_negative:
|
|
|
|
return False, prompt, negative_prompt
|
|
|
|
|
|
|
|
return True, extracted_positive, extracted_negative
|
|
|
|
|
|
|
|
|
2022-09-14 08:56:21 -06:00
|
|
|
class StyleDatabase:
|
|
|
|
def __init__(self, path: str):
|
|
|
|
self.no_style = PromptStyle("None", "", "")
|
2023-01-14 04:56:39 -07:00
|
|
|
self.styles = {}
|
|
|
|
self.path = path
|
2022-09-09 14:16:02 -06:00
|
|
|
|
2023-01-14 04:56:39 -07:00
|
|
|
self.reload()
|
|
|
|
|
|
|
|
def reload(self):
|
|
|
|
self.styles.clear()
|
|
|
|
|
|
|
|
if not os.path.exists(self.path):
|
2022-09-14 08:56:21 -06:00
|
|
|
return
|
|
|
|
|
2023-01-14 04:56:39 -07:00
|
|
|
with open(self.path, "r", encoding="utf-8-sig", newline='') as file:
|
2023-05-17 13:50:08 -06:00
|
|
|
reader = csv.DictReader(file, skipinitialspace=True)
|
2022-09-14 08:56:21 -06:00
|
|
|
for row in reader:
|
|
|
|
# Support loading old CSV format with "name, text"-columns
|
|
|
|
prompt = row["prompt"] if "prompt" in row else row["text"]
|
|
|
|
negative_prompt = row.get("negative_prompt", "")
|
|
|
|
self.styles[row["name"]] = PromptStyle(row["name"], prompt, negative_prompt)
|
|
|
|
|
2022-09-29 21:01:32 -06:00
|
|
|
def get_style_prompts(self, styles):
|
|
|
|
return [self.styles.get(x, self.no_style).prompt for x in styles]
|
|
|
|
|
|
|
|
def get_negative_style_prompts(self, styles):
|
|
|
|
return [self.styles.get(x, self.no_style).negative_prompt for x in styles]
|
|
|
|
|
2022-09-14 08:56:21 -06:00
|
|
|
def apply_styles_to_prompt(self, prompt, styles):
|
|
|
|
return apply_styles_to_prompt(prompt, [self.styles.get(x, self.no_style).prompt for x in styles])
|
|
|
|
|
|
|
|
def apply_negative_styles_to_prompt(self, prompt, styles):
|
|
|
|
return apply_styles_to_prompt(prompt, [self.styles.get(x, self.no_style).negative_prompt for x in styles])
|
|
|
|
|
|
|
|
def save_styles(self, path: str) -> None:
|
2023-04-03 10:19:57 -06:00
|
|
|
# Always keep a backup file around
|
|
|
|
if os.path.exists(path):
|
2023-05-09 13:17:58 -06:00
|
|
|
shutil.copy(path, f"{path}.bak")
|
2023-04-03 10:19:57 -06:00
|
|
|
|
2023-08-03 13:46:57 -06:00
|
|
|
with open(path, "w", encoding="utf-8-sig", newline='') as file:
|
2022-09-14 08:56:21 -06:00
|
|
|
writer = csv.DictWriter(file, fieldnames=PromptStyle._fields)
|
|
|
|
writer.writeheader()
|
2023-06-04 01:56:48 -06:00
|
|
|
writer.writerows(style._asdict() for k, style in self.styles.items())
|
|
|
|
|
|
|
|
def extract_styles_from_prompt(self, prompt, negative_prompt):
|
|
|
|
extracted = []
|
|
|
|
|
|
|
|
applicable_styles = list(self.styles.values())
|
|
|
|
|
|
|
|
while True:
|
|
|
|
found_style = None
|
|
|
|
|
|
|
|
for style in applicable_styles:
|
|
|
|
is_match, new_prompt, new_neg_prompt = extract_style_from_prompts(style, prompt, negative_prompt)
|
|
|
|
if is_match:
|
|
|
|
found_style = style
|
|
|
|
prompt = new_prompt
|
|
|
|
negative_prompt = new_neg_prompt
|
|
|
|
break
|
|
|
|
|
|
|
|
if not found_style:
|
|
|
|
break
|
|
|
|
|
|
|
|
applicable_styles.remove(found_style)
|
|
|
|
extracted.append(found_style.name)
|
|
|
|
|
|
|
|
return list(reversed(extracted)), prompt, negative_prompt
|