comrender commited on
Commit
33c9103
·
verified ·
1 Parent(s): 6aedd69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -56
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
- print("Could not import load_extra_path_config from main.py. Looking in utils.extra_config instead.")
53
- from utils.extra_config import load_extra_path_config
54
-
55
- extra_model_paths = find_path("extra_model_paths.yaml")
56
- if extra_model_paths is not None:
57
- load_extra_path_config(extra_model_paths)
58
- else:
59
- print("Could not find the extra_model_paths config file.")
 
60
 
61
  add_comfyui_directory_to_sys_path()
62
- add_extra_model_paths()
 
 
 
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
- import asyncio
67
- import execution
68
- from nodes import init_extra_nodes
69
- import server
70
-
71
- loop = asyncio.new_event_loop()
72
- asyncio.set_event_loop(loop)
73
-
74
- server_instance = server.PromptServer(loop)
75
- execution.PromptQueue(server_instance)
76
- init_extra_nodes()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  from nodes import NODE_CLASS_MAPPINGS
79
 
80
  # Pre-load models outside the decorated function for ZeroGPU efficiency
81
- import_custom_nodes()
82
-
83
- # Initialize model loaders
84
- dualcliploader = NODE_CLASS_MAPPINGS["DualCLIPLoader"]()
85
- dualcliploader_54 = dualcliploader.load_clip(
86
- clip_name1="clip_l.safetensors",
87
- clip_name2="t5xxl_fp16.safetensors",
88
- type="flux",
89
- device="default",
90
- )
91
-
92
- upscalemodelloader = NODE_CLASS_MAPPINGS["UpscaleModelLoader"]()
93
- upscalemodelloader_44 = upscalemodelloader.load_model(model_name="4x-UltraSharp.pth")
94
-
95
- vaeloader = NODE_CLASS_MAPPINGS["VAELoader"]()
96
- vaeloader_55 = vaeloader.load_vae(vae_name="ae.safetensors")
97
-
98
- unetloader = NODE_CLASS_MAPPINGS["UNETLoader"]()
99
- unetloader_58 = unetloader.load_unet(
100
- unet_name="flux1-dev.safetensors", weight_dtype="default"
101
- )
102
-
103
- downloadandloadflorence2model = NODE_CLASS_MAPPINGS["DownloadAndLoadFlorence2Model"]()
104
- downloadandloadflorence2model_52 = downloadandloadflorence2model.loadmodel(
105
- model="microsoft/Florence-2-large", precision="fp16", attention="sdpa"
106
- )
107
-
108
- # Pre-load models to GPU for efficiency
109
- from comfy import model_management
110
- model_loaders = [dualcliploader_54, vaeloader_55, unetloader_58, downloadandloadflorence2model_52]
111
- valid_models = [
112
- getattr(loader[0], 'patcher', loader[0])
113
- for loader in model_loaders
114
- if not isinstance(loader[0], dict) and not isinstance(getattr(loader[0], 'patcher', None), dict)
115
- ]
116
- model_management.load_models_gpu(valid_models)
 
 
 
 
 
 
 
 
 
 
 
 
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):