Upload 10 files
Browse filesFiles changed (10)
- Mile_High_Styler/.gitignoreAdded+1
- Mile_High_Styler/LICENSEAdded+21
- Mile_High_Styler/README.mdAdded+64
- Mile_High_Styler/__init__.pyAdded+3
- Mile_High_Styler/__pycache__/__init__.cpython-310.pycAdded
- Mile_High_Styler/__pycache__/mile_high_styler.cpython-310.pycAdded
- Mile_High_Styler/__pycache__/sdxl_prompt_styler.cpython-310.pycAdded
- Mile_High_Styler/examples/sdxl_prompt_styler.pngAdded
- Mile_High_Styler/mile_high_styler.pyAdded+116
- Mile_High_Styler/mile_high_styles.jsonAdded
1__pycache__/123456789101112131415161718192021MIT License
Copyright (c) 2023 twri
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364SDXL Prompt Styler=======Custom node for ComfyUI-----------
SDXL Prompt Styler is a node that enables you to style prompts based on predefined templates stored in a JSON file. The node specifically replaces a {prompt} placeholder in the 'prompt' field of each template with provided positive text.
The node also effectively manages negative prompts. If negative text is provided, the node combines this with the 'negative_prompt' field from the template. If no negative text is supplied, the system defaults to using the 'negative_prompt' from the JSON template. This flexibility enables the creation of a diverse and specific range of negative prompts.
### Usage Example with SDXL Prompt Styler
Template example from a JSON file:
```json[ { "name": "base", "prompt": "{prompt}", "negative_prompt": "" }, { "name": "enhance", "prompt": "breathtaking {prompt} . award-winning, professional, highly detailed", "negative_prompt": "ugly, deformed, noisy, blurry, distorted, grainy" }]```
```pythonstyle = "enhance"positive_prompt = "a futuristic pop up tent in a forest"negative_prompt = "dark"```
This will generate the following styled prompts as outputs:
```breathtaking a futuristic pop up tent in a forest . award-winning, professional, highly detailedugly, deformed, noisy, blurry, distorted, grainy, dark```
### Installation
To install and use the SDXL Prompt Styler nodes, follow these steps:
1. Open a terminal or command line interface.2. Navigate to the `ComfyUI/custom_nodes/` directory.3. Run the following command:```git clone https://github.com/twri/sdxl_prompt_styler.git```4. Restart ComfyUI.
This command clones the SDXL Prompt Styler repository into your `ComfyUI/custom_nodes/` directory. You should now be able to access and use the nodes from this repository.
### Inputs
* **text_positive** - text for the positive base prompt G* **text_negative** - text for the negative base prompt G* **log_prompt** - print inputs and outputs to stdout
### Outputs
* **positive_prompt_text_g** - combined prompt with style for positive promt G* **negative_prompt_text_g** - combined prompt with style for negative promt G123from .mile_high_styler import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116import jsonimport os
def read_json_file(file_path): try: # Open file, load JSON content into python dictionary, and return it. with open(file_path, 'r') as file: json_data = json.load(file) return json_data except Exception as e: print(f"An error occurred: {str(e)}")
def read_sdxl_styles(json_data): # Check that data is a list if not isinstance(json_data, list): print("Error: input data must be a list") return None
names = []
# Iterate over each item in the data list for item in json_data: # Check that the item is a dictionary if isinstance(item, dict): # Check that 'name' is a key in the dictionary if 'name' in item: # Append the value of 'name' to the names list names.append(item['name'])
return names
def read_sdxl_templates_replace_and_combine(json_data, template_name, positive_prompt, negative_prompt): try: # Check if json_data is a list if not isinstance(json_data, list): raise ValueError("Invalid JSON data. Expected a list of templates.") for template in json_data: # Check if template contains 'name' and 'prompt' fields if 'name' not in template or 'prompt' not in template: raise ValueError("Invalid template. Missing 'name' or 'prompt' field.") # Replace {prompt} in the matching template if template['name'] == template_name: positive_prompt = template['prompt'].replace('{prompt}', positive_prompt) json_negative_prompt = template.get('negative_prompt', "") if negative_prompt: negative_prompt = f"{json_negative_prompt}, {negative_prompt}" if json_negative_prompt else negative_prompt else: negative_prompt = json_negative_prompt return positive_prompt, negative_prompt
# If function hasn't returned yet, no matching template was found raise ValueError(f"No template found with name '{template_name}'.")
except Exception as e: print(f"An error occurred: {str(e)}")
class MileHighStyler:
def __init__(self): pass
@classmethod def INPUT_TYPES(self): # Get current file's directory p = os.path.dirname(os.path.realpath(__file__)) # Construct 'mile_high_styles.json' path file_path = os.path.join(p, 'mile_high_styles.json')
# Read JSON from file self.json_data = read_json_file(file_path) # Retrieve styles from JSON data styles = read_sdxl_styles(self.json_data) return { "required": { "text_positive": ("STRING", {"default": "", "multiline": True}), "text_negative": ("STRING", {"default": "", "multiline": True}), "style": ((styles), ), "log_prompt": (["No", "Yes"], {"default":"No"}), }, }
RETURN_TYPES = ('STRING','STRING',) RETURN_NAMES = ('positive_prompt_text_g','negative_prompt_text_g',) FUNCTION = 'prompt_styler' CATEGORY = 'utils'
def prompt_styler(self, text_positive, text_negative, style, log_prompt): # Process and combine prompts in templates positive_prompt, negative_prompt = read_sdxl_templates_replace_and_combine(self.json_data, style, text_positive, text_negative) # If logging is enabled (log_prompt is set to "Yes"), # print the style, positive and negative text, and positive and negative prompts to the console if log_prompt == "Yes": print(f"style: {style}") print(f"text_positive: {text_positive}") print(f"text_negative: {text_negative}") print(f"positive_prompt: {positive_prompt}") print(f"negative_prompt: {negative_prompt}")
return positive_prompt, negative_prompt
NODE_CLASS_MAPPINGS = { "MileHighStyler": MileHighStyler,}
NODE_DISPLAY_NAME_MAPPINGS = { "MileHighStyler": "Mile High Styler", }Mile_High_Styler/.gitignoreAdded+1
Mile_High_Styler/LICENSEAdded+21
Mile_High_Styler/README.mdAdded+64
Mile_High_Styler/__init__.pyAdded+3
Mile_High_Styler/__pycache__/__init__.cpython-310.pycAdded
Mile_High_Styler/__pycache__/mile_high_styler.cpython-310.pycAdded
Mile_High_Styler/__pycache__/sdxl_prompt_styler.cpython-310.pycAdded
Mile_High_Styler/examples/sdxl_prompt_styler.pngAdded

Mile_High_Styler/mile_high_styler.pyAdded+116
Mile_High_Styler/mile_high_styles.jsonAdded