Deadmon commited on
Commit
2911f3b
·
verified ·
1 Parent(s): e19c312

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -70
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 spaces
6
-
7
- # Ensure that you're using the appropriate data type for your GPU
8
- torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
 
 
 
9
 
 
10
  base_model = 'black-forest-labs/FLUX.1-dev'
11
- controlnet_model_union = 'InstantX/FLUX.1-dev-Controlnet-Union'
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
- # If you encounter issues with CUDA, you can run this on the CPU for debugging
19
- pipe.to("cuda" if torch.cuda.is_available() else "cpu")
 
20
 
21
- control_modes = [
22
- "canny",
23
- "tile",
24
- "depth",
25
- "blur",
26
- "pose",
27
- "gray",
28
- "lq"
29
- ]
 
30
 
31
- def adjust_dimensions(width, height):
32
- adjusted_width = width - (width % 8)
33
- adjusted_height = height - (height % 8)
34
- return adjusted_width, adjusted_height
35
-
36
- @spaces.GPU
37
- def generate_image(prompt, control_image_depth, control_mode_depth_index, use_depth, control_image_canny, control_mode_canny_index):
38
- control_images = []
39
- control_modes = []
40
- conditioning_scales = []
41
-
42
- if use_depth:
43
- control_images.append(control_image_depth)
44
- control_modes.append(control_mode_depth_index)
45
- conditioning_scales.append(0.2)
46
 
47
- control_images.append(control_image_canny)
48
- control_modes.append(control_mode_canny_index)
49
- conditioning_scales.append(0.4)
 
 
 
50
 
51
- width, height = control_image_canny.shape[:2]
52
- adjusted_width, adjusted_height = adjust_dimensions(width, height)
 
53
 
54
- try:
55
- image = pipe(
56
- prompt,
57
- control_image=control_images,
58
- control_mode=control_modes,
59
- width=adjusted_width,
60
- height=adjusted_height,
61
- controlnet_conditioning_scale=conditioning_scales,
62
- num_inference_steps=24,
63
- guidance_scale=3.5,
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
- iface = gr.Interface(
73
  fn=generate_image,
74
  inputs=[
75
- gr.Text(label="Prompt"),
76
- gr.Image(label="Control Image (Depth)"),
77
- gr.Dropdown(choices=control_modes, value=control_modes.index("depth"), label="Control Mode (Depth)"),
78
- gr.Checkbox(label="Use Depth Control Image", value=True),
79
- gr.Image(label="Control Image (Canny)"),
80
- gr.Dropdown(choices=control_modes, value=control_modes.index("canny"), label="Control Mode (Canny)")
 
 
 
 
81
  ],
82
- outputs=gr.Image(label="Generated Image"),
83
- title="FluxControlNet Image Generation",
84
- description="Generate an image using FluxControlNet with depth and canny control images.",
85
  )
86
 
87
- iface.launch(share=True)
 
 
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()