HatmanStack
commited on
Commit
·
4b05d70
1
Parent(s):
627731d
comfy script
Browse files
README.md
CHANGED
@@ -42,4 +42,129 @@ print(final_answer)
|
|
42 |
# stone pathway leading to the front door, text on the house reads "hello" in all caps,
|
43 |
# blue sky above, shadows cast by the trees, sunlight creating contrast on the house's facade,
|
44 |
# some plants visible near the bottom right corner, overall warm and serene atmosphere.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
```
|
|
|
42 |
# stone pathway leading to the front door, text on the house reads "hello" in all caps,
|
43 |
# blue sky above, shadows cast by the trees, sunlight creating contrast on the house's facade,
|
44 |
# some plants visible near the bottom right corner, overall warm and serene atmosphere.
|
45 |
+
```
|
46 |
+
|
47 |
+
<h1>A Script for Comfy</h1>
|
48 |
+
|
49 |
+
```python
|
50 |
+
import torch
|
51 |
+
import random
|
52 |
+
import hashlib
|
53 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
54 |
+
|
55 |
+
class PromptEnhancer:
|
56 |
+
def __init__(self):
|
57 |
+
# Set up device
|
58 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
59 |
+
|
60 |
+
# Model checkpoint
|
61 |
+
self.model_checkpoint = "gokaygokay/Flux-Prompt-Enhance"
|
62 |
+
|
63 |
+
# Tokenizer and Model
|
64 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model_checkpoint)
|
65 |
+
self.model = AutoModelForSeq2SeqLM.from_pretrained(self.model_checkpoint).to(self.device)
|
66 |
+
|
67 |
+
# Initialize the node title and generated prompt
|
68 |
+
self.node_title = "Prompt Enhancer"
|
69 |
+
self.generated_prompt = ""
|
70 |
+
|
71 |
+
@classmethod
|
72 |
+
def INPUT_TYPES(cls):
|
73 |
+
return {
|
74 |
+
"required": {
|
75 |
+
"prompt": ("STRING",),
|
76 |
+
"seed": ("INT", {"default": 42, "min": 0, "max": 4294967295}), # Default seed, larger range
|
77 |
+
"repetition_penalty": ("FLOAT", {"default": 1.2, "min": 0.0, "max": 10.0}), # Default repetition penalty
|
78 |
+
"max_target_length": ("INT", {"default": 256, "min": 1, "max": 1024}), # Default max target length
|
79 |
+
"temperature": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0}), # Default temperature
|
80 |
+
"top_k": ("INT", {"default": 50, "min": 1, "max": 1000}), # Default top-k
|
81 |
+
"top_p": ("FLOAT", {"default": 0.9, "min": 0.0, "max": 1.0}), # Default top-p
|
82 |
+
},
|
83 |
+
"optional": {
|
84 |
+
"prompts_list": ("LIST",), # List of prompts
|
85 |
+
}
|
86 |
+
}
|
87 |
+
|
88 |
+
RETURN_TYPES = ("STRING",) # Return only one string: the enhanced prompt
|
89 |
+
FUNCTION = "enhance_prompt"
|
90 |
+
CATEGORY = "TextEnhancement"
|
91 |
+
|
92 |
+
def generate_large_seed(self, seed, prompt):
|
93 |
+
# Combine the seed and prompt to create a unique string
|
94 |
+
unique_string = f"{seed}_{prompt}"
|
95 |
+
|
96 |
+
# Use a hash function to generate a large seed
|
97 |
+
hash_object = hashlib.sha256(unique_string.encode())
|
98 |
+
large_seed = int(hash_object.hexdigest(), 16) % (2**32)
|
99 |
+
|
100 |
+
return large_seed
|
101 |
+
|
102 |
+
def enhance_prompt(self, prompt, seed=42, repetition_penalty=1.2, max_target_length=256, temperature=0.7, top_k=50, top_p=0.9, prompts_list=None):
|
103 |
+
# Generate a large seed value
|
104 |
+
large_seed = self.generate_large_seed(seed, prompt)
|
105 |
+
|
106 |
+
# Set random seed for reproducibility
|
107 |
+
torch.manual_seed(large_seed)
|
108 |
+
random.seed(large_seed)
|
109 |
+
|
110 |
+
# Determine the prompts to process
|
111 |
+
prompts = [prompt] if prompts_list is None else prompts_list
|
112 |
+
|
113 |
+
enhanced_prompts = []
|
114 |
+
for p in prompts:
|
115 |
+
# Enhance prompt
|
116 |
+
prefix = "enhance prompt: "
|
117 |
+
input_text = prefix + p
|
118 |
+
input_ids = self.tokenizer(input_text, return_tensors="pt").input_ids.to(self.device)
|
119 |
+
|
120 |
+
# Generate a random seed for this generation
|
121 |
+
random_seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
122 |
+
torch.manual_seed(random_seed)
|
123 |
+
random.seed(random_seed)
|
124 |
+
|
125 |
+
outputs = self.model.generate(
|
126 |
+
input_ids,
|
127 |
+
max_length=max_target_length,
|
128 |
+
num_return_sequences=1,
|
129 |
+
do_sample=True,
|
130 |
+
temperature=temperature,
|
131 |
+
repetition_penalty=repetition_penalty,
|
132 |
+
top_k=top_k,
|
133 |
+
top_p=top_p
|
134 |
+
)
|
135 |
+
|
136 |
+
final_answer = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
137 |
+
confidence_score = 1.0 # Default to 1.0 if no score is provided
|
138 |
+
|
139 |
+
# Print the generated prompt and confidence score
|
140 |
+
print(f"Generated Prompt: {final_answer} (Confidence: {confidence_score:.2f})")
|
141 |
+
enhanced_prompts.append((f"Enhanced Prompt: {final_answer}", confidence_score))
|
142 |
+
|
143 |
+
# Update the node title and generated prompt
|
144 |
+
if prompts_list is None:
|
145 |
+
self.node_title = f"Prompt Enhancer (Confidence: {confidence_score:.2f})"
|
146 |
+
self.generated_prompt = f"Enhanced Prompt: {final_answer}"
|
147 |
+
return (f"Enhanced Prompt: {final_answer}",)
|
148 |
+
else:
|
149 |
+
self.node_title = "Prompt Enhancer (Multiple Prompts)"
|
150 |
+
self.generated_prompt = "Multiple Prompts"
|
151 |
+
return enhanced_prompts
|
152 |
+
|
153 |
+
@property
|
154 |
+
def NODE_TITLE(self):
|
155 |
+
return self.node_title
|
156 |
+
|
157 |
+
@property
|
158 |
+
def GENERATED_PROMPT(self):
|
159 |
+
return self.generated_prompt
|
160 |
+
|
161 |
+
# A dictionary that contains all nodes you want to export with their names
|
162 |
+
NODE_CLASS_MAPPINGS = {
|
163 |
+
"PromptEnhancer": PromptEnhancer
|
164 |
+
}
|
165 |
+
|
166 |
+
# A dictionary that contains the friendly/humanly readable titles for the nodes
|
167 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
168 |
+
"PromptEnhancer": "Prompt Enhancer"
|
169 |
+
}
|
170 |
```
|