2022-09-09 23:16:02 +03:00
|
|
|
import csv
|
2022-09-11 17:35:12 +03:00
|
|
|
import os
|
2022-09-09 23:16:02 +03:00
|
|
|
import os.path
|
2022-09-11 17:35:12 +03:00
|
|
|
import typing
|
|
|
|
import shutil
|
2022-09-09 23:16:02 +03:00
|
|
|
|
|
|
|
|
2022-09-11 17:35:12 +03:00
|
|
|
class PromptStyle(typing.NamedTuple):
|
|
|
|
name: str
|
|
|
|
prompt: str
|
|
|
|
negative_prompt: str
|
2022-09-09 23:16:02 +03:00
|
|
|
|
|
|
|
|
2022-09-14 17:56:21 +03: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 23:16:02 +03:00
|
|
|
|
2022-09-14 17:56:21 +03:00
|
|
|
return res
|
2022-09-09 23:16:02 +03:00
|
|
|
|
|
|
|
|
2022-09-14 17:56:21 +03:00
|
|
|
def apply_styles_to_prompt(prompt, styles):
|
|
|
|
for style in styles:
|
|
|
|
prompt = merge_prompts(style, prompt)
|
2022-09-10 00:51:07 +03:00
|
|
|
|
2022-09-14 17:56:21 +03:00
|
|
|
return prompt
|
2022-09-09 23:16:02 +03:00
|
|
|
|
|
|
|
|
2022-09-14 17:56:21 +03:00
|
|
|
class StyleDatabase:
|
|
|
|
def __init__(self, path: str):
|
|
|
|
self.no_style = PromptStyle("None", "", "")
|
2023-01-14 14:56:39 +03:00
|
|
|
self.styles = {}
|
|
|
|
self.path = path
|
2022-09-09 23:16:02 +03:00
|
|
|
|
2023-01-14 14:56:39 +03:00
|
|
|
self.reload()
|
|
|
|
|
|
|
|
def reload(self):
|
|
|
|
self.styles.clear()
|
|
|
|
|
|
|
|
if not os.path.exists(self.path):
|
2022-09-14 17:56:21 +03:00
|
|
|
return
|
|
|
|
|
2023-01-14 14:56:39 +03:00
|
|
|
with open(self.path, "r", encoding="utf-8-sig", newline='') as file:
|
2022-09-14 17:56:21 +03:00
|
|
|
reader = csv.DictReader(file)
|
|
|
|
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-30 06:01:32 +03: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 17:56:21 +03: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 19:19:57 +03:00
|
|
|
# Always keep a backup file around
|
|
|
|
if os.path.exists(path):
|
2023-05-09 22:17:58 +03:00
|
|
|
shutil.copy(path, f"{path}.bak")
|
2023-04-03 19:19:57 +03:00
|
|
|
|
|
|
|
fd = os.open(path, os.O_RDWR|os.O_CREAT)
|
2022-10-17 17:18:21 +03:00
|
|
|
with os.fdopen(fd, "w", encoding="utf-8-sig", newline='') as file:
|
2022-09-14 17:56:21 +03:00
|
|
|
# _fields is actually part of the public API: typing.NamedTuple is a replacement for collections.NamedTuple,
|
|
|
|
# and collections.NamedTuple has explicit documentation for accessing _fields. Same goes for _asdict()
|
|
|
|
writer = csv.DictWriter(file, fieldnames=PromptStyle._fields)
|
|
|
|
writer.writeheader()
|
|
|
|
writer.writerows(style._asdict() for k, style in self.styles.items())
|