Manishh-07 commited on
Commit
45851fc
·
verified ·
1 Parent(s): 9cae6ea

Upload hr.py

Browse files
Files changed (1) hide show
  1. ComfyUI/hr.py +170 -0
ComfyUI/hr.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 main():
119
+ import_custom_nodes()
120
+ with torch.inference_mode():
121
+ checkpointloadersimple = NODE_CLASS_MAPPINGS["CheckpointLoaderSimple"]()
122
+ checkpointloadersimple_4 = checkpointloadersimple.load_checkpoint(
123
+ ckpt_name="DreamShaper_8_pruned.safetensors"
124
+ )
125
+
126
+ emptylatentimage = NODE_CLASS_MAPPINGS["EmptyLatentImage"]()
127
+ emptylatentimage_5 = emptylatentimage.generate(
128
+ width=512, height=512, batch_size=1
129
+ )
130
+
131
+ cliptextencode = NODE_CLASS_MAPPINGS["CLIPTextEncode"]()
132
+ cliptextencode_6 = cliptextencode.encode(
133
+ text="Indian Cricket Team is playing against Australia,",
134
+ clip=get_value_at_index(checkpointloadersimple_4, 1),
135
+ )
136
+
137
+ cliptextencode_7 = cliptextencode.encode(
138
+ text="text, watermark", clip=get_value_at_index(checkpointloadersimple_4, 1)
139
+ )
140
+
141
+ ksampler = NODE_CLASS_MAPPINGS["KSampler"]()
142
+ vaedecode = NODE_CLASS_MAPPINGS["VAEDecode"]()
143
+ saveimage = NODE_CLASS_MAPPINGS["SaveImage"]()
144
+
145
+ for q in range(1):
146
+ ksampler_3 = ksampler.sample(
147
+ seed=random.randint(1, 2**64),
148
+ steps=20,
149
+ cfg=8,
150
+ sampler_name="euler",
151
+ scheduler="normal",
152
+ denoise=1,
153
+ model=get_value_at_index(checkpointloadersimple_4, 0),
154
+ positive=get_value_at_index(cliptextencode_6, 0),
155
+ negative=get_value_at_index(cliptextencode_7, 0),
156
+ latent_image=get_value_at_index(emptylatentimage_5, 0),
157
+ )
158
+
159
+ vaedecode_8 = vaedecode.decode(
160
+ samples=get_value_at_index(ksampler_3, 0),
161
+ vae=get_value_at_index(checkpointloadersimple_4, 2),
162
+ )
163
+
164
+ saveimage_9 = saveimage.save_images(
165
+ filename_prefix="ComfyUI", images=get_value_at_index(vaedecode_8, 0)
166
+ )
167
+
168
+
169
+ if __name__ == "__main__":
170
+ main()