model_checkpoints / workflow_api.py
Crockrocks12's picture
Upload workflow_api.py
e35456b verified
raw
history blame
8.02 kB
import os
import random
import sys
from typing import Sequence, Mapping, Any, Union
import torch
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
"""Returns the value at the given index of a sequence or mapping.
If the object is a sequence (like list or string), returns the value at the given index.
If the object is a mapping (like a dictionary), returns the value at the index-th key.
Some return a dictionary, in these cases, we look for the "results" key
Args:
obj (Union[Sequence, Mapping]): The object to retrieve the value from.
index (int): The index of the value to retrieve.
Returns:
Any: The value at the given index.
Raises:
IndexError: If the index is out of bounds for the object and the object is not a mapping.
"""
try:
return obj[index]
except KeyError:
return obj["result"][index]
def find_path(name: str, path: str = None) -> str:
"""
Recursively looks at parent folders starting from the given path until it finds the given name.
Returns the path as a Path object if found, or None otherwise.
"""
# If no path is given, use the current working directory
if path is None:
path = os.getcwd()
# Check if the current directory contains the name
if name in os.listdir(path):
path_name = os.path.join(path, name)
print(f"{name} found: {path_name}")
return path_name
# Get the parent directory
parent_directory = os.path.dirname(path)
# If the parent directory is the same as the current directory, we've reached the root and stop the search
if parent_directory == path:
return None
# Recursively call the function with the parent directory
return find_path(name, parent_directory)
def add_comfyui_directory_to_sys_path() -> None:
"""
Add 'ComfyUI' to the sys.path
"""
comfyui_path = find_path("ComfyUI")
if comfyui_path is not None and os.path.isdir(comfyui_path):
sys.path.append(comfyui_path)
print(f"'{comfyui_path}' added to sys.path")
def add_extra_model_paths() -> None:
"""
Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path.
"""
from main 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.")
add_comfyui_directory_to_sys_path()
add_extra_model_paths()
def import_custom_nodes() -> None:
"""Find all custom nodes in the custom_nodes folder and add those node objects to NODE_CLASS_MAPPINGS
This function sets up a new asyncio event loop, initializes the PromptServer,
creates a PromptQueue, and initializes the custom nodes.
"""
import asyncio
import execution
from nodes import init_custom_nodes
import server
# Creating a new event loop and setting it as the default loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Creating an instance of PromptServer with the loop
server_instance = server.PromptServer(loop)
execution.PromptQueue(server_instance)
# Initializing custom nodes
init_custom_nodes()
from nodes import SaveImage, NODE_CLASS_MAPPINGS, LoadImage
def main():
import_custom_nodes()
with torch.inference_mode():
loadimage = LoadImage()
loadimage_13 = loadimage.load_image(image="tree-736885_640.jpg")
supir_model_loader = NODE_CLASS_MAPPINGS["SUPIR_model_loader"]()
supir_model_loader_58 = supir_model_loader.process(
supir_model="supir-voq.ckpt",
sdxl_model="juggernautxl.safetensors",
fp8_unet=False,
diffusion_dtype="auto",
)
imageresize = NODE_CLASS_MAPPINGS["ImageResize+"]()
imageresize_18 = imageresize.execute(
width=1280,
height=1280,
interpolation="lanczos",
keep_proportion=False,
condition="always",
multiple_of=0,
image=get_value_at_index(loadimage_13, 0),
)
supir_first_stage = NODE_CLASS_MAPPINGS["SUPIR_first_stage"]()
supir_first_stage_7 = supir_first_stage.process(
use_tiled_vae=True,
encoder_tile_size=512,
decoder_tile_size=512,
encoder_dtype="auto",
SUPIR_VAE=get_value_at_index(supir_model_loader_58, 1),
image=get_value_at_index(imageresize_18, 0),
)
supir_encode = NODE_CLASS_MAPPINGS["SUPIR_encode"]()
supir_encode_21 = supir_encode.encode(
use_tiled_vae=True,
encoder_tile_size=512,
encoder_dtype="auto",
SUPIR_VAE=get_value_at_index(supir_first_stage_7, 0),
image=get_value_at_index(supir_first_stage_7, 1),
)
supir_conditioner = NODE_CLASS_MAPPINGS["SUPIR_conditioner"]()
supir_sample = NODE_CLASS_MAPPINGS["SUPIR_sample"]()
supir_decode = NODE_CLASS_MAPPINGS["SUPIR_decode"]()
image_comparer_rgthree = NODE_CLASS_MAPPINGS["Image Comparer (rgthree)"]()
playsoundpysssss = NODE_CLASS_MAPPINGS["PlaySound|pysssss"]()
colormatch = NODE_CLASS_MAPPINGS["ColorMatch"]()
saveimage = SaveImage()
for q in range(10):
supir_conditioner_12 = supir_conditioner.condition(
positive_prompt="a red car, high quality, detailed",
negative_prompt="bad quality, blurry, messy",
SUPIR_model=get_value_at_index(supir_model_loader_58, 0),
latents=get_value_at_index(supir_first_stage_7, 2),
)
supir_sample_8 = supir_sample.sample(
seed=random.randint(1, 2**64),
steps=10,
cfg_scale_start=5,
cfg_scale_end=5,
EDM_s_churn=5,
s_noise=1.003,
DPMPP_eta=1,
control_scale_start=0.9,
control_scale_end=0.9500000000000001,
restore_cfg=10,
keep_model_loaded=False,
sampler="RestoreDPMPP2MSampler",
sampler_tile_size=1024,
sampler_tile_stride=512,
SUPIR_model=get_value_at_index(supir_model_loader_58, 0),
latents=get_value_at_index(supir_encode_21, 0),
positive=get_value_at_index(supir_conditioner_12, 0),
negative=get_value_at_index(supir_conditioner_12, 1),
)
supir_decode_9 = supir_decode.decode(
use_tiled_vae=True,
decoder_tile_size=512,
SUPIR_VAE=get_value_at_index(supir_model_loader_58, 1),
latents=get_value_at_index(supir_sample_8, 0),
)
image_comparer_rgthree_15 = image_comparer_rgthree.compare_images(
image_a=get_value_at_index(loadimage_13, 0),
image_b=get_value_at_index(supir_first_stage_7, 1),
)
playsoundpysssss_38 = playsoundpysssss.nop(
mode="always",
volume=0.5,
file="notify.mp3",
any=get_value_at_index(supir_decode_9, 0),
)
colormatch_57 = colormatch.colormatch(
method="mkl",
image_ref=get_value_at_index(loadimage_13, 0),
image_target=get_value_at_index(supir_decode_9, 0),
)
saveimage_39 = saveimage.save_images(
filename_prefix="SUPIR_", images=get_value_at_index(colormatch_57, 0)
)
image_comparer_rgthree_59 = image_comparer_rgthree.compare_images(
image_a=get_value_at_index(colormatch_57, 0),
image_b=get_value_at_index(loadimage_13, 0),
)
if __name__ == "__main__":
main()