231 lines
7.0 KiB
Python
231 lines
7.0 KiB
Python
# Imports
|
|
import json
|
|
import os
|
|
import random
|
|
import sys
|
|
from typing import Sequence, Mapping, Any, Union
|
|
|
|
|
|
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
|
|
"""Return a sequence or mapping result item by index."""
|
|
try:
|
|
return obj[index]
|
|
except KeyError:
|
|
return obj["result"][index]
|
|
|
|
|
|
def get_comfyui_path() -> str:
|
|
"""Return the configured ComfyUI path, preferring COMFYUI_PATH when set."""
|
|
comfyui_path = os.environ.get("COMFYUI_PATH")
|
|
if comfyui_path:
|
|
return comfyui_path
|
|
return find_path("ComfyUI")
|
|
|
|
|
|
def find_path(name: str, path: str = None) -> str:
|
|
"""Recursively search parent folders until the named entry is found."""
|
|
if path is None:
|
|
path = os.getcwd()
|
|
|
|
if name in os.listdir(path):
|
|
path_name = os.path.join(path, name)
|
|
print(f"{name} found: {path_name}")
|
|
return path_name
|
|
|
|
parent_directory = os.path.dirname(path)
|
|
if parent_directory == path:
|
|
return None
|
|
|
|
return find_path(name, parent_directory)
|
|
|
|
|
|
def add_comfyui_directory_to_sys_path() -> None:
|
|
"""Add the ComfyUI checkout to sys.path."""
|
|
comfyui_path = get_comfyui_path()
|
|
if comfyui_path is not None and os.path.isdir(comfyui_path):
|
|
if comfyui_path in sys.path:
|
|
sys.path.remove(comfyui_path)
|
|
sys.path.insert(0, comfyui_path)
|
|
print(f"'{comfyui_path}' added to sys.path")
|
|
|
|
|
|
def add_extra_model_paths() -> None:
|
|
"""Load ComfyUI extra model paths configuration when available."""
|
|
try:
|
|
from main import load_extra_path_config
|
|
except ImportError:
|
|
print(
|
|
"Could not import load_extra_path_config from main.py. Looking in utils.extra_config instead."
|
|
)
|
|
from utils.extra_config import load_extra_path_config
|
|
|
|
extra_model_paths = find_path("extra_model_paths.yaml")
|
|
if extra_model_paths is not None:
|
|
load_extra_path_config(extra_model_paths)
|
|
else:
|
|
print("Could not find the extra_model_paths config file.")
|
|
|
|
|
|
def bootstrap_comfyui_runtime() -> None:
|
|
"""Mirror the allocator-related ComfyUI startup steps before torch import."""
|
|
add_comfyui_directory_to_sys_path()
|
|
|
|
import comfy.options
|
|
|
|
comfy.options.enable_args_parsing()
|
|
|
|
from comfy.cli_args import args
|
|
|
|
if os.name == "nt":
|
|
os.environ["MIMALLOC_PURGE_DELAY"] = "0"
|
|
|
|
if args.default_device is not None:
|
|
default_dev = args.default_device
|
|
devices = list(range(32))
|
|
devices.remove(default_dev)
|
|
devices.insert(0, default_dev)
|
|
devices = ",".join(map(str, devices))
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = str(devices)
|
|
os.environ["HIP_VISIBLE_DEVICES"] = str(devices)
|
|
|
|
if args.cuda_device is not None:
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.cuda_device)
|
|
os.environ["HIP_VISIBLE_DEVICES"] = str(args.cuda_device)
|
|
os.environ["ASCEND_RT_VISIBLE_DEVICES"] = str(args.cuda_device)
|
|
|
|
if args.oneapi_device_selector is not None:
|
|
os.environ["ONEAPI_DEVICE_SELECTOR"] = args.oneapi_device_selector
|
|
|
|
if args.deterministic and "CUBLAS_WORKSPACE_CONFIG" not in os.environ:
|
|
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
|
|
|
|
import cuda_malloc
|
|
|
|
if "rocm" in cuda_malloc.get_torch_version_noimport():
|
|
os.environ["OCL_SET_SVM_SIZE"] = "262144"
|
|
|
|
|
|
def cleanup_comfyui_runtime(unload_models: bool | None = None) -> None:
|
|
"""Best-effort cleanup for embedded or repeated generated-script execution."""
|
|
import gc
|
|
|
|
should_unload = unload_models
|
|
if should_unload is None:
|
|
should_unload = os.environ.get(
|
|
"COMFYUI_TOPYTHON_UNLOAD_MODELS", ""
|
|
).lower() in {
|
|
"1",
|
|
"true",
|
|
"yes",
|
|
"on",
|
|
}
|
|
|
|
try:
|
|
import comfy.model_management as model_management
|
|
except ModuleNotFoundError:
|
|
gc.collect()
|
|
return
|
|
|
|
if hasattr(model_management, "cleanup_models_gc"):
|
|
model_management.cleanup_models_gc()
|
|
if should_unload and hasattr(model_management, "unload_all_models"):
|
|
model_management.unload_all_models()
|
|
if hasattr(model_management, "soft_empty_cache"):
|
|
model_management.soft_empty_cache()
|
|
gc.collect()
|
|
|
|
|
|
def import_custom_nodes() -> None:
|
|
"""Initialize ComfyUI custom nodes in the exporter runtime."""
|
|
comfyui_path = get_comfyui_path()
|
|
if comfyui_path and comfyui_path not in sys.path:
|
|
sys.path.insert(0, comfyui_path)
|
|
|
|
import asyncio
|
|
import execution
|
|
from nodes import init_extra_nodes
|
|
|
|
if comfyui_path in sys.path:
|
|
sys.path.remove(comfyui_path)
|
|
sys.path.insert(0, comfyui_path)
|
|
|
|
import server
|
|
|
|
loop = asyncio.new_event_loop()
|
|
asyncio.set_event_loop(loop)
|
|
server_instance = server.PromptServer(loop)
|
|
execution.PromptQueue(server_instance)
|
|
asyncio.run(init_extra_nodes())
|
|
|
|
|
|
# Workflow data
|
|
def build_workflow() -> dict[str, Any]:
|
|
return {
|
|
"1": {"class_type": "LoadImage", "inputs": {"image": "e2e_upscale_input.png"}},
|
|
"2": {
|
|
"class_type": "UpscaleModelLoader",
|
|
"inputs": {"model_name": "RealESRGAN_x4plus.safetensors"},
|
|
},
|
|
"3": {
|
|
"class_type": "ImageUpscaleWithModel",
|
|
"inputs": {"upscale_model": ["2", 0], "image": ["1", 0]},
|
|
},
|
|
"4": {
|
|
"class_type": "SaveImage",
|
|
"inputs": {
|
|
"filename_prefix": "E2E_upscale_model_loader",
|
|
"images": ["3", 0],
|
|
},
|
|
},
|
|
}
|
|
|
|
|
|
def build_extra_pnginfo() -> dict[str, Any] | None:
|
|
return None
|
|
|
|
|
|
workflow = build_workflow()
|
|
prompt = json.loads(json.dumps(workflow))
|
|
extra_pnginfo = build_extra_pnginfo()
|
|
|
|
|
|
# Workflow execution
|
|
def main(unload_models: bool | None = None):
|
|
bootstrap_comfyui_runtime()
|
|
add_extra_model_paths()
|
|
import_custom_nodes()
|
|
|
|
# Node imports
|
|
from nodes import LoadImage, NODE_CLASS_MAPPINGS, SaveImage
|
|
|
|
import torch
|
|
|
|
try:
|
|
with torch.inference_mode():
|
|
loadimage = LoadImage()
|
|
loadimage_1 = loadimage.load_image(image="e2e_upscale_input.png")
|
|
upscalemodelloader = NODE_CLASS_MAPPINGS["UpscaleModelLoader"]()
|
|
upscalemodelloader_2 = upscalemodelloader.EXECUTE_NORMALIZED(
|
|
model_name="RealESRGAN_x4plus.safetensors"
|
|
)
|
|
imageupscalewithmodel = NODE_CLASS_MAPPINGS["ImageUpscaleWithModel"]()
|
|
saveimage = SaveImage()
|
|
for q in range(1):
|
|
imageupscalewithmodel_3 = imageupscalewithmodel.EXECUTE_NORMALIZED(
|
|
upscale_model=get_value_at_index(upscalemodelloader_2, 0),
|
|
image=get_value_at_index(loadimage_1, 0),
|
|
)
|
|
saveimage_4 = saveimage.save_images(
|
|
filename_prefix="E2E_upscale_model_loader",
|
|
images=get_value_at_index(imageupscalewithmodel_3, 0),
|
|
prompt=prompt,
|
|
extra_pnginfo=extra_pnginfo,
|
|
)
|
|
finally:
|
|
cleanup_comfyui_runtime(unload_models=unload_models)
|
|
|
|
|
|
# Entrypoint
|
|
if __name__ == "__main__":
|
|
main()
|