|
import os |
|
import random |
|
import sys |
|
from typing import Sequence, Mapping, Any, Union |
|
import torch |
|
import sys |
|
import cv2 |
|
import glob |
|
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient |
|
args = sys.argv[1:] |
|
connection_string = "DefaultEndpointsProtocol=https;AccountName=transcribedblobstorage;AccountKey=1Z7yKPP5DLbxnoHdh7NmHgwg3dFLaDiYHUELdid7dzfzR6/DvkZnnzpJ30lrXIMhtD5GYKo+71jP+AStC1TEvA==;EndpointSuffix=core.windows.net" |
|
container_name="saasdev" |
|
blob_service_client = BlobServiceClient.from_connection_string(connection_string) |
|
for arg in args: |
|
def download_blob(blob_name, download_file_path): |
|
container_client = blob_service_client.get_container_client(container_name) |
|
|
|
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name) |
|
|
|
|
|
os.makedirs(os.path.dirname(download_file_path), exist_ok=True) |
|
|
|
|
|
with open(download_file_path, "wb") as download_file: |
|
download_stream = blob_client.download_blob() |
|
download_file.write(download_stream.readall()) |
|
print(f"Blob '{blob_name}' downloaded to '{download_file_path}'.") |
|
download_blob(arg, download_file_path="/app/1.jpg") |
|
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 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 '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 |
|
|
|
|
|
loop = asyncio.new_event_loop() |
|
asyncio.set_event_loop(loop) |
|
|
|
|
|
server_instance = server.PromptServer(loop) |
|
execution.PromptQueue(server_instance) |
|
|
|
|
|
init_custom_nodes() |
|
|
|
|
|
from nodes import SaveImage, NODE_CLASS_MAPPINGS, LoadImage |
|
|
|
def img_return() : |
|
img_dir = "/output" |
|
filename_prefix="SUPIR_00001_.png" |
|
data_path = os. path. join(img_dir, filename_prefix) |
|
files = glob.glob(data_path) |
|
data = [] |
|
for f1 in files: |
|
img = cv2.imread(f1) |
|
data.append(img) |
|
return data |
|
|
|
def main(): |
|
import_custom_nodes() |
|
with torch.inference_mode(): |
|
loadimage = LoadImage() |
|
loadimage_13 = loadimage.load_image(image="/app/1.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), |
|
) |
|
img_return() |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|