Crockrocks12 commited on
Commit
fc584d4
·
verified ·
1 Parent(s): a939cd7

Upload supirworkflow_api.py

Browse files
Files changed (1) hide show
  1. supirworkflow_api.py +260 -0
supirworkflow_api.py ADDED
@@ -0,0 +1,260 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import sys
4
+ from typing import Sequence, Mapping, Any, Union
5
+ import torch
6
+ import sys
7
+ import cv2
8
+ import glob
9
+ from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
10
+ args = sys.argv[1:]
11
+ connection_string = "DefaultEndpointsProtocol=https;AccountName=transcribedblobstorage;AccountKey=1Z7yKPP5DLbxnoHdh7NmHgwg3dFLaDiYHUELdid7dzfzR6/DvkZnnzpJ30lrXIMhtD5GYKo+71jP+AStC1TEvA==;EndpointSuffix=core.windows.net"
12
+ container_name="saasdev"
13
+ blob_service_client = BlobServiceClient.from_connection_string(connection_string)
14
+ for arg in args:
15
+ def download_blob(blob_name, download_file_path):
16
+ container_client = blob_service_client.get_container_client(container_name)
17
+ # Create a blob client
18
+ blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
19
+
20
+ # Ensure the directory exists
21
+ os.makedirs(os.path.dirname(download_file_path), exist_ok=True)
22
+
23
+ # Download the blob to a local file
24
+ with open(download_file_path, "wb") as download_file:
25
+ download_stream = blob_client.download_blob()
26
+ download_file.write(download_stream.readall())
27
+ print(f"Blob '{blob_name}' downloaded to '{download_file_path}'.")
28
+ download_blob(arg, download_file_path="/app/1.jpg")
29
+ def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
30
+ """Returns the value at the given index of a sequence or mapping.
31
+
32
+ If the object is a sequence (like list or string), returns the value at the given index.
33
+ If the object is a mapping (like a dictionary), returns the value at the index-th key.
34
+
35
+ Some return a dictionary, in these cases, we look for the "results" key
36
+
37
+ Args:
38
+ obj (Union[Sequence, Mapping]): The object to retrieve the value from.
39
+ index (int): The index of the value to retrieve.
40
+
41
+ Returns:
42
+ Any: The value at the given index.
43
+
44
+ Raises:
45
+ IndexError: If the index is out of bounds for the object and the object is not a mapping.
46
+ """
47
+ try:
48
+ return obj[index]
49
+ except KeyError:
50
+ return obj["result"][index]
51
+
52
+
53
+ def find_path(name: str, path: str = None) -> str:
54
+ """
55
+ Recursively looks at parent folders starting from the given path until it finds the given name.
56
+ Returns the path as a Path object if found, or None otherwise.
57
+ """
58
+ # If no path is given, use the current working directory
59
+ if path is None:
60
+ path = os.getcwd()
61
+
62
+ # Check if the current directory contains the name
63
+ if name in os.listdir(path):
64
+ path_name = os.path.join(path, name)
65
+ print(f"{name} found: {path_name}")
66
+ return path_name
67
+
68
+ # Get the parent directory
69
+ parent_directory = os.path.dirname(path)
70
+
71
+ # If the parent directory is the same as the current directory, we've reached the root and stop the search
72
+ if parent_directory == path:
73
+ return None
74
+
75
+ # Recursively call the function with the parent directory
76
+ return find_path(name, parent_directory)
77
+
78
+
79
+ def add_comfyui_directory_to_sys_path() -> None:
80
+ """
81
+ Add 'ComfyUI' to the sys.path
82
+ """
83
+ comfyui_path = find_path("ComfyUI")
84
+ if comfyui_path is not None and os.path.isdir(comfyui_path):
85
+ sys.path.append(comfyui_path)
86
+ print(f"'{comfyui_path}' added to sys.path")
87
+
88
+
89
+ def add_extra_model_paths() -> None:
90
+ """
91
+ Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path.
92
+ """
93
+ from main import load_extra_path_config
94
+
95
+ extra_model_paths = find_path("extra_model_paths.yaml")
96
+
97
+ if extra_model_paths is not None:
98
+ load_extra_path_config(extra_model_paths)
99
+ else:
100
+ print("Could not find the extra_model_paths config file.")
101
+
102
+
103
+ add_comfyui_directory_to_sys_path()
104
+ add_extra_model_paths()
105
+
106
+
107
+ def import_custom_nodes() -> None:
108
+ """Find all custom nodes in the custom_nodes folder and add those node objects to NODE_CLASS_MAPPINGS
109
+
110
+ This function sets up a new asyncio event loop, initializes the PromptServer,
111
+ creates a PromptQueue, and initializes the custom nodes.
112
+ """
113
+ import asyncio
114
+ import execution
115
+ from nodes import init_custom_nodes
116
+ import server
117
+
118
+ # Creating a new event loop and setting it as the default loop
119
+ loop = asyncio.new_event_loop()
120
+ asyncio.set_event_loop(loop)
121
+
122
+ # Creating an instance of PromptServer with the loop
123
+ server_instance = server.PromptServer(loop)
124
+ execution.PromptQueue(server_instance)
125
+
126
+ # Initializing custom nodes
127
+ init_custom_nodes()
128
+
129
+
130
+ from nodes import SaveImage, NODE_CLASS_MAPPINGS, LoadImage
131
+
132
+ def img_return() :
133
+ img_dir = "/output"
134
+ filename_prefix="SUPIR_00001_.png"
135
+ data_path = os. path. join(img_dir, filename_prefix)
136
+ files = glob.glob(data_path)
137
+ data = []
138
+ for f1 in files:
139
+ img = cv2.imread(f1)
140
+ data.append(img)
141
+ return data
142
+
143
+ def main():
144
+ import_custom_nodes()
145
+ with torch.inference_mode():
146
+ loadimage = LoadImage()
147
+ loadimage_13 = loadimage.load_image(image="/app/1.jpg")
148
+
149
+ supir_model_loader = NODE_CLASS_MAPPINGS["SUPIR_model_loader"]()
150
+ supir_model_loader_58 = supir_model_loader.process(
151
+ supir_model="supir-voq.ckpt",
152
+ sdxl_model="juggernautxl.safetensors",
153
+ fp8_unet=False,
154
+ diffusion_dtype="auto",
155
+ )
156
+
157
+ imageresize = NODE_CLASS_MAPPINGS["ImageResize+"]()
158
+ imageresize_18 = imageresize.execute(
159
+ width=1280,
160
+ height=1280,
161
+ interpolation="lanczos",
162
+ keep_proportion=False,
163
+ condition="always",
164
+ multiple_of=0,
165
+ image=get_value_at_index(loadimage_13, 0),
166
+ )
167
+
168
+ supir_first_stage = NODE_CLASS_MAPPINGS["SUPIR_first_stage"]()
169
+ supir_first_stage_7 = supir_first_stage.process(
170
+ use_tiled_vae=True,
171
+ encoder_tile_size=512,
172
+ decoder_tile_size=512,
173
+ encoder_dtype="auto",
174
+ SUPIR_VAE=get_value_at_index(supir_model_loader_58, 1),
175
+ image=get_value_at_index(imageresize_18, 0),
176
+ )
177
+
178
+ supir_encode = NODE_CLASS_MAPPINGS["SUPIR_encode"]()
179
+ supir_encode_21 = supir_encode.encode(
180
+ use_tiled_vae=True,
181
+ encoder_tile_size=512,
182
+ encoder_dtype="auto",
183
+ SUPIR_VAE=get_value_at_index(supir_first_stage_7, 0),
184
+ image=get_value_at_index(supir_first_stage_7, 1),
185
+ )
186
+
187
+ supir_conditioner = NODE_CLASS_MAPPINGS["SUPIR_conditioner"]()
188
+ supir_sample = NODE_CLASS_MAPPINGS["SUPIR_sample"]()
189
+ supir_decode = NODE_CLASS_MAPPINGS["SUPIR_decode"]()
190
+ image_comparer_rgthree = NODE_CLASS_MAPPINGS["Image Comparer (rgthree)"]()
191
+ playsoundpysssss = NODE_CLASS_MAPPINGS["PlaySound|pysssss"]()
192
+ colormatch = NODE_CLASS_MAPPINGS["ColorMatch"]()
193
+ saveimage = SaveImage()
194
+
195
+ for q in range(10):
196
+ supir_conditioner_12 = supir_conditioner.condition(
197
+ positive_prompt="a red car, high quality, detailed",
198
+ negative_prompt="bad quality, blurry, messy",
199
+ SUPIR_model=get_value_at_index(supir_model_loader_58, 0),
200
+ latents=get_value_at_index(supir_first_stage_7, 2),
201
+ )
202
+
203
+ supir_sample_8 = supir_sample.sample(
204
+ seed=random.randint(1, 2**64),
205
+ steps=10,
206
+ cfg_scale_start=5,
207
+ cfg_scale_end=5,
208
+ EDM_s_churn=5,
209
+ s_noise=1.003,
210
+ DPMPP_eta=1,
211
+ control_scale_start=0.9,
212
+ control_scale_end=0.9500000000000001,
213
+ restore_cfg=10,
214
+ keep_model_loaded=False,
215
+ sampler="RestoreDPMPP2MSampler",
216
+ sampler_tile_size=1024,
217
+ sampler_tile_stride=512,
218
+ SUPIR_model=get_value_at_index(supir_model_loader_58, 0),
219
+ latents=get_value_at_index(supir_encode_21, 0),
220
+ positive=get_value_at_index(supir_conditioner_12, 0),
221
+ negative=get_value_at_index(supir_conditioner_12, 1),
222
+ )
223
+
224
+ supir_decode_9 = supir_decode.decode(
225
+ use_tiled_vae=True,
226
+ decoder_tile_size=512,
227
+ SUPIR_VAE=get_value_at_index(supir_model_loader_58, 1),
228
+ latents=get_value_at_index(supir_sample_8, 0),
229
+ )
230
+
231
+ image_comparer_rgthree_15 = image_comparer_rgthree.compare_images(
232
+ image_a=get_value_at_index(loadimage_13, 0),
233
+ image_b=get_value_at_index(supir_first_stage_7, 1),
234
+ )
235
+
236
+ playsoundpysssss_38 = playsoundpysssss.nop(
237
+ mode="always",
238
+ volume=0.5,
239
+ file="notify.mp3",
240
+ any=get_value_at_index(supir_decode_9, 0),
241
+ )
242
+
243
+ colormatch_57 = colormatch.colormatch(
244
+ method="mkl",
245
+ image_ref=get_value_at_index(loadimage_13, 0),
246
+ image_target=get_value_at_index(supir_decode_9, 0),
247
+ )
248
+
249
+ saveimage_39 = saveimage.save_images(
250
+ filename_prefix="SUPIR_", images=get_value_at_index(colormatch_57, 0)
251
+ )
252
+
253
+ image_comparer_rgthree_59 = image_comparer_rgthree.compare_images(
254
+ image_a=get_value_at_index(colormatch_57, 0),
255
+ image_b=get_value_at_index(loadimage_13, 0),
256
+ )
257
+ img_return()
258
+
259
+ if __name__ == "__main__":
260
+ main()