Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,19 +5,28 @@ import numpy as np
|
|
5 |
from PIL import Image
|
6 |
from einops import rearrange
|
7 |
import requests
|
|
|
|
|
|
|
8 |
from diffusers.utils import load_image
|
9 |
from diffusers import FluxControlNetPipeline, FluxControlNetModel
|
10 |
-
from gradio_imageslider import ImageSlider # Import ImageSlider
|
11 |
|
12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
13 |
base_model = 'black-forest-labs/FLUX.1-dev'
|
14 |
controlnet_model = 'InstantX/FLUX.1-dev-Controlnet-Union'
|
15 |
|
|
|
16 |
controlnet = FluxControlNetModel.from_pretrained(controlnet_model, torch_dtype=torch.bfloat16)
|
17 |
pipe = FluxControlNetPipeline.from_pretrained(base_model, controlnet=controlnet, torch_dtype=torch.bfloat16)
|
18 |
-
pipe.to(
|
|
|
|
|
19 |
|
20 |
-
# Define control modes
|
21 |
control_modes = {
|
22 |
"canny": 0,
|
23 |
"tile": 1,
|
@@ -25,17 +34,21 @@ control_modes = {
|
|
25 |
"blur": 3,
|
26 |
"pose": 4,
|
27 |
"gray": 5,
|
28 |
-
"lq": 6
|
29 |
}
|
30 |
|
31 |
-
# Preprocess image
|
32 |
def preprocess_image(image, target_width, target_height, crop=True):
|
33 |
if crop:
|
34 |
original_width, original_height = image.size
|
|
|
|
|
35 |
scale = max(target_width / original_width, target_height / original_height)
|
36 |
resized_width = int(scale * original_width)
|
37 |
resized_height = int(scale * original_height)
|
|
|
38 |
image = image.resize((resized_width, resized_height), Image.LANCZOS)
|
|
|
|
|
39 |
left = (resized_width - target_width) // 2
|
40 |
top = (resized_height - target_height) // 2
|
41 |
image = image.crop((left, top, left + target_width, top + target_height))
|
@@ -44,29 +57,32 @@ def preprocess_image(image, target_width, target_height, crop=True):
|
|
44 |
|
45 |
return image
|
46 |
|
47 |
-
@
|
48 |
-
def generate_image(prompt, control_image, control_mode,
|
49 |
if random_seed:
|
50 |
seed = np.random.randint(0, 10000)
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
control_mode_idx = control_modes.get(control_mode, 0)
|
55 |
-
|
56 |
-
processed_input = preprocess_image(control_image, width, height)
|
57 |
|
58 |
-
|
59 |
-
prompt,
|
60 |
-
control_image=processed_input,
|
61 |
-
control_mode=control_mode_idx,
|
62 |
-
width=width,
|
63 |
-
height=height,
|
64 |
-
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
65 |
-
num_inference_steps=num_steps,
|
66 |
-
guidance_scale=guidance,
|
67 |
-
).images[0]
|
68 |
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
interface = gr.Interface(
|
72 |
fn=generate_image,
|
@@ -74,7 +90,6 @@ interface = gr.Interface(
|
|
74 |
gr.Textbox(label="Prompt"),
|
75 |
gr.Image(type="pil", label="Control Image"),
|
76 |
gr.Dropdown(choices=list(control_modes.keys()), label="Control Mode", value="canny"),
|
77 |
-
gr.Slider(minimum=0.1, maximum=10.0, value=0.5, label="ControlNet Conditioning Scale"),
|
78 |
gr.Slider(step=1, minimum=1, maximum=64, value=28, label="Num Steps"),
|
79 |
gr.Slider(minimum=0.1, maximum=10, value=4, label="Guidance"),
|
80 |
gr.Slider(minimum=128, maximum=2048, step=128, value=1024, label="Width"),
|
@@ -83,8 +98,8 @@ interface = gr.Interface(
|
|
83 |
gr.Checkbox(label="Random Seed")
|
84 |
],
|
85 |
outputs=ImageSlider(label="Before / After"), # Use ImageSlider as the output
|
86 |
-
title="FLUX.1 Controlnet
|
87 |
-
description="Generate images using ControlNet
|
88 |
)
|
89 |
|
90 |
if __name__ == "__main__":
|
|
|
5 |
from PIL import Image
|
6 |
from einops import rearrange
|
7 |
import requests
|
8 |
+
import spaces
|
9 |
+
from huggingface_hub import login
|
10 |
+
from gradio_imageslider import ImageSlider # Import ImageSlider
|
11 |
from diffusers.utils import load_image
|
12 |
from diffusers import FluxControlNetPipeline, FluxControlNetModel
|
|
|
13 |
|
14 |
+
# Source: https://github.com/XLabs-AI/x-flux.git
|
15 |
+
name = "flux-dev"
|
16 |
+
device = torch.device("cuda")
|
17 |
+
offload = False
|
18 |
+
is_schnell = name == "flux-schnell"
|
19 |
+
|
20 |
base_model = 'black-forest-labs/FLUX.1-dev'
|
21 |
controlnet_model = 'InstantX/FLUX.1-dev-Controlnet-Union'
|
22 |
|
23 |
+
# Load the new ControlNet model and pipeline
|
24 |
controlnet = FluxControlNetModel.from_pretrained(controlnet_model, torch_dtype=torch.bfloat16)
|
25 |
pipe = FluxControlNetPipeline.from_pretrained(base_model, controlnet=controlnet, torch_dtype=torch.bfloat16)
|
26 |
+
pipe.to(device)
|
27 |
+
|
28 |
+
controlnet_conditioning_scale = 0.5
|
29 |
|
|
|
30 |
control_modes = {
|
31 |
"canny": 0,
|
32 |
"tile": 1,
|
|
|
34 |
"blur": 3,
|
35 |
"pose": 4,
|
36 |
"gray": 5,
|
37 |
+
"lq": 6,
|
38 |
}
|
39 |
|
|
|
40 |
def preprocess_image(image, target_width, target_height, crop=True):
|
41 |
if crop:
|
42 |
original_width, original_height = image.size
|
43 |
+
|
44 |
+
# Resize to match the target size without stretching
|
45 |
scale = max(target_width / original_width, target_height / original_height)
|
46 |
resized_width = int(scale * original_width)
|
47 |
resized_height = int(scale * original_height)
|
48 |
+
|
49 |
image = image.resize((resized_width, resized_height), Image.LANCZOS)
|
50 |
+
|
51 |
+
# Center crop to match the target dimensions
|
52 |
left = (resized_width - target_width) // 2
|
53 |
top = (resized_height - target_height) // 2
|
54 |
image = image.crop((left, top, left + target_width, top + target_height))
|
|
|
57 |
|
58 |
return image
|
59 |
|
60 |
+
@spaces.GPU(duration=120)
|
61 |
+
def generate_image(prompt, control_image, control_mode, num_steps=50, guidance=4, width=512, height=512, seed=42, random_seed=False):
|
62 |
if random_seed:
|
63 |
seed = np.random.randint(0, 10000)
|
64 |
|
65 |
+
if not os.path.isdir("./controlnet_results/"):
|
66 |
+
os.makedirs("./controlnet_results/")
|
|
|
|
|
|
|
67 |
|
68 |
+
torch_device = torch.device("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
control_image = preprocess_image(control_image, width, height)
|
71 |
+
|
72 |
+
torch.manual_seed(seed)
|
73 |
+
with torch.no_grad():
|
74 |
+
image = pipe(
|
75 |
+
prompt,
|
76 |
+
control_image=control_image,
|
77 |
+
control_mode=control_modes[control_mode],
|
78 |
+
width=width,
|
79 |
+
height=height,
|
80 |
+
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
81 |
+
num_inference_steps=num_steps,
|
82 |
+
guidance_scale=guidance,
|
83 |
+
).images[0]
|
84 |
+
|
85 |
+
return [control_image, image] # Return both images for slider
|
86 |
|
87 |
interface = gr.Interface(
|
88 |
fn=generate_image,
|
|
|
90 |
gr.Textbox(label="Prompt"),
|
91 |
gr.Image(type="pil", label="Control Image"),
|
92 |
gr.Dropdown(choices=list(control_modes.keys()), label="Control Mode", value="canny"),
|
|
|
93 |
gr.Slider(step=1, minimum=1, maximum=64, value=28, label="Num Steps"),
|
94 |
gr.Slider(minimum=0.1, maximum=10, value=4, label="Guidance"),
|
95 |
gr.Slider(minimum=128, maximum=2048, step=128, value=1024, label="Width"),
|
|
|
98 |
gr.Checkbox(label="Random Seed")
|
99 |
],
|
100 |
outputs=ImageSlider(label="Before / After"), # Use ImageSlider as the output
|
101 |
+
title="FLUX.1 Controlnet Canny",
|
102 |
+
description="Generate images using ControlNet and a text prompt.\n[[non-commercial license, Flux.1 Dev](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)]"
|
103 |
)
|
104 |
|
105 |
if __name__ == "__main__":
|