Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -48,72 +48,109 @@ def add_extra_model_paths() -> None:
|
|
48 |
"""Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path."""
|
49 |
try:
|
50 |
from main import load_extra_path_config
|
|
|
|
|
|
|
|
|
|
|
51 |
except ImportError:
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
60 |
|
61 |
add_comfyui_directory_to_sys_path()
|
62 |
-
|
|
|
|
|
|
|
63 |
|
64 |
def import_custom_nodes() -> None:
|
65 |
"""Find all custom nodes in the custom_nodes folder and add those node objects to NODE_CLASS_MAPPINGS"""
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
from nodes import NODE_CLASS_MAPPINGS
|
79 |
|
80 |
# Pre-load models outside the decorated function for ZeroGPU efficiency
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
@spaces.GPU(duration=120) # Adjust duration based on your workflow speed
|
119 |
def enhance_image(image_input, upscale_factor, steps, cfg_scale, denoise_strength, guidance_scale):
|
|
|
48 |
"""Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path."""
|
49 |
try:
|
50 |
from main import load_extra_path_config
|
51 |
+
extra_model_paths = find_path("extra_model_paths.yaml")
|
52 |
+
if extra_model_paths is not None:
|
53 |
+
load_extra_path_config(extra_model_paths)
|
54 |
+
else:
|
55 |
+
print("Could not find the extra_model_paths config file.")
|
56 |
except ImportError:
|
57 |
+
try:
|
58 |
+
from utils.extra_config import load_extra_path_config
|
59 |
+
extra_model_paths = find_path("extra_model_paths.yaml")
|
60 |
+
if extra_model_paths is not None:
|
61 |
+
load_extra_path_config(extra_model_paths)
|
62 |
+
else:
|
63 |
+
print("Could not find the extra_model_paths config file.")
|
64 |
+
except ImportError:
|
65 |
+
print("Could not import extra config. Continuing without extra model paths.")
|
66 |
|
67 |
add_comfyui_directory_to_sys_path()
|
68 |
+
try:
|
69 |
+
add_extra_model_paths()
|
70 |
+
except Exception as e:
|
71 |
+
print(f"Warning: Could not load extra model paths: {e}")
|
72 |
|
73 |
def import_custom_nodes() -> None:
|
74 |
"""Find all custom nodes in the custom_nodes folder and add those node objects to NODE_CLASS_MAPPINGS"""
|
75 |
+
try:
|
76 |
+
import asyncio
|
77 |
+
import execution
|
78 |
+
from nodes import init_extra_nodes
|
79 |
+
import server
|
80 |
+
|
81 |
+
# Check if we're already in an event loop
|
82 |
+
try:
|
83 |
+
loop = asyncio.get_event_loop()
|
84 |
+
if loop.is_running():
|
85 |
+
# We're in an existing loop, use it
|
86 |
+
pass
|
87 |
+
else:
|
88 |
+
# Loop exists but not running, set a new one
|
89 |
+
loop = asyncio.new_event_loop()
|
90 |
+
asyncio.set_event_loop(loop)
|
91 |
+
except RuntimeError:
|
92 |
+
# No loop exists, create one
|
93 |
+
loop = asyncio.new_event_loop()
|
94 |
+
asyncio.set_event_loop(loop)
|
95 |
+
|
96 |
+
server_instance = server.PromptServer(loop)
|
97 |
+
execution.PromptQueue(server_instance)
|
98 |
+
init_extra_nodes()
|
99 |
+
except Exception as e:
|
100 |
+
print(f"Warning: Could not initialize custom nodes: {e}")
|
101 |
+
print("Continuing with basic ComfyUI nodes only...")
|
102 |
|
103 |
from nodes import NODE_CLASS_MAPPINGS
|
104 |
|
105 |
# Pre-load models outside the decorated function for ZeroGPU efficiency
|
106 |
+
try:
|
107 |
+
import_custom_nodes()
|
108 |
+
|
109 |
+
# Initialize model loaders
|
110 |
+
dualcliploader = NODE_CLASS_MAPPINGS["DualCLIPLoader"]()
|
111 |
+
dualcliploader_54 = dualcliploader.load_clip(
|
112 |
+
clip_name1="clip_l.safetensors",
|
113 |
+
clip_name2="t5xxl_fp16.safetensors",
|
114 |
+
type="flux",
|
115 |
+
device="default",
|
116 |
+
)
|
117 |
+
|
118 |
+
upscalemodelloader = NODE_CLASS_MAPPINGS["UpscaleModelLoader"]()
|
119 |
+
upscalemodelloader_44 = upscalemodelloader.load_model(model_name="4x-UltraSharp.pth")
|
120 |
+
|
121 |
+
vaeloader = NODE_CLASS_MAPPINGS["VAELoader"]()
|
122 |
+
vaeloader_55 = vaeloader.load_vae(vae_name="ae.safetensors")
|
123 |
+
|
124 |
+
unetloader = NODE_CLASS_MAPPINGS["UNETLoader"]()
|
125 |
+
unetloader_58 = unetloader.load_unet(
|
126 |
+
unet_name="flux1-dev.safetensors", weight_dtype="default"
|
127 |
+
)
|
128 |
+
|
129 |
+
downloadandloadflorence2model = NODE_CLASS_MAPPINGS["DownloadAndLoadFlorence2Model"]()
|
130 |
+
downloadandloadflorence2model_52 = downloadandloadflorence2model.loadmodel(
|
131 |
+
model="microsoft/Florence-2-large", precision="fp16", attention="sdpa"
|
132 |
+
)
|
133 |
+
|
134 |
+
# Pre-load models to GPU for efficiency
|
135 |
+
try:
|
136 |
+
from comfy import model_management
|
137 |
+
model_loaders = [dualcliploader_54, vaeloader_55, unetloader_58, downloadandloadflorence2model_52]
|
138 |
+
valid_models = [
|
139 |
+
getattr(loader[0], 'patcher', loader[0])
|
140 |
+
for loader in model_loaders
|
141 |
+
if not isinstance(loader[0], dict) and not isinstance(getattr(loader[0], 'patcher', None), dict)
|
142 |
+
]
|
143 |
+
model_management.load_models_gpu(valid_models)
|
144 |
+
print("Models successfully pre-loaded to GPU")
|
145 |
+
except Exception as e:
|
146 |
+
print(f"Warning: Could not pre-load models to GPU: {e}")
|
147 |
+
|
148 |
+
print("ComfyUI setup completed successfully!")
|
149 |
+
|
150 |
+
except Exception as e:
|
151 |
+
print(f"Error during ComfyUI setup: {e}")
|
152 |
+
print("Please check that all required custom nodes are installed.")
|
153 |
+
raise
|
154 |
|
155 |
@spaces.GPU(duration=120) # Adjust duration based on your workflow speed
|
156 |
def enhance_image(image_input, upscale_factor, steps, cfg_scale, denoise_strength, guidance_scale):
|