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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import sys
|
| 4 |
+
from typing import Sequence, Mapping, Any, Union, Tuple
|
| 5 |
+
import torch
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import spaces
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from huggingface_hub import hf_hub_download
|
| 10 |
+
from comfy import model_management
|
| 11 |
+
|
| 12 |
+
# Download required models from huggingface
|
| 13 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 14 |
+
hf_hub_download(
|
| 15 |
+
repo_id="Comfy-Org/stable-diffusion-v1-5-archive",
|
| 16 |
+
filename="v1-5-pruned-emaonly-fp16.safetensors",
|
| 17 |
+
local_dir="models/checkpoints",
|
| 18 |
+
token=hf_token,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
|
| 23 |
+
try:
|
| 24 |
+
return obj[index]
|
| 25 |
+
except KeyError:
|
| 26 |
+
return obj["result"][index]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def find_path(name: str, path: str = None) -> str:
|
| 30 |
+
"""
|
| 31 |
+
Recursively looks at parent folders starting from the given path until it finds the given name.
|
| 32 |
+
Returns the path as a Path object if found, or None otherwise.
|
| 33 |
+
"""
|
| 34 |
+
# If no path is given, use the current working directory
|
| 35 |
+
if path is None:
|
| 36 |
+
path = os.getcwd()
|
| 37 |
+
|
| 38 |
+
# Check if the current directory contains the name
|
| 39 |
+
if name in os.listdir(path):
|
| 40 |
+
path_name = os.path.join(path, name)
|
| 41 |
+
print(f"{name} found: {path_name}")
|
| 42 |
+
return path_name
|
| 43 |
+
|
| 44 |
+
# Get the parent directory
|
| 45 |
+
parent_directory = os.path.dirname(path)
|
| 46 |
+
|
| 47 |
+
# If the parent directory is the same as the current directory, we've reached the root and stop the search
|
| 48 |
+
if parent_directory == path:
|
| 49 |
+
return None
|
| 50 |
+
|
| 51 |
+
# Recursively call the function with the parent directory
|
| 52 |
+
return find_path(name, parent_directory)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def add_comfyui_directory_to_sys_path() -> None:
|
| 56 |
+
"""
|
| 57 |
+
Add 'ComfyUI' to the sys.path
|
| 58 |
+
"""
|
| 59 |
+
comfyui_path = find_path("ComfyUI")
|
| 60 |
+
if comfyui_path is not None and os.path.isdir(comfyui_path):
|
| 61 |
+
sys.path.append(comfyui_path)
|
| 62 |
+
print(f"'{comfyui_path}' added to sys.path")
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def add_extra_model_paths() -> None:
|
| 66 |
+
try:
|
| 67 |
+
from main import load_extra_path_config
|
| 68 |
+
except ImportError:
|
| 69 |
+
print(
|
| 70 |
+
"Could not import load_extra_path_config from main.py. Looking in utils.extra_config instead."
|
| 71 |
+
)
|
| 72 |
+
from utils.extra_config import load_extra_path_config
|
| 73 |
+
|
| 74 |
+
extra_model_paths = find_path("extra_model_paths.yaml")
|
| 75 |
+
|
| 76 |
+
if extra_model_paths is not None:
|
| 77 |
+
load_extra_path_config(extra_model_paths)
|
| 78 |
+
else:
|
| 79 |
+
print("Could not find the extra_model_paths config file.")
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
add_comfyui_directory_to_sys_path()
|
| 83 |
+
add_extra_model_paths()
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def import_custom_nodes() -> None:
|
| 87 |
+
import asyncio
|
| 88 |
+
import execution
|
| 89 |
+
from nodes import init_extra_nodes
|
| 90 |
+
import server
|
| 91 |
+
|
| 92 |
+
# Creating a new event loop and setting it as the default loop
|
| 93 |
+
loop = asyncio.new_event_loop()
|
| 94 |
+
asyncio.set_event_loop(loop)
|
| 95 |
+
|
| 96 |
+
# Creating an instance of PromptServer with the loop
|
| 97 |
+
server_instance = server.PromptServer(loop)
|
| 98 |
+
execution.PromptQueue(server_instance)
|
| 99 |
+
|
| 100 |
+
# Initializing custom nodes
|
| 101 |
+
init_extra_nodes()
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
from nodes import NODE_CLASS_MAPPINGS
|
| 105 |
+
|
| 106 |
+
import_custom_nodes()
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
checkpointloadersimple = NODE_CLASS_MAPPINGS["CheckpointLoaderSimple"]()
|
| 110 |
+
emptylatentimage = NODE_CLASS_MAPPINGS["EmptyLatentImage"]()
|
| 111 |
+
emptylatentimage_5 = emptylatentimage.generate(width=512, height=512, batch_size=1)
|
| 112 |
+
|
| 113 |
+
cliptextencode = NODE_CLASS_MAPPINGS["CLIPTextEncode"]()
|
| 114 |
+
ksampler = NODE_CLASS_MAPPINGS["KSampler"]()
|
| 115 |
+
vaedecode = NODE_CLASS_MAPPINGS["VAEDecode"]()
|
| 116 |
+
saveimage = NODE_CLASS_MAPPINGS["SaveImage"]()
|
| 117 |
+
framercomfysaveimagenode = NODE_CLASS_MAPPINGS["FramerComfySaveImageNode"]()
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
checkpointloadersimple_4 = checkpointloadersimple.load_checkpoint(
|
| 121 |
+
ckpt_name="v1-5-pruned-emaonly-fp16.safetensors"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
model_loaders = [checkpointloadersimple_4]
|
| 126 |
+
|
| 127 |
+
model_management.load_models_gpu(
|
| 128 |
+
[
|
| 129 |
+
loader[0].patcher if hasattr(loader[0], "patcher") else loader[0]
|
| 130 |
+
for loader in model_loaders
|
| 131 |
+
]
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
@spaces.GPU
|
| 136 |
+
def run_workflow(string_input) -> Tuple[Any, ...]:
|
| 137 |
+
with torch.inference_mode():
|
| 138 |
+
cliptextencode_6 = cliptextencode.encode(
|
| 139 |
+
text=string_input, clip=get_value_at_index(checkpointloadersimple_4, 1)
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
cliptextencode_7 = cliptextencode.encode(
|
| 143 |
+
text="text, watermark", clip=get_value_at_index(checkpointloadersimple_4, 1)
|
| 144 |
+
)
|
| 145 |
+
|
| 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 |
+
framercomfysaveimagenode_11 = framercomfysaveimagenode.save_images(
|
| 169 |
+
filename_prefix="ComfyUI",
|
| 170 |
+
output_name="output_input",
|
| 171 |
+
images=get_value_at_index(vaedecode_8, 0),
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
framercomfysaveimagenode_11_path = (
|
| 175 |
+
"output/" + framercomfysaveimagenode_11["ui"]["images"][0]["filename"]
|
| 176 |
+
)
|
| 177 |
+
return framercomfysaveimagenode_11_path
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
# Create Gradio interface
|
| 181 |
+
image11_output = gr.Image(label="Generated Image11")
|
| 182 |
+
|
| 183 |
+
with gr.Blocks() as app:
|
| 184 |
+
with gr.Row():
|
| 185 |
+
with gr.Column():
|
| 186 |
+
string_input_input = gr.Textbox(
|
| 187 |
+
label="String_Input",
|
| 188 |
+
value="None" if "None" else None,
|
| 189 |
+
placeholder=f"Enter string_input here...",
|
| 190 |
+
)
|
| 191 |
+
generate_btn = gr.Button("Generate")
|
| 192 |
+
with gr.Column():
|
| 193 |
+
image11_output.render()
|
| 194 |
+
generate_btn.click(
|
| 195 |
+
fn=run_workflow, inputs=[string_input_input], outputs=[image11_output]
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
if __name__ == "__main__":
|
| 199 |
+
app.launch(share=True)
|