Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,91 @@
|
|
|
|
1 |
import torch
|
2 |
-
from diffusers.utils import load_image
|
3 |
-
from diffusers import FluxControlNetPipeline, FluxControlNetModel, FluxMultiControlNetModel
|
4 |
import gradio as gr
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
9 |
|
|
|
10 |
base_model = 'black-forest-labs/FLUX.1-dev'
|
11 |
-
|
12 |
-
|
13 |
-
controlnet_union = FluxControlNetModel.from_pretrained(controlnet_model_union, torch_dtype=torch_dtype)
|
14 |
-
controlnet = FluxMultiControlNetModel([controlnet_union])
|
15 |
-
|
16 |
-
pipe = FluxControlNetPipeline.from_pretrained(base_model, controlnet=controlnet, torch_dtype=torch_dtype)
|
17 |
|
18 |
-
|
19 |
-
pipe.
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
"
|
24 |
-
"
|
25 |
-
"
|
26 |
-
"
|
27 |
-
"
|
28 |
-
"
|
29 |
-
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
|
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
generator=torch.manual_seed(42),
|
65 |
-
).images[0]
|
66 |
-
except RuntimeError as e:
|
67 |
-
torch.cuda.empty_cache()
|
68 |
-
raise e
|
69 |
|
70 |
-
return image
|
71 |
|
72 |
-
|
73 |
fn=generate_image,
|
74 |
inputs=[
|
75 |
-
gr.
|
76 |
-
gr.Image(label="Control Image
|
77 |
-
gr.Dropdown(choices=control_modes
|
78 |
-
gr.
|
79 |
-
gr.
|
80 |
-
gr.
|
|
|
|
|
|
|
|
|
81 |
],
|
82 |
-
outputs=
|
83 |
-
title="
|
84 |
-
description="Generate
|
85 |
)
|
86 |
|
87 |
-
|
|
|
|
1 |
+
import os
|
2 |
import torch
|
|
|
|
|
3 |
import gradio as gr
|
4 |
+
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 |
+
# Model and pipeline initialization
|
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("cuda")
|
19 |
|
20 |
+
# Define control modes
|
21 |
+
control_modes = {
|
22 |
+
"canny": 0,
|
23 |
+
"tile": 1,
|
24 |
+
"depth": 2,
|
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))
|
42 |
+
else:
|
43 |
+
image = image.resize((target_width, target_height), Image.LANCZOS)
|
44 |
+
|
45 |
+
return image
|
46 |
|
47 |
+
@gr.Interface
|
48 |
+
def generate_image(prompt, control_image, control_mode, controlnet_conditioning_scale, num_steps=50, guidance=4, width=512, height=512, seed=42, random_seed=False):
|
49 |
+
if random_seed:
|
50 |
+
seed = np.random.randint(0, 10000)
|
51 |
+
|
52 |
+
torch.manual_seed(seed)
|
53 |
|
54 |
+
control_mode_idx = control_modes.get(control_mode, 0)
|
55 |
+
|
56 |
+
processed_input = preprocess_image(control_image, width, height)
|
57 |
|
58 |
+
image = pipe(
|
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 |
+
return [processed_input, image] # Return both the control image and the generated image
|
70 |
|
71 |
+
interface = gr.Interface(
|
72 |
fn=generate_image,
|
73 |
inputs=[
|
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"),
|
81 |
+
gr.Slider(minimum=128, maximum=2048, step=128, value=1024, label="Height"),
|
82 |
+
gr.Number(value=42, label="Seed"),
|
83 |
+
gr.Checkbox(label="Random Seed")
|
84 |
],
|
85 |
+
outputs=ImageSlider(label="Before / After"), # Use ImageSlider as the output
|
86 |
+
title="FLUX.1 Controlnet with Different Modes",
|
87 |
+
description="Generate images using ControlNet with selectable control modes and a text prompt."
|
88 |
)
|
89 |
|
90 |
+
if __name__ == "__main__":
|
91 |
+
interface.launch()
|