commit 1f78469631c51d3c9e1b2a3e208bf7d4ea6fc190 Author: David Fischer Date: Mon Aug 7 21:51:00 2023 +0200 inital commit added new node for loading styles.csv diff --git a/README.md b/README.md new file mode 100644 index 0000000..a2ccfe3 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Styles CSV Loader Extension for ComfyUI +Extension for [ComfyUI](https://github.com/comfyanonymous/ComfyUI) that loads styles from a CSV file. +## Description +This extension allows users to load styles from a CSV file, primarily for migration purposes from the [automatic1111 Stable Diffusion web UI](https://github.com/AUTOMATIC1111/stable-diffusion-webui). + +**Important**: The `styles.csv` file must be located in the root of `ComfyUI` where `main.py` resides. You can also symlink it if necessary. + +Example of symlinking: +```bash +ln -s /path/to/your/styles.csv /path/to/ComfyUI/styles.csv +``` + +## Nodes Description +Each style is represented as a dictionary with the keys being `style_name` and the values being a list containing `positive_prompt` and `negative_prompt`. The prompts are outputs of this Node. + +## Author +- David Fischer +- GitHub: [theUpsider](https://github.com/theUpsider) +- Support me on [BuyMeACoffee](https://www.buymeacoffee.com/theupsider) \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/styles_csv_loader.py b/styles_csv_loader.py new file mode 100644 index 0000000..894c592 --- /dev/null +++ b/styles_csv_loader.py @@ -0,0 +1,50 @@ +import re + +class StylesCSVLoader: + """ + Loads csv file with styles. For migration purposes from automatic11111 webui. + """ + def __init__(self): + if not hasattr(self, "styles_csv"): + self.styles_csv = self.load_styles_csv("styles.csv") + + def load_styles_csv(styles_path): + """Loads csv file with styles. It has only one column. + Ignore the first row (header). + positive_prompt are strings separated by comma. Each string is a prompt. + negative_prompt are strings separated by comma. Each string is a prompt. + + Returns: + list: List of styles. Each style is a dict with keys: style_name and value: [positive_prompt, negative_prompt] + """ + + with open(styles_path, "r", encoding="utf-8") as f: + styles = [[x.replace('"', '').replace('\n','') for x in re.split(',(?=(?:[^"]*"[^"]*")*[^"]*$)', line)] for line in f.readlines()[1:]] + styles = {x[0]: [x[1],x[2]] for x in styles} + return styles + + @classmethod + def INPUT_TYPES(s): + if not hasattr(s, "styles_csv"): + s.styles_csv = s.load_styles_csv("styles.csv") + return { + "required": { + "styles": (list(s.styles_csv.keys()),), + }, + + } + + RETURN_TYPES = ("STRING","STRING") + RETURN_NAMES = ("positive prompt", "negative prompt") + FUNCTION = "execute" + CATEGORY = "loaders" + + def execute(self, styles): + return (self.styles_csv[styles][0], self.styles_csv[styles][1]) + +NODE_CLASS_MAPPINGS = { + "Load Styles CSV": StylesCSVLoader +} +NODE_DISPLAY_NAME_MAPPINGS = { + "StylesCSVLoader": "Load Styles CSV Node" +}