Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- """
- 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.")
- 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_extra_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_extra_nodes()
- from nodes import NODE_CLASS_MAPPINGS
- def main():
- import_custom_nodes()
- with torch.inference_mode():
- checkpointloadersimple = NODE_CLASS_MAPPINGS["CheckpointLoaderSimple"]()
- checkpointloadersimple_4 = checkpointloadersimple.load_checkpoint(
- ckpt_name="XL\nightvisionxl_V900.safetensors"
- )
- cliptextencode = NODE_CLASS_MAPPINGS["CLIPTextEncode"]()
- cliptextencode_6 = cliptextencode.encode(
- text="a woman, up close, neck",
- clip=get_value_at_index(checkpointloadersimple_4, 1),
- )
- cliptextencode_7 = cliptextencode.encode(
- text="lowres, low quality, cropped, worst quality, watermark",
- clip=get_value_at_index(checkpointloadersimple_4, 1),
- )
- loadimage = NODE_CLASS_MAPPINGS["LoadImage"]()
- loadimage_78 = loadimage.load_image(
- image="TINY-SOLITAIRE-PEARL-NECKLACE-FRONT-1_750x-removebg-preview.png"
- )
- imageresize = NODE_CLASS_MAPPINGS["ImageResize+"]()
- imageresize_14 = imageresize.execute(
- width=1024,
- height=1024,
- interpolation="nearest",
- method="stretch",
- condition="always",
- multiple_of=0,
- image=get_value_at_index(loadimage_78, 0),
- )
- emptylatentimage = NODE_CLASS_MAPPINGS["EmptyLatentImage"]()
- emptylatentimage_42 = emptylatentimage.generate(
- width=get_value_at_index(imageresize_14, 1),
- height=get_value_at_index(imageresize_14, 2),
- batch_size=1,
- )
- vaedecode = NODE_CLASS_MAPPINGS["VAEDecode"]()
- vaedecode_43 = vaedecode.decode(
- samples=get_value_at_index(emptylatentimage_42, 0),
- vae=get_value_at_index(checkpointloadersimple_4, 2),
- )
- easy_imagerembg = NODE_CLASS_MAPPINGS["easy imageRemBg"]()
- easy_imagerembg_12 = easy_imagerembg.remove(
- rem_mode="RMBG-1.4",
- image_output="Preview",
- save_prefix="ComfyUI",
- torchscript_jit=False,
- images=get_value_at_index(imageresize_14, 0),
- )
- splitimagewithalpha = NODE_CLASS_MAPPINGS["SplitImageWithAlpha"]()
- splitimagewithalpha_47 = splitimagewithalpha.split_image_with_alpha(
- image=get_value_at_index(easy_imagerembg_12, 0)
- )
- imagecompositemasked = NODE_CLASS_MAPPINGS["ImageCompositeMasked"]()
- imagecompositemasked_46 = imagecompositemasked.composite(
- x=0,
- y=0,
- resize_source=False,
- destination=get_value_at_index(vaedecode_43, 0),
- source=get_value_at_index(splitimagewithalpha_47, 0),
- mask=get_value_at_index(easy_imagerembg_12, 1),
- )
- vaeencodeargmax = NODE_CLASS_MAPPINGS["VAEEncodeArgMax"]()
- vaeencodeargmax_37 = vaeencodeargmax.encode(
- pixels=get_value_at_index(imagecompositemasked_46, 0),
- vae=get_value_at_index(checkpointloadersimple_4, 2),
- )
- easy_ipadapterapply = NODE_CLASS_MAPPINGS["easy ipadapterApply"]()
- ksampler = NODE_CLASS_MAPPINGS["KSampler"]()
- for q in range(1):
- easy_ipadapterapply_58 = easy_ipadapterapply.apply(
- preset="PLUS (high strength)",
- lora_strength=0.6,
- provider="CPU",
- weight=1,
- weight_faceidv2=1,
- start_at=0,
- end_at=1,
- cache_mode="all",
- use_tiled=False,
- model=get_value_at_index(checkpointloadersimple_4, 0),
- image=get_value_at_index(imagecompositemasked_46, 0),
- attn_mask=get_value_at_index(easy_imagerembg_12, 1),
- )
- ksampler_16 = ksampler.sample(
- seed=random.randint(1, 2**64),
- steps=25,
- cfg=4,
- sampler_name="dpmpp_2m_sde",
- scheduler="karras",
- denoise=0.9,
- model=get_value_at_index(easy_ipadapterapply_58, 0),
- positive=get_value_at_index(cliptextencode_6, 0),
- negative=get_value_at_index(cliptextencode_7, 0),
- latent_image=get_value_at_index(vaeencodeargmax_37, 0),
- )
- vaedecode_17 = vaedecode.decode(
- samples=get_value_at_index(ksampler_16, 0),
- vae=get_value_at_index(checkpointloadersimple_4, 2),
- )
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement