Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -10,9 +10,6 @@ import gradio as gr
|
|
10 |
import spaces # for ZeroGPU usage
|
11 |
from typing import Sequence, Mapping, Any, Union
|
12 |
|
13 |
-
# ----------------------------------------------------------------
|
14 |
-
# NEW IMPORTS + INSTALLATION LOGIC FOR CatVTON & DETECTRON2
|
15 |
-
# ----------------------------------------------------------------
|
16 |
import subprocess
|
17 |
|
18 |
def install_catvton_dependencies():
|
@@ -21,19 +18,24 @@ def install_catvton_dependencies():
|
|
21 |
|
22 |
# ----------------------------------------------------------------
|
23 |
# 0) Clone the CatVTON repository if not already present
|
|
|
24 |
# ----------------------------------------------------------------
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
#
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
else:
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# ----------------------------------------------------------------
|
39 |
# 1) Install detectron2 from GitHub
|
@@ -47,10 +49,12 @@ def install_catvton_dependencies():
|
|
47 |
# ----------------------------------------------------------------
|
48 |
# 2) Strip detectron2 lines from CatVTON requirements
|
49 |
# ----------------------------------------------------------------
|
50 |
-
|
|
|
51 |
catvton_req_path = os.path.join(catvton_repo_path, "requirements.txt")
|
52 |
catvton_req_modified = os.path.join(catvton_repo_path, "requirements_modified.txt")
|
53 |
|
|
|
54 |
with open(catvton_req_path, "r") as fin, open(catvton_req_modified, "w") as fout:
|
55 |
for line in fin:
|
56 |
if "detectron2" not in line.lower():
|
@@ -76,22 +80,16 @@ def install_catvton_dependencies():
|
|
76 |
print("[CatVTON Setup] All CatVTON dependencies installed!\n")
|
77 |
|
78 |
|
79 |
-
#
|
80 |
-
# Call the above function once on startup.
|
81 |
-
# Hugging Face Spaces will run this when building the container.
|
82 |
-
# ----------------------------------------------------------------
|
83 |
install_catvton_dependencies()
|
84 |
|
85 |
-
# 1) Load your token from environment (make sure you set HF_TOKEN in Space settings)
|
86 |
token = os.environ["HF_TOKEN"]
|
87 |
-
|
88 |
-
# 2) We'll use huggingface_hub to download each gated model
|
89 |
from huggingface_hub import hf_hub_download
|
90 |
|
91 |
import shutil
|
92 |
import pathlib
|
93 |
|
94 |
-
# Create the directories we need
|
95 |
pathlib.Path("ComfyUI/models/vae").mkdir(parents=True, exist_ok=True)
|
96 |
pathlib.Path("ComfyUI/models/clip").mkdir(parents=True, exist_ok=True)
|
97 |
pathlib.Path("ComfyUI/models/clip_vision").mkdir(parents=True, exist_ok=True)
|
@@ -99,28 +97,25 @@ pathlib.Path("ComfyUI/models/unet").mkdir(parents=True, exist_ok=True)
|
|
99 |
pathlib.Path("ComfyUI/models/loras").mkdir(parents=True, exist_ok=True)
|
100 |
pathlib.Path("ComfyUI/models/style_models").mkdir(parents=True, exist_ok=True)
|
101 |
|
102 |
-
# Download each gated model
|
103 |
hf_hub_download(
|
104 |
repo_id="black-forest-labs/FLUX.1-dev",
|
105 |
filename="ae.safetensors",
|
106 |
local_dir="ComfyUI/models/vae",
|
107 |
use_auth_token=token
|
108 |
)
|
109 |
-
|
110 |
hf_hub_download(
|
111 |
repo_id="comfyanonymous/flux_text_encoders",
|
112 |
filename="t5xxl_fp16.safetensors",
|
113 |
local_dir="ComfyUI/models/clip",
|
114 |
use_auth_token=token
|
115 |
)
|
116 |
-
|
117 |
hf_hub_download(
|
118 |
repo_id="comfyanonymous/flux_text_encoders",
|
119 |
filename="clip_l.safetensors",
|
120 |
local_dir="ComfyUI/models/clip",
|
121 |
use_auth_token=token
|
122 |
)
|
123 |
-
|
124 |
hf_hub_download(
|
125 |
repo_id="black-forest-labs/FLUX.1-Fill-dev",
|
126 |
filename="flux1-fill-dev.safetensors",
|
@@ -134,7 +129,6 @@ download_path = hf_hub_download(
|
|
134 |
local_dir="ComfyUI/models/loras",
|
135 |
use_auth_token=token
|
136 |
)
|
137 |
-
# Rename after download
|
138 |
os.rename(download_path, os.path.join("ComfyUI/models/loras", "catvton-flux-lora.safetensors"))
|
139 |
|
140 |
download_path = hf_hub_download(
|
@@ -143,7 +137,6 @@ download_path = hf_hub_download(
|
|
143 |
local_dir="ComfyUI/models/loras",
|
144 |
use_auth_token=token
|
145 |
)
|
146 |
-
# Rename after download
|
147 |
os.rename(download_path, os.path.join("ComfyUI/models/loras", "alimama-flux-turbo-alpha.safetensors"))
|
148 |
|
149 |
hf_hub_download(
|
@@ -152,7 +145,6 @@ hf_hub_download(
|
|
152 |
local_dir="ComfyUI/models/clip_vision",
|
153 |
use_auth_token=token
|
154 |
)
|
155 |
-
|
156 |
hf_hub_download(
|
157 |
repo_id="black-forest-labs/FLUX.1-Redux-dev",
|
158 |
filename="flux1-redux-dev.safetensors",
|
@@ -163,9 +155,7 @@ hf_hub_download(
|
|
163 |
#############################
|
164 |
# ComfyUI support functions
|
165 |
#############################
|
166 |
-
|
167 |
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
|
168 |
-
"""Returns the value at the given index of a sequence or mapping."""
|
169 |
try:
|
170 |
return obj[index]
|
171 |
except KeyError:
|
@@ -174,7 +164,6 @@ def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
|
|
174 |
def find_path(name: str, path: str = None) -> str:
|
175 |
if path is None:
|
176 |
path = os.getcwd()
|
177 |
-
|
178 |
if name in os.listdir(path):
|
179 |
path_name = os.path.join(path, name)
|
180 |
print(f"{name} found: {path_name}")
|
@@ -183,7 +172,6 @@ def find_path(name: str, path: str = None) -> str:
|
|
183 |
parent_directory = os.path.dirname(path)
|
184 |
if parent_directory == path:
|
185 |
return None
|
186 |
-
|
187 |
return find_path(name, parent_directory)
|
188 |
|
189 |
def add_comfyui_directory_to_sys_path() -> None:
|
@@ -220,21 +208,13 @@ def import_custom_nodes() -> None:
|
|
220 |
execution.PromptQueue(server_instance)
|
221 |
init_extra_nodes()
|
222 |
|
223 |
-
##########################
|
224 |
-
# Import node mappings
|
225 |
-
##########################
|
226 |
from nodes import NODE_CLASS_MAPPINGS
|
227 |
|
228 |
#############################################
|
229 |
# MAIN PIPELINE with ZeroGPU Decorator
|
230 |
#############################################
|
231 |
-
@spaces.GPU(duration=90)
|
232 |
def generate_images(user_image_path):
|
233 |
-
"""
|
234 |
-
This function runs your node-based pipeline,
|
235 |
-
using user_image_path for loadimage_264 and
|
236 |
-
returning the final saveimage_295 path.
|
237 |
-
"""
|
238 |
import_custom_nodes()
|
239 |
with torch.inference_mode():
|
240 |
loadimage = NODE_CLASS_MAPPINGS["LoadImage"]()
|
@@ -251,7 +231,6 @@ def generate_images(user_image_path):
|
|
251 |
)
|
252 |
|
253 |
loadimage_264 = loadimage.load_image(image=user_image_path)
|
254 |
-
|
255 |
randomnoise = NODE_CLASS_MAPPINGS["RandomNoise"]()
|
256 |
randomnoise_273 = randomnoise.get_noise(noise_seed=random.randint(1, 2**64))
|
257 |
|
@@ -320,7 +299,6 @@ def generate_images(user_image_path):
|
|
320 |
)
|
321 |
|
322 |
loadimage_289 = loadimage.load_image(image="assets_broc_ref.jpg")
|
323 |
-
|
324 |
clipvisionencode = NODE_CLASS_MAPPINGS["CLIPVisionEncode"]()
|
325 |
clipvisionencode_282 = clipvisionencode.encode(
|
326 |
crop="center",
|
@@ -347,7 +325,8 @@ def generate_images(user_image_path):
|
|
347 |
|
348 |
fluxguidance = NODE_CLASS_MAPPINGS["FluxGuidance"]()
|
349 |
fluxguidance_288 = fluxguidance.append(
|
350 |
-
guidance=30,
|
|
|
351 |
)
|
352 |
|
353 |
conditioningzeroout = NODE_CLASS_MAPPINGS["ConditioningZeroOut"]()
|
@@ -423,7 +402,8 @@ def generate_images(user_image_path):
|
|
423 |
|
424 |
unetloader = NODE_CLASS_MAPPINGS["UNETLoader"]()
|
425 |
unetloader_291 = unetloader.load_unet(
|
426 |
-
unet_name="flux1-fill-dev.safetensors",
|
|
|
427 |
)
|
428 |
|
429 |
loraloadermodelonly = NODE_CLASS_MAPPINGS["LoraLoaderModelOnly"]()
|
@@ -462,10 +442,8 @@ def generate_images(user_image_path):
|
|
462 |
images=get_value_at_index(vaedecode_294, 0),
|
463 |
)
|
464 |
|
465 |
-
#
|
466 |
-
|
467 |
-
return final_output_path
|
468 |
-
|
469 |
|
470 |
###################################
|
471 |
# A simple Gradio interface
|
|
|
10 |
import spaces # for ZeroGPU usage
|
11 |
from typing import Sequence, Mapping, Any, Union
|
12 |
|
|
|
|
|
|
|
13 |
import subprocess
|
14 |
|
15 |
def install_catvton_dependencies():
|
|
|
18 |
|
19 |
# ----------------------------------------------------------------
|
20 |
# 0) Clone the CatVTON repository if not already present
|
21 |
+
# (Currently commented out to avoid syntax error)
|
22 |
# ----------------------------------------------------------------
|
23 |
+
# catvton_repo_path = "ComfyUI/custom_nodes/Comfyui-CatVTON"
|
24 |
+
# if not os.path.exists(os.path.join(catvton_repo_path, "requirements.txt")):
|
25 |
+
# print("[CatVTON Setup] Cloning CatVTON repo...")
|
26 |
+
# pathlib.Path("ComfyUI/custom_nodes").mkdir(parents=True, exist_ok=True)
|
27 |
+
# subprocess.check_call([
|
28 |
+
# "git", "clone",
|
29 |
+
# "--depth", "1",
|
30 |
+
# "https://github.com/zhengchong/CatVTON.git",
|
31 |
+
# catvton_repo_path
|
32 |
+
# ])
|
33 |
+
# else:
|
34 |
+
# print("[CatVTON Setup] CatVTON repo already exists. Skipping clone.")
|
35 |
+
|
36 |
+
# ----------------------------------------------------------------
|
37 |
+
# (Assuming you already have CatVTON code locally, or you are manually
|
38 |
+
# including it in your repo. If not, you'll want to uncomment the above block.)
|
39 |
|
40 |
# ----------------------------------------------------------------
|
41 |
# 1) Install detectron2 from GitHub
|
|
|
49 |
# ----------------------------------------------------------------
|
50 |
# 2) Strip detectron2 lines from CatVTON requirements
|
51 |
# ----------------------------------------------------------------
|
52 |
+
# Make sure catvton_repo_path is defined (if you commented out the clone).
|
53 |
+
catvton_repo_path = "ComfyUI/custom_nodes/Comfyui-CatVTON"
|
54 |
catvton_req_path = os.path.join(catvton_repo_path, "requirements.txt")
|
55 |
catvton_req_modified = os.path.join(catvton_repo_path, "requirements_modified.txt")
|
56 |
|
57 |
+
print("[CatVTON Setup] Removing detectron2 lines from CatVTON’s requirements...")
|
58 |
with open(catvton_req_path, "r") as fin, open(catvton_req_modified, "w") as fout:
|
59 |
for line in fin:
|
60 |
if "detectron2" not in line.lower():
|
|
|
80 |
print("[CatVTON Setup] All CatVTON dependencies installed!\n")
|
81 |
|
82 |
|
83 |
+
# Call the install function on startup
|
|
|
|
|
|
|
84 |
install_catvton_dependencies()
|
85 |
|
|
|
86 |
token = os.environ["HF_TOKEN"]
|
|
|
|
|
87 |
from huggingface_hub import hf_hub_download
|
88 |
|
89 |
import shutil
|
90 |
import pathlib
|
91 |
|
92 |
+
# Create the directories we need
|
93 |
pathlib.Path("ComfyUI/models/vae").mkdir(parents=True, exist_ok=True)
|
94 |
pathlib.Path("ComfyUI/models/clip").mkdir(parents=True, exist_ok=True)
|
95 |
pathlib.Path("ComfyUI/models/clip_vision").mkdir(parents=True, exist_ok=True)
|
|
|
97 |
pathlib.Path("ComfyUI/models/loras").mkdir(parents=True, exist_ok=True)
|
98 |
pathlib.Path("ComfyUI/models/style_models").mkdir(parents=True, exist_ok=True)
|
99 |
|
100 |
+
# Download each gated model
|
101 |
hf_hub_download(
|
102 |
repo_id="black-forest-labs/FLUX.1-dev",
|
103 |
filename="ae.safetensors",
|
104 |
local_dir="ComfyUI/models/vae",
|
105 |
use_auth_token=token
|
106 |
)
|
|
|
107 |
hf_hub_download(
|
108 |
repo_id="comfyanonymous/flux_text_encoders",
|
109 |
filename="t5xxl_fp16.safetensors",
|
110 |
local_dir="ComfyUI/models/clip",
|
111 |
use_auth_token=token
|
112 |
)
|
|
|
113 |
hf_hub_download(
|
114 |
repo_id="comfyanonymous/flux_text_encoders",
|
115 |
filename="clip_l.safetensors",
|
116 |
local_dir="ComfyUI/models/clip",
|
117 |
use_auth_token=token
|
118 |
)
|
|
|
119 |
hf_hub_download(
|
120 |
repo_id="black-forest-labs/FLUX.1-Fill-dev",
|
121 |
filename="flux1-fill-dev.safetensors",
|
|
|
129 |
local_dir="ComfyUI/models/loras",
|
130 |
use_auth_token=token
|
131 |
)
|
|
|
132 |
os.rename(download_path, os.path.join("ComfyUI/models/loras", "catvton-flux-lora.safetensors"))
|
133 |
|
134 |
download_path = hf_hub_download(
|
|
|
137 |
local_dir="ComfyUI/models/loras",
|
138 |
use_auth_token=token
|
139 |
)
|
|
|
140 |
os.rename(download_path, os.path.join("ComfyUI/models/loras", "alimama-flux-turbo-alpha.safetensors"))
|
141 |
|
142 |
hf_hub_download(
|
|
|
145 |
local_dir="ComfyUI/models/clip_vision",
|
146 |
use_auth_token=token
|
147 |
)
|
|
|
148 |
hf_hub_download(
|
149 |
repo_id="black-forest-labs/FLUX.1-Redux-dev",
|
150 |
filename="flux1-redux-dev.safetensors",
|
|
|
155 |
#############################
|
156 |
# ComfyUI support functions
|
157 |
#############################
|
|
|
158 |
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
|
|
|
159 |
try:
|
160 |
return obj[index]
|
161 |
except KeyError:
|
|
|
164 |
def find_path(name: str, path: str = None) -> str:
|
165 |
if path is None:
|
166 |
path = os.getcwd()
|
|
|
167 |
if name in os.listdir(path):
|
168 |
path_name = os.path.join(path, name)
|
169 |
print(f"{name} found: {path_name}")
|
|
|
172 |
parent_directory = os.path.dirname(path)
|
173 |
if parent_directory == path:
|
174 |
return None
|
|
|
175 |
return find_path(name, parent_directory)
|
176 |
|
177 |
def add_comfyui_directory_to_sys_path() -> None:
|
|
|
208 |
execution.PromptQueue(server_instance)
|
209 |
init_extra_nodes()
|
210 |
|
|
|
|
|
|
|
211 |
from nodes import NODE_CLASS_MAPPINGS
|
212 |
|
213 |
#############################################
|
214 |
# MAIN PIPELINE with ZeroGPU Decorator
|
215 |
#############################################
|
216 |
+
@spaces.GPU(duration=90)
|
217 |
def generate_images(user_image_path):
|
|
|
|
|
|
|
|
|
|
|
218 |
import_custom_nodes()
|
219 |
with torch.inference_mode():
|
220 |
loadimage = NODE_CLASS_MAPPINGS["LoadImage"]()
|
|
|
231 |
)
|
232 |
|
233 |
loadimage_264 = loadimage.load_image(image=user_image_path)
|
|
|
234 |
randomnoise = NODE_CLASS_MAPPINGS["RandomNoise"]()
|
235 |
randomnoise_273 = randomnoise.get_noise(noise_seed=random.randint(1, 2**64))
|
236 |
|
|
|
299 |
)
|
300 |
|
301 |
loadimage_289 = loadimage.load_image(image="assets_broc_ref.jpg")
|
|
|
302 |
clipvisionencode = NODE_CLASS_MAPPINGS["CLIPVisionEncode"]()
|
303 |
clipvisionencode_282 = clipvisionencode.encode(
|
304 |
crop="center",
|
|
|
325 |
|
326 |
fluxguidance = NODE_CLASS_MAPPINGS["FluxGuidance"]()
|
327 |
fluxguidance_288 = fluxguidance.append(
|
328 |
+
guidance=30,
|
329 |
+
conditioning=get_value_at_index(stylemodelapply_280, 0)
|
330 |
)
|
331 |
|
332 |
conditioningzeroout = NODE_CLASS_MAPPINGS["ConditioningZeroOut"]()
|
|
|
402 |
|
403 |
unetloader = NODE_CLASS_MAPPINGS["UNETLoader"]()
|
404 |
unetloader_291 = unetloader.load_unet(
|
405 |
+
unet_name="flux1-fill-dev.safetensors",
|
406 |
+
weight_dtype="default"
|
407 |
)
|
408 |
|
409 |
loraloadermodelonly = NODE_CLASS_MAPPINGS["LoraLoaderModelOnly"]()
|
|
|
442 |
images=get_value_at_index(vaedecode_294, 0),
|
443 |
)
|
444 |
|
445 |
+
# final output
|
446 |
+
return f"output/{saveimage_295['ui']['images'][0]['filename']}"
|
|
|
|
|
447 |
|
448 |
###################################
|
449 |
# A simple Gradio interface
|