Spaces:
Running
on
Zero
Running
on
Zero
add examples and change title
#1
by
ultimaxxl
- opened
- .gitattributes +3 -1
- app.py +198 -109
- examples_db.py +22 -0
- imgs/output1.png +3 -0
- imgs/structure1.png +3 -0
- imgs/style1.png +3 -0
.gitattributes
CHANGED
@@ -44,4 +44,6 @@ custom_nodes/ComfyUI-to-Python-Extension/images/SDXL-UI-Example.PNG filter=lfs d
|
|
44 |
custom_nodes/rgthree-comfy/docs/rgthree_advanced_metadata.png filter=lfs diff=lfs merge=lfs -text
|
45 |
custom_nodes/rgthree-comfy/docs/rgthree_advanced.png filter=lfs diff=lfs merge=lfs -text
|
46 |
custom_nodes/rgthree-comfy/docs/rgthree_context.png filter=lfs diff=lfs merge=lfs -text
|
47 |
-
|
|
|
|
|
|
44 |
custom_nodes/rgthree-comfy/docs/rgthree_advanced_metadata.png filter=lfs diff=lfs merge=lfs -text
|
45 |
custom_nodes/rgthree-comfy/docs/rgthree_advanced.png filter=lfs diff=lfs merge=lfs -text
|
46 |
custom_nodes/rgthree-comfy/docs/rgthree_context.png filter=lfs diff=lfs merge=lfs -text
|
47 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
48 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
49 |
+
*.webp filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
@@ -2,15 +2,17 @@ import os
|
|
2 |
import random
|
3 |
import sys
|
4 |
import subprocess
|
5 |
-
|
6 |
import torch
|
7 |
import gradio as gr
|
|
|
|
|
|
|
8 |
from PIL import Image, ImageChops
|
9 |
from huggingface_hub import hf_hub_download
|
10 |
-
import spaces
|
11 |
|
12 |
# Setup ComfyUI if not already set up
|
13 |
-
#if not os.path.exists("ComfyUI"):
|
14 |
# print("Setting up ComfyUI...")
|
15 |
# subprocess.run(["bash", "setup_comfyui.sh"], check=True)
|
16 |
|
@@ -19,25 +21,63 @@ os.makedirs("output", exist_ok=True)
|
|
19 |
|
20 |
# Download models if not already present
|
21 |
print("Checking and downloading models...")
|
22 |
-
hf_hub_download(
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
hf_hub_download(
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Import required functions and setup ComfyUI path
|
33 |
import folder_paths
|
34 |
|
|
|
35 |
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
|
36 |
try:
|
37 |
return obj[index]
|
38 |
except KeyError:
|
39 |
return obj["result"][index]
|
40 |
|
|
|
41 |
def find_path(name: str, path: str = None) -> str:
|
42 |
if path is None:
|
43 |
path = os.getcwd()
|
@@ -50,12 +90,14 @@ def find_path(name: str, path: str = None) -> str:
|
|
50 |
return None
|
51 |
return find_path(name, parent_directory)
|
52 |
|
|
|
53 |
def add_comfyui_directory_to_sys_path() -> None:
|
54 |
comfyui_path = find_path("ComfyUI")
|
55 |
if comfyui_path is not None and os.path.isdir(comfyui_path):
|
56 |
sys.path.append(comfyui_path)
|
57 |
print(f"'{comfyui_path}' added to sys.path")
|
58 |
|
|
|
59 |
def add_extra_model_paths() -> None:
|
60 |
try:
|
61 |
from main import load_extra_path_config
|
@@ -67,27 +109,30 @@ def add_extra_model_paths() -> None:
|
|
67 |
else:
|
68 |
print("Could not find the extra_model_paths config file.")
|
69 |
|
|
|
70 |
# Initialize paths
|
71 |
add_comfyui_directory_to_sys_path()
|
72 |
add_extra_model_paths()
|
73 |
|
|
|
74 |
def import_custom_nodes() -> None:
|
75 |
import asyncio
|
76 |
import execution
|
77 |
from nodes import init_extra_nodes
|
78 |
import server
|
79 |
-
|
80 |
# Create a new event loop if running in a new thread
|
81 |
try:
|
82 |
loop = asyncio.get_event_loop()
|
83 |
except RuntimeError:
|
84 |
loop = asyncio.new_event_loop()
|
85 |
asyncio.set_event_loop(loop)
|
86 |
-
|
87 |
server_instance = server.PromptServer(loop)
|
88 |
execution.PromptQueue(server_instance)
|
89 |
init_extra_nodes()
|
90 |
|
|
|
91 |
# Import all necessary nodes
|
92 |
print("Importing ComfyUI nodes...")
|
93 |
try:
|
@@ -106,7 +151,7 @@ try:
|
|
106 |
UNETLoader,
|
107 |
CLIPTextEncode,
|
108 |
)
|
109 |
-
|
110 |
# Initialize all constant nodes and models in global context
|
111 |
import_custom_nodes()
|
112 |
except Exception as e:
|
@@ -154,7 +199,9 @@ SAMPLER = ksamplerselect.get_sampler(sampler_name="euler")
|
|
154 |
|
155 |
# Initialize depth model
|
156 |
cr_clip_input_switch = NODE_CLASS_MAPPINGS["CR Clip Input Switch"]()
|
157 |
-
downloadandloaddepthanythingv2model = NODE_CLASS_MAPPINGS[
|
|
|
|
|
158 |
DEPTH_MODEL = downloadandloaddepthanythingv2model.loadmodel(
|
159 |
model="depth_anything_v2_vitl_fp32.safetensors"
|
160 |
)
|
@@ -175,7 +222,7 @@ clipvisionencode = CLIPVisionEncode()
|
|
175 |
stylemodelapplyadvanced = NODE_CLASS_MAPPINGS["StyleModelApplyAdvanced"]()
|
176 |
emptylatentimage = EmptyLatentImage()
|
177 |
basicguider = NODE_CLASS_MAPPINGS["BasicGuider"]()
|
178 |
-
basicscheduler = NODE_CLASS_MAPPINGS["BasicScheduler"]()
|
179 |
randomnoise = NODE_CLASS_MAPPINGS["RandomNoise"]()
|
180 |
samplercustomadvanced = NODE_CLASS_MAPPINGS["SamplerCustomAdvanced"]()
|
181 |
vaedecode = VAEDecode()
|
@@ -191,18 +238,31 @@ from comfy import model_management
|
|
191 |
model_loaders = [CLIP_MODEL, VAE_MODEL, UNET_MODEL, CLIP_VISION_MODEL]
|
192 |
|
193 |
print("Loading models to GPU...")
|
194 |
-
model_management.load_models_gpu(
|
195 |
-
|
196 |
-
])
|
|
|
|
|
|
|
197 |
|
198 |
print("Setup complete!")
|
199 |
|
|
|
200 |
@spaces.GPU
|
201 |
-
def generate_image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
"""Main generation function that processes inputs and returns the path to the generated image."""
|
203 |
timestamp = random.randint(10000, 99999)
|
204 |
output_filename = f"flux_zen_{timestamp}.png"
|
205 |
-
|
206 |
with torch.inference_mode():
|
207 |
# Set up CLIP
|
208 |
clip_switch = cr_clip_input_switch.switch(
|
@@ -210,7 +270,7 @@ def generate_image(prompt, structure_image, style_image, depth_strength=15, cann
|
|
210 |
clip1=get_value_at_index(CLIP_MODEL, 0),
|
211 |
clip2=get_value_at_index(CLIP_MODEL, 0),
|
212 |
)
|
213 |
-
|
214 |
# Encode text
|
215 |
text_encoded = cliptextencode.encode(
|
216 |
text=prompt,
|
@@ -220,10 +280,10 @@ def generate_image(prompt, structure_image, style_image, depth_strength=15, cann
|
|
220 |
text="",
|
221 |
clip=get_value_at_index(clip_switch, 0),
|
222 |
)
|
223 |
-
|
224 |
# Process structure image
|
225 |
structure_img = loadimage.load_image(image=structure_image)
|
226 |
-
|
227 |
# Resize image
|
228 |
resized_img = imageresize.execute(
|
229 |
width=get_value_at_index(CONST_1024, 0),
|
@@ -234,59 +294,59 @@ def generate_image(prompt, structure_image, style_image, depth_strength=15, cann
|
|
234 |
multiple_of=16,
|
235 |
image=get_value_at_index(structure_img, 0),
|
236 |
)
|
237 |
-
|
238 |
# Get image size
|
239 |
size_info = getimagesizeandcount.getsize(
|
240 |
image=get_value_at_index(resized_img, 0)
|
241 |
)
|
242 |
-
|
243 |
# Encode VAE
|
244 |
vae_encoded = vaeencode.encode(
|
245 |
pixels=get_value_at_index(size_info, 0),
|
246 |
vae=get_value_at_index(VAE_MODEL, 0),
|
247 |
)
|
248 |
-
|
249 |
# Process canny
|
250 |
canny_processed = canny_prossessor.detect_edge(
|
251 |
-
image=get_value_at_index(size_info, 0),
|
252 |
-
low_threshold=0.4,
|
253 |
-
high_threshold=0.8
|
254 |
-
|
255 |
-
|
256 |
-
#Apply canny Advanced
|
257 |
canny_conditions = controlNetApplyAdvanced.apply_controlnet(
|
258 |
-
positive=get_value_at_index(text_encoded, 0),
|
259 |
-
negative=get_value_at_index(empty_text, 0),
|
260 |
-
control_net=get_value_at_index(CANNY_XLABS_MODEL, 0),
|
261 |
-
image=get_value_at_index(canny_processed, 0),
|
262 |
-
strength=canny_strength,
|
263 |
-
start_percent=0.0,
|
264 |
-
end_percent=0.5,
|
265 |
-
vae=get_value_at_index(VAE_MODEL, 0)
|
266 |
-
|
267 |
-
|
268 |
# Process depth
|
269 |
depth_processed = depthanything_v2.process(
|
270 |
da_model=get_value_at_index(DEPTH_MODEL, 0),
|
271 |
images=get_value_at_index(size_info, 0),
|
272 |
)
|
273 |
-
|
274 |
# Apply Flux guidance
|
275 |
flux_guided = fluxguidance.append(
|
276 |
guidance=depth_strength,
|
277 |
conditioning=get_value_at_index(canny_conditions, 0),
|
278 |
)
|
279 |
-
|
280 |
# Process style image
|
281 |
style_img = loadimage.load_image(image=style_image)
|
282 |
-
|
283 |
# Encode style with CLIP Vision
|
284 |
style_encoded = clipvisionencode.encode(
|
285 |
crop="center",
|
286 |
clip_vision=get_value_at_index(CLIP_VISION_MODEL, 0),
|
287 |
image=get_value_at_index(style_img, 0),
|
288 |
)
|
289 |
-
|
290 |
# Set up conditioning
|
291 |
conditioning = instructpixtopixconditioning.encode(
|
292 |
positive=get_value_at_index(flux_guided, 0),
|
@@ -294,7 +354,7 @@ def generate_image(prompt, structure_image, style_image, depth_strength=15, cann
|
|
294 |
vae=get_value_at_index(VAE_MODEL, 0),
|
295 |
pixels=get_value_at_index(depth_processed, 0),
|
296 |
)
|
297 |
-
|
298 |
# Apply style
|
299 |
style_applied = stylemodelapplyadvanced.apply_stylemodel(
|
300 |
strength=style_strength,
|
@@ -302,20 +362,20 @@ def generate_image(prompt, structure_image, style_image, depth_strength=15, cann
|
|
302 |
style_model=get_value_at_index(STYLE_MODEL, 0),
|
303 |
clip_vision_output=get_value_at_index(style_encoded, 0),
|
304 |
)
|
305 |
-
|
306 |
# Set up empty latent
|
307 |
empty_latent = emptylatentimage.generate(
|
308 |
width=get_value_at_index(resized_img, 1),
|
309 |
height=get_value_at_index(resized_img, 2),
|
310 |
batch_size=1,
|
311 |
)
|
312 |
-
|
313 |
# Set up guidance
|
314 |
guided = basicguider.get_guider(
|
315 |
model=get_value_at_index(UNET_MODEL, 0),
|
316 |
conditioning=get_value_at_index(style_applied, 0),
|
317 |
)
|
318 |
-
|
319 |
# Set up scheduler
|
320 |
schedule = basicscheduler.get_sigmas(
|
321 |
scheduler="simple",
|
@@ -323,10 +383,10 @@ def generate_image(prompt, structure_image, style_image, depth_strength=15, cann
|
|
323 |
denoise=1,
|
324 |
model=get_value_at_index(UNET_MODEL, 0),
|
325 |
)
|
326 |
-
|
327 |
# Generate random noise
|
328 |
noise = randomnoise.get_noise(noise_seed=random.randint(1, 2**64))
|
329 |
-
|
330 |
# Sample
|
331 |
sampled = samplercustomadvanced.sample(
|
332 |
noise=get_value_at_index(noise, 0),
|
@@ -335,108 +395,136 @@ def generate_image(prompt, structure_image, style_image, depth_strength=15, cann
|
|
335 |
sigmas=get_value_at_index(schedule, 0),
|
336 |
latent_image=get_value_at_index(empty_latent, 0),
|
337 |
)
|
338 |
-
|
339 |
# Decode VAE
|
340 |
decoded = vaedecode.decode(
|
341 |
samples=get_value_at_index(sampled, 0),
|
342 |
vae=get_value_at_index(VAE_MODEL, 0),
|
343 |
)
|
344 |
-
|
345 |
# Create text node for prefix
|
346 |
prefix = cr_text.text_multiline(text=f"flux_zen_{timestamp}")
|
347 |
-
|
348 |
# Use SaveImage node to save the image
|
349 |
saved_data = saveimage.save_images(
|
350 |
filename_prefix=get_value_at_index(prefix, 0),
|
351 |
images=get_value_at_index(decoded, 0),
|
352 |
)
|
353 |
-
|
354 |
try:
|
355 |
saved_path = f"output/{saved_data['ui']['images'][0]['filename']}"
|
356 |
-
|
357 |
return saved_path
|
358 |
except Exception as e:
|
359 |
print(f"Error getting saved image path: {e}")
|
360 |
# Fall back to the expected path
|
361 |
return os.path.join("output", output_filename)
|
|
|
|
|
|
|
|
|
362 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
363 |
|
364 |
-
with gr.Blocks(css="footer {visibility: hidden}") as app:
|
365 |
-
gr.Markdown("# 🎨 FLUX Zen Style Depth+Canny")
|
366 |
-
gr.Markdown("Flux[dev] Redux + Flux[dev] Depth and XLabs Canny based on the space FLUX Style Shaping")
|
367 |
-
|
368 |
with gr.Row():
|
369 |
with gr.Column(scale=1):
|
370 |
prompt_input = gr.Textbox(
|
371 |
-
label="Prompt",
|
372 |
placeholder="Enter your prompt here...",
|
373 |
-
info="Describe the image you want to generate"
|
374 |
)
|
375 |
with gr.Row():
|
376 |
with gr.Column(scale=1):
|
377 |
structure_image = gr.Image(
|
378 |
-
image_mode=
|
379 |
-
label="Structure Image",
|
380 |
-
type="filepath"
|
381 |
)
|
382 |
depth_strength = gr.Slider(
|
383 |
-
minimum=0,
|
384 |
-
maximum=50,
|
385 |
-
value=15,
|
386 |
label="Depth Strength",
|
387 |
-
info="Controls how much the depth map influences the result"
|
388 |
)
|
389 |
canny_strength = gr.Slider(
|
390 |
-
minimum=0,
|
391 |
-
maximum=1.0,
|
392 |
-
value=0.30,
|
393 |
label="Canny Strength",
|
394 |
-
info="Controls how much the edge detection influences the result"
|
395 |
)
|
396 |
steps = gr.Slider(
|
397 |
-
minimum=10,
|
398 |
-
maximum=50,
|
399 |
-
value=28,
|
400 |
label="Steps",
|
401 |
-
info="More steps = better quality but slower generation"
|
402 |
)
|
403 |
with gr.Column(scale=1):
|
404 |
-
style_image = gr.Image(
|
405 |
-
label="Style Image",
|
406 |
-
type="filepath"
|
407 |
-
)
|
408 |
style_strength = gr.Slider(
|
409 |
-
minimum=0,
|
410 |
-
maximum=1,
|
411 |
-
value=0.5,
|
412 |
label="Style Strength",
|
413 |
-
info="Controls how much the style image influences the result"
|
414 |
)
|
415 |
-
|
416 |
with gr.Row():
|
417 |
-
generate_btn = gr.Button("Generate",value=True, variant="primary")
|
418 |
-
|
419 |
with gr.Column(scale=1):
|
420 |
output_image = gr.Image(label="Generated Image")
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
432 |
|
433 |
generate_btn.click(
|
434 |
fn=generate_image,
|
435 |
-
inputs=[
|
436 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
437 |
)
|
438 |
-
|
439 |
-
gr.Markdown(
|
|
|
440 |
## How to use
|
441 |
1. Enter a prompt describing the image you want to generate
|
442 |
2. Upload a structure image to provide the basic shape/composition
|
@@ -447,11 +535,12 @@ with gr.Blocks(css="footer {visibility: hidden}") as app:
|
|
447 |
## About
|
448 |
This demo uses FLUX.1-Redux-dev for style transfer, FLUX.1-Depth-dev for depth-guided generation,
|
449 |
and XLabs Canny for edge detection and structure preservation.
|
450 |
-
"""
|
|
|
451 |
|
452 |
if __name__ == "__main__":
|
453 |
# Create an examples directory if it doesn't exist , for now it is empty
|
454 |
os.makedirs("examples", exist_ok=True)
|
455 |
-
|
456 |
# Launch the app
|
457 |
-
|
|
|
2 |
import random
|
3 |
import sys
|
4 |
import subprocess
|
5 |
+
import spaces
|
6 |
import torch
|
7 |
import gradio as gr
|
8 |
+
|
9 |
+
from typing import Sequence, Mapping, Any, Union
|
10 |
+
from examples_db import ZEN_EXAMPLES
|
11 |
from PIL import Image, ImageChops
|
12 |
from huggingface_hub import hf_hub_download
|
|
|
13 |
|
14 |
# Setup ComfyUI if not already set up
|
15 |
+
# if not os.path.exists("ComfyUI"):
|
16 |
# print("Setting up ComfyUI...")
|
17 |
# subprocess.run(["bash", "setup_comfyui.sh"], check=True)
|
18 |
|
|
|
21 |
|
22 |
# Download models if not already present
|
23 |
print("Checking and downloading models...")
|
24 |
+
hf_hub_download(
|
25 |
+
repo_id="black-forest-labs/FLUX.1-Redux-dev",
|
26 |
+
filename="flux1-redux-dev.safetensors",
|
27 |
+
local_dir="models/style_models",
|
28 |
+
)
|
29 |
+
hf_hub_download(
|
30 |
+
repo_id="black-forest-labs/FLUX.1-Depth-dev",
|
31 |
+
filename="flux1-depth-dev.safetensors",
|
32 |
+
local_dir="models/diffusion_models",
|
33 |
+
)
|
34 |
+
hf_hub_download(
|
35 |
+
repo_id="black-forest-labs/FLUX.1-Canny-dev",
|
36 |
+
filename="flux1-canny-dev.safetensors",
|
37 |
+
local_dir="models/controlnet",
|
38 |
+
)
|
39 |
+
hf_hub_download(
|
40 |
+
repo_id="XLabs-AI/flux-controlnet-collections",
|
41 |
+
filename="flux-canny-controlnet-v3.safetensors",
|
42 |
+
local_dir="models/controlnet",
|
43 |
+
)
|
44 |
+
hf_hub_download(
|
45 |
+
repo_id="Comfy-Org/sigclip_vision_384",
|
46 |
+
filename="sigclip_vision_patch14_384.safetensors",
|
47 |
+
local_dir="models/clip_vision",
|
48 |
+
)
|
49 |
+
hf_hub_download(
|
50 |
+
repo_id="Kijai/DepthAnythingV2-safetensors",
|
51 |
+
filename="depth_anything_v2_vitl_fp32.safetensors",
|
52 |
+
local_dir="models/depthanything",
|
53 |
+
)
|
54 |
+
hf_hub_download(
|
55 |
+
repo_id="black-forest-labs/FLUX.1-dev",
|
56 |
+
filename="ae.safetensors",
|
57 |
+
local_dir="models/vae/FLUX1",
|
58 |
+
)
|
59 |
+
hf_hub_download(
|
60 |
+
repo_id="comfyanonymous/flux_text_encoders",
|
61 |
+
filename="clip_l.safetensors",
|
62 |
+
local_dir="models/text_encoders",
|
63 |
+
)
|
64 |
+
t5_path = hf_hub_download(
|
65 |
+
repo_id="comfyanonymous/flux_text_encoders",
|
66 |
+
filename="t5xxl_fp16.safetensors",
|
67 |
+
local_dir="models/text_encoders/t5",
|
68 |
+
)
|
69 |
|
70 |
# Import required functions and setup ComfyUI path
|
71 |
import folder_paths
|
72 |
|
73 |
+
|
74 |
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
|
75 |
try:
|
76 |
return obj[index]
|
77 |
except KeyError:
|
78 |
return obj["result"][index]
|
79 |
|
80 |
+
|
81 |
def find_path(name: str, path: str = None) -> str:
|
82 |
if path is None:
|
83 |
path = os.getcwd()
|
|
|
90 |
return None
|
91 |
return find_path(name, parent_directory)
|
92 |
|
93 |
+
|
94 |
def add_comfyui_directory_to_sys_path() -> None:
|
95 |
comfyui_path = find_path("ComfyUI")
|
96 |
if comfyui_path is not None and os.path.isdir(comfyui_path):
|
97 |
sys.path.append(comfyui_path)
|
98 |
print(f"'{comfyui_path}' added to sys.path")
|
99 |
|
100 |
+
|
101 |
def add_extra_model_paths() -> None:
|
102 |
try:
|
103 |
from main import load_extra_path_config
|
|
|
109 |
else:
|
110 |
print("Could not find the extra_model_paths config file.")
|
111 |
|
112 |
+
|
113 |
# Initialize paths
|
114 |
add_comfyui_directory_to_sys_path()
|
115 |
add_extra_model_paths()
|
116 |
|
117 |
+
|
118 |
def import_custom_nodes() -> None:
|
119 |
import asyncio
|
120 |
import execution
|
121 |
from nodes import init_extra_nodes
|
122 |
import server
|
123 |
+
|
124 |
# Create a new event loop if running in a new thread
|
125 |
try:
|
126 |
loop = asyncio.get_event_loop()
|
127 |
except RuntimeError:
|
128 |
loop = asyncio.new_event_loop()
|
129 |
asyncio.set_event_loop(loop)
|
130 |
+
|
131 |
server_instance = server.PromptServer(loop)
|
132 |
execution.PromptQueue(server_instance)
|
133 |
init_extra_nodes()
|
134 |
|
135 |
+
|
136 |
# Import all necessary nodes
|
137 |
print("Importing ComfyUI nodes...")
|
138 |
try:
|
|
|
151 |
UNETLoader,
|
152 |
CLIPTextEncode,
|
153 |
)
|
154 |
+
|
155 |
# Initialize all constant nodes and models in global context
|
156 |
import_custom_nodes()
|
157 |
except Exception as e:
|
|
|
199 |
|
200 |
# Initialize depth model
|
201 |
cr_clip_input_switch = NODE_CLASS_MAPPINGS["CR Clip Input Switch"]()
|
202 |
+
downloadandloaddepthanythingv2model = NODE_CLASS_MAPPINGS[
|
203 |
+
"DownloadAndLoadDepthAnythingV2Model"
|
204 |
+
]()
|
205 |
DEPTH_MODEL = downloadandloaddepthanythingv2model.loadmodel(
|
206 |
model="depth_anything_v2_vitl_fp32.safetensors"
|
207 |
)
|
|
|
222 |
stylemodelapplyadvanced = NODE_CLASS_MAPPINGS["StyleModelApplyAdvanced"]()
|
223 |
emptylatentimage = EmptyLatentImage()
|
224 |
basicguider = NODE_CLASS_MAPPINGS["BasicGuider"]()
|
225 |
+
basicscheduler = NODE_CLASS_MAPPINGS["BasicScheduler"]()
|
226 |
randomnoise = NODE_CLASS_MAPPINGS["RandomNoise"]()
|
227 |
samplercustomadvanced = NODE_CLASS_MAPPINGS["SamplerCustomAdvanced"]()
|
228 |
vaedecode = VAEDecode()
|
|
|
238 |
model_loaders = [CLIP_MODEL, VAE_MODEL, UNET_MODEL, CLIP_VISION_MODEL]
|
239 |
|
240 |
print("Loading models to GPU...")
|
241 |
+
model_management.load_models_gpu(
|
242 |
+
[
|
243 |
+
loader[0].patcher if hasattr(loader[0], "patcher") else loader[0]
|
244 |
+
for loader in model_loaders
|
245 |
+
]
|
246 |
+
)
|
247 |
|
248 |
print("Setup complete!")
|
249 |
|
250 |
+
|
251 |
@spaces.GPU
|
252 |
+
def generate_image(
|
253 |
+
prompt,
|
254 |
+
structure_image,
|
255 |
+
style_image,
|
256 |
+
depth_strength=15,
|
257 |
+
canny_strength=30,
|
258 |
+
style_strength=0.5,
|
259 |
+
steps=28,
|
260 |
+
progress=gr.Progress(track_tqdm=True),
|
261 |
+
):
|
262 |
"""Main generation function that processes inputs and returns the path to the generated image."""
|
263 |
timestamp = random.randint(10000, 99999)
|
264 |
output_filename = f"flux_zen_{timestamp}.png"
|
265 |
+
|
266 |
with torch.inference_mode():
|
267 |
# Set up CLIP
|
268 |
clip_switch = cr_clip_input_switch.switch(
|
|
|
270 |
clip1=get_value_at_index(CLIP_MODEL, 0),
|
271 |
clip2=get_value_at_index(CLIP_MODEL, 0),
|
272 |
)
|
273 |
+
|
274 |
# Encode text
|
275 |
text_encoded = cliptextencode.encode(
|
276 |
text=prompt,
|
|
|
280 |
text="",
|
281 |
clip=get_value_at_index(clip_switch, 0),
|
282 |
)
|
283 |
+
|
284 |
# Process structure image
|
285 |
structure_img = loadimage.load_image(image=structure_image)
|
286 |
+
|
287 |
# Resize image
|
288 |
resized_img = imageresize.execute(
|
289 |
width=get_value_at_index(CONST_1024, 0),
|
|
|
294 |
multiple_of=16,
|
295 |
image=get_value_at_index(structure_img, 0),
|
296 |
)
|
297 |
+
|
298 |
# Get image size
|
299 |
size_info = getimagesizeandcount.getsize(
|
300 |
image=get_value_at_index(resized_img, 0)
|
301 |
)
|
302 |
+
|
303 |
# Encode VAE
|
304 |
vae_encoded = vaeencode.encode(
|
305 |
pixels=get_value_at_index(size_info, 0),
|
306 |
vae=get_value_at_index(VAE_MODEL, 0),
|
307 |
)
|
308 |
+
|
309 |
# Process canny
|
310 |
canny_processed = canny_prossessor.detect_edge(
|
311 |
+
image=get_value_at_index(size_info, 0),
|
312 |
+
low_threshold=0.4,
|
313 |
+
high_threshold=0.8,
|
314 |
+
)
|
315 |
+
|
316 |
+
# Apply canny Advanced
|
317 |
canny_conditions = controlNetApplyAdvanced.apply_controlnet(
|
318 |
+
positive=get_value_at_index(text_encoded, 0),
|
319 |
+
negative=get_value_at_index(empty_text, 0),
|
320 |
+
control_net=get_value_at_index(CANNY_XLABS_MODEL, 0),
|
321 |
+
image=get_value_at_index(canny_processed, 0),
|
322 |
+
strength=canny_strength,
|
323 |
+
start_percent=0.0,
|
324 |
+
end_percent=0.5,
|
325 |
+
vae=get_value_at_index(VAE_MODEL, 0),
|
326 |
+
)
|
327 |
+
|
328 |
# Process depth
|
329 |
depth_processed = depthanything_v2.process(
|
330 |
da_model=get_value_at_index(DEPTH_MODEL, 0),
|
331 |
images=get_value_at_index(size_info, 0),
|
332 |
)
|
333 |
+
|
334 |
# Apply Flux guidance
|
335 |
flux_guided = fluxguidance.append(
|
336 |
guidance=depth_strength,
|
337 |
conditioning=get_value_at_index(canny_conditions, 0),
|
338 |
)
|
339 |
+
|
340 |
# Process style image
|
341 |
style_img = loadimage.load_image(image=style_image)
|
342 |
+
|
343 |
# Encode style with CLIP Vision
|
344 |
style_encoded = clipvisionencode.encode(
|
345 |
crop="center",
|
346 |
clip_vision=get_value_at_index(CLIP_VISION_MODEL, 0),
|
347 |
image=get_value_at_index(style_img, 0),
|
348 |
)
|
349 |
+
|
350 |
# Set up conditioning
|
351 |
conditioning = instructpixtopixconditioning.encode(
|
352 |
positive=get_value_at_index(flux_guided, 0),
|
|
|
354 |
vae=get_value_at_index(VAE_MODEL, 0),
|
355 |
pixels=get_value_at_index(depth_processed, 0),
|
356 |
)
|
357 |
+
|
358 |
# Apply style
|
359 |
style_applied = stylemodelapplyadvanced.apply_stylemodel(
|
360 |
strength=style_strength,
|
|
|
362 |
style_model=get_value_at_index(STYLE_MODEL, 0),
|
363 |
clip_vision_output=get_value_at_index(style_encoded, 0),
|
364 |
)
|
365 |
+
|
366 |
# Set up empty latent
|
367 |
empty_latent = emptylatentimage.generate(
|
368 |
width=get_value_at_index(resized_img, 1),
|
369 |
height=get_value_at_index(resized_img, 2),
|
370 |
batch_size=1,
|
371 |
)
|
372 |
+
|
373 |
# Set up guidance
|
374 |
guided = basicguider.get_guider(
|
375 |
model=get_value_at_index(UNET_MODEL, 0),
|
376 |
conditioning=get_value_at_index(style_applied, 0),
|
377 |
)
|
378 |
+
|
379 |
# Set up scheduler
|
380 |
schedule = basicscheduler.get_sigmas(
|
381 |
scheduler="simple",
|
|
|
383 |
denoise=1,
|
384 |
model=get_value_at_index(UNET_MODEL, 0),
|
385 |
)
|
386 |
+
|
387 |
# Generate random noise
|
388 |
noise = randomnoise.get_noise(noise_seed=random.randint(1, 2**64))
|
389 |
+
|
390 |
# Sample
|
391 |
sampled = samplercustomadvanced.sample(
|
392 |
noise=get_value_at_index(noise, 0),
|
|
|
395 |
sigmas=get_value_at_index(schedule, 0),
|
396 |
latent_image=get_value_at_index(empty_latent, 0),
|
397 |
)
|
398 |
+
|
399 |
# Decode VAE
|
400 |
decoded = vaedecode.decode(
|
401 |
samples=get_value_at_index(sampled, 0),
|
402 |
vae=get_value_at_index(VAE_MODEL, 0),
|
403 |
)
|
404 |
+
|
405 |
# Create text node for prefix
|
406 |
prefix = cr_text.text_multiline(text=f"flux_zen_{timestamp}")
|
407 |
+
|
408 |
# Use SaveImage node to save the image
|
409 |
saved_data = saveimage.save_images(
|
410 |
filename_prefix=get_value_at_index(prefix, 0),
|
411 |
images=get_value_at_index(decoded, 0),
|
412 |
)
|
413 |
+
|
414 |
try:
|
415 |
saved_path = f"output/{saved_data['ui']['images'][0]['filename']}"
|
416 |
+
|
417 |
return saved_path
|
418 |
except Exception as e:
|
419 |
print(f"Error getting saved image path: {e}")
|
420 |
# Fall back to the expected path
|
421 |
return os.path.join("output", output_filename)
|
422 |
+
css = """
|
423 |
+
footer {
|
424 |
+
visibility: hidden;
|
425 |
+
}
|
426 |
|
427 |
+
.title {
|
428 |
+
font-size: 2.5em;
|
429 |
+
background: linear-gradient(109deg, rgba(34,193,195,1) 0%, rgba(67,253,45,1) 100%);
|
430 |
+
-webkit-background-clip: text;
|
431 |
+
-webkit-text-fill-color: transparent;
|
432 |
+
font-weight: bold;
|
433 |
+
}
|
434 |
+
"""
|
435 |
+
|
436 |
+
with gr.Blocks(css=css) as demo:
|
437 |
+
gr.HTML(
|
438 |
+
"""
|
439 |
+
<h1><center>🎨 FLUX <span class="title">Zen Style</span> Depth+Canny</center></h1>
|
440 |
+
"""
|
441 |
+
)
|
442 |
+
gr.Markdown(
|
443 |
+
"Flux[dev] Redux + Flux[dev] Depth and XLabs Canny based on the space FLUX Style Shaping"
|
444 |
+
)
|
445 |
|
|
|
|
|
|
|
|
|
446 |
with gr.Row():
|
447 |
with gr.Column(scale=1):
|
448 |
prompt_input = gr.Textbox(
|
449 |
+
label="Prompt",
|
450 |
placeholder="Enter your prompt here...",
|
451 |
+
info="Describe the image you want to generate",
|
452 |
)
|
453 |
with gr.Row():
|
454 |
with gr.Column(scale=1):
|
455 |
structure_image = gr.Image(
|
456 |
+
image_mode="RGB", label="Structure Image", type="filepath"
|
|
|
|
|
457 |
)
|
458 |
depth_strength = gr.Slider(
|
459 |
+
minimum=0,
|
460 |
+
maximum=50,
|
461 |
+
value=15,
|
462 |
label="Depth Strength",
|
463 |
+
info="Controls how much the depth map influences the result",
|
464 |
)
|
465 |
canny_strength = gr.Slider(
|
466 |
+
minimum=0,
|
467 |
+
maximum=1.0,
|
468 |
+
value=0.30,
|
469 |
label="Canny Strength",
|
470 |
+
info="Controls how much the edge detection influences the result",
|
471 |
)
|
472 |
steps = gr.Slider(
|
473 |
+
minimum=10,
|
474 |
+
maximum=50,
|
475 |
+
value=28,
|
476 |
label="Steps",
|
477 |
+
info="More steps = better quality but slower generation",
|
478 |
)
|
479 |
with gr.Column(scale=1):
|
480 |
+
style_image = gr.Image(label="Style Image", type="filepath")
|
|
|
|
|
|
|
481 |
style_strength = gr.Slider(
|
482 |
+
minimum=0,
|
483 |
+
maximum=1,
|
484 |
+
value=0.5,
|
485 |
label="Style Strength",
|
486 |
+
info="Controls how much the style image influences the result",
|
487 |
)
|
488 |
+
|
489 |
with gr.Row():
|
490 |
+
generate_btn = gr.Button("Generate", value=True, variant="primary")
|
491 |
+
|
492 |
with gr.Column(scale=1):
|
493 |
output_image = gr.Image(label="Generated Image")
|
494 |
+
|
495 |
+
gr.Examples(
|
496 |
+
examples=ZEN_EXAMPLES,
|
497 |
+
inputs=[
|
498 |
+
prompt_input,
|
499 |
+
structure_image,
|
500 |
+
style_image,
|
501 |
+
output_image,
|
502 |
+
depth_strength,
|
503 |
+
canny_strength,
|
504 |
+
style_strength,
|
505 |
+
steps,
|
506 |
+
],
|
507 |
+
fn=generate_image,
|
508 |
+
label="Presets",
|
509 |
+
examples_per_page=6,
|
510 |
+
)
|
511 |
|
512 |
generate_btn.click(
|
513 |
fn=generate_image,
|
514 |
+
inputs=[
|
515 |
+
prompt_input,
|
516 |
+
structure_image,
|
517 |
+
style_image,
|
518 |
+
depth_strength,
|
519 |
+
canny_strength,
|
520 |
+
style_strength,
|
521 |
+
steps,
|
522 |
+
],
|
523 |
+
outputs=[output_image],
|
524 |
)
|
525 |
+
|
526 |
+
gr.Markdown(
|
527 |
+
"""
|
528 |
## How to use
|
529 |
1. Enter a prompt describing the image you want to generate
|
530 |
2. Upload a structure image to provide the basic shape/composition
|
|
|
535 |
## About
|
536 |
This demo uses FLUX.1-Redux-dev for style transfer, FLUX.1-Depth-dev for depth-guided generation,
|
537 |
and XLabs Canny for edge detection and structure preservation.
|
538 |
+
"""
|
539 |
+
)
|
540 |
|
541 |
if __name__ == "__main__":
|
542 |
# Create an examples directory if it doesn't exist , for now it is empty
|
543 |
os.makedirs("examples", exist_ok=True)
|
544 |
+
|
545 |
# Launch the app
|
546 |
+
demo.launch(share=True)
|
examples_db.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
ZEN_EXAMPLES = [
|
2 |
+
[
|
3 |
+
"A beautiful landscape with mountains and a lake",
|
4 |
+
"imgs/structure1.png",
|
5 |
+
"imgs/style1.png",
|
6 |
+
"imgs/output1.png",
|
7 |
+
20,
|
8 |
+
0.4,
|
9 |
+
0.6,
|
10 |
+
30,
|
11 |
+
],
|
12 |
+
# [
|
13 |
+
# "A cyberpunk cityscape at night",
|
14 |
+
# "imgs/structure2.jpg",
|
15 |
+
# "imgs/style2.jpg",
|
16 |
+
# "imgs/output2.jpg",
|
17 |
+
# 15,
|
18 |
+
# 0.35,
|
19 |
+
# 0.7,
|
20 |
+
# 28,
|
21 |
+
# ],
|
22 |
+
]
|
imgs/output1.png
ADDED
![]() |
Git LFS Details
|
imgs/structure1.png
ADDED
![]() |
Git LFS Details
|
imgs/style1.png
ADDED
![]() |
Git LFS Details
|