2022-09-11 14:35:12 +00:00
|
|
|
# We need this so Python doesn't complain about the unknown StableDiffusionProcessing-typehint at runtime
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-09-09 20:16:02 +00:00
|
|
|
import csv
|
2022-09-11 14:35:12 +00:00
|
|
|
import os
|
2022-09-09 20:16:02 +00:00
|
|
|
import os.path
|
2022-09-11 14:35:12 +00:00
|
|
|
import typing
|
|
|
|
import collections.abc as abc
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
2022-09-09 20:16:02 +00:00
|
|
|
|
2022-09-11 14:35:12 +00:00
|
|
|
if typing.TYPE_CHECKING:
|
|
|
|
# Only import this when code is being type-checked, it doesn't have any effect at runtime
|
|
|
|
from .processing import StableDiffusionProcessing
|
2022-09-09 20:16:02 +00:00
|
|
|
|
|
|
|
|
2022-09-11 14:35:12 +00:00
|
|
|
class PromptStyle(typing.NamedTuple):
|
|
|
|
name: str
|
|
|
|
prompt: str
|
|
|
|
negative_prompt: str
|
2022-09-09 20:16:02 +00:00
|
|
|
|
|
|
|
|
2022-09-11 14:35:12 +00:00
|
|
|
def load_styles(path: str) -> dict[str, PromptStyle]:
|
|
|
|
styles = {"None": PromptStyle("None", "", "")}
|
2022-09-09 20:16:02 +00:00
|
|
|
|
2022-09-11 14:35:12 +00:00
|
|
|
if os.path.exists(path):
|
|
|
|
with open(path, "r", encoding="utf8", newline='') as file:
|
|
|
|
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", "")
|
|
|
|
styles[row["name"]] = PromptStyle(row["name"], prompt, negative_prompt)
|
2022-09-09 20:16:02 +00:00
|
|
|
|
2022-09-11 14:35:12 +00:00
|
|
|
return styles
|
2022-09-09 20:16:02 +00:00
|
|
|
|
2022-09-09 21:51:07 +00:00
|
|
|
|
2022-09-11 14:35:12 +00:00
|
|
|
def merge_prompts(style_prompt: str, prompt: str) -> str:
|
|
|
|
parts = filter(None, (prompt.strip(), style_prompt.strip()))
|
|
|
|
return ", ".join(parts)
|
2022-09-09 20:16:02 +00:00
|
|
|
|
|
|
|
|
2022-09-11 14:35:12 +00:00
|
|
|
def apply_style(processing: StableDiffusionProcessing, style: PromptStyle) -> None:
|
|
|
|
if isinstance(processing.prompt, list):
|
|
|
|
processing.prompt = [merge_prompts(style.prompt, p) for p in processing.prompt]
|
2022-09-09 20:16:02 +00:00
|
|
|
else:
|
2022-09-11 14:35:12 +00:00
|
|
|
processing.prompt = merge_prompts(style.prompt, processing.prompt)
|
2022-09-09 20:16:02 +00:00
|
|
|
|
2022-09-11 14:35:12 +00:00
|
|
|
if isinstance(processing.negative_prompt, list):
|
|
|
|
processing.negative_prompt = [merge_prompts(style.negative_prompt, p) for p in processing.negative_prompt]
|
|
|
|
else:
|
|
|
|
processing.negative_prompt = merge_prompts(style.negative_prompt, processing.negative_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
def save_styles(path: str, styles: abc.Iterable[PromptStyle]) -> None:
|
|
|
|
# Write to temporary file first, so we don't nuke the file if something goes wrong
|
|
|
|
fd, temp_path = tempfile.mkstemp(".csv")
|
|
|
|
with os.fdopen(fd, "w", encoding="utf8", newline='') as file:
|
|
|
|
# _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 style in styles)
|
|
|
|
|
|
|
|
# Always keep a backup file around
|
2022-09-11 18:17:09 +00:00
|
|
|
if os.path.exists(path):
|
|
|
|
shutil.move(path, path + ".bak")
|
2022-09-11 14:35:12 +00:00
|
|
|
shutil.move(temp_path, path)
|