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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
import sys
|
4 |
+
from typing import Sequence, Mapping, Any, Union
|
5 |
+
import torch
|
6 |
+
|
7 |
+
|
8 |
+
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
|
9 |
+
"""Returns the value at the given index of a sequence or mapping.
|
10 |
+
|
11 |
+
If the object is a sequence (like list or string), returns the value at the given index.
|
12 |
+
If the object is a mapping (like a dictionary), returns the value at the index-th key.
|
13 |
+
|
14 |
+
Some return a dictionary, in these cases, we look for the "results" key
|
15 |
+
|
16 |
+
Args:
|
17 |
+
obj (Union[Sequence, Mapping]): The object to retrieve the value from.
|
18 |
+
index (int): The index of the value to retrieve.
|
19 |
+
|
20 |
+
Returns:
|
21 |
+
Any: The value at the given index.
|
22 |
+
|
23 |
+
Raises:
|
24 |
+
IndexError: If the index is out of bounds for the object and the object is not a mapping.
|
25 |
+
"""
|
26 |
+
try:
|
27 |
+
return obj[index]
|
28 |
+
except KeyError:
|
29 |
+
return obj["result"][index]
|
30 |
+
|
31 |
+
|
32 |
+
def find_path(name: str, path: str = None) -> str:
|
33 |
+
"""
|
34 |
+
Recursively looks at parent folders starting from the given path until it finds the given name.
|
35 |
+
Returns the path as a Path object if found, or None otherwise.
|
36 |
+
"""
|
37 |
+
# If no path is given, use the current working directory
|
38 |
+
if path is None:
|
39 |
+
path = os.getcwd()
|
40 |
+
|
41 |
+
# Check if the current directory contains the name
|
42 |
+
if name in os.listdir(path):
|
43 |
+
path_name = os.path.join(path, name)
|
44 |
+
print(f"{name} found: {path_name}")
|
45 |
+
return path_name
|
46 |
+
|
47 |
+
# Get the parent directory
|
48 |
+
parent_directory = os.path.dirname(path)
|
49 |
+
|
50 |
+
# If the parent directory is the same as the current directory, we've reached the root and stop the search
|
51 |
+
if parent_directory == path:
|
52 |
+
return None
|
53 |
+
|
54 |
+
# Recursively call the function with the parent directory
|
55 |
+
return find_path(name, parent_directory)
|
56 |
+
|
57 |
+
|
58 |
+
def add_comfyui_directory_to_sys_path() -> None:
|
59 |
+
"""
|
60 |
+
Add 'ComfyUI' to the sys.path
|
61 |
+
"""
|
62 |
+
comfyui_path = find_path("ComfyUI")
|
63 |
+
if comfyui_path is not None and os.path.isdir(comfyui_path):
|
64 |
+
sys.path.append(comfyui_path)
|
65 |
+
print(f"'{comfyui_path}' added to sys.path")
|
66 |
+
|
67 |
+
|
68 |
+
def add_extra_model_paths() -> None:
|
69 |
+
"""
|
70 |
+
Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path.
|
71 |
+
"""
|
72 |
+
try:
|
73 |
+
from main import load_extra_path_config
|
74 |
+
except ImportError:
|
75 |
+
print(
|
76 |
+
"Could not import load_extra_path_config from main.py. Looking in utils.extra_config instead."
|
77 |
+
)
|
78 |
+
from utils.extra_config import load_extra_path_config
|
79 |
+
|
80 |
+
extra_model_paths = find_path("extra_model_paths.yaml")
|
81 |
+
|
82 |
+
if extra_model_paths is not None:
|
83 |
+
load_extra_path_config(extra_model_paths)
|
84 |
+
else:
|
85 |
+
print("Could not find the extra_model_paths config file.")
|
86 |
+
|
87 |
+
|
88 |
+
add_comfyui_directory_to_sys_path()
|
89 |
+
add_extra_model_paths()
|
90 |
+
|
91 |
+
|
92 |
+
def import_custom_nodes() -> None:
|
93 |
+
"""Find all custom nodes in the custom_nodes folder and add those node objects to NODE_CLASS_MAPPINGS
|
94 |
+
|
95 |
+
This function sets up a new asyncio event loop, initializes the PromptServer,
|
96 |
+
creates a PromptQueue, and initializes the custom nodes.
|
97 |
+
"""
|
98 |
+
import asyncio
|
99 |
+
import execution
|
100 |
+
from nodes import init_extra_nodes
|
101 |
+
import server
|
102 |
+
|
103 |
+
# Creating a new event loop and setting it as the default loop
|
104 |
+
loop = asyncio.new_event_loop()
|
105 |
+
asyncio.set_event_loop(loop)
|
106 |
+
|
107 |
+
# Creating an instance of PromptServer with the loop
|
108 |
+
server_instance = server.PromptServer(loop)
|
109 |
+
execution.PromptQueue(server_instance)
|
110 |
+
|
111 |
+
# Initializing custom nodes
|
112 |
+
init_extra_nodes()
|
113 |
+
|
114 |
+
|
115 |
+
from nodes import NODE_CLASS_MAPPINGS
|
116 |
+
|
117 |
+
|
118 |
+
def generate_image(prompt, structure_image, style_image, depth_strength, style_strength):
|
119 |
+
import_custom_nodes()
|
120 |
+
with torch.inference_mode():
|
121 |
+
vaeloader = NODE_CLASS_MAPPINGS["VAELoader"]()
|
122 |
+
vaeloader_10 = vaeloader.load_vae(vae_name="FLUX1/ae.safetensors")
|
123 |
+
|
124 |
+
dualcliploader = NODE_CLASS_MAPPINGS["DualCLIPLoader"]()
|
125 |
+
dualcliploader_11 = dualcliploader.load_clip(
|
126 |
+
clip_name1="clip_l.safetensors",
|
127 |
+
clip_name2="t5xxl_fp8_e4m3fn.safetensors",
|
128 |
+
type="flux",
|
129 |
+
)
|
130 |
+
|
131 |
+
loadimage = NODE_CLASS_MAPPINGS["LoadImage"]()
|
132 |
+
loadimage_97 = loadimage.load_image(image=structure_image)
|
133 |
+
|
134 |
+
pulidfluxinsightfaceloader = NODE_CLASS_MAPPINGS["PulidFluxInsightFaceLoader"]()
|
135 |
+
pulidfluxinsightfaceloader_98 = pulidfluxinsightfaceloader.load_insightface(
|
136 |
+
provider="CUDA"
|
137 |
+
)
|
138 |
+
|
139 |
+
pulidfluxmodelloader = NODE_CLASS_MAPPINGS["PulidFluxModelLoader"]()
|
140 |
+
pulidfluxmodelloader_99 = pulidfluxmodelloader.load_model(
|
141 |
+
pulid_file="pulid_flux_v0.9.1.safetensors"
|
142 |
+
)
|
143 |
+
|
144 |
+
pulidfluxevacliploader = NODE_CLASS_MAPPINGS["PulidFluxEvaClipLoader"]()
|
145 |
+
pulidfluxevacliploader_100 = pulidfluxevacliploader.load_eva_clip()
|
146 |
+
|
147 |
+
cliptextencode = NODE_CLASS_MAPPINGS["CLIPTextEncode"]()
|
148 |
+
cliptextencode_121 = cliptextencode.encode(
|
149 |
+
text=prompt, clip=get_value_at_index(dualcliploader_11, 0)
|
150 |
+
)
|
151 |
+
|
152 |
+
conditioningzeroout = NODE_CLASS_MAPPINGS["ConditioningZeroOut"]()
|
153 |
+
conditioningzeroout_116 = conditioningzeroout.zero_out(
|
154 |
+
conditioning=get_value_at_index(cliptextencode_121, 0)
|
155 |
+
)
|
156 |
+
|
157 |
+
loadimage_129 = loadimage.load_image(
|
158 |
+
image=style_image
|
159 |
+
)
|
160 |
+
|
161 |
+
getimagesize = NODE_CLASS_MAPPINGS["GetImageSize+"]()
|
162 |
+
getimagesize_113 = getimagesize.execute(
|
163 |
+
image=get_value_at_index(loadimage_129, 0)
|
164 |
+
)
|
165 |
+
|
166 |
+
imageresize = NODE_CLASS_MAPPINGS["ImageResize+"]()
|
167 |
+
imageresize_112 = imageresize.execute(
|
168 |
+
width=get_value_at_index(getimagesize_113, 0),
|
169 |
+
height=get_value_at_index(getimagesize_113, 1),
|
170 |
+
interpolation="nearest",
|
171 |
+
method="keep proportion",
|
172 |
+
condition="always",
|
173 |
+
multiple_of=0,
|
174 |
+
image=get_value_at_index(loadimage_129, 0),
|
175 |
+
)
|
176 |
+
|
177 |
+
layermask_personmaskultra = NODE_CLASS_MAPPINGS["LayerMask: PersonMaskUltra"]()
|
178 |
+
layermask_personmaskultra_120 = layermask_personmaskultra.person_mask_ultra(
|
179 |
+
face=True,
|
180 |
+
hair=False,
|
181 |
+
body=False,
|
182 |
+
clothes=False,
|
183 |
+
accessories=False,
|
184 |
+
background=False,
|
185 |
+
confidence=0.4,
|
186 |
+
detail_range=16,
|
187 |
+
black_point=0.01,
|
188 |
+
white_point=0.99,
|
189 |
+
process_detail=True,
|
190 |
+
images=get_value_at_index(imageresize_112, 0),
|
191 |
+
)
|
192 |
+
|
193 |
+
growmask = NODE_CLASS_MAPPINGS["GrowMask"]()
|
194 |
+
growmask_118 = growmask.expand_mask(
|
195 |
+
expand=43,
|
196 |
+
tapered_corners=True,
|
197 |
+
mask=get_value_at_index(layermask_personmaskultra_120, 1),
|
198 |
+
)
|
199 |
+
|
200 |
+
maskblur = NODE_CLASS_MAPPINGS["MaskBlur+"]()
|
201 |
+
maskblur_119 = maskblur.execute(
|
202 |
+
amount=60, device="auto", mask=get_value_at_index(growmask_118, 0)
|
203 |
+
)
|
204 |
+
|
205 |
+
inpaintmodelconditioning = NODE_CLASS_MAPPINGS["InpaintModelConditioning"]()
|
206 |
+
inpaintmodelconditioning_110 = inpaintmodelconditioning.encode(
|
207 |
+
noise_mask=True,
|
208 |
+
positive=get_value_at_index(cliptextencode_121, 0),
|
209 |
+
negative=get_value_at_index(conditioningzeroout_116, 0),
|
210 |
+
vae=get_value_at_index(vaeloader_10, 0),
|
211 |
+
pixels=get_value_at_index(imageresize_112, 0),
|
212 |
+
mask=get_value_at_index(maskblur_119, 0),
|
213 |
+
)
|
214 |
+
|
215 |
+
unetloader = NODE_CLASS_MAPPINGS["UNETLoader"]()
|
216 |
+
unetloader_111 = unetloader.load_unet(
|
217 |
+
unet_name="FLUX1/flux1-dev.safetensors", weight_dtype="fp8_e4m3fn"
|
218 |
+
)
|
219 |
+
|
220 |
+
randomnoise = NODE_CLASS_MAPPINGS["RandomNoise"]()
|
221 |
+
randomnoise_114 = randomnoise.get_noise(noise_seed=random.randint(1, 2**64))
|
222 |
+
|
223 |
+
ksamplerselect = NODE_CLASS_MAPPINGS["KSamplerSelect"]()
|
224 |
+
ksamplerselect_115 = ksamplerselect.get_sampler(sampler_name="euler")
|
225 |
+
|
226 |
+
applypulidflux = NODE_CLASS_MAPPINGS["ApplyPulidFlux"]()
|
227 |
+
repeatlatentbatch = NODE_CLASS_MAPPINGS["RepeatLatentBatch"]()
|
228 |
+
basicguider = NODE_CLASS_MAPPINGS["BasicGuider"]()
|
229 |
+
basicscheduler = NODE_CLASS_MAPPINGS["BasicScheduler"]()
|
230 |
+
samplercustomadvanced = NODE_CLASS_MAPPINGS["SamplerCustomAdvanced"]()
|
231 |
+
vaedecode = NODE_CLASS_MAPPINGS["VAEDecode"]()
|
232 |
+
saveimage = NODE_CLASS_MAPPINGS["SaveImage"]()
|
233 |
+
|
234 |
+
applypulidflux_101 = applypulidflux.apply_pulid_flux(
|
235 |
+
weight=1.1,
|
236 |
+
start_at=0,
|
237 |
+
end_at=1,
|
238 |
+
fusion="max",
|
239 |
+
fusion_weight_max=1,
|
240 |
+
fusion_weight_min=0,
|
241 |
+
train_step=1000,
|
242 |
+
use_gray=True,
|
243 |
+
model=get_value_at_index(unetloader_111, 0),
|
244 |
+
pulid_flux=get_value_at_index(pulidfluxmodelloader_99, 0),
|
245 |
+
eva_clip=get_value_at_index(pulidfluxevacliploader_100, 0),
|
246 |
+
face_analysis=get_value_at_index(pulidfluxinsightfaceloader_98, 0),
|
247 |
+
image=get_value_at_index(loadimage_97, 0),
|
248 |
+
unique_id=12000670301720322250,
|
249 |
+
)
|
250 |
+
|
251 |
+
repeatlatentbatch_107 = repeatlatentbatch.repeat(
|
252 |
+
amount=1, samples=get_value_at_index(inpaintmodelconditioning_110, 2)
|
253 |
+
)
|
254 |
+
|
255 |
+
basicguider_117 = basicguider.get_guider(
|
256 |
+
model=get_value_at_index(applypulidflux_101, 0),
|
257 |
+
conditioning=get_value_at_index(inpaintmodelconditioning_110, 0),
|
258 |
+
)
|
259 |
+
|
260 |
+
basicscheduler_130 = basicscheduler.get_sigmas(
|
261 |
+
scheduler="normal",
|
262 |
+
steps=14,
|
263 |
+
denoise=0.6,
|
264 |
+
model=get_value_at_index(unetloader_111, 0),
|
265 |
+
)
|
266 |
+
|
267 |
+
samplercustomadvanced_109 = samplercustomadvanced.sample(
|
268 |
+
noise=get_value_at_index(randomnoise_114, 0),
|
269 |
+
guider=get_value_at_index(basicguider_117, 0),
|
270 |
+
sampler=get_value_at_index(ksamplerselect_115, 0),
|
271 |
+
sigmas=get_value_at_index(basicscheduler_130, 0),
|
272 |
+
latent_image=get_value_at_index(repeatlatentbatch_107, 0),
|
273 |
+
)
|
274 |
+
|
275 |
+
vaedecode_122 = vaedecode.decode(
|
276 |
+
samples=get_value_at_index(samplercustomadvanced_109, 0),
|
277 |
+
vae=get_value_at_index(vaeloader_10, 0),
|
278 |
+
)
|
279 |
+
|
280 |
+
saveimage_127 = saveimage.save_images(
|
281 |
+
filename_prefix="ComfyUI", images=get_value_at_index(vaedecode_122, 0)
|
282 |
+
)
|
283 |
+
saved_path = f"output/{saveimage_127['ui']['images'][0]['filename']}"
|
284 |
+
return saved_path
|
285 |
+
|
286 |
+
|
287 |
+
#if __name__ == "__main__":
|
288 |
+
# main()
|