styles.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. from pathlib import Path
  2. import csv
  3. import os
  4. import typing
  5. import shutil
  6. class PromptStyle(typing.NamedTuple):
  7. name: str
  8. prompt: str | None
  9. negative_prompt: str | None
  10. path: str | None = None
  11. def merge_prompts(style_prompt: str, prompt: str) -> str:
  12. if "{prompt}" in style_prompt:
  13. res = style_prompt.replace("{prompt}", prompt)
  14. else:
  15. parts = filter(None, (prompt.strip(), style_prompt.strip()))
  16. res = ", ".join(parts)
  17. return res
  18. def apply_styles_to_prompt(prompt, styles):
  19. for style in styles:
  20. prompt = merge_prompts(style, prompt)
  21. return prompt
  22. def extract_style_text_from_prompt(style_text, prompt):
  23. """This function extracts the text from a given prompt based on a provided style text. It checks if the style text contains the placeholder {prompt} or if it appears at the end of the prompt. If a match is found, it returns True along with the extracted text. Otherwise, it returns False and the original prompt.
  24. extract_style_text_from_prompt("masterpiece", "1girl, art by greg, masterpiece") outputs (True, "1girl, art by greg")
  25. extract_style_text_from_prompt("masterpiece, {prompt}", "masterpiece, 1girl, art by greg") outputs (True, "1girl, art by greg")
  26. extract_style_text_from_prompt("masterpiece, {prompt}", "exquisite, 1girl, art by greg") outputs (False, "exquisite, 1girl, art by greg")
  27. """
  28. stripped_prompt = prompt.strip()
  29. stripped_style_text = style_text.strip()
  30. if "{prompt}" in stripped_style_text:
  31. left, right = stripped_style_text.split("{prompt}", 2)
  32. if stripped_prompt.startswith(left) and stripped_prompt.endswith(right):
  33. prompt = stripped_prompt[len(left):len(stripped_prompt)-len(right)]
  34. return True, prompt
  35. else:
  36. if stripped_prompt.endswith(stripped_style_text):
  37. prompt = stripped_prompt[:len(stripped_prompt)-len(stripped_style_text)]
  38. if prompt.endswith(', '):
  39. prompt = prompt[:-2]
  40. return True, prompt
  41. return False, prompt
  42. def extract_original_prompts(style: PromptStyle, prompt, negative_prompt):
  43. """
  44. Takes a style and compares it to the prompt and negative prompt. If the style
  45. matches, returns True plus the prompt and negative prompt with the style text
  46. removed. Otherwise, returns False with the original prompt and negative prompt.
  47. """
  48. if not style.prompt and not style.negative_prompt:
  49. return False, prompt, negative_prompt
  50. match_positive, extracted_positive = extract_style_text_from_prompt(style.prompt, prompt)
  51. if not match_positive:
  52. return False, prompt, negative_prompt
  53. match_negative, extracted_negative = extract_style_text_from_prompt(style.negative_prompt, negative_prompt)
  54. if not match_negative:
  55. return False, prompt, negative_prompt
  56. return True, extracted_positive, extracted_negative
  57. class StyleDatabase:
  58. def __init__(self, paths: list[str | Path]):
  59. self.no_style = PromptStyle("None", "", "", None)
  60. self.styles = {}
  61. self.paths = paths
  62. self.all_styles_files: list[Path] = []
  63. folder, file = os.path.split(self.paths[0])
  64. if '*' in file or '?' in file:
  65. # if the first path is a wildcard pattern, find the first match else use "folder/styles.csv" as the default path
  66. self.default_path = next(Path(folder).glob(file), Path(os.path.join(folder, 'styles.csv')))
  67. self.paths.insert(0, self.default_path)
  68. else:
  69. self.default_path = Path(self.paths[0])
  70. self.prompt_fields = [field for field in PromptStyle._fields if field != "path"]
  71. self.reload()
  72. def reload(self):
  73. """
  74. Clears the style database and reloads the styles from the CSV file(s)
  75. matching the path used to initialize the database.
  76. """
  77. self.styles.clear()
  78. # scans for all styles files
  79. all_styles_files = []
  80. for pattern in self.paths:
  81. folder, file = os.path.split(pattern)
  82. if '*' in file or '?' in file:
  83. found_files = Path(folder).glob(file)
  84. [all_styles_files.append(file) for file in found_files]
  85. else:
  86. # if os.path.exists(pattern):
  87. all_styles_files.append(Path(pattern))
  88. # Remove any duplicate entries
  89. seen = set()
  90. self.all_styles_files = [s for s in all_styles_files if not (s in seen or seen.add(s))]
  91. for styles_file in self.all_styles_files:
  92. if len(all_styles_files) > 1:
  93. # add divider when more than styles file
  94. # '---------------- STYLES ----------------'
  95. divider = f' {styles_file.stem.upper()} '.center(40, '-')
  96. self.styles[divider] = PromptStyle(f"{divider}", None, None, "do_not_save")
  97. if styles_file.is_file():
  98. self.load_from_csv(styles_file)
  99. def load_from_csv(self, path: str | Path):
  100. with open(path, "r", encoding="utf-8-sig", newline="") as file:
  101. reader = csv.DictReader(file, skipinitialspace=True)
  102. for row in reader:
  103. # Ignore empty rows or rows starting with a comment
  104. if not row or row["name"].startswith("#"):
  105. continue
  106. # Support loading old CSV format with "name, text"-columns
  107. prompt = row["prompt"] if "prompt" in row else row["text"]
  108. negative_prompt = row.get("negative_prompt", "")
  109. # Add style to database
  110. self.styles[row["name"]] = PromptStyle(
  111. row["name"], prompt, negative_prompt, str(path)
  112. )
  113. def get_style_paths(self) -> set:
  114. """Returns a set of all distinct paths of files that styles are loaded from."""
  115. # Update any styles without a path to the default path
  116. for style in list(self.styles.values()):
  117. if not style.path:
  118. self.styles[style.name] = style._replace(path=str(self.default_path))
  119. # Create a list of all distinct paths, including the default path
  120. style_paths = set()
  121. style_paths.add(str(self.default_path))
  122. for _, style in self.styles.items():
  123. if style.path:
  124. style_paths.add(style.path)
  125. # Remove any paths for styles that are just list dividers
  126. style_paths.discard("do_not_save")
  127. return style_paths
  128. def get_style_prompts(self, styles):
  129. return [self.styles.get(x, self.no_style).prompt for x in styles]
  130. def get_negative_style_prompts(self, styles):
  131. return [self.styles.get(x, self.no_style).negative_prompt for x in styles]
  132. def apply_styles_to_prompt(self, prompt, styles):
  133. return apply_styles_to_prompt(
  134. prompt, [self.styles.get(x, self.no_style).prompt for x in styles]
  135. )
  136. def apply_negative_styles_to_prompt(self, prompt, styles):
  137. return apply_styles_to_prompt(
  138. prompt, [self.styles.get(x, self.no_style).negative_prompt for x in styles]
  139. )
  140. def save_styles(self, path: str = None) -> None:
  141. # The path argument is deprecated, but kept for backwards compatibility
  142. style_paths = self.get_style_paths()
  143. csv_names = [os.path.split(path)[1].lower() for path in style_paths]
  144. for style_path in style_paths:
  145. # Always keep a backup file around
  146. if os.path.exists(style_path):
  147. shutil.copy(style_path, f"{style_path}.bak")
  148. # Write the styles to the CSV file
  149. with open(style_path, "w", encoding="utf-8-sig", newline="") as file:
  150. writer = csv.DictWriter(file, fieldnames=self.prompt_fields)
  151. writer.writeheader()
  152. for style in (s for s in self.styles.values() if s.path == style_path):
  153. # Skip style list dividers, e.g. "STYLES.CSV"
  154. if style.name.lower().strip("# ") in csv_names:
  155. continue
  156. # Write style fields, ignoring the path field
  157. writer.writerow(
  158. {k: v for k, v in style._asdict().items() if k != "path"}
  159. )
  160. def extract_styles_from_prompt(self, prompt, negative_prompt):
  161. extracted = []
  162. applicable_styles = list(self.styles.values())
  163. while True:
  164. found_style = None
  165. for style in applicable_styles:
  166. is_match, new_prompt, new_neg_prompt = extract_original_prompts(
  167. style, prompt, negative_prompt
  168. )
  169. if is_match:
  170. found_style = style
  171. prompt = new_prompt
  172. negative_prompt = new_neg_prompt
  173. break
  174. if not found_style:
  175. break
  176. applicable_styles.remove(found_style)
  177. extracted.append(found_style.name)
  178. return list(reversed(extracted)), prompt, negative_prompt