-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_args.py
356 lines (329 loc) · 12.4 KB
/
api_args.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import random
class ApiArgs:
def __init__(self):
pass
def so_upscale(self, img_gen_args):
prompt = {
"1": {
"inputs": {
"image": img_gen_args["so_upscale_image"],
"choose file to upload": "image",
},
"class_type": "LoadImage",
},
"2": {
"inputs": {"model_name": img_gen_args["so_upscale_model"]},
"class_type": "UpscaleModelLoader",
},
"3": {
"inputs": {"upscale_model": ["2", 0], "image": ["1", 0]},
"class_type": "ImageUpscaleWithModel",
},
"4": {
"inputs": {"filename_prefix": "SnugQt/SnugQt", "images": ["3", 0]},
"class_type": "SaveImage",
},
}
if img_gen_args["downscale"]:
downscale_node = {
"inputs": {
"upscale_method": "bicubic",
"scale_by": img_gen_args["downscale"],
"image": ["3", 0],
},
"class_type": "ImageScaleBy",
}
prompt["5"] = downscale_node
prompt["4"]["inputs"]["images"][0] = "5"
return prompt
def gen_base(self, img_gen_args):
base_node = {
"1": {
"inputs": {"ckpt_name": img_gen_args["ckpt_name"]},
"class_type": "CheckpointLoaderSimple",
},
"4": {
"inputs": {"text": img_gen_args["pos_prompt"], "clip": ["1", 1]},
"class_type": "CLIPTextEncode",
},
"5": {
"inputs": {"text": img_gen_args["neg_prompt"], "clip": ["1", 1]},
"class_type": "CLIPTextEncode",
},
"6": {
"inputs": {
"width": img_gen_args["width"],
"height": img_gen_args["height"],
"batch_size": img_gen_args["batch_size"],
},
"class_type": "EmptyLatentImage",
},
"7": {
"inputs": {
"seed": img_gen_args["seed"],
"steps": img_gen_args["steps"],
"cfg": img_gen_args["cfg"],
"sampler_name": img_gen_args["sampler_name"],
"scheduler": img_gen_args["scheduler"],
"denoise": 1.0,
"model": ["1", 0],
"positive": ["4", 0],
"negative": ["5", 0],
"latent_image": ["6", 0],
},
"class_type": "KSampler",
},
"10": {
"inputs": {"samples": ["7", 0], "vae": ["1", 2]},
"class_type": "VAEDecode",
},
"11": {
"inputs": {
"filename_prefix": img_gen_args["filename_prefix"],
"images": ["10", 0],
},
"class_type": "SaveImage",
},
"12": {
"inputs": {
"stop_at_clip_layer": img_gen_args["clip_skip"],
"clip": ["1", 1],
},
"class_type": "CLIPSetLastLayer",
},
}
return base_node
def gen_external_vae(self, img_gen_args):
ex_vae_node = {
"inputs": {"vae_name": img_gen_args["external_vae"]},
"class_type": "VAELoader",
}
return ex_vae_node
def gen_lora(self, img_gen_args):
lora_node = {
"inputs": {
"lora_name": img_gen_args["lora"],
"strength_model": img_gen_args["lora_strength"],
"strength_clip": img_gen_args["lora_clip_strength"],
"model": ["1", 0],
"clip": ["1", 1],
},
"class_type": "LoraLoader",
}
return lora_node
def gen_hires_fix(self, img_gen_args):
node1 = {
"inputs": {
"upscale_method": "nearest-exact",
"scale_by": img_gen_args["hiresfix_scale_by"],
"samples": ["7", 0],
},
"class_type": "LatentUpscaleBy",
}
node2 = {
"inputs": {
"seed": random.randint(1, 4294967294),
"steps": img_gen_args["hiresfix_steps"],
"cfg": img_gen_args["cfg"],
"sampler_name": img_gen_args["sampler_name"],
"scheduler": img_gen_args["scheduler"],
"denoise": img_gen_args["hiresfix_denoise"],
"model": ["1", 0],
"positive": ["4", 0],
"negative": ["5", 0],
"latent_image": ["8", 0],
},
"class_type": "KSampler",
}
return node1, node2
def gen_refiner_node(self, img_gen_args):
total_steps = int(img_gen_args["steps"]) + int(
img_gen_args["sdxl_refiner_steps"]
)
refiner_model = {
"inputs": {"ckpt_name": img_gen_args["sdxl_refiner_ckpt"]},
"class_type": "CheckpointLoaderSimple",
}
refiner_node = {
"inputs": {
"add_noise": "disable",
"noise_seed": 0,
"steps": total_steps,
"cfg": img_gen_args["cfg"],
"sampler_name": img_gen_args["sampler_name"],
"scheduler": img_gen_args["scheduler"],
"start_at_step": img_gen_args["steps"],
"end_at_step": 10000,
"return_with_leftover_noise": "disable",
"model": ["98", 0],
"positive": ["96", 0],
"negative": ["97", 0],
"latent_image": ["7", 0],
},
"class_type": "KSamplerAdvanced",
}
pos_clip = {
"inputs": {"text": img_gen_args["pos_prompt"], "clip": ["98", 1]},
"class_type": "CLIPTextEncode",
}
neg_clip = {
"inputs": {
"text": img_gen_args["neg_prompt"],
"clip": ["98", 1],
},
"class_type": "CLIPTextEncode",
}
return refiner_model, refiner_node, pos_clip, neg_clip
def gen_upscale_model(self, img_gen_args):
node1 = {
"inputs": {"model_name": img_gen_args["upscale_model"]},
"class_type": "UpscaleModelLoader",
}
node2 = {
"inputs": {"upscale_model": ["16", 0], "image": ["10", 0]},
"class_type": "ImageUpscaleWithModel",
}
return node1, node2
def gen_img2img(self, img_gen_args):
LoadImage = {
"inputs": {
"image": img_gen_args["img2img_load"],
"choose file to upload": "image",
},
"class_type": "LoadImage",
}
VAEEncode = {
"inputs": {"pixels": ["80", 0], "vae": ["1", 2]},
"class_type": "VAEEncode",
}
return LoadImage, VAEEncode
def gen_inpainting(self, img_gen_args):
LoadImage = {
"inputs": {
"image": str(img_gen_args["inpainting_load"]),
"choose file to upload": "image",
},
"class_type": "LoadImage",
}
VAEEncode = {
"inputs": {
"grow_mask_by": 18,
"pixels": ["80", 0],
"vae": ["1", 2],
"mask": ["80", 1],
},
"class_type": "VAEEncodeForInpaint",
}
ImagePadForOutpaint = (
{
"inputs": {
"left": img_gen_args["outpaint_l"],
"top": img_gen_args["outpaint_t"],
"right": img_gen_args["outpaint_r"],
"bottom": img_gen_args["outpaint_b"],
"feathering": 40,
"image": ["80", 0],
},
"class_type": "ImagePadForOutpaint",
}
if img_gen_args["outpaint_check"]
else None
)
return LoadImage, VAEEncode, ImagePadForOutpaint
def control_net(self, img_gen_args):
LoadImage = {
"inputs": {
"image": img_gen_args["controlnet"],
"choose file to upload": "image",
},
"class_type": "LoadImage",
}
DiffControlNetLoader = {
"inputs": {
"control_net_name": img_gen_args["model"],
"model": ["1", 0],
},
"class_type": "DiffControlNetLoader",
}
ControlNetApply = {
"inputs": {
"strength": img_gen_args["controlnet_strength"],
"conditioning": ["4", 0],
"control_net": ["71", 0],
"image": ["70", 0],
},
"class_type": "ControlNetApply",
}
return LoadImage, DiffControlNetLoader, ControlNetApply
def generate_api_prompt(self, img_gen_args):
api_prompt = self.gen_base(img_gen_args)
if img_gen_args["external_vae"]:
ex_vae_node = self.gen_external_vae(img_gen_args)
api_prompt["2"] = ex_vae_node
api_prompt["10"]["inputs"]["vae"][0] = "2"
api_prompt["10"]["inputs"]["vae"][1] = 0
if img_gen_args["lora"]:
lora_node = self.gen_lora(img_gen_args)
api_prompt["3"] = lora_node
api_prompt["4"]["inputs"]["clip"][0] = "3"
api_prompt["5"]["inputs"]["clip"][0] = "3"
api_prompt["7"]["inputs"]["model"][0] = "3"
if (
img_gen_args["hiresfix_steps"]
and not img_gen_args["sdxl_refiner_ckpt"]
and not img_gen_args["img2img_load"]
and not img_gen_args["inpainting_load"]
):
node1, node2 = self.gen_hires_fix(img_gen_args)
api_prompt["8"] = node1
api_prompt["9"] = node2
api_prompt["10"]["inputs"]["samples"][0] = "9"
if img_gen_args["lora"]:
api_prompt["9"]["inputs"]["model"][0] = "3"
if img_gen_args["upscale_model"]:
node1, node2 = self.gen_upscale_model(img_gen_args)
api_prompt["16"] = node1
api_prompt["17"] = node2
api_prompt["11"]["inputs"]["images"][0] = "17"
if img_gen_args["sdxl_refiner_ckpt"]:
refiner_model, refiner_node, pos_clip, neg_clip = self.gen_refiner_node(
img_gen_args
)
api_prompt["98"] = refiner_model
api_prompt["99"] = refiner_node
api_prompt["96"] = pos_clip
api_prompt["97"] = neg_clip
api_prompt["10"]["inputs"]["samples"][0] = "99"
if img_gen_args["img2img_load"]:
LoadImage, VAEEncode = self.gen_img2img(img_gen_args)
api_prompt["80"] = LoadImage
api_prompt["81"] = VAEEncode
api_prompt["7"]["inputs"]["latent_image"][0] = "81"
api_prompt["7"]["inputs"]["denoise"] = img_gen_args["img2img_denoise"]
if img_gen_args["external_vae"]:
api_prompt["81"]["inputs"]["vae"][0] = "2"
api_prompt["81"]["inputs"]["vae"][1] = 0
if img_gen_args["inpainting_load"]:
LoadImage, VAEEncode, ImagePadForOutpaint = self.gen_inpainting(
img_gen_args
)
api_prompt["80"] = LoadImage
api_prompt["81"] = VAEEncode
api_prompt["7"]["inputs"]["latent_image"][0] = "81"
api_prompt["7"]["inputs"]["denoise"] = img_gen_args["inpaint_denoise"]
if img_gen_args["external_vae"]:
api_prompt["81"]["inputs"]["vae"][0] = "2"
api_prompt["81"]["inputs"]["vae"][1] = 0
if img_gen_args["outpaint_check"]:
api_prompt["82"] = ImagePadForOutpaint
api_prompt["81"]["inputs"]["pixels"][0] = "82"
api_prompt["81"]["inputs"]["mask"][0] = "82"
if img_gen_args["controlnet"]:
LoadImage, DiffControlNetLoader, ControlNetApply = self.control_net(
img_gen_args
)
api_prompt["70"] = LoadImage
api_prompt["71"] = DiffControlNetLoader
api_prompt["72"] = ControlNetApply
api_prompt["7"]["inputs"]["positive"][0] = "72"
return api_prompt