2022-09-10 04:16:02 +08:00
|
|
|
import csv
|
2022-09-11 22:35:12 +08:00
|
|
|
import os
|
2022-09-10 04:16:02 +08:00
|
|
|
import os.path
|
2022-09-11 22:35:12 +08:00
|
|
|
import typing
|
|
|
|
import shutil
|
2022-09-10 04:16:02 +08:00
|
|
|
|
|
|
|
|
2022-09-11 22:35:12 +08:00
|
|
|
class PromptStyle(typing.NamedTuple):
|
|
|
|
name: str
|
|
|
|
prompt: str
|
|
|
|
negative_prompt: str
|
2022-09-10 04:16:02 +08:00
|
|
|
|
|
|
|
|
2022-09-14 22:56:21 +08: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-10 04:16:02 +08:00
|
|
|
|
2022-09-14 22:56:21 +08:00
|
|
|
return res
|
2022-09-10 04:16:02 +08:00
|
|
|
|
|
|
|
|
2022-09-14 22:56:21 +08:00
|
|
|
def apply_styles_to_prompt(prompt, styles):
|
|
|
|
for style in styles:
|
|
|
|
prompt = merge_prompts(style, prompt)
|
2022-09-10 05:51:07 +08:00
|
|
|
|
2022-09-14 22:56:21 +08:00
|
|
|
return prompt
|
2022-09-10 04:16:02 +08:00
|
|
|
|
|
|
|
|
2022-09-14 22:56:21 +08:00
|
|
|
class StyleDatabase:
|
|
|
|
def __init__(self, path: str):
|
|
|
|
self.no_style = PromptStyle("None", "", "")
|
2023-01-14 19:56:39 +08:00
|
|
|
self.styles = {}
|
|
|
|
self.path = path
|
2022-09-10 04:16:02 +08:00
|
|
|
|
2023-01-14 19:56:39 +08:00
|
|
|
self.reload()
|
|
|
|
|
|
|
|
def reload(self):
|
|
|
|
self.styles.clear()
|
|
|
|
|
|
|
|
if not os.path.exists(self.path):
|
2022-09-14 22:56:21 +08:00
|
|
|
return
|
|
|
|
|
2023-01-14 19:56:39 +08:00
|
|
|
with open(self.path, "r", encoding="utf-8-sig", newline='') as file:
|
2023-05-18 03:50:08 +08:00
|
|
|
reader = csv.DictReader(file, skipinitialspace=True)
|
2022-09-14 22:56:21 +08: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-30 11:01:32 +08: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 22:56:21 +08: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-04 00:19:57 +08:00
|
|
|
# Always keep a backup file around
|
|
|
|
if os.path.exists(path):
|
2023-05-10 03:17:58 +08:00
|
|
|
shutil.copy(path, f"{path}.bak")
|
2023-04-04 00:19:57 +08:00
|
|
|
|
|
|
|
fd = os.open(path, os.O_RDWR|os.O_CREAT)
|
2022-10-17 22:18:21 +08:00
|
|
|
with os.fdopen(fd, "w", encoding="utf-8-sig", newline='') as file:
|
2022-09-14 22:56:21 +08: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())
|