-
Notifications
You must be signed in to change notification settings - Fork 2
/
ComfyuiPromptQuill.py
316 lines (256 loc) · 8.99 KB
/
ComfyuiPromptQuill.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Copyright 2024 osiworx
# Licensed under the Apache License, Version 2.0 (the "License"); you
# may not use this file except in compliance with the License. You
# may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
import requests
default_url = "http://127.0.0.1:64738"
class Client:
default_url = default_url
def __init__(self, url=None):
if url is None:
# Use class variable if no argument provided
url = Client.default_url
self.url = url
self.generate_url = f'{self.url}/get_prompt'
self.sail_url = f'{self.url}/get_next_prompt'
def send_api_call(self,payload, url):
# Set the Content-Type header to indicate JSON data
headers = {"Content-Type": "application/json"}
# Send the POST request
response = requests.post(url, json=payload, headers=headers)
# Check for successful response
if response.status_code == 200:
# Access the response data (if any)
response_data = response.json()
return response_data
else:
return 'Something went wrong, did you install Prompt Quill? https://github.com/osi1880vr/prompt_quill'
def generate(self, query, model=None):
# Prepare your JSON data
payload = {'query': query,
'model': model}
return self.send_api_call(payload, self.generate_url)
def sail(self, query, distance, summary, rephrase, rephrase_prompt, add_style, style, add_search, search, reset_journey,unload_llm):
payload = {'query': query,
'distance': distance,
'summary': summary,
'rephrase': rephrase,
'rephrase_prompt': rephrase_prompt,
'add_style': add_style,
'style': style,
'add_search': add_search,
'search': search,
'reset_journey': reset_journey,
'unload_llm': unload_llm
}
return self.send_api_call(payload, self.sail_url)
class PromptQuillGenerate:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"prompt": ("STRING", {
"multiline": True,
"default": "A cute cat?"
}),
"add_negative": ("BOOLEAN", {"default": False}),
"negative": ("STRING", {
"multiline": True,
"default": ""
}),
"url": ("STRING", {
"multiline": False,
"default": default_url
}),
},
}
RETURN_TYPES = ("STRING", "STRING",)
RETURN_NAMES = ("Prompt", "NegativePrompt",)
FUNCTION = "prompt_quill_generate"
CATEGORY = "PromptQuill"
def prompt_quill_generate(self, prompt, add_negative, negative, url, model=None):
client = Client(url=url)
response = client.generate(model=model, query=prompt)
if add_negative is True:
response['neg_prompt'] = f'{response["neg_prompt"]},{negative}'
return (response['prompt'], response['neg_prompt'],)
class PromptQuillGenerateConditioning:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"prompt": ("STRING", {
"multiline": True,
"default": "A cute cat?"
}),
"add_negative": ("BOOLEAN", {"default": False}),
"negative": ("STRING", {
"multiline": True,
"default": ""
}),
"url": ("STRING", {
"multiline": False,
"default": default_url
}),
"clip": ("CLIP", )
},
}
RETURN_TYPES = ("STRING", "STRING","CONDITIONING","CONDITIONING",)
RETURN_NAMES = ("Prompt", "NegativePrompt","Prompt Conditioning", "Negative Conditioning",)
FUNCTION = "prompt_quill_generate"
CATEGORY = "PromptQuill"
def encode(self, clip, text):
tokens = clip.tokenize(text)
cond, pooled = clip.encode_from_tokens(tokens, return_pooled=True)
return ([[cond, {"pooled_output": pooled}]], )
def prompt_quill_generate(self, prompt, add_negative, negative, url, clip):
client = Client(url=url)
response = client.generate(query=prompt)
if add_negative != 'false':
response['neg_prompt'] = f'{response["neg_prompt"]},{negative}'
prompt_encoded = self.encode(clip=clip, text=response['prompt'])
neg_prompt_encoded = self.encode(clip=clip, text=response['neg_prompt'])
return (response['prompt'], response['neg_prompt'],prompt_encoded[0],neg_prompt_encoded[0],)
class PromptQuillSail:
def __init__(self):
pass
@classmethod
def IS_CHANGED(cls, **kwargs):
return float("NaN")
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"prompt": ("STRING", {
"multiline": True,
"default": "A cute cat?"
}),
"distance": ("INT", {"default": 20, "min": 1, "max": 10000, "step": 1}),
"summary": ("BOOLEAN", {"default": False}),
"rephrase": ("BOOLEAN", {"default": False}),
"rephrase_prompt": ("STRING", {
"multiline": True,
"default": ""
}),
"add_style": ("BOOLEAN", {"default": False}),
"style": ("STRING", {
"multiline": True,
"default": ""
}),
"add_search": ("BOOLEAN", {"default": False}),
"search": ("STRING", {
"multiline": True,
"default": ""
}),
"add_negative": ("BOOLEAN", {"default": False}),
"negative": ("STRING", {
"multiline": True,
"default": ""
}),
"reset_journey": ("BOOLEAN", {"default": False}),
"unload_llm": ("BOOLEAN", {"default": False}),
"url": ("STRING", {
"multiline": False,
"default": default_url
}),
},
}
RETURN_TYPES = ("STRING", "STRING",)
RETURN_NAMES = ("Prompt", "NegativePrompt",)
FUNCTION = "prompt_quill_sail"
CATEGORY = "PromptQuill"
def prompt_quill_sail(self, prompt, distance, summary, rephrase, rephrase_prompt, add_style, style, add_search,
search, add_negative, negative, reset_journey, unload_llm, url):
client = Client(url=url)
response = client.sail(query=prompt, distance=distance, summary=summary, rephrase=rephrase,
rephrase_prompt=rephrase_prompt, add_style=add_style, style=style, add_search=add_search,
search=search, reset_journey=reset_journey,unload_llm=unload_llm)
if add_negative != 'false':
response['neg_prompt'] = f'{response["neg_prompt"]},{negative}'
return (response['prompt'], response['neg_prompt'],)
class PromptQuillSailConditioning:
def __init__(self):
pass
@classmethod
def IS_CHANGED(cls, **kwargs):
return float("NaN")
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"prompt": ("STRING", {
"multiline": True,
"default": "A cute cat?"
}),
"distance": ("INT", {"default": 20, "min": 1, "max": 10000, "step": 1}),
"summary": ("BOOLEAN", {"default": False}),
"rephrase": ("BOOLEAN", {"default": False}),
"rephrase_prompt": ("STRING", {
"multiline": True,
"default": ""
}),
"add_style": ("BOOLEAN", {"default": False}),
"style": ("STRING", {
"multiline": True,
"default": ""
}),
"add_search": ("BOOLEAN", {"default": False}),
"search": ("STRING", {
"multiline": True,
"default": ""
}),
"add_negative": ("BOOLEAN", {"default": False}),
"negative": ("STRING", {
"multiline": True,
"default": ""
}),
"reset_journey": ("BOOLEAN", {"default": False}),
"unload_llm": ("BOOLEAN", {"default": False}),
"url": ("STRING", {
"multiline": False,
"default": default_url
}),
"clip": ("CLIP", )
},
}
RETURN_TYPES = ("STRING", "STRING","CONDITIONING","CONDITIONING",)
RETURN_NAMES = ("Prompt", "NegativePrompt","Prompt Conditioning", "Negative Conditioning")
FUNCTION = "prompt_quill_sail"
CATEGORY = "PromptQuill"
def encode(self, clip, text):
tokens = clip.tokenize(text)
cond, pooled = clip.encode_from_tokens(tokens, return_pooled=True)
return ([[cond, {"pooled_output": pooled}]], )
def prompt_quill_sail(self, prompt, distance, summary, rephrase, rephrase_prompt, add_style, style, add_search,
search, add_negative, negative, reset_journey,unload_llm, url, clip):
client = Client(url=url)
response = client.sail(query=prompt, distance=distance, summary=summary, rephrase=rephrase,
rephrase_prompt=rephrase_prompt, add_style=add_style, style=style, add_search=add_search,
search=search, reset_journey=reset_journey,unload_llm=unload_llm)
if add_negative is True:
response['neg_prompt'] = f'{response["neg_prompt"]},{negative}'
prompt_encoded = self.encode(clip=clip, text=response['prompt'])
neg_prompt_encoded = self.encode(clip=clip, text=response['neg_prompt'])
return (response['prompt'], response['neg_prompt'],prompt_encoded[0],neg_prompt_encoded[0],)
NODE_CLASS_MAPPINGS = {
"PromptQuillGenerate": PromptQuillGenerate,
"PromptQuillGenerateConditioning": PromptQuillGenerateConditioning,
"PromptQuillSail": PromptQuillSail,
"PromptQuillSailConditioning": PromptQuillSailConditioning,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"PromptQuillGenerate": "Prompt Quill Generate",
"PromptQuillGenerateConditioning": "Prompt Quill Generate Conditioning",
"PromptQuillSail": "Prompt Quill Sailing",
"PromptQuillSailConditioning": "Prompt Quill Sailing to Conditioning",
}