kadirnar commited on
Commit
8dce1f7
1 Parent(s): 57e53cf

Delete diffusion_webui

Browse files
Files changed (37) hide show
  1. diffusion_webui/__init__.py +0 -32
  2. diffusion_webui/app.py +0 -77
  3. diffusion_webui/diffusion_models/__init__.py +0 -0
  4. diffusion_webui/diffusion_models/controlnet/__init__.py +0 -39
  5. diffusion_webui/diffusion_models/controlnet/controlnet_canny.py +0 -183
  6. diffusion_webui/diffusion_models/controlnet/controlnet_depth.py +0 -187
  7. diffusion_webui/diffusion_models/controlnet/controlnet_hed.py +0 -181
  8. diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/__init__.py +0 -21
  9. diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_canny.py +0 -231
  10. diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_depth.py +0 -228
  11. diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_hed.py +0 -223
  12. diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_mlsd.py +0 -224
  13. diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_pose.py +0 -225
  14. diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_scribble.py +0 -231
  15. diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_seg.py +0 -403
  16. diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/pipeline_stable_diffusion_controlnet_inpaint.py +0 -521
  17. diffusion_webui/diffusion_models/controlnet/controlnet_lineart.py +0 -178
  18. diffusion_webui/diffusion_models/controlnet/controlnet_lineart_anime.py +0 -191
  19. diffusion_webui/diffusion_models/controlnet/controlnet_mlsd.py +0 -185
  20. diffusion_webui/diffusion_models/controlnet/controlnet_normal.py +0 -193
  21. diffusion_webui/diffusion_models/controlnet/controlnet_pix2pix.py +0 -174
  22. diffusion_webui/diffusion_models/controlnet/controlnet_pose.py +0 -189
  23. diffusion_webui/diffusion_models/controlnet/controlnet_scribble.py +0 -188
  24. diffusion_webui/diffusion_models/controlnet/controlnet_seg.py +0 -353
  25. diffusion_webui/diffusion_models/controlnet/controlnet_shuffle.py +0 -176
  26. diffusion_webui/diffusion_models/controlnet/controlnet_softedge.py +0 -179
  27. diffusion_webui/diffusion_models/stable_diffusion/__init__.py +0 -9
  28. diffusion_webui/diffusion_models/stable_diffusion/img2img_app.py +0 -153
  29. diffusion_webui/diffusion_models/stable_diffusion/inpaint_app.py +0 -148
  30. diffusion_webui/diffusion_models/stable_diffusion/text2img_app.py +0 -167
  31. diffusion_webui/helpers.py +0 -25
  32. diffusion_webui/upscaler_models/__init__.py +0 -3
  33. diffusion_webui/upscaler_models/codeformer_upscaler.py +0 -81
  34. diffusion_webui/utils/__init__.py +0 -0
  35. diffusion_webui/utils/data_utils.py +0 -12
  36. diffusion_webui/utils/model_list.py +0 -78
  37. diffusion_webui/utils/scheduler_list.py +0 -47
diffusion_webui/__init__.py DELETED
@@ -1,32 +0,0 @@
1
- from diffusion_webui.diffusion_models.controlnet import (
2
- StableDiffusionControlNetCannyGenerator,
3
- StableDiffusionControlNetDepthGenerator,
4
- StableDiffusionControlNetHEDGenerator,
5
- StableDiffusionControlNetLineArtAnimeGenerator,
6
- StableDiffusionControlNetLineArtGenerator,
7
- StableDiffusionControlNetMLSDGenerator,
8
- StableDiffusionControlNetNormalGenerator,
9
- StableDiffusionControlNetPix2PixGenerator,
10
- StableDiffusionControlNetPoseGenerator,
11
- StableDiffusionControlNetScribbleGenerator,
12
- StableDiffusionControlNetSegGenerator,
13
- StableDiffusionControlNetShuffleGenerator,
14
- StableDiffusionControlNetSoftEdgeGenerator,
15
- )
16
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint import (
17
- StableDiffusionControlInpaintNetDepthGenerator,
18
- StableDiffusionControlNetInpaintCannyGenerator,
19
- StableDiffusionControlNetInpaintHedGenerator,
20
- StableDiffusionControlNetInpaintMlsdGenerator,
21
- StableDiffusionControlNetInpaintPoseGenerator,
22
- StableDiffusionControlNetInpaintScribbleGenerator,
23
- StableDiffusionControlNetInpaintSegGenerator,
24
- )
25
- from diffusion_webui.diffusion_models.stable_diffusion import (
26
- StableDiffusionImage2ImageGenerator,
27
- StableDiffusionInpaintGenerator,
28
- StableDiffusionText2ImageGenerator,
29
- )
30
- from diffusion_webui.upscaler_models import CodeformerUpscalerGenerator
31
-
32
- __version__ = "2.4.0"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/app.py DELETED
@@ -1,77 +0,0 @@
1
- import gradio as gr
2
-
3
- from diffusion_webui import (
4
- CodeformerUpscalerGenerator,
5
- StableDiffusionControlInpaintNetDepthGenerator,
6
- StableDiffusionControlNetCannyGenerator,
7
- StableDiffusionControlNetDepthGenerator,
8
- StableDiffusionControlNetHEDGenerator,
9
- StableDiffusionControlNetInpaintCannyGenerator,
10
- StableDiffusionControlNetInpaintHedGenerator,
11
- StableDiffusionControlNetInpaintMlsdGenerator,
12
- StableDiffusionControlNetInpaintPoseGenerator,
13
- StableDiffusionControlNetInpaintScribbleGenerator,
14
- StableDiffusionControlNetInpaintSegGenerator,
15
- StableDiffusionControlNetMLSDGenerator,
16
- StableDiffusionControlNetPoseGenerator,
17
- StableDiffusionControlNetScribbleGenerator,
18
- StableDiffusionControlNetSegGenerator,
19
- StableDiffusionImage2ImageGenerator,
20
- StableDiffusionInpaintGenerator,
21
- StableDiffusionText2ImageGenerator,
22
- StableDiffusionControlNetNormalGenerator,
23
- )
24
-
25
-
26
- def diffusion_app():
27
- app = gr.Blocks()
28
- with app:
29
- with gr.Row():
30
- with gr.Column():
31
- with gr.Tab("Text2Img"):
32
- StableDiffusionText2ImageGenerator.app()
33
- with gr.Tab("Img2Img"):
34
- StableDiffusionImage2ImageGenerator.app()
35
- with gr.Tab("Inpaint"):
36
- StableDiffusionInpaintGenerator.app()
37
- with gr.Tab("ControlNet"):
38
- with gr.Tab("Canny"):
39
- StableDiffusionControlNetCannyGenerator.app()
40
- with gr.Tab("Depth"):
41
- StableDiffusionControlNetDepthGenerator.app()
42
- with gr.Tab("HED"):
43
- StableDiffusionControlNetHEDGenerator.app()
44
- with gr.Tab("MLSD"):
45
- StableDiffusionControlNetMLSDGenerator.app()
46
- with gr.Tab("Pose"):
47
- StableDiffusionControlNetPoseGenerator.app()
48
- with gr.Tab("Scribble"):
49
- StableDiffusionControlNetScribbleGenerator.app()
50
- with gr.Tab("Normal"):
51
- StableDiffusionControlNetNormalGenerator.app()
52
- with gr.Tab("Seg"):
53
- StableDiffusionControlNetSegGenerator.app()
54
- with gr.Tab("ControlNet Inpaint"):
55
- with gr.Tab("Canny"):
56
- StableDiffusionControlNetInpaintCannyGenerator.app()
57
- with gr.Tab("Depth"):
58
- StableDiffusionControlInpaintNetDepthGenerator.app()
59
- with gr.Tab("HED"):
60
- StableDiffusionControlNetInpaintHedGenerator.app()
61
- with gr.Tab("MLSD"):
62
- StableDiffusionControlNetInpaintMlsdGenerator.app()
63
- with gr.Tab("Pose"):
64
- StableDiffusionControlNetInpaintPoseGenerator.app()
65
- with gr.Tab("Scribble"):
66
- StableDiffusionControlNetInpaintScribbleGenerator.app()
67
- with gr.Tab("Seg"):
68
- StableDiffusionControlNetInpaintSegGenerator.app()
69
- with gr.Tab("Upscaler"):
70
- CodeformerUpscalerGenerator.app()
71
-
72
- app.queue(concurrency_count=2)
73
- app.launch(debug=True, enable_queue=True)
74
-
75
-
76
- if __name__ == "__main__":
77
- diffusion_app()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/__init__.py DELETED
File without changes
diffusion_webui/diffusion_models/controlnet/__init__.py DELETED
@@ -1,39 +0,0 @@
1
- from diffusion_webui.diffusion_models.controlnet.controlnet_canny import (
2
- StableDiffusionControlNetCannyGenerator,
3
- )
4
- from diffusion_webui.diffusion_models.controlnet.controlnet_depth import (
5
- StableDiffusionControlNetDepthGenerator,
6
- )
7
- from diffusion_webui.diffusion_models.controlnet.controlnet_hed import (
8
- StableDiffusionControlNetHEDGenerator,
9
- )
10
- from diffusion_webui.diffusion_models.controlnet.controlnet_lineart import (
11
- StableDiffusionControlNetLineArtGenerator,
12
- )
13
- from diffusion_webui.diffusion_models.controlnet.controlnet_lineart_anime import (
14
- StableDiffusionControlNetLineArtAnimeGenerator,
15
- )
16
- from diffusion_webui.diffusion_models.controlnet.controlnet_mlsd import (
17
- StableDiffusionControlNetMLSDGenerator,
18
- )
19
- from diffusion_webui.diffusion_models.controlnet.controlnet_normal import (
20
- StableDiffusionControlNetNormalGenerator,
21
- )
22
- from diffusion_webui.diffusion_models.controlnet.controlnet_pix2pix import (
23
- StableDiffusionControlNetPix2PixGenerator,
24
- )
25
- from diffusion_webui.diffusion_models.controlnet.controlnet_pose import (
26
- StableDiffusionControlNetPoseGenerator,
27
- )
28
- from diffusion_webui.diffusion_models.controlnet.controlnet_scribble import (
29
- StableDiffusionControlNetScribbleGenerator,
30
- )
31
- from diffusion_webui.diffusion_models.controlnet.controlnet_seg import (
32
- StableDiffusionControlNetSegGenerator,
33
- )
34
- from diffusion_webui.diffusion_models.controlnet.controlnet_shuffle import (
35
- StableDiffusionControlNetShuffleGenerator,
36
- )
37
- from diffusion_webui.diffusion_models.controlnet.controlnet_softedge import (
38
- StableDiffusionControlNetSoftEdgeGenerator,
39
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_canny.py DELETED
@@ -1,183 +0,0 @@
1
- import cv2
2
- import gradio as gr
3
- import numpy as np
4
- import torch
5
- from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
6
- from PIL import Image
7
-
8
- from diffusion_webui.utils.model_list import (
9
- controlnet_canny_model_list,
10
- stable_model_list,
11
- )
12
- from diffusion_webui.utils.scheduler_list import (
13
- SCHEDULER_LIST,
14
- get_scheduler_list,
15
- )
16
-
17
-
18
- class StableDiffusionControlNetCannyGenerator:
19
- def __init__(self):
20
- self.pipe = None
21
-
22
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
23
- if self.pipe is None:
24
- controlnet = ControlNetModel.from_pretrained(
25
- controlnet_model_path, torch_dtype=torch.float16
26
- )
27
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
28
- pretrained_model_name_or_path=stable_model_path,
29
- controlnet=controlnet,
30
- safety_checker=None,
31
- torch_dtype=torch.float16,
32
- )
33
-
34
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
35
- self.pipe.to("cuda")
36
- self.pipe.enable_xformers_memory_efficient_attention()
37
-
38
- return self.pipe
39
-
40
- def controlnet_canny(
41
- self,
42
- image_path: str,
43
- ):
44
- image = Image.open(image_path)
45
- image = np.array(image)
46
-
47
- image = cv2.Canny(image, 100, 200)
48
- image = image[:, :, None]
49
- image = np.concatenate([image, image, image], axis=2)
50
- image = Image.fromarray(image)
51
-
52
- return image
53
-
54
- def generate_image(
55
- self,
56
- image_path: str,
57
- stable_model_path: str,
58
- controlnet_model_path: str,
59
- prompt: str,
60
- negative_prompt: str,
61
- num_images_per_prompt: int,
62
- guidance_scale: int,
63
- num_inference_step: int,
64
- scheduler: str,
65
- seed_generator: int,
66
- ):
67
- pipe = self.load_model(
68
- stable_model_path=stable_model_path,
69
- controlnet_model_path=controlnet_model_path,
70
- scheduler=scheduler,
71
- )
72
-
73
- image = self.controlnet_canny(image_path=image_path)
74
-
75
- if seed_generator == 0:
76
- random_seed = torch.randint(0, 1000000, (1,))
77
- generator = torch.manual_seed(random_seed)
78
- else:
79
- generator = torch.manual_seed(seed_generator)
80
-
81
- output = pipe(
82
- prompt=prompt,
83
- image=image,
84
- negative_prompt=negative_prompt,
85
- num_images_per_prompt=num_images_per_prompt,
86
- num_inference_steps=num_inference_step,
87
- guidance_scale=guidance_scale,
88
- generator=generator,
89
- ).images
90
-
91
- return output
92
-
93
- def app():
94
- with gr.Blocks():
95
- with gr.Row():
96
- with gr.Column():
97
- controlnet_canny_image_file = gr.Image(
98
- type="filepath", label="Image"
99
- )
100
-
101
- controlnet_canny_prompt = gr.Textbox(
102
- lines=1,
103
- placeholder="Prompt",
104
- show_label=False,
105
- )
106
-
107
- controlnet_canny_negative_prompt = gr.Textbox(
108
- lines=1,
109
- placeholder="Negative Prompt",
110
- show_label=False,
111
- )
112
- with gr.Row():
113
- with gr.Column():
114
- controlnet_canny_stable_model_id = gr.Dropdown(
115
- choices=stable_model_list,
116
- value=stable_model_list[0],
117
- label="Stable Model Id",
118
- )
119
-
120
- controlnet_canny_guidance_scale = gr.Slider(
121
- minimum=0.1,
122
- maximum=15,
123
- step=0.1,
124
- value=7.5,
125
- label="Guidance Scale",
126
- )
127
- controlnet_canny_num_inference_step = gr.Slider(
128
- minimum=1,
129
- maximum=100,
130
- step=1,
131
- value=50,
132
- label="Num Inference Step",
133
- )
134
- controlnet_canny_num_images_per_prompt = gr.Slider(
135
- minimum=1,
136
- maximum=10,
137
- step=1,
138
- value=1,
139
- label="Number Of Images",
140
- )
141
- with gr.Row():
142
- with gr.Column():
143
- controlnet_canny_model_id = gr.Dropdown(
144
- choices=controlnet_canny_model_list,
145
- value=controlnet_canny_model_list[0],
146
- label="ControlNet Model Id",
147
- )
148
-
149
- controlnet_canny_scheduler = gr.Dropdown(
150
- choices=SCHEDULER_LIST,
151
- value=SCHEDULER_LIST[0],
152
- label="Scheduler",
153
- )
154
-
155
- controlnet_canny_seed_generator = gr.Number(
156
- value=0,
157
- label="Seed Generator",
158
- )
159
- controlnet_canny_predict = gr.Button(value="Generator")
160
-
161
- with gr.Column():
162
- output_image = gr.Gallery(
163
- label="Generated images",
164
- show_label=False,
165
- elem_id="gallery",
166
- ).style(grid=(1, 2))
167
-
168
- controlnet_canny_predict.click(
169
- fn=StableDiffusionControlNetCannyGenerator().generate_image,
170
- inputs=[
171
- controlnet_canny_image_file,
172
- controlnet_canny_stable_model_id,
173
- controlnet_canny_model_id,
174
- controlnet_canny_prompt,
175
- controlnet_canny_negative_prompt,
176
- controlnet_canny_num_images_per_prompt,
177
- controlnet_canny_guidance_scale,
178
- controlnet_canny_num_inference_step,
179
- controlnet_canny_scheduler,
180
- controlnet_canny_seed_generator,
181
- ],
182
- outputs=[output_image],
183
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_depth.py DELETED
@@ -1,187 +0,0 @@
1
- import gradio as gr
2
- import numpy as np
3
- import torch
4
- from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
5
- from PIL import Image
6
- from transformers import pipeline
7
-
8
- from diffusion_webui.utils.model_list import (
9
- controlnet_depth_model_list,
10
- stable_model_list,
11
- )
12
- from diffusion_webui.utils.scheduler_list import (
13
- SCHEDULER_LIST,
14
- get_scheduler_list,
15
- )
16
-
17
-
18
- class StableDiffusionControlNetDepthGenerator:
19
- def __init__(self):
20
- self.pipe = None
21
-
22
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
23
- if self.pipe is None:
24
- controlnet = ControlNetModel.from_pretrained(
25
- controlnet_model_path, torch_dtype=torch.float16
26
- )
27
-
28
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
29
- pretrained_model_name_or_path=stable_model_path,
30
- controlnet=controlnet,
31
- safety_checker=None,
32
- torch_dtype=torch.float16,
33
- )
34
-
35
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
36
- self.pipe.to("cuda")
37
- self.pipe.enable_xformers_memory_efficient_attention()
38
-
39
- return self.pipe
40
-
41
- def controlnet_depth(self, image_path: str):
42
- depth_estimator = pipeline("depth-estimation")
43
- image = Image.open(image_path)
44
- image = depth_estimator(image)["depth"]
45
- image = np.array(image)
46
- image = image[:, :, None]
47
- image = np.concatenate([image, image, image], axis=2)
48
- image = Image.fromarray(image)
49
-
50
- return image
51
-
52
- def generate_image(
53
- self,
54
- image_path: str,
55
- stable_model_path: str,
56
- depth_model_path: str,
57
- prompt: str,
58
- negative_prompt: str,
59
- num_images_per_prompt: int,
60
- guidance_scale: int,
61
- num_inference_step: int,
62
- scheduler: str,
63
- seed_generator: int,
64
- ):
65
- image = self.controlnet_depth(image_path)
66
-
67
- pipe = self.load_model(
68
- stable_model_path=stable_model_path,
69
- controlnet_model_path=depth_model_path,
70
- scheduler=scheduler,
71
- )
72
-
73
- if seed_generator == 0:
74
- random_seed = torch.randint(0, 1000000, (1,))
75
- generator = torch.manual_seed(random_seed)
76
- else:
77
- generator = torch.manual_seed(seed_generator)
78
-
79
- output = pipe(
80
- prompt=prompt,
81
- image=image,
82
- negative_prompt=negative_prompt,
83
- num_images_per_prompt=num_images_per_prompt,
84
- num_inference_steps=num_inference_step,
85
- guidance_scale=guidance_scale,
86
- generator=generator,
87
- ).images
88
-
89
- return output
90
-
91
- def app():
92
- with gr.Blocks():
93
- with gr.Row():
94
- with gr.Column():
95
- controlnet_depth_image_file = gr.Image(
96
- type="filepath", label="Image"
97
- )
98
-
99
- controlnet_depth_prompt = gr.Textbox(
100
- lines=1,
101
- show_label=False,
102
- placeholder="Prompt",
103
- )
104
-
105
- controlnet_depth_negative_prompt = gr.Textbox(
106
- lines=1,
107
- show_label=False,
108
- placeholder="Negative Prompt",
109
- )
110
-
111
- with gr.Row():
112
- with gr.Column():
113
- controlnet_depth_stable_model_id = gr.Dropdown(
114
- choices=stable_model_list,
115
- value=stable_model_list[0],
116
- label="Stable Model Id",
117
- )
118
- controlnet_depth_guidance_scale = gr.Slider(
119
- minimum=0.1,
120
- maximum=15,
121
- step=0.1,
122
- value=7.5,
123
- label="Guidance Scale",
124
- )
125
-
126
- controlnet_depth_num_inference_step = gr.Slider(
127
- minimum=1,
128
- maximum=100,
129
- step=1,
130
- value=50,
131
- label="Num Inference Step",
132
- )
133
-
134
- controlnet_depth_num_images_per_prompt = gr.Slider(
135
- minimum=1,
136
- maximum=10,
137
- step=1,
138
- value=1,
139
- label="Number Of Images",
140
- )
141
- with gr.Row():
142
- with gr.Column():
143
- controlnet_depth_model_id = gr.Dropdown(
144
- choices=controlnet_depth_model_list,
145
- value=controlnet_depth_model_list[0],
146
- label="ControlNet Model Id",
147
- )
148
-
149
- controlnet_depth_scheduler = gr.Dropdown(
150
- choices=SCHEDULER_LIST,
151
- value=SCHEDULER_LIST[0],
152
- label="Scheduler",
153
- )
154
-
155
- controlnet_depth_seed_generator = gr.Number(
156
- minimum=0,
157
- maximum=1000000,
158
- step=1,
159
- value=0,
160
- label="Seed Generator",
161
- )
162
-
163
- controlnet_depth_predict = gr.Button(value="Generator")
164
-
165
- with gr.Column():
166
- output_image = gr.Gallery(
167
- label="Generated images",
168
- show_label=False,
169
- elem_id="gallery",
170
- ).style(grid=(1, 2))
171
-
172
- controlnet_depth_predict.click(
173
- fn=StableDiffusionControlNetDepthGenerator().generate_image,
174
- inputs=[
175
- controlnet_depth_image_file,
176
- controlnet_depth_stable_model_id,
177
- controlnet_depth_model_id,
178
- controlnet_depth_prompt,
179
- controlnet_depth_negative_prompt,
180
- controlnet_depth_num_images_per_prompt,
181
- controlnet_depth_guidance_scale,
182
- controlnet_depth_num_inference_step,
183
- controlnet_depth_scheduler,
184
- controlnet_depth_seed_generator,
185
- ],
186
- outputs=output_image,
187
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_hed.py DELETED
@@ -1,181 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- from controlnet_aux import HEDdetector
4
- from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
5
- from PIL import Image
6
-
7
- from diffusion_webui.utils.model_list import (
8
- controlnet_hed_model_list,
9
- stable_model_list,
10
- )
11
- from diffusion_webui.utils.scheduler_list import (
12
- SCHEDULER_LIST,
13
- get_scheduler_list,
14
- )
15
-
16
-
17
- class StableDiffusionControlNetHEDGenerator:
18
- def __init__(self):
19
- self.pipe = None
20
-
21
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
22
- if self.pipe is None:
23
- controlnet = ControlNetModel.from_pretrained(
24
- controlnet_model_path, torch_dtype=torch.float16
25
- )
26
-
27
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
28
- pretrained_model_name_or_path=stable_model_path,
29
- controlnet=controlnet,
30
- safety_checker=None,
31
- torch_dtype=torch.float16,
32
- )
33
-
34
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
35
- self.pipe.to("cuda")
36
- self.pipe.enable_xformers_memory_efficient_attention()
37
-
38
- return self.pipe
39
-
40
- def controlnet_hed(self, image_path: str):
41
- hed = HEDdetector.from_pretrained("lllyasviel/ControlNet")
42
- image = Image.open(image_path)
43
- image = hed(image)
44
-
45
- return image
46
-
47
- def generate_image(
48
- self,
49
- image_path: str,
50
- stable_model_path: str,
51
- controlnet_hed_model_path: str,
52
- prompt: str,
53
- negative_prompt: str,
54
- num_images_per_prompt: int,
55
- guidance_scale: int,
56
- num_inference_step: int,
57
- sheduler: str,
58
- seed_generator: int,
59
- ):
60
-
61
- image = self.controlnet_hed(image_path=image_path)
62
-
63
- pipe = self.load_model(
64
- stable_model_path=stable_model_path,
65
- controlnet_model_path=controlnet_hed_model_path,
66
- scheduler=sheduler,
67
- )
68
-
69
- if seed_generator == 0:
70
- random_seed = torch.randint(0, 1000000, (1,))
71
- generator = torch.manual_seed(random_seed)
72
- else:
73
- generator = torch.manual_seed(seed_generator)
74
-
75
- output = pipe(
76
- prompt=prompt,
77
- image=image,
78
- negative_prompt=negative_prompt,
79
- num_images_per_prompt=num_images_per_prompt,
80
- num_inference_steps=num_inference_step,
81
- guidance_scale=guidance_scale,
82
- generator=generator,
83
- ).images
84
-
85
- return output
86
-
87
- def app():
88
- with gr.Blocks():
89
- with gr.Row():
90
- with gr.Column():
91
- controlnet_hed_image_file = gr.Image(
92
- type="filepath", label="Image"
93
- )
94
- controlnet_hed_prompt = gr.Textbox(
95
- lines=1,
96
- show_label=False,
97
- placeholder="Prompt",
98
- )
99
-
100
- controlnet_hed_negative_prompt = gr.Textbox(
101
- lines=1,
102
- show_label=False,
103
- placeholder="Negative Prompt",
104
- )
105
-
106
- with gr.Row():
107
- with gr.Column():
108
- controlnet_hed_stable_model_id = gr.Dropdown(
109
- choices=stable_model_list,
110
- value=stable_model_list[0],
111
- label="Stable Model Id",
112
- )
113
- controlnet_hed_guidance_scale = gr.Slider(
114
- minimum=0.1,
115
- maximum=15,
116
- step=0.1,
117
- value=7.5,
118
- label="Guidance Scale",
119
- )
120
- controlnet_hed_num_inference_step = gr.Slider(
121
- minimum=1,
122
- maximum=100,
123
- step=1,
124
- value=50,
125
- label="Num Inference Step",
126
- )
127
-
128
- controlnet_hed_num_images_per_prompt = gr.Slider(
129
- minimum=1,
130
- maximum=10,
131
- step=1,
132
- value=1,
133
- label="Number Of Images",
134
- )
135
-
136
- with gr.Row():
137
- with gr.Column():
138
- controlnet_hed_model_id = gr.Dropdown(
139
- choices=controlnet_hed_model_list,
140
- value=controlnet_hed_model_list[0],
141
- label="ControlNet Model Id",
142
- )
143
- controlnet_hed_scheduler = gr.Dropdown(
144
- choices=SCHEDULER_LIST,
145
- value=SCHEDULER_LIST[0],
146
- label="Scheduler",
147
- )
148
-
149
- controlnet_hed_seed_generator = gr.Number(
150
- minimum=0,
151
- maximum=1000000,
152
- step=1,
153
- value=0,
154
- label="Seed Generator",
155
- )
156
-
157
- controlnet_hed_predict = gr.Button(value="Generator")
158
-
159
- with gr.Column():
160
- output_image = gr.Gallery(
161
- label="Generated images",
162
- show_label=False,
163
- elem_id="gallery",
164
- ).style(grid=(1, 2))
165
-
166
- controlnet_hed_predict.click(
167
- fn=StableDiffusionControlNetHEDGenerator().generate_image,
168
- inputs=[
169
- controlnet_hed_image_file,
170
- controlnet_hed_stable_model_id,
171
- controlnet_hed_model_id,
172
- controlnet_hed_prompt,
173
- controlnet_hed_negative_prompt,
174
- controlnet_hed_num_images_per_prompt,
175
- controlnet_hed_guidance_scale,
176
- controlnet_hed_num_inference_step,
177
- controlnet_hed_scheduler,
178
- controlnet_hed_seed_generator,
179
- ],
180
- outputs=[output_image],
181
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/__init__.py DELETED
@@ -1,21 +0,0 @@
1
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint.controlnet_inpaint_canny import (
2
- StableDiffusionControlNetInpaintCannyGenerator,
3
- )
4
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint.controlnet_inpaint_depth import (
5
- StableDiffusionControlInpaintNetDepthGenerator,
6
- )
7
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint.controlnet_inpaint_hed import (
8
- StableDiffusionControlNetInpaintHedGenerator,
9
- )
10
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint.controlnet_inpaint_mlsd import (
11
- StableDiffusionControlNetInpaintMlsdGenerator,
12
- )
13
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint.controlnet_inpaint_pose import (
14
- StableDiffusionControlNetInpaintPoseGenerator,
15
- )
16
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint.controlnet_inpaint_scribble import (
17
- StableDiffusionControlNetInpaintScribbleGenerator,
18
- )
19
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint.controlnet_inpaint_seg import (
20
- StableDiffusionControlNetInpaintSegGenerator,
21
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_canny.py DELETED
@@ -1,231 +0,0 @@
1
- import cv2
2
- import gradio as gr
3
- import numpy as np
4
- import torch
5
- from diffusers import ControlNetModel
6
- from PIL import Image
7
-
8
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint.pipeline_stable_diffusion_controlnet_inpaint import (
9
- StableDiffusionControlNetInpaintPipeline,
10
- )
11
- from diffusion_webui.utils.model_list import (
12
- controlnet_canny_model_list,
13
- stable_inpiant_model_list,
14
- )
15
- from diffusion_webui.utils.scheduler_list import (
16
- SCHEDULER_LIST,
17
- get_scheduler_list,
18
- )
19
-
20
- # https://github.com/mikonvergence/ControlNetInpaint
21
-
22
-
23
- class StableDiffusionControlNetInpaintCannyGenerator:
24
- def __init__(self):
25
- self.pipe = None
26
-
27
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
28
- if self.pipe is None:
29
- controlnet = ControlNetModel.from_pretrained(
30
- controlnet_model_path, torch_dtype=torch.float16
31
- )
32
- self.pipe = (
33
- StableDiffusionControlNetInpaintPipeline.from_pretrained(
34
- pretrained_model_name_or_path=stable_model_path,
35
- controlnet=controlnet,
36
- safety_checker=None,
37
- torch_dtype=torch.float16,
38
- )
39
- )
40
-
41
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
42
- self.pipe.to("cuda")
43
- self.pipe.enable_xformers_memory_efficient_attention()
44
-
45
- return self.pipe
46
-
47
- def load_image(self, image_path):
48
- image = np.array(image_path)
49
- image = Image.fromarray(image)
50
- return image
51
-
52
- def controlnet_canny_inpaint(
53
- self,
54
- image_path: str,
55
- ):
56
- image = image_path["image"].convert("RGB").resize((512, 512))
57
- image = np.array(image)
58
-
59
- image = cv2.Canny(image, 100, 200)
60
- image = image[:, :, None]
61
- image = np.concatenate([image, image, image], axis=2)
62
- image = Image.fromarray(image)
63
-
64
- return image
65
-
66
- def generate_image(
67
- self,
68
- image_path: str,
69
- stable_model_path: str,
70
- controlnet_model_path: str,
71
- prompt: str,
72
- negative_prompt: str,
73
- num_images_per_prompt: int,
74
- guidance_scale: int,
75
- num_inference_step: int,
76
- controlnet_conditioning_scale: int,
77
- scheduler: str,
78
- seed_generator: int,
79
- ):
80
-
81
- normal_image = image_path["image"].convert("RGB").resize((512, 512))
82
- mask_image = image_path["mask"].convert("RGB").resize((512, 512))
83
-
84
- normal_image = self.load_image(image_path=normal_image)
85
- mask_image = self.load_image(image_path=mask_image)
86
-
87
- control_image = self.controlnet_canny_inpaint(image_path=image_path)
88
- pipe = self.load_model(
89
- stable_model_path=stable_model_path,
90
- controlnet_model_path=controlnet_model_path,
91
- scheduler=scheduler,
92
- )
93
-
94
- if seed_generator == 0:
95
- random_seed = torch.randint(0, 1000000, (1,))
96
- generator = torch.manual_seed(random_seed)
97
- else:
98
- generator = torch.manual_seed(seed_generator)
99
-
100
- output = pipe(
101
- prompt=prompt,
102
- image=normal_image,
103
- mask_image=mask_image,
104
- control_image=control_image,
105
- negative_prompt=negative_prompt,
106
- num_images_per_prompt=num_images_per_prompt,
107
- num_inference_steps=num_inference_step,
108
- guidance_scale=guidance_scale,
109
- controlnet_conditioning_scale=controlnet_conditioning_scale,
110
- generator=generator,
111
- ).images
112
-
113
- return output
114
-
115
- def app():
116
- with gr.Blocks():
117
- with gr.Row():
118
- with gr.Column():
119
- controlnet_canny_inpaint_image_file = gr.Image(
120
- source="upload",
121
- tool="sketch",
122
- elem_id="image_upload",
123
- type="pil",
124
- label="Upload",
125
- )
126
-
127
- controlnet_canny_inpaint_prompt = gr.Textbox(
128
- lines=1, placeholder="Prompt", show_label=False
129
- )
130
-
131
- controlnet_canny_inpaint_negative_prompt = gr.Textbox(
132
- lines=1,
133
- show_label=False,
134
- placeholder="Negative Prompt",
135
- )
136
- with gr.Row():
137
- with gr.Column():
138
- controlnet_canny_inpaint_stable_model_id = (
139
- gr.Dropdown(
140
- choices=stable_inpiant_model_list,
141
- value=stable_inpiant_model_list[0],
142
- label="Stable Model Id",
143
- )
144
- )
145
-
146
- controlnet_canny_inpaint_guidance_scale = gr.Slider(
147
- minimum=0.1,
148
- maximum=15,
149
- step=0.1,
150
- value=7.5,
151
- label="Guidance Scale",
152
- )
153
-
154
- controlnet_canny_inpaint_num_inference_step = (
155
- gr.Slider(
156
- minimum=1,
157
- maximum=100,
158
- step=1,
159
- value=50,
160
- label="Num Inference Step",
161
- )
162
- )
163
- controlnet_canny_inpaint_num_images_per_prompt = (
164
- gr.Slider(
165
- minimum=1,
166
- maximum=10,
167
- step=1,
168
- value=1,
169
- label="Number Of Images",
170
- )
171
- )
172
- with gr.Row():
173
- with gr.Column():
174
- controlnet_canny_inpaint_model_id = gr.Dropdown(
175
- choices=controlnet_canny_model_list,
176
- value=controlnet_canny_model_list[0],
177
- label="Controlnet Model Id",
178
- )
179
- controlnet_canny_inpaint_scheduler = (
180
- gr.Dropdown(
181
- choices=SCHEDULER_LIST,
182
- value=SCHEDULER_LIST[0],
183
- label="Scheduler",
184
- )
185
- )
186
- controlnet_canny_inpaint_controlnet_conditioning_scale = gr.Slider(
187
- minimum=0.1,
188
- maximum=1.0,
189
- step=0.1,
190
- value=0.5,
191
- label="Controlnet Conditioning Scale",
192
- )
193
-
194
- controlnet_canny_inpaint_seed_generator = (
195
- gr.Slider(
196
- minimum=0,
197
- maximum=1000000,
198
- step=1,
199
- value=0,
200
- label="Seed Generator",
201
- )
202
- )
203
-
204
- controlnet_canny_inpaint_predict = gr.Button(
205
- value="Generator"
206
- )
207
-
208
- with gr.Column():
209
- output_image = gr.Gallery(
210
- label="Generated images",
211
- show_label=False,
212
- elem_id="gallery",
213
- ).style(grid=(1, 2))
214
-
215
- controlnet_canny_inpaint_predict.click(
216
- fn=StableDiffusionControlNetInpaintCannyGenerator().generate_image,
217
- inputs=[
218
- controlnet_canny_inpaint_image_file,
219
- controlnet_canny_inpaint_stable_model_id,
220
- controlnet_canny_inpaint_model_id,
221
- controlnet_canny_inpaint_prompt,
222
- controlnet_canny_inpaint_negative_prompt,
223
- controlnet_canny_inpaint_num_images_per_prompt,
224
- controlnet_canny_inpaint_guidance_scale,
225
- controlnet_canny_inpaint_num_inference_step,
226
- controlnet_canny_inpaint_controlnet_conditioning_scale,
227
- controlnet_canny_inpaint_scheduler,
228
- controlnet_canny_inpaint_seed_generator,
229
- ],
230
- outputs=[output_image],
231
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_depth.py DELETED
@@ -1,228 +0,0 @@
1
- import gradio as gr
2
- import numpy as np
3
- import torch
4
- from diffusers import ControlNetModel
5
- from PIL import Image
6
- from transformers import pipeline
7
-
8
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint.pipeline_stable_diffusion_controlnet_inpaint import (
9
- StableDiffusionControlNetInpaintPipeline,
10
- )
11
- from diffusion_webui.utils.model_list import (
12
- controlnet_depth_model_list,
13
- stable_inpiant_model_list,
14
- )
15
- from diffusion_webui.utils.scheduler_list import (
16
- SCHEDULER_LIST,
17
- get_scheduler_list,
18
- )
19
-
20
- # https://github.com/mikonvergence/ControlNetInpaint
21
-
22
-
23
- class StableDiffusionControlInpaintNetDepthGenerator:
24
- def __init__(self):
25
- self.pipe = None
26
-
27
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
28
- if self.pipe is None:
29
- controlnet = ControlNetModel.from_pretrained(
30
- controlnet_model_path, torch_dtype=torch.float16
31
- )
32
- self.pipe = (
33
- StableDiffusionControlNetInpaintPipeline.from_pretrained(
34
- pretrained_model_name_or_path=stable_model_path,
35
- controlnet=controlnet,
36
- safety_checker=None,
37
- torch_dtype=torch.float16,
38
- )
39
- )
40
-
41
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
42
- self.pipe.to("cuda")
43
- self.pipe.enable_xformers_memory_efficient_attention()
44
-
45
- return self.pipe
46
-
47
- def load_image(self, image_path):
48
- image = np.array(image_path)
49
- image = Image.fromarray(image)
50
- return image
51
-
52
- def controlnet_inpaint_depth(self, image_path: str):
53
- depth_estimator = pipeline("depth-estimation", model="Intel/dpt-hybrid-midas")
54
- image = image_path["image"].convert("RGB").resize((512, 512))
55
- image = depth_estimator(image)["depth"]
56
- image = np.array(image)
57
- image = image[:, :, None]
58
- image = np.concatenate([image, image, image], axis=2)
59
- image = Image.fromarray(image)
60
-
61
- return image
62
-
63
- def generate_image(
64
- self,
65
- image_path: str,
66
- stable_model_path: str,
67
- controlnet_model_path: str,
68
- prompt: str,
69
- negative_prompt: str,
70
- num_images_per_prompt: int,
71
- guidance_scale: int,
72
- num_inference_step: int,
73
- controlnet_conditioning_scale: int,
74
- scheduler: str,
75
- seed_generator: int,
76
- ):
77
- normal_image = image_path["image"].convert("RGB").resize((512, 512))
78
- mask_image = image_path["mask"].convert("RGB").resize((512, 512))
79
-
80
- normal_image = self.load_image(image_path=normal_image)
81
- mask_image = self.load_image(image_path=mask_image)
82
-
83
- control_image = self.controlnet_inpaint_depth(image_path=image_path)
84
-
85
- pipe = self.load_model(
86
- stable_model_path=stable_model_path,
87
- controlnet_model_path=controlnet_model_path,
88
- scheduler=scheduler,
89
- )
90
-
91
- if seed_generator == 0:
92
- random_seed = torch.randint(0, 1000000, (1,))
93
- generator = torch.manual_seed(random_seed)
94
- else:
95
- generator = torch.manual_seed(seed_generator)
96
-
97
- output = pipe(
98
- prompt=prompt,
99
- image=normal_image,
100
- mask_image=mask_image,
101
- control_image=control_image,
102
- negative_prompt=negative_prompt,
103
- num_images_per_prompt=num_images_per_prompt,
104
- num_inference_steps=num_inference_step,
105
- guidance_scale=guidance_scale,
106
- controlnet_conditioning_scale=controlnet_conditioning_scale,
107
- generator=generator,
108
- ).images
109
-
110
- return output
111
-
112
- def app():
113
- with gr.Blocks():
114
- with gr.Row():
115
- with gr.Column():
116
- controlnet_depth_inpaint_image_file = gr.Image(
117
- source="upload",
118
- tool="sketch",
119
- elem_id="image_upload",
120
- type="pil",
121
- label="Upload",
122
- )
123
-
124
- controlnet_depth_inpaint_prompt = gr.Textbox(
125
- lines=1, placeholder="Prompt", show_label=False
126
- )
127
-
128
- controlnet_depth_inpaint_negative_prompt = gr.Textbox(
129
- lines=1,
130
- show_label=False,
131
- placeholder="Negative Prompt",
132
- )
133
- with gr.Row():
134
- with gr.Column():
135
- controlnet_depth_inpaint_stable_model_id = (
136
- gr.Dropdown(
137
- choices=stable_inpiant_model_list,
138
- value=stable_inpiant_model_list[0],
139
- label="Stable Model Id",
140
- )
141
- )
142
-
143
- controlnet_depth_inpaint_guidance_scale = gr.Slider(
144
- minimum=0.1,
145
- maximum=15,
146
- step=0.1,
147
- value=7.5,
148
- label="Guidance Scale",
149
- )
150
-
151
- controlnet_depth_inpaint_num_inference_step = (
152
- gr.Slider(
153
- minimum=1,
154
- maximum=100,
155
- step=1,
156
- value=50,
157
- label="Num Inference Step",
158
- )
159
- )
160
- controlnet_depth_inpaint_num_images_per_prompt = (
161
- gr.Slider(
162
- minimum=1,
163
- maximum=10,
164
- step=1,
165
- value=1,
166
- label="Number Of Images",
167
- )
168
- )
169
- with gr.Row():
170
- with gr.Column():
171
- controlnet_depth_inpaint_model_id = gr.Dropdown(
172
- choices=controlnet_depth_model_list,
173
- value=controlnet_depth_model_list[0],
174
- label="Controlnet Model Id",
175
- )
176
- controlnet_depth_inpaint_scheduler = (
177
- gr.Dropdown(
178
- choices=SCHEDULER_LIST,
179
- value=SCHEDULER_LIST[0],
180
- label="Scheduler",
181
- )
182
- )
183
- controlnet_depth_inpaint_controlnet_conditioning_scale = gr.Slider(
184
- minimum=0.1,
185
- maximum=1.0,
186
- step=0.1,
187
- value=0.5,
188
- label="Controlnet Conditioning Scale",
189
- )
190
-
191
- controlnet_depth_inpaint_seed_generator = (
192
- gr.Slider(
193
- minimum=0,
194
- maximum=1000000,
195
- step=1,
196
- value=0,
197
- label="Seed Generator",
198
- )
199
- )
200
-
201
- controlnet_depth_inpaint_predict = gr.Button(
202
- value="Generator"
203
- )
204
-
205
- with gr.Column():
206
- output_image = gr.Gallery(
207
- label="Generated images",
208
- show_label=False,
209
- elem_id="gallery",
210
- ).style(grid=(1, 2))
211
-
212
- controlnet_depth_inpaint_predict.click(
213
- fn=StableDiffusionControlInpaintNetDepthGenerator().generate_image,
214
- inputs=[
215
- controlnet_depth_inpaint_image_file,
216
- controlnet_depth_inpaint_stable_model_id,
217
- controlnet_depth_inpaint_model_id,
218
- controlnet_depth_inpaint_prompt,
219
- controlnet_depth_inpaint_negative_prompt,
220
- controlnet_depth_inpaint_num_images_per_prompt,
221
- controlnet_depth_inpaint_guidance_scale,
222
- controlnet_depth_inpaint_num_inference_step,
223
- controlnet_depth_inpaint_controlnet_conditioning_scale,
224
- controlnet_depth_inpaint_scheduler,
225
- controlnet_depth_inpaint_seed_generator,
226
- ],
227
- outputs=[output_image],
228
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_hed.py DELETED
@@ -1,223 +0,0 @@
1
- import gradio as gr
2
- import numpy as np
3
- import torch
4
- from controlnet_aux import HEDdetector
5
- from diffusers import ControlNetModel
6
- from PIL import Image
7
-
8
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint.pipeline_stable_diffusion_controlnet_inpaint import (
9
- StableDiffusionControlNetInpaintPipeline,
10
- )
11
- from diffusion_webui.utils.model_list import (
12
- controlnet_hed_model_list,
13
- stable_inpiant_model_list,
14
- )
15
- from diffusion_webui.utils.scheduler_list import (
16
- SCHEDULER_LIST,
17
- get_scheduler_list,
18
- )
19
-
20
- # https://github.com/mikonvergence/ControlNetInpaint
21
-
22
-
23
- class StableDiffusionControlNetInpaintHedGenerator:
24
- def __init__(self):
25
- self.pipe = None
26
-
27
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
28
- if self.pipe is None:
29
- controlnet = ControlNetModel.from_pretrained(
30
- controlnet_model_path, torch_dtype=torch.float16
31
- )
32
- self.pipe = (
33
- StableDiffusionControlNetInpaintPipeline.from_pretrained(
34
- pretrained_model_name_or_path=stable_model_path,
35
- controlnet=controlnet,
36
- safety_checker=None,
37
- torch_dtype=torch.float16,
38
- )
39
- )
40
-
41
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
42
- self.pipe.to("cuda")
43
- self.pipe.enable_xformers_memory_efficient_attention()
44
-
45
- return self.pipe
46
-
47
- def load_image(self, image_path):
48
- image = np.array(image_path)
49
- image = Image.fromarray(image)
50
- return image
51
-
52
- def controlnet_inpaint_hed(self, image_path: str):
53
- hed = HEDdetector.from_pretrained("lllyasviel/ControlNet")
54
- image = image_path["image"].convert("RGB").resize((512, 512))
55
- image = np.array(image)
56
- image = hed(image)
57
-
58
- return image
59
-
60
- def generate_image(
61
- self,
62
- image_path: str,
63
- stable_model_path: str,
64
- controlnet_model_path: str,
65
- prompt: str,
66
- negative_prompt: str,
67
- num_images_per_prompt: int,
68
- guidance_scale: int,
69
- num_inference_step: int,
70
- controlnet_conditioning_scale: int,
71
- scheduler: str,
72
- seed_generator: int,
73
- ):
74
- normal_image = image_path["image"].convert("RGB").resize((512, 512))
75
- mask_image = image_path["mask"].convert("RGB").resize((512, 512))
76
-
77
- normal_image = self.load_image(image_path=normal_image)
78
- mask_image = self.load_image(image_path=mask_image)
79
-
80
- control_image = self.controlnet_inpaint_hed(image_path=image_path)
81
-
82
- pipe = self.load_model(
83
- stable_model_path=stable_model_path,
84
- controlnet_model_path=controlnet_model_path,
85
- scheduler=scheduler,
86
- )
87
-
88
- if seed_generator == 0:
89
- random_seed = torch.randint(0, 1000000, (1,))
90
- generator = torch.manual_seed(random_seed)
91
- else:
92
- generator = torch.manual_seed(seed_generator)
93
-
94
- output = pipe(
95
- prompt=prompt,
96
- image=normal_image,
97
- mask_image=mask_image,
98
- control_image=control_image,
99
- negative_prompt=negative_prompt,
100
- num_images_per_prompt=num_images_per_prompt,
101
- num_inference_steps=num_inference_step,
102
- guidance_scale=guidance_scale,
103
- controlnet_conditioning_scale=controlnet_conditioning_scale,
104
- generator=generator,
105
- ).images
106
-
107
- return output
108
-
109
- def app():
110
- with gr.Blocks():
111
- with gr.Row():
112
- with gr.Column():
113
- controlnet_hed_inpaint_image_file = gr.Image(
114
- source="upload",
115
- tool="sketch",
116
- elem_id="image_upload",
117
- type="pil",
118
- label="Upload",
119
- )
120
-
121
- controlnet_hed_inpaint_prompt = gr.Textbox(
122
- lines=1, placeholder="Prompt", show_label=False
123
- )
124
-
125
- controlnet_hed_inpaint_negative_prompt = gr.Textbox(
126
- lines=1,
127
- show_label=False,
128
- placeholder="Negative Prompt",
129
- )
130
- with gr.Row():
131
- with gr.Column():
132
- controlnet_hed_inpaint_stable_model_id = (
133
- gr.Dropdown(
134
- choices=stable_inpiant_model_list,
135
- value=stable_inpiant_model_list[0],
136
- label="Stable Model Id",
137
- )
138
- )
139
-
140
- controlnet_hed_inpaint_guidance_scale = gr.Slider(
141
- minimum=0.1,
142
- maximum=15,
143
- step=0.1,
144
- value=7.5,
145
- label="Guidance Scale",
146
- )
147
-
148
- controlnet_hed_inpaint_num_inference_step = (
149
- gr.Slider(
150
- minimum=1,
151
- maximum=100,
152
- step=1,
153
- value=50,
154
- label="Num Inference Step",
155
- )
156
- )
157
- controlnet_hed_inpaint_num_images_per_prompt = (
158
- gr.Slider(
159
- minimum=1,
160
- maximum=10,
161
- step=1,
162
- value=1,
163
- label="Number Of Images",
164
- )
165
- )
166
- with gr.Row():
167
- with gr.Column():
168
- controlnet_hed_inpaint_model_id = gr.Dropdown(
169
- choices=controlnet_hed_model_list,
170
- value=controlnet_hed_model_list[0],
171
- label="Controlnet Model Id",
172
- )
173
- controlnet_hed_inpaint_scheduler = gr.Dropdown(
174
- choices=SCHEDULER_LIST,
175
- value=SCHEDULER_LIST[0],
176
- label="Scheduler",
177
- )
178
- controlnet_hed_inpaint_controlnet_conditioning_scale = gr.Slider(
179
- minimum=0.1,
180
- maximum=1.0,
181
- step=0.1,
182
- value=0.5,
183
- label="Controlnet Conditioning Scale",
184
- )
185
-
186
- controlnet_hed_inpaint_seed_generator = (
187
- gr.Slider(
188
- minimum=0,
189
- maximum=1000000,
190
- step=1,
191
- value=0,
192
- label="Seed Generator",
193
- )
194
- )
195
-
196
- controlnet_hed_inpaint_predict = gr.Button(
197
- value="Generator"
198
- )
199
-
200
- with gr.Column():
201
- output_image = gr.Gallery(
202
- label="Generated images",
203
- show_label=False,
204
- elem_id="gallery",
205
- ).style(grid=(1, 2))
206
-
207
- controlnet_hed_inpaint_predict.click(
208
- fn=StableDiffusionControlNetInpaintHedGenerator().generate_image,
209
- inputs=[
210
- controlnet_hed_inpaint_image_file,
211
- controlnet_hed_inpaint_stable_model_id,
212
- controlnet_hed_inpaint_model_id,
213
- controlnet_hed_inpaint_prompt,
214
- controlnet_hed_inpaint_negative_prompt,
215
- controlnet_hed_inpaint_num_images_per_prompt,
216
- controlnet_hed_inpaint_guidance_scale,
217
- controlnet_hed_inpaint_num_inference_step,
218
- controlnet_hed_inpaint_controlnet_conditioning_scale,
219
- controlnet_hed_inpaint_scheduler,
220
- controlnet_hed_inpaint_seed_generator,
221
- ],
222
- outputs=[output_image],
223
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_mlsd.py DELETED
@@ -1,224 +0,0 @@
1
- import gradio as gr
2
- import numpy as np
3
- import torch
4
- from controlnet_aux import MLSDdetector
5
- from diffusers import ControlNetModel
6
- from PIL import Image
7
-
8
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint.pipeline_stable_diffusion_controlnet_inpaint import (
9
- StableDiffusionControlNetInpaintPipeline,
10
- )
11
- from diffusion_webui.utils.model_list import (
12
- controlnet_mlsd_model_list,
13
- stable_inpiant_model_list,
14
- )
15
- from diffusion_webui.utils.scheduler_list import (
16
- SCHEDULER_LIST,
17
- get_scheduler_list,
18
- )
19
-
20
- # https://github.com/mikonvergence/ControlNetInpaint
21
-
22
-
23
- class StableDiffusionControlNetInpaintMlsdGenerator:
24
- def __init__(self):
25
- self.pipe = None
26
-
27
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
28
- if self.pipe is None:
29
- controlnet = ControlNetModel.from_pretrained(
30
- controlnet_model_path, torch_dtype=torch.float16
31
- )
32
- self.pipe = (
33
- StableDiffusionControlNetInpaintPipeline.from_pretrained(
34
- pretrained_model_name_or_path=stable_model_path,
35
- controlnet=controlnet,
36
- safety_checker=None,
37
- torch_dtype=torch.float16,
38
- )
39
- )
40
-
41
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
42
- self.pipe.to("cuda")
43
- self.pipe.enable_xformers_memory_efficient_attention()
44
-
45
- return self.pipe
46
-
47
- def load_image(self, image_path):
48
- image = np.array(image_path)
49
- image = Image.fromarray(image)
50
- return image
51
-
52
- def controlnet_inpaint_mlsd(self, image_path: str):
53
- mlsd = MLSDdetector.from_pretrained("lllyasviel/ControlNet")
54
- image = image_path["image"].convert("RGB").resize((512, 512))
55
- image = np.array(image)
56
- image = mlsd(image)
57
-
58
- return image
59
-
60
- def generate_image(
61
- self,
62
- image_path: str,
63
- stable_model_path: str,
64
- controlnet_model_path: str,
65
- prompt: str,
66
- negative_prompt: str,
67
- num_images_per_prompt: int,
68
- guidance_scale: int,
69
- num_inference_step: int,
70
- controlnet_conditioning_scale: int,
71
- scheduler: str,
72
- seed_generator: int,
73
- ):
74
-
75
- normal_image = image_path["image"].convert("RGB").resize((512, 512))
76
- mask_image = image_path["mask"].convert("RGB").resize((512, 512))
77
-
78
- normal_image = self.load_image(image_path=normal_image)
79
- mask_image = self.load_image(image_path=mask_image)
80
-
81
- control_image = self.controlnet_inpaint_mlsd(image_path=image_path)
82
-
83
- pipe = self.load_model(
84
- stable_model_path=stable_model_path,
85
- controlnet_model_path=controlnet_model_path,
86
- scheduler=scheduler,
87
- )
88
-
89
- if seed_generator == 0:
90
- random_seed = torch.randint(0, 1000000, (1,))
91
- generator = torch.manual_seed(random_seed)
92
- else:
93
- generator = torch.manual_seed(seed_generator)
94
-
95
- output = pipe(
96
- prompt=prompt,
97
- image=normal_image,
98
- mask_image=mask_image,
99
- control_image=control_image,
100
- negative_prompt=negative_prompt,
101
- num_images_per_prompt=num_images_per_prompt,
102
- num_inference_steps=num_inference_step,
103
- guidance_scale=guidance_scale,
104
- controlnet_conditioning_scale=controlnet_conditioning_scale,
105
- generator=generator,
106
- ).images
107
-
108
- return output
109
-
110
- def app():
111
- with gr.Blocks():
112
- with gr.Row():
113
- with gr.Column():
114
- controlnet_mlsd_inpaint_image_file = gr.Image(
115
- source="upload",
116
- tool="sketch",
117
- elem_id="image_upload",
118
- type="pil",
119
- label="Upload",
120
- )
121
-
122
- controlnet_mlsd_inpaint_prompt = gr.Textbox(
123
- lines=1, placeholder="Prompt", show_label=False
124
- )
125
-
126
- controlnet_mlsd_inpaint_negative_prompt = gr.Textbox(
127
- lines=1,
128
- show_label=False,
129
- placeholder="Negative Prompt",
130
- )
131
- with gr.Row():
132
- with gr.Column():
133
- controlnet_mlsd_inpaint_stable_model_id = (
134
- gr.Dropdown(
135
- choices=stable_inpiant_model_list,
136
- value=stable_inpiant_model_list[0],
137
- label="Stable Model Id",
138
- )
139
- )
140
-
141
- controlnet_mlsd_inpaint_guidance_scale = gr.Slider(
142
- minimum=0.1,
143
- maximum=15,
144
- step=0.1,
145
- value=7.5,
146
- label="Guidance Scale",
147
- )
148
-
149
- controlnet_mlsd_inpaint_num_inference_step = (
150
- gr.Slider(
151
- minimum=1,
152
- maximum=100,
153
- step=1,
154
- value=50,
155
- label="Num Inference Step",
156
- )
157
- )
158
- controlnet_mlsd_inpaint_num_images_per_prompt = (
159
- gr.Slider(
160
- minimum=1,
161
- maximum=10,
162
- step=1,
163
- value=1,
164
- label="Number Of Images",
165
- )
166
- )
167
- with gr.Row():
168
- with gr.Column():
169
- controlnet_mlsd_inpaint_model_id = gr.Dropdown(
170
- choices=controlnet_mlsd_model_list,
171
- value=controlnet_mlsd_model_list[0],
172
- label="Controlnet Model Id",
173
- )
174
- controlnet_mlsd_inpaint_scheduler = gr.Dropdown(
175
- choices=SCHEDULER_LIST,
176
- value=SCHEDULER_LIST[0],
177
- label="Scheduler",
178
- )
179
- controlnet_mlsd_inpaint_controlnet_conditioning_scale = gr.Slider(
180
- minimum=0.1,
181
- maximum=1.0,
182
- step=0.1,
183
- value=0.5,
184
- label="Controlnet Conditioning Scale",
185
- )
186
-
187
- controlnet_mlsd_inpaint_seed_generator = (
188
- gr.Slider(
189
- minimum=0,
190
- maximum=1000000,
191
- step=1,
192
- value=0,
193
- label="Seed Generator",
194
- )
195
- )
196
-
197
- controlnet_mlsd_inpaint_predict = gr.Button(
198
- value="Generator"
199
- )
200
-
201
- with gr.Column():
202
- output_image = gr.Gallery(
203
- label="Generated images",
204
- show_label=False,
205
- elem_id="gallery",
206
- ).style(grid=(1, 2))
207
-
208
- controlnet_mlsd_inpaint_predict.click(
209
- fn=StableDiffusionControlNetInpaintMlsdGenerator().generate_image,
210
- inputs=[
211
- controlnet_mlsd_inpaint_image_file,
212
- controlnet_mlsd_inpaint_stable_model_id,
213
- controlnet_mlsd_inpaint_model_id,
214
- controlnet_mlsd_inpaint_prompt,
215
- controlnet_mlsd_inpaint_negative_prompt,
216
- controlnet_mlsd_inpaint_num_images_per_prompt,
217
- controlnet_mlsd_inpaint_guidance_scale,
218
- controlnet_mlsd_inpaint_num_inference_step,
219
- controlnet_mlsd_inpaint_controlnet_conditioning_scale,
220
- controlnet_mlsd_inpaint_scheduler,
221
- controlnet_mlsd_inpaint_seed_generator,
222
- ],
223
- outputs=[output_image],
224
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_pose.py DELETED
@@ -1,225 +0,0 @@
1
- import gradio as gr
2
- import numpy as np
3
- import torch
4
- from controlnet_aux import OpenposeDetector
5
- from diffusers import ControlNetModel
6
- from PIL import Image
7
-
8
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint.pipeline_stable_diffusion_controlnet_inpaint import (
9
- StableDiffusionControlNetInpaintPipeline,
10
- )
11
- from diffusion_webui.utils.model_list import (
12
- controlnet_pose_model_list,
13
- stable_inpiant_model_list,
14
- )
15
- from diffusion_webui.utils.scheduler_list import (
16
- SCHEDULER_LIST,
17
- get_scheduler_list,
18
- )
19
-
20
- # https://github.com/mikonvergence/ControlNetInpaint
21
-
22
-
23
- class StableDiffusionControlNetInpaintPoseGenerator:
24
- def __init__(self):
25
- self.pipe = None
26
-
27
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
28
- if self.pipe is None:
29
- controlnet = ControlNetModel.from_pretrained(
30
- controlnet_model_path, torch_dtype=torch.float16
31
- )
32
-
33
- self.pipe = (
34
- StableDiffusionControlNetInpaintPipeline.from_pretrained(
35
- pretrained_model_name_or_path=stable_model_path,
36
- controlnet=controlnet,
37
- safety_checker=None,
38
- torch_dtype=torch.float16,
39
- )
40
- )
41
-
42
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
43
- self.pipe.to("cuda")
44
- self.pipe.enable_xformers_memory_efficient_attention()
45
-
46
- return self.pipe
47
-
48
- def load_image(self, image_path):
49
- image = np.array(image_path)
50
- image = Image.fromarray(image)
51
- return image
52
-
53
- def controlnet_pose_inpaint(self, image_path: str):
54
- openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
55
-
56
- image = image_path["image"].convert("RGB").resize((512, 512))
57
- image = np.array(image)
58
- image = openpose(image)
59
-
60
- return image
61
-
62
- def generate_image(
63
- self,
64
- image_path: str,
65
- stable_model_path: str,
66
- controlnet_model_path: str,
67
- prompt: str,
68
- negative_prompt: str,
69
- num_images_per_prompt: int,
70
- guidance_scale: int,
71
- num_inference_step: int,
72
- controlnet_conditioning_scale: int,
73
- scheduler: str,
74
- seed_generator: int,
75
- ):
76
- normal_image = image_path["image"].convert("RGB").resize((512, 512))
77
- mask_image = image_path["mask"].convert("RGB").resize((512, 512))
78
-
79
- normal_image = self.load_image(image_path=normal_image)
80
- mask_image = self.load_image(image_path=mask_image)
81
-
82
- controlnet_image = self.controlnet_pose_inpaint(image_path=image_path)
83
-
84
- pipe = self.load_model(
85
- stable_model_path=stable_model_path,
86
- controlnet_model_path=controlnet_model_path,
87
- scheduler=scheduler,
88
- )
89
-
90
- if seed_generator == 0:
91
- random_seed = torch.randint(0, 1000000, (1,))
92
- generator = torch.manual_seed(random_seed)
93
- else:
94
- generator = torch.manual_seed(seed_generator)
95
-
96
- output = pipe(
97
- prompt=prompt,
98
- image=normal_image,
99
- mask_image=mask_image,
100
- control_image=controlnet_image,
101
- negative_prompt=negative_prompt,
102
- num_images_per_prompt=num_images_per_prompt,
103
- num_inference_steps=num_inference_step,
104
- guidance_scale=guidance_scale,
105
- controlnet_conditioning_scale=controlnet_conditioning_scale,
106
- generator=generator,
107
- ).images
108
-
109
- return output
110
-
111
- def app():
112
- with gr.Blocks():
113
- with gr.Row():
114
- with gr.Column():
115
- controlnet_pose_inpaint_image_file = gr.Image(
116
- source="upload",
117
- tool="sketch",
118
- elem_id="image_upload",
119
- type="pil",
120
- label="Upload",
121
- )
122
-
123
- controlnet_pose_inpaint_prompt = gr.Textbox(
124
- lines=1, placeholder="Prompt", show_label=False
125
- )
126
-
127
- controlnet_pose_inpaint_negative_prompt = gr.Textbox(
128
- lines=1,
129
- show_label=False,
130
- placeholder="Negative Prompt",
131
- )
132
- with gr.Row():
133
- with gr.Column():
134
- controlnet_pose_inpaint_stable_model_id = (
135
- gr.Dropdown(
136
- choices=stable_inpiant_model_list,
137
- value=stable_inpiant_model_list[0],
138
- label="Stable Model Id",
139
- )
140
- )
141
-
142
- controlnet_pose_inpaint_guidance_scale = gr.Slider(
143
- minimum=0.1,
144
- maximum=15,
145
- step=0.1,
146
- value=7.5,
147
- label="Guidance Scale",
148
- )
149
-
150
- controlnet_pose_inpaint_num_inference_step = (
151
- gr.Slider(
152
- minimum=1,
153
- maximum=100,
154
- step=1,
155
- value=50,
156
- label="Num Inference Step",
157
- )
158
- )
159
- controlnet_pose_inpaint_num_images_per_prompt = (
160
- gr.Slider(
161
- minimum=1,
162
- maximum=10,
163
- step=1,
164
- value=1,
165
- label="Number Of Images",
166
- )
167
- )
168
- with gr.Row():
169
- with gr.Column():
170
- controlnet_pose_inpaint_model_id = gr.Dropdown(
171
- choices=controlnet_pose_model_list,
172
- value=controlnet_pose_model_list[0],
173
- label="Controlnet Model Id",
174
- )
175
- controlnet_pose_inpaint_scheduler = gr.Dropdown(
176
- choices=SCHEDULER_LIST,
177
- value=SCHEDULER_LIST[0],
178
- label="Scheduler",
179
- )
180
- controlnet_pose_inpaint_controlnet_conditioning_scale = gr.Slider(
181
- minimum=0.1,
182
- maximum=1.0,
183
- step=0.1,
184
- value=0.5,
185
- label="Controlnet Conditioning Scale",
186
- )
187
-
188
- controlnet_pose_inpaint_seed_generator = (
189
- gr.Slider(
190
- minimum=0,
191
- maximum=1000000,
192
- step=1,
193
- value=0,
194
- label="Seed Generator",
195
- )
196
- )
197
-
198
- controlnet_pose_inpaint_predict = gr.Button(
199
- value="Generator"
200
- )
201
-
202
- with gr.Column():
203
- output_image = gr.Gallery(
204
- label="Generated images",
205
- show_label=False,
206
- elem_id="gallery",
207
- ).style(grid=(1, 2))
208
-
209
- controlnet_pose_inpaint_predict.click(
210
- fn=StableDiffusionControlNetInpaintPoseGenerator().generate_image,
211
- inputs=[
212
- controlnet_pose_inpaint_image_file,
213
- controlnet_pose_inpaint_stable_model_id,
214
- controlnet_pose_inpaint_model_id,
215
- controlnet_pose_inpaint_prompt,
216
- controlnet_pose_inpaint_negative_prompt,
217
- controlnet_pose_inpaint_num_images_per_prompt,
218
- controlnet_pose_inpaint_guidance_scale,
219
- controlnet_pose_inpaint_num_inference_step,
220
- controlnet_pose_inpaint_controlnet_conditioning_scale,
221
- controlnet_pose_inpaint_scheduler,
222
- controlnet_pose_inpaint_seed_generator,
223
- ],
224
- outputs=[output_image],
225
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_scribble.py DELETED
@@ -1,231 +0,0 @@
1
- import gradio as gr
2
- import numpy as np
3
- import torch
4
- from controlnet_aux import HEDdetector
5
- from diffusers import ControlNetModel
6
- from PIL import Image
7
-
8
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint.pipeline_stable_diffusion_controlnet_inpaint import (
9
- StableDiffusionControlNetInpaintPipeline,
10
- )
11
- from diffusion_webui.utils.model_list import (
12
- controlnet_scribble_model_list,
13
- stable_inpiant_model_list,
14
- )
15
- from diffusion_webui.utils.scheduler_list import (
16
- SCHEDULER_LIST,
17
- get_scheduler_list,
18
- )
19
-
20
- # https://github.com/mikonvergence/ControlNetInpaint
21
-
22
-
23
- class StableDiffusionControlNetInpaintScribbleGenerator:
24
- def __init__(self):
25
- self.pipe = None
26
-
27
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
28
- if self.pipe is None:
29
- controlnet = ControlNetModel.from_pretrained(
30
- controlnet_model_path, torch_dtype=torch.float16
31
- )
32
-
33
- self.pipe = (
34
- StableDiffusionControlNetInpaintPipeline.from_pretrained(
35
- pretrained_model_name_or_path=stable_model_path,
36
- controlnet=controlnet,
37
- safety_checker=None,
38
- torch_dtype=torch.float16,
39
- )
40
- )
41
-
42
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
43
- self.pipe.to("cuda")
44
- self.pipe.enable_xformers_memory_efficient_attention()
45
-
46
- return self.pipe
47
-
48
- def load_image(self, image_path):
49
- image = np.array(image_path)
50
- image = Image.fromarray(image)
51
- return image
52
-
53
- def controlnet_inpaint_scribble(self, image_path: str):
54
- hed = HEDdetector.from_pretrained("lllyasviel/ControlNet")
55
-
56
- image = image_path["image"].convert("RGB").resize((512, 512))
57
- image = np.array(image)
58
- image = hed(image, scribble=True)
59
-
60
- return image
61
-
62
- def generate_image(
63
- self,
64
- image_path: str,
65
- stable_model_path: str,
66
- controlnet_model_path: str,
67
- prompt: str,
68
- negative_prompt: str,
69
- num_images_per_prompt: int,
70
- guidance_scale: int,
71
- num_inference_step: int,
72
- controlnet_conditioning_scale: int,
73
- scheduler: str,
74
- seed_generator: int,
75
- ):
76
- normal_image = image_path["image"].convert("RGB").resize((512, 512))
77
- mask_image = image_path["mask"].convert("RGB").resize((512, 512))
78
-
79
- normal_image = self.load_image(image_path=normal_image)
80
- mask_image = self.load_image(image_path=mask_image)
81
-
82
- controlnet_image = self.controlnet_inpaint_scribble(
83
- image_path=image_path
84
- )
85
-
86
- pipe = self.load_model(
87
- stable_model_path=stable_model_path,
88
- controlnet_model_path=controlnet_model_path,
89
- scheduler=scheduler,
90
- )
91
-
92
- if seed_generator == 0:
93
- random_seed = torch.randint(0, 1000000, (1,))
94
- generator = torch.manual_seed(random_seed)
95
- else:
96
- generator = torch.manual_seed(seed_generator)
97
-
98
- output = pipe(
99
- prompt=prompt,
100
- image=normal_image,
101
- mask_image=mask_image,
102
- control_image=controlnet_image,
103
- negative_prompt=negative_prompt,
104
- num_images_per_prompt=num_images_per_prompt,
105
- num_inference_steps=num_inference_step,
106
- guidance_scale=guidance_scale,
107
- controlnet_conditioning_scale=controlnet_conditioning_scale,
108
- generator=generator,
109
- ).images
110
-
111
- return output
112
-
113
- def app():
114
- with gr.Blocks():
115
- with gr.Row():
116
- with gr.Column():
117
- controlnet_scribble_inpaint_image_file = gr.Image(
118
- source="upload",
119
- tool="sketch",
120
- elem_id="image_upload",
121
- type="pil",
122
- label="Upload",
123
- )
124
-
125
- controlnet_scribble_inpaint_prompt = gr.Textbox(
126
- lines=1, placeholder="Prompt", show_label=False
127
- )
128
-
129
- controlnet_scribble_inpaint_negative_prompt = gr.Textbox(
130
- lines=1,
131
- show_label=False,
132
- placeholder="Negative Prompt",
133
- )
134
- with gr.Row():
135
- with gr.Column():
136
- controlnet_scribble_inpaint_stable_model_id = (
137
- gr.Dropdown(
138
- choices=stable_inpiant_model_list,
139
- value=stable_inpiant_model_list[0],
140
- label="Stable Model Id",
141
- )
142
- )
143
-
144
- controlnet_scribble_inpaint_guidance_scale = (
145
- gr.Slider(
146
- minimum=0.1,
147
- maximum=15,
148
- step=0.1,
149
- value=7.5,
150
- label="Guidance Scale",
151
- )
152
- )
153
-
154
- controlnet_scribble_inpaint_num_inference_step = (
155
- gr.Slider(
156
- minimum=1,
157
- maximum=100,
158
- step=1,
159
- value=50,
160
- label="Num Inference Step",
161
- )
162
- )
163
- controlnet_scribble_inpaint_num_images_per_prompt = gr.Slider(
164
- minimum=1,
165
- maximum=10,
166
- step=1,
167
- value=1,
168
- label="Number Of Images",
169
- )
170
- with gr.Row():
171
- with gr.Column():
172
- controlnet_scribble_inpaint_model_id = (
173
- gr.Dropdown(
174
- choices=controlnet_scribble_model_list,
175
- value=controlnet_scribble_model_list[0],
176
- label="Controlnet Model Id",
177
- )
178
- )
179
- controlnet_scribble_inpaint_scheduler = (
180
- gr.Dropdown(
181
- choices=SCHEDULER_LIST,
182
- value=SCHEDULER_LIST[0],
183
- label="Scheduler",
184
- )
185
- )
186
- controlnet_scribble_inpaint_controlnet_conditioning_scale = gr.Slider(
187
- minimum=0.1,
188
- maximum=1.0,
189
- step=0.1,
190
- value=0.5,
191
- label="Controlnet Conditioning Scale",
192
- )
193
-
194
- controlnet_scribble_inpaint_seed_generator = (
195
- gr.Slider(
196
- minimum=0,
197
- maximum=1000000,
198
- step=1,
199
- value=0,
200
- label="Seed Generator",
201
- )
202
- )
203
-
204
- controlnet_scribble_inpaint_predict = gr.Button(
205
- value="Generator"
206
- )
207
-
208
- with gr.Column():
209
- output_image = gr.Gallery(
210
- label="Generated images",
211
- show_label=False,
212
- elem_id="gallery",
213
- ).style(grid=(1, 2))
214
-
215
- controlnet_scribble_inpaint_predict.click(
216
- fn=StableDiffusionControlNetInpaintScribbleGenerator().generate_image,
217
- inputs=[
218
- controlnet_scribble_inpaint_image_file,
219
- controlnet_scribble_inpaint_stable_model_id,
220
- controlnet_scribble_inpaint_model_id,
221
- controlnet_scribble_inpaint_prompt,
222
- controlnet_scribble_inpaint_negative_prompt,
223
- controlnet_scribble_inpaint_num_images_per_prompt,
224
- controlnet_scribble_inpaint_guidance_scale,
225
- controlnet_scribble_inpaint_num_inference_step,
226
- controlnet_scribble_inpaint_controlnet_conditioning_scale,
227
- controlnet_scribble_inpaint_scheduler,
228
- controlnet_scribble_inpaint_seed_generator,
229
- ],
230
- outputs=[output_image],
231
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_seg.py DELETED
@@ -1,403 +0,0 @@
1
- import gradio as gr
2
- import numpy as np
3
- import torch
4
- from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
5
- from PIL import Image
6
- from transformers import AutoImageProcessor, UperNetForSemanticSegmentation
7
-
8
- from diffusion_webui.utils.model_list import (
9
- controlnet_seg_model_list,
10
- stable_inpiant_model_list,
11
- )
12
- from diffusion_webui.utils.scheduler_list import (
13
- SCHEDULER_LIST,
14
- get_scheduler_list,
15
- )
16
-
17
- # https://github.com/mikonvergence/ControlNetInpaint
18
-
19
-
20
- def ade_palette():
21
- """ADE20K palette that maps each class to RGB values."""
22
- return [
23
- [120, 120, 120],
24
- [180, 120, 120],
25
- [6, 230, 230],
26
- [80, 50, 50],
27
- [4, 200, 3],
28
- [120, 120, 80],
29
- [140, 140, 140],
30
- [204, 5, 255],
31
- [230, 230, 230],
32
- [4, 250, 7],
33
- [224, 5, 255],
34
- [235, 255, 7],
35
- [150, 5, 61],
36
- [120, 120, 70],
37
- [8, 255, 51],
38
- [255, 6, 82],
39
- [143, 255, 140],
40
- [204, 255, 4],
41
- [255, 51, 7],
42
- [204, 70, 3],
43
- [0, 102, 200],
44
- [61, 230, 250],
45
- [255, 6, 51],
46
- [11, 102, 255],
47
- [255, 7, 71],
48
- [255, 9, 224],
49
- [9, 7, 230],
50
- [220, 220, 220],
51
- [255, 9, 92],
52
- [112, 9, 255],
53
- [8, 255, 214],
54
- [7, 255, 224],
55
- [255, 184, 6],
56
- [10, 255, 71],
57
- [255, 41, 10],
58
- [7, 255, 255],
59
- [224, 255, 8],
60
- [102, 8, 255],
61
- [255, 61, 6],
62
- [255, 194, 7],
63
- [255, 122, 8],
64
- [0, 255, 20],
65
- [255, 8, 41],
66
- [255, 5, 153],
67
- [6, 51, 255],
68
- [235, 12, 255],
69
- [160, 150, 20],
70
- [0, 163, 255],
71
- [140, 140, 140],
72
- [250, 10, 15],
73
- [20, 255, 0],
74
- [31, 255, 0],
75
- [255, 31, 0],
76
- [255, 224, 0],
77
- [153, 255, 0],
78
- [0, 0, 255],
79
- [255, 71, 0],
80
- [0, 235, 255],
81
- [0, 173, 255],
82
- [31, 0, 255],
83
- [11, 200, 200],
84
- [255, 82, 0],
85
- [0, 255, 245],
86
- [0, 61, 255],
87
- [0, 255, 112],
88
- [0, 255, 133],
89
- [255, 0, 0],
90
- [255, 163, 0],
91
- [255, 102, 0],
92
- [194, 255, 0],
93
- [0, 143, 255],
94
- [51, 255, 0],
95
- [0, 82, 255],
96
- [0, 255, 41],
97
- [0, 255, 173],
98
- [10, 0, 255],
99
- [173, 255, 0],
100
- [0, 255, 153],
101
- [255, 92, 0],
102
- [255, 0, 255],
103
- [255, 0, 245],
104
- [255, 0, 102],
105
- [255, 173, 0],
106
- [255, 0, 20],
107
- [255, 184, 184],
108
- [0, 31, 255],
109
- [0, 255, 61],
110
- [0, 71, 255],
111
- [255, 0, 204],
112
- [0, 255, 194],
113
- [0, 255, 82],
114
- [0, 10, 255],
115
- [0, 112, 255],
116
- [51, 0, 255],
117
- [0, 194, 255],
118
- [0, 122, 255],
119
- [0, 255, 163],
120
- [255, 153, 0],
121
- [0, 255, 10],
122
- [255, 112, 0],
123
- [143, 255, 0],
124
- [82, 0, 255],
125
- [163, 255, 0],
126
- [255, 235, 0],
127
- [8, 184, 170],
128
- [133, 0, 255],
129
- [0, 255, 92],
130
- [184, 0, 255],
131
- [255, 0, 31],
132
- [0, 184, 255],
133
- [0, 214, 255],
134
- [255, 0, 112],
135
- [92, 255, 0],
136
- [0, 224, 255],
137
- [112, 224, 255],
138
- [70, 184, 160],
139
- [163, 0, 255],
140
- [153, 0, 255],
141
- [71, 255, 0],
142
- [255, 0, 163],
143
- [255, 204, 0],
144
- [255, 0, 143],
145
- [0, 255, 235],
146
- [133, 255, 0],
147
- [255, 0, 235],
148
- [245, 0, 255],
149
- [255, 0, 122],
150
- [255, 245, 0],
151
- [10, 190, 212],
152
- [214, 255, 0],
153
- [0, 204, 255],
154
- [20, 0, 255],
155
- [255, 255, 0],
156
- [0, 153, 255],
157
- [0, 41, 255],
158
- [0, 255, 204],
159
- [41, 0, 255],
160
- [41, 255, 0],
161
- [173, 0, 255],
162
- [0, 245, 255],
163
- [71, 0, 255],
164
- [122, 0, 255],
165
- [0, 255, 184],
166
- [0, 92, 255],
167
- [184, 255, 0],
168
- [0, 133, 255],
169
- [255, 214, 0],
170
- [25, 194, 194],
171
- [102, 255, 0],
172
- [92, 0, 255],
173
- ]
174
-
175
-
176
- class StableDiffusionControlNetInpaintSegGenerator:
177
- def __init__(self):
178
- self.pipe = None
179
-
180
- def load_model(
181
- self,
182
- stable_model_path,
183
- controlnet_model_path,
184
- scheduler,
185
- ):
186
-
187
- if self.pipe is None:
188
- controlnet = ControlNetModel.from_pretrained(
189
- controlnet_model_path, torch_dtype=torch.float16
190
- )
191
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
192
- pretrained_model_name_or_path=stable_model_path,
193
- controlnet=controlnet,
194
- safety_checker=None,
195
- torch_dtype=torch.float16,
196
- )
197
-
198
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
199
- self.pipe.to("cuda")
200
- self.pipe.enable_xformers_memory_efficient_attention()
201
-
202
- return self.pipe
203
-
204
- def load_image(self, image_path):
205
- image = np.array(image_path)
206
- image = Image.fromarray(image)
207
- return image
208
-
209
- def controlnet_seg_inpaint(self, image_path: str):
210
- image_processor = AutoImageProcessor.from_pretrained(
211
- "openmmlab/upernet-convnext-small"
212
- )
213
- image_segmentor = UperNetForSemanticSegmentation.from_pretrained(
214
- "openmmlab/upernet-convnext-small"
215
- )
216
-
217
- image = image_path["image"].convert("RGB").resize((512, 512))
218
- image = np.array(image)
219
- pixel_values = image_processor(image, return_tensors="pt").pixel_values
220
-
221
- with torch.no_grad():
222
- outputs = image_segmentor(pixel_values)
223
-
224
- seg = image_processor.post_process_semantic_segmentation(
225
- outputs, target_sizes=[image.size[::-1]]
226
- )[0]
227
-
228
- color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
229
- palette = np.array(ade_palette())
230
-
231
- for label, color in enumerate(palette):
232
- color_seg[seg == label, :] = color
233
-
234
- color_seg = color_seg.astype(np.uint8)
235
- image = Image.fromarray(color_seg)
236
-
237
- return image
238
-
239
- def generate_image(
240
- self,
241
- image_path: str,
242
- stable_model_path: str,
243
- controlnet_model_path: str,
244
- prompt: str,
245
- negative_prompt: str,
246
- num_images_per_prompt: int,
247
- guidance_scale: int,
248
- num_inference_step: int,
249
- controlnet_conditioning_scale: int,
250
- scheduler: str,
251
- seed_generator: int,
252
- ):
253
-
254
- normal_image = image_path["image"].convert("RGB").resize((512, 512))
255
- mask_image = image_path["mask"].convert("RGB").resize((512, 512))
256
-
257
- normal_image = self.load_image(image_path=normal_image)
258
- mask_image = self.load_image(image_path=mask_image)
259
-
260
- controlnet_image = self.controlnet_seg_inpaint(image_path=image_path)
261
-
262
- pipe = self.load_model(
263
- stable_model_path=stable_model_path,
264
- controlnet_model_path=controlnet_model_path,
265
- scheduler=scheduler,
266
- )
267
-
268
- if seed_generator == 0:
269
- random_seed = torch.randint(0, 1000000, (1,))
270
- generator = torch.manual_seed(random_seed)
271
- else:
272
- generator = torch.manual_seed(seed_generator)
273
-
274
- output = pipe(
275
- prompt=prompt,
276
- image=normal_image,
277
- mask_image=mask_image,
278
- control_image=controlnet_image,
279
- negative_prompt=negative_prompt,
280
- num_images_per_prompt=num_images_per_prompt,
281
- num_inference_steps=num_inference_step,
282
- guidance_scale=guidance_scale,
283
- controlnet_conditioning_scale=controlnet_conditioning_scale,
284
- generator=generator,
285
- ).images
286
-
287
- return output
288
-
289
- def app():
290
- with gr.Blocks():
291
- with gr.Row():
292
- with gr.Column():
293
- controlnet_seg_inpaint_image_file = gr.Image(
294
- source="upload",
295
- tool="sketch",
296
- elem_id="image_upload",
297
- type="pil",
298
- label="Upload",
299
- )
300
-
301
- controlnet_seg_inpaint_prompt = gr.Textbox(
302
- lines=1, placeholder="Prompt", show_label=False
303
- )
304
-
305
- controlnet_seg_inpaint_negative_prompt = gr.Textbox(
306
- lines=1,
307
- show_label=False,
308
- placeholder="Negative Prompt",
309
- )
310
- with gr.Row():
311
- with gr.Column():
312
- controlnet_seg_inpaint_stable_model_id = (
313
- gr.Dropdown(
314
- choices=stable_inpiant_model_list,
315
- value=stable_inpiant_model_list[0],
316
- label="Stable Model Id",
317
- )
318
- )
319
-
320
- controlnet_seg_inpaint_guidance_scale = gr.Slider(
321
- minimum=0.1,
322
- maximum=15,
323
- step=0.1,
324
- value=7.5,
325
- label="Guidance Scale",
326
- )
327
-
328
- controlnet_seg_inpaint_num_inference_step = (
329
- gr.Slider(
330
- minimum=1,
331
- maximum=100,
332
- step=1,
333
- value=50,
334
- label="Num Inference Step",
335
- )
336
- )
337
- controlnet_seg_inpaint_num_images_per_prompt = (
338
- gr.Slider(
339
- minimum=1,
340
- maximum=10,
341
- step=1,
342
- value=1,
343
- label="Number Of Images",
344
- )
345
- )
346
- with gr.Row():
347
- with gr.Column():
348
- controlnet_seg_inpaint_model_id = gr.Dropdown(
349
- choices=controlnet_seg_model_list,
350
- value=controlnet_seg_model_list[0],
351
- label="Controlnet Model Id",
352
- )
353
- controlnet_seg_inpaint_scheduler = gr.Dropdown(
354
- choices=SCHEDULER_LIST,
355
- value=SCHEDULER_LIST[0],
356
- label="Scheduler",
357
- )
358
- controlnet_seg_inpaint_controlnet_conditioning_scale = gr.Slider(
359
- minimum=0.1,
360
- maximum=1.0,
361
- step=0.1,
362
- value=0.5,
363
- label="Controlnet Conditioning Scale",
364
- )
365
-
366
- controlnet_seg_inpaint_seed_generator = (
367
- gr.Slider(
368
- minimum=0,
369
- maximum=1000000,
370
- step=1,
371
- value=0,
372
- label="Seed Generator",
373
- )
374
- )
375
-
376
- controlnet_seg_inpaint_predict = gr.Button(
377
- value="Generator"
378
- )
379
-
380
- with gr.Column():
381
- output_image = gr.Gallery(
382
- label="Generated images",
383
- show_label=False,
384
- elem_id="gallery",
385
- ).style(grid=(1, 2))
386
-
387
- controlnet_seg_inpaint_predict.click(
388
- fn=StableDiffusionControlNetInpaintSegGenerator().generate_image,
389
- inputs=[
390
- controlnet_seg_inpaint_image_file,
391
- controlnet_seg_inpaint_stable_model_id,
392
- controlnet_seg_inpaint_model_id,
393
- controlnet_seg_inpaint_prompt,
394
- controlnet_seg_inpaint_negative_prompt,
395
- controlnet_seg_inpaint_num_images_per_prompt,
396
- controlnet_seg_inpaint_guidance_scale,
397
- controlnet_seg_inpaint_num_inference_step,
398
- controlnet_seg_inpaint_controlnet_conditioning_scale,
399
- controlnet_seg_inpaint_scheduler,
400
- controlnet_seg_inpaint_seed_generator,
401
- ],
402
- outputs=[output_image],
403
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/pipeline_stable_diffusion_controlnet_inpaint.py DELETED
@@ -1,521 +0,0 @@
1
- # Copyright 2023 The HuggingFace Team. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- import torch
16
- import PIL.Image
17
- import numpy as np
18
-
19
- from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import *
20
-
21
- EXAMPLE_DOC_STRING = """
22
- Examples:
23
- ```py
24
- >>> # !pip install opencv-python transformers accelerate
25
- >>> from diffusers import StableDiffusionControlNetInpaintPipeline, ControlNetModel, UniPCMultistepScheduler
26
- >>> from diffusers.utils import load_image
27
- >>> import numpy as np
28
- >>> import torch
29
-
30
- >>> import cv2
31
- >>> from PIL import Image
32
- >>> # download an image
33
- >>> image = load_image(
34
- ... "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
35
- ... )
36
- >>> image = np.array(image)
37
- >>> mask_image = load_image(
38
- ... "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
39
- ... )
40
- >>> mask_image = np.array(mask_image)
41
- >>> # get canny image
42
- >>> canny_image = cv2.Canny(image, 100, 200)
43
- >>> canny_image = canny_image[:, :, None]
44
- >>> canny_image = np.concatenate([canny_image, canny_image, canny_image], axis=2)
45
- >>> canny_image = Image.fromarray(canny_image)
46
-
47
- >>> # load control net and stable diffusion v1-5
48
- >>> controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
49
- >>> pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
50
- ... "runwayml/stable-diffusion-inpainting", controlnet=controlnet, torch_dtype=torch.float16
51
- ... )
52
-
53
- >>> # speed up diffusion process with faster scheduler and memory optimization
54
- >>> pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
55
- >>> # remove following line if xformers is not installed
56
- >>> pipe.enable_xformers_memory_efficient_attention()
57
-
58
- >>> pipe.enable_model_cpu_offload()
59
-
60
- >>> # generate image
61
- >>> generator = torch.manual_seed(0)
62
- >>> image = pipe(
63
- ... "futuristic-looking doggo",
64
- ... num_inference_steps=20,
65
- ... generator=generator,
66
- ... image=image,
67
- ... control_image=canny_image,
68
- ... mask_image=mask_image
69
- ... ).images[0]
70
- ```
71
- """
72
-
73
-
74
- def prepare_mask_and_masked_image(image, mask):
75
- """
76
- Prepares a pair (image, mask) to be consumed by the Stable Diffusion pipeline. This means that those inputs will be
77
- converted to ``torch.Tensor`` with shapes ``batch x channels x height x width`` where ``channels`` is ``3`` for the
78
- ``image`` and ``1`` for the ``mask``.
79
- The ``image`` will be converted to ``torch.float32`` and normalized to be in ``[-1, 1]``. The ``mask`` will be
80
- binarized (``mask > 0.5``) and cast to ``torch.float32`` too.
81
- Args:
82
- image (Union[np.array, PIL.Image, torch.Tensor]): The image to inpaint.
83
- It can be a ``PIL.Image``, or a ``height x width x 3`` ``np.array`` or a ``channels x height x width``
84
- ``torch.Tensor`` or a ``batch x channels x height x width`` ``torch.Tensor``.
85
- mask (_type_): The mask to apply to the image, i.e. regions to inpaint.
86
- It can be a ``PIL.Image``, or a ``height x width`` ``np.array`` or a ``1 x height x width``
87
- ``torch.Tensor`` or a ``batch x 1 x height x width`` ``torch.Tensor``.
88
- Raises:
89
- ValueError: ``torch.Tensor`` images should be in the ``[-1, 1]`` range. ValueError: ``torch.Tensor`` mask
90
- should be in the ``[0, 1]`` range. ValueError: ``mask`` and ``image`` should have the same spatial dimensions.
91
- TypeError: ``mask`` is a ``torch.Tensor`` but ``image`` is not
92
- (ot the other way around).
93
- Returns:
94
- tuple[torch.Tensor]: The pair (mask, masked_image) as ``torch.Tensor`` with 4
95
- dimensions: ``batch x channels x height x width``.
96
- """
97
- if isinstance(image, torch.Tensor):
98
- if not isinstance(mask, torch.Tensor):
99
- raise TypeError(f"`image` is a torch.Tensor but `mask` (type: {type(mask)} is not")
100
-
101
- # Batch single image
102
- if image.ndim == 3:
103
- assert image.shape[0] == 3, "Image outside a batch should be of shape (3, H, W)"
104
- image = image.unsqueeze(0)
105
-
106
- # Batch and add channel dim for single mask
107
- if mask.ndim == 2:
108
- mask = mask.unsqueeze(0).unsqueeze(0)
109
-
110
- # Batch single mask or add channel dim
111
- if mask.ndim == 3:
112
- # Single batched mask, no channel dim or single mask not batched but channel dim
113
- if mask.shape[0] == 1:
114
- mask = mask.unsqueeze(0)
115
-
116
- # Batched masks no channel dim
117
- else:
118
- mask = mask.unsqueeze(1)
119
-
120
- assert image.ndim == 4 and mask.ndim == 4, "Image and Mask must have 4 dimensions"
121
- assert image.shape[-2:] == mask.shape[-2:], "Image and Mask must have the same spatial dimensions"
122
- assert image.shape[0] == mask.shape[0], "Image and Mask must have the same batch size"
123
-
124
- # Check image is in [-1, 1]
125
- if image.min() < -1 or image.max() > 1:
126
- raise ValueError("Image should be in [-1, 1] range")
127
-
128
- # Check mask is in [0, 1]
129
- if mask.min() < 0 or mask.max() > 1:
130
- raise ValueError("Mask should be in [0, 1] range")
131
-
132
- # Binarize mask
133
- mask[mask < 0.5] = 0
134
- mask[mask >= 0.5] = 1
135
-
136
- # Image as float32
137
- image = image.to(dtype=torch.float32)
138
- elif isinstance(mask, torch.Tensor):
139
- raise TypeError(f"`mask` is a torch.Tensor but `image` (type: {type(image)} is not")
140
- else:
141
- # preprocess image
142
- if isinstance(image, (PIL.Image.Image, np.ndarray)):
143
- image = [image]
144
-
145
- if isinstance(image, list) and isinstance(image[0], PIL.Image.Image):
146
- image = [np.array(i.convert("RGB"))[None, :] for i in image]
147
- image = np.concatenate(image, axis=0)
148
- elif isinstance(image, list) and isinstance(image[0], np.ndarray):
149
- image = np.concatenate([i[None, :] for i in image], axis=0)
150
-
151
- image = image.transpose(0, 3, 1, 2)
152
- image = torch.from_numpy(image).to(dtype=torch.float32) / 127.5 - 1.0
153
-
154
- # preprocess mask
155
- if isinstance(mask, (PIL.Image.Image, np.ndarray)):
156
- mask = [mask]
157
-
158
- if isinstance(mask, list) and isinstance(mask[0], PIL.Image.Image):
159
- mask = np.concatenate([np.array(m.convert("L"))[None, None, :] for m in mask], axis=0)
160
- mask = mask.astype(np.float32) / 255.0
161
- elif isinstance(mask, list) and isinstance(mask[0], np.ndarray):
162
- mask = np.concatenate([m[None, None, :] for m in mask], axis=0)
163
-
164
- mask[mask < 0.5] = 0
165
- mask[mask >= 0.5] = 1
166
- mask = torch.from_numpy(mask)
167
-
168
- masked_image = image * (mask < 0.5)
169
-
170
- return mask, masked_image
171
-
172
- class StableDiffusionControlNetInpaintPipeline(StableDiffusionControlNetPipeline):
173
- r"""
174
- Pipeline for text-guided image inpainting using Stable Diffusion with ControlNet guidance.
175
-
176
- This model inherits from [`StableDiffusionControlNetPipeline`]. Check the superclass documentation for the generic methods the
177
- library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
178
-
179
- Args:
180
- vae ([`AutoencoderKL`]):
181
- Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.
182
- text_encoder ([`CLIPTextModel`]):
183
- Frozen text-encoder. Stable Diffusion uses the text portion of
184
- [CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel), specifically
185
- the [clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14) variant.
186
- tokenizer (`CLIPTokenizer`):
187
- Tokenizer of class
188
- [CLIPTokenizer](https://huggingface.co/docs/transformers/v4.21.0/en/model_doc/clip#transformers.CLIPTokenizer).
189
- unet ([`UNet2DConditionModel`]): Conditional U-Net architecture to denoise the encoded image latents.
190
- controlnet ([`ControlNetModel`]):
191
- Provides additional conditioning to the unet during the denoising process
192
- scheduler ([`SchedulerMixin`]):
193
- A scheduler to be used in combination with `unet` to denoise the encoded image latents. Can be one of
194
- [`DDIMScheduler`], [`LMSDiscreteScheduler`], or [`PNDMScheduler`].
195
- safety_checker ([`StableDiffusionSafetyChecker`]):
196
- Classification module that estimates whether generated images could be considered offensive or harmful.
197
- Please, refer to the [model card](https://huggingface.co/runwayml/stable-diffusion-v1-5) for details.
198
- feature_extractor ([`CLIPFeatureExtractor`]):
199
- Model that extracts features from generated images to be used as inputs for the `safety_checker`.
200
- """
201
-
202
- def prepare_mask_latents(
203
- self, mask, masked_image, batch_size, height, width, dtype, device, generator, do_classifier_free_guidance
204
- ):
205
- # resize the mask to latents shape as we concatenate the mask to the latents
206
- # we do that before converting to dtype to avoid breaking in case we're using cpu_offload
207
- # and half precision
208
- mask = torch.nn.functional.interpolate(
209
- mask, size=(height // self.vae_scale_factor, width // self.vae_scale_factor)
210
- )
211
- mask = mask.to(device=device, dtype=dtype)
212
-
213
- masked_image = masked_image.to(device=device, dtype=dtype)
214
-
215
- # encode the mask image into latents space so we can concatenate it to the latents
216
- if isinstance(generator, list):
217
- masked_image_latents = [
218
- self.vae.encode(masked_image[i : i + 1]).latent_dist.sample(generator=generator[i])
219
- for i in range(batch_size)
220
- ]
221
- masked_image_latents = torch.cat(masked_image_latents, dim=0)
222
- else:
223
- masked_image_latents = self.vae.encode(masked_image).latent_dist.sample(generator=generator)
224
- masked_image_latents = self.vae.config.scaling_factor * masked_image_latents
225
-
226
- # duplicate mask and masked_image_latents for each generation per prompt, using mps friendly method
227
- if mask.shape[0] < batch_size:
228
- if not batch_size % mask.shape[0] == 0:
229
- raise ValueError(
230
- "The passed mask and the required batch size don't match. Masks are supposed to be duplicated to"
231
- f" a total batch size of {batch_size}, but {mask.shape[0]} masks were passed. Make sure the number"
232
- " of masks that you pass is divisible by the total requested batch size."
233
- )
234
- mask = mask.repeat(batch_size // mask.shape[0], 1, 1, 1)
235
- if masked_image_latents.shape[0] < batch_size:
236
- if not batch_size % masked_image_latents.shape[0] == 0:
237
- raise ValueError(
238
- "The passed images and the required batch size don't match. Images are supposed to be duplicated"
239
- f" to a total batch size of {batch_size}, but {masked_image_latents.shape[0]} images were passed."
240
- " Make sure the number of images that you pass is divisible by the total requested batch size."
241
- )
242
- masked_image_latents = masked_image_latents.repeat(batch_size // masked_image_latents.shape[0], 1, 1, 1)
243
-
244
- mask = torch.cat([mask] * 2) if do_classifier_free_guidance else mask
245
- masked_image_latents = (
246
- torch.cat([masked_image_latents] * 2) if do_classifier_free_guidance else masked_image_latents
247
- )
248
-
249
- # aligning device to prevent device errors when concating it with the latent model input
250
- masked_image_latents = masked_image_latents.to(device=device, dtype=dtype)
251
- return mask, masked_image_latents
252
-
253
- @torch.no_grad()
254
- @replace_example_docstring(EXAMPLE_DOC_STRING)
255
- def __call__(
256
- self,
257
- prompt: Union[str, List[str]] = None,
258
- image: Union[torch.FloatTensor, PIL.Image.Image] = None,
259
- control_image: Union[torch.FloatTensor, PIL.Image.Image, List[torch.FloatTensor], List[PIL.Image.Image]] = None,
260
- mask_image: Union[torch.FloatTensor, PIL.Image.Image] = None,
261
- height: Optional[int] = None,
262
- width: Optional[int] = None,
263
- num_inference_steps: int = 50,
264
- guidance_scale: float = 7.5,
265
- negative_prompt: Optional[Union[str, List[str]]] = None,
266
- num_images_per_prompt: Optional[int] = 1,
267
- eta: float = 0.0,
268
- generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
269
- latents: Optional[torch.FloatTensor] = None,
270
- prompt_embeds: Optional[torch.FloatTensor] = None,
271
- negative_prompt_embeds: Optional[torch.FloatTensor] = None,
272
- output_type: Optional[str] = "pil",
273
- return_dict: bool = True,
274
- callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None,
275
- callback_steps: int = 1,
276
- cross_attention_kwargs: Optional[Dict[str, Any]] = None,
277
- controlnet_conditioning_scale: float = 1.0,
278
- ):
279
- r"""
280
- Function invoked when calling the pipeline for generation.
281
- Args:
282
- prompt (`str` or `List[str]`, *optional*):
283
- The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
284
- instead.
285
- image (`PIL.Image.Image`):
286
- `Image`, or tensor representing an image batch which will be inpainted, *i.e.* parts of the image will
287
- be masked out with `mask_image` and repainted according to `prompt`.
288
- control_image (`torch.FloatTensor`, `PIL.Image.Image`, `List[torch.FloatTensor]` or `List[PIL.Image.Image]`):
289
- The ControlNet input condition. ControlNet uses this input condition to generate guidance to Unet. If
290
- the type is specified as `Torch.FloatTensor`, it is passed to ControlNet as is. PIL.Image.Image` can
291
- also be accepted as an image. The control image is automatically resized to fit the output image.
292
- mask_image (`PIL.Image.Image`):
293
- `Image`, or tensor representing an image batch, to mask `image`. White pixels in the mask will be
294
- repainted, while black pixels will be preserved. If `mask_image` is a PIL image, it will be converted
295
- to a single channel (luminance) before use. If it's a tensor, it should contain one color channel (L)
296
- instead of 3, so the expected shape would be `(B, H, W, 1)`.
297
- height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
298
- The height in pixels of the generated image.
299
- width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
300
- The width in pixels of the generated image.
301
- num_inference_steps (`int`, *optional*, defaults to 50):
302
- The number of denoising steps. More denoising steps usually lead to a higher quality image at the
303
- expense of slower inference.
304
- guidance_scale (`float`, *optional*, defaults to 7.5):
305
- Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598).
306
- `guidance_scale` is defined as `w` of equation 2. of [Imagen
307
- Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale >
308
- 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`,
309
- usually at the expense of lower image quality.
310
- negative_prompt (`str` or `List[str]`, *optional*):
311
- The prompt or prompts not to guide the image generation. If not defined, one has to pass
312
- `negative_prompt_embeds`. instead. If not defined, one has to pass `negative_prompt_embeds`. instead.
313
- Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
314
- num_images_per_prompt (`int`, *optional*, defaults to 1):
315
- The number of images to generate per prompt.
316
- eta (`float`, *optional*, defaults to 0.0):
317
- Corresponds to parameter eta (η) in the DDIM paper: https://arxiv.org/abs/2010.02502. Only applies to
318
- [`schedulers.DDIMScheduler`], will be ignored for others.
319
- generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
320
- One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
321
- to make generation deterministic.
322
- latents (`torch.FloatTensor`, *optional*):
323
- Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
324
- generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
325
- tensor will ge generated by sampling using the supplied random `generator`.
326
- prompt_embeds (`torch.FloatTensor`, *optional*):
327
- Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
328
- provided, text embeddings will be generated from `prompt` input argument.
329
- negative_prompt_embeds (`torch.FloatTensor`, *optional*):
330
- Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
331
- weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
332
- argument.
333
- output_type (`str`, *optional*, defaults to `"pil"`):
334
- The output format of the generate image. Choose between
335
- [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
336
- return_dict (`bool`, *optional*, defaults to `True`):
337
- Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a
338
- plain tuple.
339
- callback (`Callable`, *optional*):
340
- A function that will be called every `callback_steps` steps during inference. The function will be
341
- called with the following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`.
342
- callback_steps (`int`, *optional*, defaults to 1):
343
- The frequency at which the `callback` function will be called. If not specified, the callback will be
344
- called at every step.
345
- cross_attention_kwargs (`dict`, *optional*):
346
- A kwargs dictionary that if specified is passed along to the `AttnProcessor` as defined under
347
- `self.processor` in
348
- [diffusers.cross_attention](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/cross_attention.py).
349
- controlnet_conditioning_scale (`float`, *optional*, defaults to 1.0):
350
- The outputs of the controlnet are multiplied by `controlnet_conditioning_scale` before they are added
351
- to the residual in the original unet.
352
- Examples:
353
- Returns:
354
- [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] or `tuple`:
355
- [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] if `return_dict` is True, otherwise a `tuple.
356
- When returning a tuple, the first element is a list with the generated images, and the second element is a
357
- list of `bool`s denoting whether the corresponding generated image likely represents "not-safe-for-work"
358
- (nsfw) content, according to the `safety_checker`.
359
- """
360
- # 0. Default height and width to unet
361
- height, width = self._default_height_width(height, width, control_image)
362
-
363
- # 1. Check inputs. Raise error if not correct
364
- self.check_inputs(
365
- prompt, control_image, height, width, callback_steps, negative_prompt, prompt_embeds, negative_prompt_embeds
366
- )
367
-
368
- # 2. Define call parameters
369
- if prompt is not None and isinstance(prompt, str):
370
- batch_size = 1
371
- elif prompt is not None and isinstance(prompt, list):
372
- batch_size = len(prompt)
373
- else:
374
- batch_size = prompt_embeds.shape[0]
375
-
376
- device = self._execution_device
377
- # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
378
- # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
379
- # corresponds to doing no classifier free guidance.
380
- do_classifier_free_guidance = guidance_scale > 1.0
381
-
382
- # 3. Encode input prompt
383
- prompt_embeds = self._encode_prompt(
384
- prompt,
385
- device,
386
- num_images_per_prompt,
387
- do_classifier_free_guidance,
388
- negative_prompt,
389
- prompt_embeds=prompt_embeds,
390
- negative_prompt_embeds=negative_prompt_embeds,
391
- )
392
-
393
- # 4. Prepare image
394
- control_image = self.prepare_image(
395
- control_image,
396
- width,
397
- height,
398
- batch_size * num_images_per_prompt,
399
- num_images_per_prompt,
400
- device,
401
- self.controlnet.dtype,
402
- )
403
-
404
- if do_classifier_free_guidance:
405
- control_image = torch.cat([control_image] * 2)
406
-
407
- # 5. Prepare timesteps
408
- self.scheduler.set_timesteps(num_inference_steps, device=device)
409
- timesteps = self.scheduler.timesteps
410
-
411
- # 6. Prepare latent variables
412
- num_channels_latents = self.controlnet.config.in_channels
413
- latents = self.prepare_latents(
414
- batch_size * num_images_per_prompt,
415
- num_channels_latents,
416
- height,
417
- width,
418
- prompt_embeds.dtype,
419
- device,
420
- generator,
421
- latents,
422
- )
423
-
424
- # EXTRA: prepare mask latents
425
- mask, masked_image = prepare_mask_and_masked_image(image, mask_image)
426
- mask, masked_image_latents = self.prepare_mask_latents(
427
- mask,
428
- masked_image,
429
- batch_size * num_images_per_prompt,
430
- height,
431
- width,
432
- prompt_embeds.dtype,
433
- device,
434
- generator,
435
- do_classifier_free_guidance,
436
- )
437
-
438
- # 7. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
439
- extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)
440
-
441
- # 8. Denoising loop
442
- num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order
443
- with self.progress_bar(total=num_inference_steps) as progress_bar:
444
- for i, t in enumerate(timesteps):
445
- # expand the latents if we are doing classifier free guidance
446
- latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents
447
- latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
448
-
449
- down_block_res_samples, mid_block_res_sample = self.controlnet(
450
- latent_model_input,
451
- t,
452
- encoder_hidden_states=prompt_embeds,
453
- controlnet_cond=control_image,
454
- return_dict=False,
455
- )
456
-
457
- down_block_res_samples = [
458
- down_block_res_sample * controlnet_conditioning_scale
459
- for down_block_res_sample in down_block_res_samples
460
- ]
461
- mid_block_res_sample *= controlnet_conditioning_scale
462
-
463
- # predict the noise residual
464
- latent_model_input = torch.cat([latent_model_input, mask, masked_image_latents], dim=1)
465
- noise_pred = self.unet(
466
- latent_model_input,
467
- t,
468
- encoder_hidden_states=prompt_embeds,
469
- cross_attention_kwargs=cross_attention_kwargs,
470
- down_block_additional_residuals=down_block_res_samples,
471
- mid_block_additional_residual=mid_block_res_sample,
472
- ).sample
473
-
474
- # perform guidance
475
- if do_classifier_free_guidance:
476
- noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
477
- noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
478
-
479
- # compute the previous noisy sample x_t -> x_t-1
480
- latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs).prev_sample
481
-
482
- # call the callback, if provided
483
- if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
484
- progress_bar.update()
485
- if callback is not None and i % callback_steps == 0:
486
- callback(i, t, latents)
487
-
488
- # If we do sequential model offloading, let's offload unet and controlnet
489
- # manually for max memory savings
490
- if hasattr(self, "final_offload_hook") and self.final_offload_hook is not None:
491
- self.unet.to("cpu")
492
- self.controlnet.to("cpu")
493
- torch.cuda.empty_cache()
494
-
495
- if output_type == "latent":
496
- image = latents
497
- has_nsfw_concept = None
498
- elif output_type == "pil":
499
- # 8. Post-processing
500
- image = self.decode_latents(latents)
501
-
502
- # 9. Run safety checker
503
- image, has_nsfw_concept = self.run_safety_checker(image, device, prompt_embeds.dtype)
504
-
505
- # 10. Convert to PIL
506
- image = self.numpy_to_pil(image)
507
- else:
508
- # 8. Post-processing
509
- image = self.decode_latents(latents)
510
-
511
- # 9. Run safety checker
512
- image, has_nsfw_concept = self.run_safety_checker(image, device, prompt_embeds.dtype)
513
-
514
- # Offload last model to CPU
515
- if hasattr(self, "final_offload_hook") and self.final_offload_hook is not None:
516
- self.final_offload_hook.offload()
517
-
518
- if not return_dict:
519
- return (image, has_nsfw_concept)
520
-
521
- return StableDiffusionPipelineOutput(images=image, nsfw_content_detected=has_nsfw_concept)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_lineart.py DELETED
@@ -1,178 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- from controlnet_aux import LineartDetector
4
- from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
5
- from diffusers.utils import load_image
6
-
7
- from diffusion_webui.utils.model_list import (
8
- controlnet_lineart_model_list,
9
- stable_model_list,
10
- )
11
- from diffusion_webui.utils.scheduler_list import (
12
- SCHEDULER_LIST,
13
- get_scheduler_list,
14
- )
15
-
16
-
17
- class StableDiffusionControlNetLineArtGenerator:
18
- def __init__(self):
19
- self.pipe = None
20
-
21
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
22
- if self.pipe is None:
23
- controlnet = ControlNetModel.from_pretrained(
24
- controlnet_model_path, torch_dtype=torch.float16
25
- )
26
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
27
- pretrained_model_name_or_path=stable_model_path,
28
- controlnet=controlnet,
29
- safety_checker=None,
30
- torch_dtype=torch.float16,
31
- )
32
-
33
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
34
- self.pipe.to("cuda")
35
- self.pipe.enable_xformers_memory_efficient_attention()
36
-
37
- return self.pipe
38
-
39
- def controlnet_lineart(
40
- self,
41
- image_path: str,
42
- ):
43
- image = load_image(image_path)
44
- image = image.resize((512, 512))
45
- processor = LineartDetector.from_pretrained("lllyasviel/Annotators")
46
- control_image = processor(image)
47
- return control_image
48
-
49
- def generate_image(
50
- self,
51
- image_path: str,
52
- stable_model_path: str,
53
- controlnet_model_path: str,
54
- prompt: str,
55
- negative_prompt: str,
56
- num_images_per_prompt: int,
57
- guidance_scale: int,
58
- num_inference_step: int,
59
- scheduler: str,
60
- seed_generator: int,
61
- ):
62
- pipe = self.load_model(
63
- stable_model_path=stable_model_path,
64
- controlnet_model_path=controlnet_model_path,
65
- scheduler=scheduler,
66
- )
67
-
68
- image = self.controlnet_lineart(image_path)
69
-
70
- if seed_generator == 0:
71
- random_seed = torch.randint(0, 1000000, (1,))
72
- generator = torch.manual_seed(random_seed)
73
- else:
74
- generator = torch.manual_seed(seed_generator)
75
-
76
- output = pipe(
77
- prompt=prompt,
78
- image=image,
79
- negative_prompt=negative_prompt,
80
- num_images_per_prompt=num_images_per_prompt,
81
- num_inference_steps=num_inference_step,
82
- guidance_scale=guidance_scale,
83
- generator=generator,
84
- ).images
85
-
86
- return output
87
-
88
- def app():
89
- with gr.Blocks():
90
- with gr.Row():
91
- with gr.Column():
92
- controlnet_canny_image_file = gr.Image(
93
- type="filepath", label="Image"
94
- )
95
-
96
- controlnet_canny_prompt = gr.Textbox(
97
- lines=1,
98
- placeholder="Prompt",
99
- show_label=False,
100
- )
101
-
102
- controlnet_canny_negative_prompt = gr.Textbox(
103
- lines=1,
104
- placeholder="Negative Prompt",
105
- show_label=False,
106
- )
107
- with gr.Row():
108
- with gr.Column():
109
- controlnet_canny_stable_model_id = gr.Dropdown(
110
- choices=stable_model_list,
111
- value=stable_model_list[0],
112
- label="Stable Model Id",
113
- )
114
-
115
- controlnet_canny_guidance_scale = gr.Slider(
116
- minimum=0.1,
117
- maximum=15,
118
- step=0.1,
119
- value=7.5,
120
- label="Guidance Scale",
121
- )
122
- controlnet_canny_num_inference_step = gr.Slider(
123
- minimum=1,
124
- maximum=100,
125
- step=1,
126
- value=50,
127
- label="Num Inference Step",
128
- )
129
- controlnet_canny_num_images_per_prompt = gr.Slider(
130
- minimum=1,
131
- maximum=10,
132
- step=1,
133
- value=1,
134
- label="Number Of Images",
135
- )
136
- with gr.Row():
137
- with gr.Column():
138
- controlnet_canny_model_id = gr.Dropdown(
139
- choices=controlnet_lineart_model_list,
140
- value=controlnet_lineart_model_list[0],
141
- label="ControlNet Model Id",
142
- )
143
-
144
- controlnet_canny_scheduler = gr.Dropdown(
145
- choices=SCHEDULER_LIST,
146
- value=SCHEDULER_LIST[0],
147
- label="Scheduler",
148
- )
149
-
150
- controlnet_canny_seed_generator = gr.Number(
151
- value=0,
152
- label="Seed Generator",
153
- )
154
- controlnet_canny_predict = gr.Button(value="Generator")
155
-
156
- with gr.Column():
157
- output_image = gr.Gallery(
158
- label="Generated images",
159
- show_label=False,
160
- elem_id="gallery",
161
- ).style(grid=(1, 2))
162
-
163
- controlnet_canny_predict.click(
164
- fn=StableDiffusionControlNetLineArtGenerator().generate_image,
165
- inputs=[
166
- controlnet_canny_image_file,
167
- controlnet_canny_stable_model_id,
168
- controlnet_canny_model_id,
169
- controlnet_canny_prompt,
170
- controlnet_canny_negative_prompt,
171
- controlnet_canny_num_images_per_prompt,
172
- controlnet_canny_guidance_scale,
173
- controlnet_canny_num_inference_step,
174
- controlnet_canny_scheduler,
175
- controlnet_canny_seed_generator,
176
- ],
177
- outputs=[output_image],
178
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_lineart_anime.py DELETED
@@ -1,191 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- from controlnet_aux import LineartAnimeDetector
4
- from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
5
- from diffusers.utils import load_image
6
- from transformers import CLIPTextModel
7
-
8
- from diffusion_webui.utils.model_list import (
9
- controlnet_lineart_anime_model_list,
10
- stable_model_list,
11
- )
12
- from diffusion_webui.utils.scheduler_list import (
13
- SCHEDULER_LIST,
14
- get_scheduler_list,
15
- )
16
-
17
-
18
- class StableDiffusionControlNetLineArtAnimeGenerator:
19
- def __init__(self):
20
- self.pipe = None
21
-
22
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
23
- if self.pipe is None:
24
- text_encoder = CLIPTextModel.from_pretrained(
25
- stable_model_path,
26
- subfolder="text_encoder",
27
- num_hidden_layers=11,
28
- torch_dtype=torch.float16,
29
- )
30
-
31
- controlnet = ControlNetModel.from_pretrained(
32
- controlnet_model_path, torch_dtype=torch.float16
33
- )
34
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
35
- pretrained_model_name_or_path=stable_model_path,
36
- text_encoder=text_encoder,
37
- controlnet=controlnet,
38
- safety_checker=None,
39
- torch_dtype=torch.float16,
40
- )
41
-
42
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
43
- self.pipe.to("cuda")
44
- self.pipe.enable_xformers_memory_efficient_attention()
45
-
46
- return self.pipe
47
-
48
- def controlnet_lineart_anime(
49
- self,
50
- image_path: str,
51
- ):
52
- image = load_image(image_path)
53
- image = image.resize((512, 512))
54
- processor = LineartAnimeDetector.from_pretrained(
55
- "lllyasviel/Annotators"
56
- )
57
- control_image = processor(image)
58
- return control_image
59
-
60
- def generate_image(
61
- self,
62
- image_path: str,
63
- stable_model_path: str,
64
- controlnet_model_path: str,
65
- prompt: str,
66
- negative_prompt: str,
67
- num_images_per_prompt: int,
68
- guidance_scale: int,
69
- num_inference_step: int,
70
- scheduler: str,
71
- seed_generator: int,
72
- ):
73
- pipe = self.load_model(
74
- stable_model_path=stable_model_path,
75
- controlnet_model_path=controlnet_model_path,
76
- scheduler=scheduler,
77
- )
78
-
79
- image = self.controlnet_lineart_anime(image_path)
80
-
81
- if seed_generator == 0:
82
- random_seed = torch.randint(0, 1000000, (1,))
83
- generator = torch.manual_seed(random_seed)
84
- else:
85
- generator = torch.manual_seed(seed_generator)
86
-
87
- output = pipe(
88
- prompt=prompt,
89
- image=image,
90
- negative_prompt=negative_prompt,
91
- num_images_per_prompt=num_images_per_prompt,
92
- num_inference_steps=num_inference_step,
93
- guidance_scale=guidance_scale,
94
- generator=generator,
95
- ).images
96
-
97
- return output
98
-
99
- def app():
100
- with gr.Blocks():
101
- with gr.Row():
102
- with gr.Column():
103
- controlnet_canny_image_file = gr.Image(
104
- type="filepath", label="Image"
105
- )
106
-
107
- controlnet_canny_prompt = gr.Textbox(
108
- lines=1,
109
- placeholder="Prompt",
110
- show_label=False,
111
- )
112
-
113
- controlnet_canny_negative_prompt = gr.Textbox(
114
- lines=1,
115
- placeholder="Negative Prompt",
116
- show_label=False,
117
- )
118
- with gr.Row():
119
- with gr.Column():
120
- controlnet_canny_stable_model_id = gr.Dropdown(
121
- choices=stable_model_list,
122
- value=stable_model_list[0],
123
- label="Stable Model Id",
124
- )
125
-
126
- controlnet_canny_guidance_scale = gr.Slider(
127
- minimum=0.1,
128
- maximum=15,
129
- step=0.1,
130
- value=7.5,
131
- label="Guidance Scale",
132
- )
133
- controlnet_canny_num_inference_step = gr.Slider(
134
- minimum=1,
135
- maximum=100,
136
- step=1,
137
- value=50,
138
- label="Num Inference Step",
139
- )
140
- controlnet_canny_num_images_per_prompt = gr.Slider(
141
- minimum=1,
142
- maximum=10,
143
- step=1,
144
- value=1,
145
- label="Number Of Images",
146
- )
147
- with gr.Row():
148
- with gr.Column():
149
- controlnet_canny_model_id = gr.Dropdown(
150
- choices=controlnet_lineart_anime_model_list,
151
- value=controlnet_lineart_anime_model_list[
152
- 0
153
- ],
154
- label="ControlNet Model Id",
155
- )
156
-
157
- controlnet_canny_scheduler = gr.Dropdown(
158
- choices=SCHEDULER_LIST,
159
- value=SCHEDULER_LIST[0],
160
- label="Scheduler",
161
- )
162
-
163
- controlnet_canny_seed_generator = gr.Number(
164
- value=0,
165
- label="Seed Generator",
166
- )
167
- controlnet_canny_predict = gr.Button(value="Generator")
168
-
169
- with gr.Column():
170
- output_image = gr.Gallery(
171
- label="Generated images",
172
- show_label=False,
173
- elem_id="gallery",
174
- ).style(grid=(1, 2))
175
-
176
- controlnet_canny_predict.click(
177
- fn=StableDiffusionControlNetLineArtAnimeGenerator().generate_image,
178
- inputs=[
179
- controlnet_canny_image_file,
180
- controlnet_canny_stable_model_id,
181
- controlnet_canny_model_id,
182
- controlnet_canny_prompt,
183
- controlnet_canny_negative_prompt,
184
- controlnet_canny_num_images_per_prompt,
185
- controlnet_canny_guidance_scale,
186
- controlnet_canny_num_inference_step,
187
- controlnet_canny_scheduler,
188
- controlnet_canny_seed_generator,
189
- ],
190
- outputs=[output_image],
191
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_mlsd.py DELETED
@@ -1,185 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- from controlnet_aux import MLSDdetector
4
- from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
5
- from PIL import Image
6
-
7
- from diffusion_webui.utils.model_list import (
8
- controlnet_mlsd_model_list,
9
- stable_model_list,
10
- )
11
- from diffusion_webui.utils.scheduler_list import (
12
- SCHEDULER_LIST,
13
- get_scheduler_list,
14
- )
15
-
16
-
17
- class StableDiffusionControlNetMLSDGenerator:
18
- def __init__(self):
19
- self.pipe = None
20
-
21
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
22
- if self.pipe is None:
23
- controlnet = ControlNetModel.from_pretrained(
24
- controlnet_model_path, torch_dtype=torch.float16
25
- )
26
-
27
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
28
- pretrained_model_name_or_path=stable_model_path,
29
- controlnet=controlnet,
30
- safety_checker=None,
31
- torch_dtype=torch.float16,
32
- )
33
-
34
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
35
- self.pipe.to("cuda")
36
- self.pipe.enable_xformers_memory_efficient_attention()
37
-
38
- return self.pipe
39
-
40
- def controlnet_mlsd(self, image_path: str):
41
- mlsd = MLSDdetector.from_pretrained("lllyasviel/ControlNet")
42
-
43
- image = Image.open(image_path)
44
- image = mlsd(image)
45
-
46
- return image
47
-
48
- def generate_image(
49
- self,
50
- image_path: str,
51
- stable_model_path: str,
52
- controlnet_model_path: str,
53
- prompt: str,
54
- negative_prompt: str,
55
- num_images_per_prompt: int,
56
- guidance_scale: int,
57
- num_inference_step: int,
58
- scheduler: str,
59
- seed_generator: int,
60
- ):
61
- image = self.controlnet_mlsd(image_path=image_path)
62
-
63
- pipe = self.load_model(
64
- stable_model_path=stable_model_path,
65
- controlnet_model_path=controlnet_model_path,
66
- scheduler=scheduler,
67
- )
68
-
69
- if seed_generator == 0:
70
- random_seed = torch.randint(0, 1000000, (1,))
71
- generator = torch.manual_seed(random_seed)
72
- else:
73
- generator = torch.manual_seed(seed_generator)
74
-
75
- output = pipe(
76
- prompt=prompt,
77
- image=image,
78
- negative_prompt=negative_prompt,
79
- num_images_per_prompt=num_images_per_prompt,
80
- num_inference_steps=num_inference_step,
81
- guidance_scale=guidance_scale,
82
- generator=generator,
83
- ).images
84
-
85
- return output
86
-
87
- def app():
88
- with gr.Blocks():
89
- with gr.Row():
90
- with gr.Column():
91
- controlnet_mlsd_image_file = gr.Image(
92
- type="filepath", label="Image"
93
- )
94
-
95
- controlnet_mlsd_prompt = gr.Textbox(
96
- lines=1,
97
- show_label=False,
98
- placeholder="Prompt",
99
- )
100
-
101
- controlnet_mlsd_negative_prompt = gr.Textbox(
102
- lines=1,
103
- show_label=False,
104
- placeholder="Negative Prompt",
105
- )
106
-
107
- with gr.Row():
108
- with gr.Column():
109
- controlnet_mlsd_model_id = gr.Dropdown(
110
- choices=stable_model_list,
111
- value=stable_model_list[0],
112
- label="Stable Model Id",
113
- )
114
- controlnet_mlsd_guidance_scale = gr.Slider(
115
- minimum=0.1,
116
- maximum=15,
117
- step=0.1,
118
- value=7.5,
119
- label="Guidance Scale",
120
- )
121
- controlnet_mlsd_num_inference_step = gr.Slider(
122
- minimum=1,
123
- maximum=100,
124
- step=1,
125
- value=50,
126
- label="Num Inference Step",
127
- )
128
-
129
- with gr.Row():
130
- with gr.Column():
131
- controlnet_mlsd_controlnet_model_id = (
132
- gr.Dropdown(
133
- choices=controlnet_mlsd_model_list,
134
- value=controlnet_mlsd_model_list[0],
135
- label="ControlNet Model Id",
136
- )
137
- )
138
- controlnet_mlsd_scheduler = gr.Dropdown(
139
- choices=SCHEDULER_LIST,
140
- value=SCHEDULER_LIST[0],
141
- label="Scheduler",
142
- )
143
-
144
- controlnet_mlsd_seed_generator = gr.Slider(
145
- minimum=0,
146
- maximum=1000000,
147
- step=1,
148
- value=0,
149
- label="Seed Generator",
150
- )
151
- controlnet_mlsd_num_images_per_prompt = (
152
- gr.Slider(
153
- minimum=1,
154
- maximum=10,
155
- step=1,
156
- value=1,
157
- label="Number Of Images",
158
- )
159
- )
160
-
161
- controlnet_mlsd_predict = gr.Button(value="Generator")
162
-
163
- with gr.Column():
164
- output_image = gr.Gallery(
165
- label="Generated images",
166
- show_label=False,
167
- elem_id="gallery",
168
- ).style(grid=(1, 2))
169
-
170
- controlnet_mlsd_predict.click(
171
- fn=StableDiffusionControlNetMLSDGenerator().generate_image,
172
- inputs=[
173
- controlnet_mlsd_image_file,
174
- controlnet_mlsd_model_id,
175
- controlnet_mlsd_controlnet_model_id,
176
- controlnet_mlsd_prompt,
177
- controlnet_mlsd_negative_prompt,
178
- controlnet_mlsd_num_images_per_prompt,
179
- controlnet_mlsd_guidance_scale,
180
- controlnet_mlsd_num_inference_step,
181
- controlnet_mlsd_scheduler,
182
- controlnet_mlsd_seed_generator,
183
- ],
184
- outputs=output_image,
185
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_normal.py DELETED
@@ -1,193 +0,0 @@
1
- import cv2
2
- import gradio as gr
3
- import numpy as np
4
- import torch
5
- from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
6
- from diffusers.utils import load_image
7
- from PIL import Image
8
- from transformers import pipeline
9
-
10
- from diffusion_webui.utils.model_list import (
11
- controlnet_normal_model_list,
12
- stable_model_list,
13
- )
14
- from diffusion_webui.utils.scheduler_list import (
15
- SCHEDULER_LIST,
16
- get_scheduler_list,
17
- )
18
-
19
-
20
- class StableDiffusionControlNetNormalGenerator:
21
- def __init__(self):
22
- self.pipe = None
23
-
24
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
25
- if self.pipe is None:
26
- controlnet = ControlNetModel.from_pretrained(
27
- controlnet_model_path, torch_dtype=torch.float16
28
- )
29
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
30
- pretrained_model_name_or_path=stable_model_path,
31
- controlnet=controlnet,
32
- safety_checker=None,
33
- torch_dtype=torch.float16,
34
- )
35
-
36
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
37
- self.pipe.to("cuda")
38
- self.pipe.enable_xformers_memory_efficient_attention()
39
-
40
- return self.pipe
41
-
42
- def controlnet_normal(
43
- self,
44
- image_path: str,
45
- ):
46
- image = load_image(image_path).convert("RGB")
47
- depth_estimator = pipeline(
48
- "depth-estimation", model="Intel/dpt-hybrid-midas"
49
- )
50
- image = depth_estimator(image)["predicted_depth"][0]
51
- image = image.numpy()
52
- image_depth = image.copy()
53
- image_depth -= np.min(image_depth)
54
- image_depth /= np.max(image_depth)
55
- bg_threhold = 0.4
56
- x = cv2.Sobel(image, cv2.CV_32F, 1, 0, ksize=3)
57
- x[image_depth < bg_threhold] = 0
58
- y = cv2.Sobel(image, cv2.CV_32F, 0, 1, ksize=3)
59
- y[image_depth < bg_threhold] = 0
60
- z = np.ones_like(x) * np.pi * 2.0
61
- image = np.stack([x, y, z], axis=2)
62
- image /= np.sum(image ** 2.0, axis=2, keepdims=True) ** 0.5
63
- image = (image * 127.5 + 127.5).clip(0, 255).astype(np.uint8)
64
- image = Image.fromarray(image)
65
- return image
66
-
67
- def generate_image(
68
- self,
69
- image_path: str,
70
- stable_model_path: str,
71
- controlnet_model_path: str,
72
- prompt: str,
73
- negative_prompt: str,
74
- num_images_per_prompt: int,
75
- guidance_scale: int,
76
- num_inference_step: int,
77
- scheduler: str,
78
- seed_generator: int,
79
- ):
80
- pipe = self.load_model(
81
- stable_model_path, controlnet_model_path, scheduler
82
- )
83
- image = self.controlnet_normal(image_path)
84
-
85
- if seed_generator == 0:
86
- random_seed = torch.randint(0, 1000000, (1,))
87
- generator = torch.manual_seed(random_seed)
88
- else:
89
- generator = torch.manual_seed(seed_generator)
90
-
91
- output = pipe(
92
- prompt=prompt,
93
- image=image,
94
- negative_prompt=negative_prompt,
95
- num_images_per_prompt=num_images_per_prompt,
96
- num_inference_steps=num_inference_step,
97
- guidance_scale=guidance_scale,
98
- generator=generator,
99
- ).images
100
-
101
- return output
102
-
103
- def app():
104
- with gr.Blocks():
105
- with gr.Row():
106
- with gr.Column():
107
- controlnet_normal_image_file = gr.Image(
108
- type="filepath", label="Image"
109
- )
110
-
111
- controlnet_normal_prompt = gr.Textbox(
112
- lines=1,
113
- placeholder="Prompt",
114
- show_label=False,
115
- )
116
-
117
- controlnet_normal_negative_prompt = gr.Textbox(
118
- lines=1,
119
- placeholder="Negative Prompt",
120
- show_label=False,
121
- )
122
- with gr.Row():
123
- with gr.Column():
124
- controlnet_normal_stable_model_id = gr.Dropdown(
125
- choices=stable_model_list,
126
- value=stable_model_list[0],
127
- label="Stable Model Id",
128
- )
129
-
130
- controlnet_normal_guidance_scale = gr.Slider(
131
- minimum=0.1,
132
- maximum=15,
133
- step=0.1,
134
- value=7.5,
135
- label="Guidance Scale",
136
- )
137
- controlnet_normal_num_inference_step = gr.Slider(
138
- minimum=1,
139
- maximum=100,
140
- step=1,
141
- value=50,
142
- label="Num Inference Step",
143
- )
144
- controlnet_normal_num_images_per_prompt = gr.Slider(
145
- minimum=1,
146
- maximum=10,
147
- step=1,
148
- value=1,
149
- label="Number Of Images",
150
- )
151
- with gr.Row():
152
- with gr.Column():
153
- controlnet_normal_model_id = gr.Dropdown(
154
- choices=controlnet_normal_model_list,
155
- value=controlnet_normal_model_list[0],
156
- label="ControlNet Model Id",
157
- )
158
-
159
- controlnet_normal_scheduler = gr.Dropdown(
160
- choices=SCHEDULER_LIST,
161
- value=SCHEDULER_LIST[0],
162
- label="Scheduler",
163
- )
164
-
165
- controlnet_normal_seed_generator = gr.Number(
166
- value=0,
167
- label="Seed Generator",
168
- )
169
- controlnet_normal_predict = gr.Button(value="Generator")
170
-
171
- with gr.Column():
172
- output_image = gr.Gallery(
173
- label="Generated images",
174
- show_label=False,
175
- elem_id="gallery",
176
- ).style(grid=(1, 2))
177
-
178
- controlnet_normal_predict.click(
179
- fn=StableDiffusionControlNetNormalGenerator().generate_image,
180
- inputs=[
181
- controlnet_normal_image_file,
182
- controlnet_normal_stable_model_id,
183
- controlnet_normal_model_id,
184
- controlnet_normal_prompt,
185
- controlnet_normal_negative_prompt,
186
- controlnet_normal_num_images_per_prompt,
187
- controlnet_normal_guidance_scale,
188
- controlnet_normal_num_inference_step,
189
- controlnet_normal_scheduler,
190
- controlnet_normal_seed_generator,
191
- ],
192
- outputs=[output_image],
193
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_pix2pix.py DELETED
@@ -1,174 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
4
- from diffusers.utils import load_image
5
-
6
- from diffusion_webui.utils.model_list import (
7
- controlnet_lineart_model_list,
8
- stable_model_list,
9
- )
10
- from diffusion_webui.utils.scheduler_list import (
11
- SCHEDULER_LIST,
12
- get_scheduler_list,
13
- )
14
-
15
-
16
- class StableDiffusionControlNetPix2PixGenerator:
17
- def __init__(self):
18
- self.pipe = None
19
-
20
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
21
- if self.pipe is None:
22
- controlnet = ControlNetModel.from_pretrained(
23
- controlnet_model_path, torch_dtype=torch.float16
24
- )
25
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
26
- pretrained_model_name_or_path=stable_model_path,
27
- controlnet=controlnet,
28
- safety_checker=None,
29
- torch_dtype=torch.float16,
30
- )
31
-
32
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
33
- self.pipe.to("cuda")
34
- self.pipe.enable_xformers_memory_efficient_attention()
35
-
36
- return self.pipe
37
-
38
- def controlnet_pix2pix(
39
- self,
40
- image_path: str,
41
- ):
42
- control_image = load_image(image_path).convert("RGB")
43
- return control_image
44
-
45
- def generate_image(
46
- self,
47
- image_path: str,
48
- stable_model_path: str,
49
- controlnet_model_path: str,
50
- prompt: str,
51
- negative_prompt: str,
52
- num_images_per_prompt: int,
53
- guidance_scale: int,
54
- num_inference_step: int,
55
- scheduler: str,
56
- seed_generator: int,
57
- ):
58
- pipe = self.load_model(
59
- stable_model_path=stable_model_path,
60
- controlnet_model_path=controlnet_model_path,
61
- scheduler=scheduler,
62
- )
63
-
64
- image = self.controlnet_pix2pix(image_path)
65
-
66
- if seed_generator == 0:
67
- random_seed = torch.randint(0, 1000000, (1,))
68
- generator = torch.manual_seed(random_seed)
69
- else:
70
- generator = torch.manual_seed(seed_generator)
71
-
72
- output = pipe(
73
- prompt=prompt,
74
- image=image,
75
- negative_prompt=negative_prompt,
76
- num_images_per_prompt=num_images_per_prompt,
77
- num_inference_steps=num_inference_step,
78
- guidance_scale=guidance_scale,
79
- generator=generator,
80
- ).images
81
-
82
- return output
83
-
84
- def app():
85
- with gr.Blocks():
86
- with gr.Row():
87
- with gr.Column():
88
- controlnet_canny_image_file = gr.Image(
89
- type="filepath", label="Image"
90
- )
91
-
92
- controlnet_canny_prompt = gr.Textbox(
93
- lines=1,
94
- placeholder="Prompt",
95
- show_label=False,
96
- )
97
-
98
- controlnet_canny_negative_prompt = gr.Textbox(
99
- lines=1,
100
- placeholder="Negative Prompt",
101
- show_label=False,
102
- )
103
- with gr.Row():
104
- with gr.Column():
105
- controlnet_canny_stable_model_id = gr.Dropdown(
106
- choices=stable_model_list,
107
- value=stable_model_list[0],
108
- label="Stable Model Id",
109
- )
110
-
111
- controlnet_canny_guidance_scale = gr.Slider(
112
- minimum=0.1,
113
- maximum=15,
114
- step=0.1,
115
- value=7.5,
116
- label="Guidance Scale",
117
- )
118
- controlnet_canny_num_inference_step = gr.Slider(
119
- minimum=1,
120
- maximum=100,
121
- step=1,
122
- value=50,
123
- label="Num Inference Step",
124
- )
125
- controlnet_canny_num_images_per_prompt = gr.Slider(
126
- minimum=1,
127
- maximum=10,
128
- step=1,
129
- value=1,
130
- label="Number Of Images",
131
- )
132
- with gr.Row():
133
- with gr.Column():
134
- controlnet_canny_model_id = gr.Dropdown(
135
- choices=controlnet_lineart_model_list,
136
- value=controlnet_lineart_model_list[0],
137
- label="ControlNet Model Id",
138
- )
139
-
140
- controlnet_canny_scheduler = gr.Dropdown(
141
- choices=SCHEDULER_LIST,
142
- value=SCHEDULER_LIST[0],
143
- label="Scheduler",
144
- )
145
-
146
- controlnet_canny_seed_generator = gr.Number(
147
- value=0,
148
- label="Seed Generator",
149
- )
150
- controlnet_canny_predict = gr.Button(value="Generator")
151
-
152
- with gr.Column():
153
- output_image = gr.Gallery(
154
- label="Generated images",
155
- show_label=False,
156
- elem_id="gallery",
157
- ).style(grid=(1, 2))
158
-
159
- controlnet_canny_predict.click(
160
- fn=StableDiffusionControlNetPix2PixGenerator().generate_image,
161
- inputs=[
162
- controlnet_canny_image_file,
163
- controlnet_canny_stable_model_id,
164
- controlnet_canny_model_id,
165
- controlnet_canny_prompt,
166
- controlnet_canny_negative_prompt,
167
- controlnet_canny_num_images_per_prompt,
168
- controlnet_canny_guidance_scale,
169
- controlnet_canny_num_inference_step,
170
- controlnet_canny_scheduler,
171
- controlnet_canny_seed_generator,
172
- ],
173
- outputs=[output_image],
174
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_pose.py DELETED
@@ -1,189 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- from controlnet_aux import OpenposeDetector
4
- from diffusers import (
5
- ControlNetModel,
6
- StableDiffusionControlNetPipeline,
7
- UniPCMultistepScheduler,
8
- )
9
- from PIL import Image
10
-
11
- from diffusion_webui.utils.model_list import (
12
- controlnet_pose_model_list,
13
- stable_model_list,
14
- )
15
- from diffusion_webui.utils.scheduler_list import (
16
- SCHEDULER_LIST,
17
- get_scheduler_list,
18
- )
19
-
20
-
21
- class StableDiffusionControlNetPoseGenerator:
22
- def __init__(self):
23
- self.pipe = None
24
-
25
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
26
- if self.pipe is None:
27
- controlnet = ControlNetModel.from_pretrained(
28
- controlnet_model_path, torch_dtype=torch.float16
29
- )
30
-
31
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
32
- pretrained_model_name_or_path=stable_model_path,
33
- controlnet=controlnet,
34
- safety_checker=None,
35
- torch_dtype=torch.float16,
36
- )
37
-
38
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
39
- self.pipe.to("cuda")
40
- self.pipe.enable_xformers_memory_efficient_attention()
41
-
42
- return self.pipe
43
-
44
- def controlnet_pose(self, image_path: str):
45
- openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
46
-
47
- image = Image.open(image_path)
48
- image = openpose(image)
49
-
50
- return image
51
-
52
- def generate_image(
53
- self,
54
- image_path: str,
55
- stable_model_path: str,
56
- controlnet_pose_model_path: str,
57
- prompt: str,
58
- negative_prompt: str,
59
- num_images_per_prompt: int,
60
- guidance_scale: int,
61
- num_inference_step: int,
62
- scheduler: str,
63
- seed_generator: int,
64
- ):
65
-
66
- image = self.controlnet_pose(image_path=image_path)
67
-
68
- pipe = self.load_model(
69
- stable_model_path=stable_model_path,
70
- controlnet_model_path=controlnet_pose_model_path,
71
- scheduler=scheduler,
72
- )
73
-
74
- if seed_generator == 0:
75
- random_seed = torch.randint(0, 1000000, (1,))
76
- generator = torch.manual_seed(random_seed)
77
- else:
78
- generator = torch.manual_seed(seed_generator)
79
-
80
- output = pipe(
81
- prompt=prompt,
82
- image=image,
83
- negative_prompt=negative_prompt,
84
- num_images_per_prompt=num_images_per_prompt,
85
- num_inference_steps=num_inference_step,
86
- guidance_scale=guidance_scale,
87
- generator=generator,
88
- ).images
89
-
90
- return output
91
-
92
- def app():
93
- with gr.Blocks():
94
- with gr.Row():
95
- with gr.Column():
96
- controlnet_pose_image_file = gr.Image(
97
- type="filepath", label="Image"
98
- )
99
-
100
- controlnet_pose_prompt = gr.Textbox(
101
- lines=1,
102
- show_label=False,
103
- placeholder="Prompt",
104
- )
105
-
106
- controlnet_pose_negative_prompt = gr.Textbox(
107
- lines=1,
108
- show_label=False,
109
- placeholder="Negative Prompt",
110
- )
111
-
112
- with gr.Row():
113
- with gr.Column():
114
- controlnet_pose_stable_model_id = gr.Dropdown(
115
- choices=stable_model_list,
116
- value=stable_model_list[0],
117
- label="Stable Model Id",
118
- )
119
- controlnet_pose_guidance_scale = gr.Slider(
120
- minimum=0.1,
121
- maximum=15,
122
- step=0.1,
123
- value=7.5,
124
- label="Guidance Scale",
125
- )
126
-
127
- controlnet_pose_num_inference_step = gr.Slider(
128
- minimum=1,
129
- maximum=100,
130
- step=1,
131
- value=50,
132
- label="Num Inference Step",
133
- )
134
-
135
- controlnet_pose_num_images_per_prompt = gr.Slider(
136
- minimum=1,
137
- maximum=10,
138
- step=1,
139
- value=1,
140
- label="Number Of Images",
141
- )
142
-
143
- with gr.Row():
144
- with gr.Column():
145
- controlnet_pose_model_id = gr.Dropdown(
146
- choices=controlnet_pose_model_list,
147
- value=controlnet_pose_model_list[0],
148
- label="ControlNet Model Id",
149
- )
150
-
151
- controlnet_pose_scheduler = gr.Dropdown(
152
- choices=SCHEDULER_LIST,
153
- value=SCHEDULER_LIST[0],
154
- label="Scheduler",
155
- )
156
-
157
- controlnet_pose_seed_generator = gr.Number(
158
- minimum=0,
159
- maximum=1000000,
160
- step=1,
161
- value=0,
162
- label="Seed Generator",
163
- )
164
-
165
- controlnet_pose_predict = gr.Button(value="Generator")
166
-
167
- with gr.Column():
168
- output_image = gr.Gallery(
169
- label="Generated images",
170
- show_label=False,
171
- elem_id="gallery",
172
- ).style(grid=(1, 2))
173
-
174
- controlnet_pose_predict.click(
175
- fn=StableDiffusionControlNetPoseGenerator().generate_image,
176
- inputs=[
177
- controlnet_pose_image_file,
178
- controlnet_pose_stable_model_id,
179
- controlnet_pose_model_id,
180
- controlnet_pose_prompt,
181
- controlnet_pose_negative_prompt,
182
- controlnet_pose_num_images_per_prompt,
183
- controlnet_pose_guidance_scale,
184
- controlnet_pose_num_inference_step,
185
- controlnet_pose_scheduler,
186
- controlnet_pose_seed_generator,
187
- ],
188
- outputs=output_image,
189
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_scribble.py DELETED
@@ -1,188 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- from controlnet_aux import HEDdetector
4
- from diffusers import (
5
- ControlNetModel,
6
- StableDiffusionControlNetPipeline,
7
- UniPCMultistepScheduler,
8
- )
9
- from PIL import Image
10
-
11
- from diffusion_webui.utils.model_list import (
12
- controlnet_scribble_model_list,
13
- stable_model_list,
14
- )
15
- from diffusion_webui.utils.scheduler_list import (
16
- SCHEDULER_LIST,
17
- get_scheduler_list,
18
- )
19
-
20
-
21
- class StableDiffusionControlNetScribbleGenerator:
22
- def __init__(self):
23
- self.pipe = None
24
-
25
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
26
- if self.pipe is None:
27
- controlnet = ControlNetModel.from_pretrained(
28
- controlnet_model_path, torch_dtype=torch.float16
29
- )
30
-
31
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
32
- pretrained_model_name_or_path=stable_model_path,
33
- controlnet=controlnet,
34
- safety_checker=None,
35
- torch_dtype=torch.float16,
36
- )
37
-
38
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
39
- self.pipe.to("cuda")
40
- self.pipe.enable_xformers_memory_efficient_attention()
41
-
42
- return self.pipe
43
-
44
- def controlnet_scribble(self, image_path: str):
45
- hed = HEDdetector.from_pretrained("lllyasviel/ControlNet")
46
-
47
- image = Image.open(image_path)
48
- image = hed(image, scribble=True)
49
-
50
- return image
51
-
52
- def generate_image(
53
- self,
54
- image_path: str,
55
- stable_model_path: str,
56
- controlnet_hed_model_path: str,
57
- prompt: str,
58
- negative_prompt: str,
59
- num_images_per_prompt: int,
60
- guidance_scale: int,
61
- num_inference_step: int,
62
- scheduler: str,
63
- seed_generator: int,
64
- ):
65
-
66
- image = self.controlnet_scribble(image_path=image_path)
67
-
68
- pipe = self.load_model(
69
- stable_model_path=stable_model_path,
70
- controlnet_model_path=controlnet_hed_model_path,
71
- scheduler=scheduler,
72
- )
73
- if seed_generator == 0:
74
- random_seed = torch.randint(0, 1000000, (1,))
75
- generator = torch.manual_seed(random_seed)
76
- else:
77
- generator = torch.manual_seed(seed_generator)
78
-
79
- output = pipe(
80
- prompt=prompt,
81
- image=image,
82
- negative_prompt=negative_prompt,
83
- num_images_per_prompt=num_images_per_prompt,
84
- num_inference_steps=num_inference_step,
85
- guidance_scale=guidance_scale,
86
- generator=generator,
87
- ).images
88
-
89
- return output
90
-
91
- def app():
92
- with gr.Blocks():
93
- with gr.Row():
94
- with gr.Column():
95
- controlnet_scribble_image_file = gr.Image(
96
- type="filepath", label="Image"
97
- )
98
- controlnet_scribble_prompt = gr.Textbox(
99
- lines=1,
100
- show_label=False,
101
- placeholder="Prompt",
102
- )
103
-
104
- controlnet_scribble_negative_prompt = gr.Textbox(
105
- lines=1,
106
- show_label=False,
107
- placeholder="Negative Prompt",
108
- )
109
-
110
- with gr.Row():
111
- with gr.Column():
112
- controlnet_scribble_stable_model_id = gr.Dropdown(
113
- choices=stable_model_list,
114
- value=stable_model_list[0],
115
- label="Stable Model Id",
116
- )
117
- controlnet_scribble_guidance_scale = gr.Slider(
118
- minimum=0.1,
119
- maximum=15,
120
- step=0.1,
121
- value=7.5,
122
- label="Guidance Scale",
123
- )
124
-
125
- controlnet_scribble_num_inference_step = gr.Slider(
126
- minimum=1,
127
- maximum=100,
128
- step=1,
129
- value=50,
130
- label="Num Inference Step",
131
- )
132
-
133
- controlnet_scribble_num_images_per_prompt = (
134
- gr.Slider(
135
- minimum=1,
136
- maximum=10,
137
- step=1,
138
- value=1,
139
- label="Number Of Images",
140
- )
141
- )
142
- with gr.Row():
143
- with gr.Column():
144
- controlnet_scribble_model_id = gr.Dropdown(
145
- choices=controlnet_scribble_model_list,
146
- value=controlnet_scribble_model_list[0],
147
- label="ControlNet Model Id",
148
- )
149
-
150
- controlnet_scribble_scheduler = gr.Dropdown(
151
- choices=SCHEDULER_LIST,
152
- value=SCHEDULER_LIST[0],
153
- label="Scheduler",
154
- )
155
-
156
- controlnet_scribble_seed_generator = gr.Number(
157
- minimum=0,
158
- maximum=1000000,
159
- step=1,
160
- value=0,
161
- label="Seed Generator",
162
- )
163
-
164
- controlnet_scribble_predict = gr.Button(value="Generator")
165
-
166
- with gr.Column():
167
- output_image = gr.Gallery(
168
- label="Generated images",
169
- show_label=False,
170
- elem_id="gallery",
171
- ).style(grid=(1, 2))
172
-
173
- controlnet_scribble_predict.click(
174
- fn=StableDiffusionControlNetScribbleGenerator().generate_image,
175
- inputs=[
176
- controlnet_scribble_image_file,
177
- controlnet_scribble_stable_model_id,
178
- controlnet_scribble_model_id,
179
- controlnet_scribble_prompt,
180
- controlnet_scribble_negative_prompt,
181
- controlnet_scribble_num_images_per_prompt,
182
- controlnet_scribble_guidance_scale,
183
- controlnet_scribble_num_inference_step,
184
- controlnet_scribble_scheduler,
185
- controlnet_scribble_seed_generator,
186
- ],
187
- outputs=output_image,
188
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_seg.py DELETED
@@ -1,353 +0,0 @@
1
- import gradio as gr
2
- import numpy as np
3
- import torch
4
- from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
5
- from PIL import Image
6
- from transformers import AutoImageProcessor, UperNetForSemanticSegmentation
7
-
8
- from diffusion_webui.utils.model_list import stable_model_list
9
- from diffusion_webui.utils.scheduler_list import (
10
- SCHEDULER_LIST,
11
- get_scheduler_list,
12
- )
13
-
14
-
15
- def ade_palette():
16
- """ADE20K palette that maps each class to RGB values."""
17
- return [
18
- [120, 120, 120],
19
- [180, 120, 120],
20
- [6, 230, 230],
21
- [80, 50, 50],
22
- [4, 200, 3],
23
- [120, 120, 80],
24
- [140, 140, 140],
25
- [204, 5, 255],
26
- [230, 230, 230],
27
- [4, 250, 7],
28
- [224, 5, 255],
29
- [235, 255, 7],
30
- [150, 5, 61],
31
- [120, 120, 70],
32
- [8, 255, 51],
33
- [255, 6, 82],
34
- [143, 255, 140],
35
- [204, 255, 4],
36
- [255, 51, 7],
37
- [204, 70, 3],
38
- [0, 102, 200],
39
- [61, 230, 250],
40
- [255, 6, 51],
41
- [11, 102, 255],
42
- [255, 7, 71],
43
- [255, 9, 224],
44
- [9, 7, 230],
45
- [220, 220, 220],
46
- [255, 9, 92],
47
- [112, 9, 255],
48
- [8, 255, 214],
49
- [7, 255, 224],
50
- [255, 184, 6],
51
- [10, 255, 71],
52
- [255, 41, 10],
53
- [7, 255, 255],
54
- [224, 255, 8],
55
- [102, 8, 255],
56
- [255, 61, 6],
57
- [255, 194, 7],
58
- [255, 122, 8],
59
- [0, 255, 20],
60
- [255, 8, 41],
61
- [255, 5, 153],
62
- [6, 51, 255],
63
- [235, 12, 255],
64
- [160, 150, 20],
65
- [0, 163, 255],
66
- [140, 140, 140],
67
- [250, 10, 15],
68
- [20, 255, 0],
69
- [31, 255, 0],
70
- [255, 31, 0],
71
- [255, 224, 0],
72
- [153, 255, 0],
73
- [0, 0, 255],
74
- [255, 71, 0],
75
- [0, 235, 255],
76
- [0, 173, 255],
77
- [31, 0, 255],
78
- [11, 200, 200],
79
- [255, 82, 0],
80
- [0, 255, 245],
81
- [0, 61, 255],
82
- [0, 255, 112],
83
- [0, 255, 133],
84
- [255, 0, 0],
85
- [255, 163, 0],
86
- [255, 102, 0],
87
- [194, 255, 0],
88
- [0, 143, 255],
89
- [51, 255, 0],
90
- [0, 82, 255],
91
- [0, 255, 41],
92
- [0, 255, 173],
93
- [10, 0, 255],
94
- [173, 255, 0],
95
- [0, 255, 153],
96
- [255, 92, 0],
97
- [255, 0, 255],
98
- [255, 0, 245],
99
- [255, 0, 102],
100
- [255, 173, 0],
101
- [255, 0, 20],
102
- [255, 184, 184],
103
- [0, 31, 255],
104
- [0, 255, 61],
105
- [0, 71, 255],
106
- [255, 0, 204],
107
- [0, 255, 194],
108
- [0, 255, 82],
109
- [0, 10, 255],
110
- [0, 112, 255],
111
- [51, 0, 255],
112
- [0, 194, 255],
113
- [0, 122, 255],
114
- [0, 255, 163],
115
- [255, 153, 0],
116
- [0, 255, 10],
117
- [255, 112, 0],
118
- [143, 255, 0],
119
- [82, 0, 255],
120
- [163, 255, 0],
121
- [255, 235, 0],
122
- [8, 184, 170],
123
- [133, 0, 255],
124
- [0, 255, 92],
125
- [184, 0, 255],
126
- [255, 0, 31],
127
- [0, 184, 255],
128
- [0, 214, 255],
129
- [255, 0, 112],
130
- [92, 255, 0],
131
- [0, 224, 255],
132
- [112, 224, 255],
133
- [70, 184, 160],
134
- [163, 0, 255],
135
- [153, 0, 255],
136
- [71, 255, 0],
137
- [255, 0, 163],
138
- [255, 204, 0],
139
- [255, 0, 143],
140
- [0, 255, 235],
141
- [133, 255, 0],
142
- [255, 0, 235],
143
- [245, 0, 255],
144
- [255, 0, 122],
145
- [255, 245, 0],
146
- [10, 190, 212],
147
- [214, 255, 0],
148
- [0, 204, 255],
149
- [20, 0, 255],
150
- [255, 255, 0],
151
- [0, 153, 255],
152
- [0, 41, 255],
153
- [0, 255, 204],
154
- [41, 0, 255],
155
- [41, 255, 0],
156
- [173, 0, 255],
157
- [0, 245, 255],
158
- [71, 0, 255],
159
- [122, 0, 255],
160
- [0, 255, 184],
161
- [0, 92, 255],
162
- [184, 255, 0],
163
- [0, 133, 255],
164
- [255, 214, 0],
165
- [25, 194, 194],
166
- [102, 255, 0],
167
- [92, 0, 255],
168
- ]
169
-
170
-
171
- class StableDiffusionControlNetSegGenerator:
172
- def __init__(self):
173
- self.pipe = None
174
-
175
- def load_model(
176
- self,
177
- stable_model_path,
178
- scheduler,
179
- ):
180
-
181
- if self.pipe is None:
182
- controlnet = ControlNetModel.from_pretrained(
183
- "lllyasviel/sd-controlnet-seg", torch_dtype=torch.float16
184
- )
185
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
186
- pretrained_model_name_or_path=stable_model_path,
187
- controlnet=controlnet,
188
- safety_checker=None,
189
- torch_dtype=torch.float16,
190
- )
191
-
192
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
193
- self.pipe.to("cuda")
194
- self.pipe.enable_xformers_memory_efficient_attention()
195
-
196
- return self.pipe
197
-
198
- def controlnet_seg(self, image_path: str):
199
- image_processor = AutoImageProcessor.from_pretrained(
200
- "openmmlab/upernet-convnext-small"
201
- )
202
- image_segmentor = UperNetForSemanticSegmentation.from_pretrained(
203
- "openmmlab/upernet-convnext-small"
204
- )
205
-
206
- image = Image.open(image_path).convert("RGB")
207
- pixel_values = image_processor(image, return_tensors="pt").pixel_values
208
-
209
- with torch.no_grad():
210
- outputs = image_segmentor(pixel_values)
211
-
212
- seg = image_processor.post_process_semantic_segmentation(
213
- outputs, target_sizes=[image.size[::-1]]
214
- )[0]
215
-
216
- color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
217
- palette = np.array(ade_palette())
218
-
219
- for label, color in enumerate(palette):
220
- color_seg[seg == label, :] = color
221
-
222
- color_seg = color_seg.astype(np.uint8)
223
- image = Image.fromarray(color_seg)
224
-
225
- return image
226
-
227
- def generate_image(
228
- self,
229
- image_path: str,
230
- model_path: str,
231
- prompt: str,
232
- negative_prompt: str,
233
- num_images_per_prompt: int,
234
- guidance_scale: int,
235
- num_inference_step: int,
236
- scheduler: str,
237
- seed_generator: int,
238
- ):
239
-
240
- image = self.controlnet_seg(image_path=image_path)
241
- pipe = self.load_model(
242
- stable_model_path=model_path,
243
- scheduler=scheduler,
244
- )
245
- if seed_generator == 0:
246
- random_seed = torch.randint(0, 1000000, (1,))
247
- generator = torch.manual_seed(random_seed)
248
- else:
249
- generator = torch.manual_seed(seed_generator)
250
-
251
- output = pipe(
252
- prompt=prompt,
253
- image=image,
254
- negative_prompt=negative_prompt,
255
- num_images_per_prompt=num_images_per_prompt,
256
- num_inference_steps=num_inference_step,
257
- guidance_scale=guidance_scale,
258
- generator=generator,
259
- ).images
260
-
261
- return output
262
-
263
- def app():
264
- with gr.Blocks():
265
- with gr.Row():
266
- with gr.Column():
267
- controlnet_seg_image_file = gr.Image(
268
- type="filepath", label="Image"
269
- )
270
-
271
- controlnet_seg_prompt = gr.Textbox(
272
- lines=1,
273
- show_label=False,
274
- placeholder="Prompt",
275
- )
276
-
277
- controlnet_seg_negative_prompt = gr.Textbox(
278
- lines=1,
279
- show_label=False,
280
- placeholder="Negative Prompt",
281
- )
282
-
283
- with gr.Row():
284
- with gr.Column():
285
- controlnet_seg_model_id = gr.Dropdown(
286
- choices=stable_model_list,
287
- value=stable_model_list[0],
288
- label="Stable Model Id",
289
- )
290
- controlnet_seg_guidance_scale = gr.Slider(
291
- minimum=0.1,
292
- maximum=15,
293
- step=0.1,
294
- value=7.5,
295
- label="Guidance Scale",
296
- )
297
-
298
- controlnet_seg_num_inference_step = gr.Slider(
299
- minimum=1,
300
- maximum=100,
301
- step=1,
302
- value=50,
303
- label="Num Inference Step",
304
- )
305
-
306
- with gr.Row():
307
- with gr.Column():
308
- controlnet_seg_scheduler = gr.Dropdown(
309
- choices=SCHEDULER_LIST,
310
- value=SCHEDULER_LIST[0],
311
- label="Scheduler",
312
- )
313
- controlnet_seg_num_images_per_prompt = (
314
- gr.Slider(
315
- minimum=1,
316
- maximum=10,
317
- step=1,
318
- value=1,
319
- label="Number Of Images",
320
- )
321
- )
322
- controlnet_seg_seed_generator = gr.Slider(
323
- minimum=0,
324
- maximum=1000000,
325
- step=1,
326
- value=0,
327
- label="Seed Generator",
328
- )
329
-
330
- controlnet_seg_predict = gr.Button(value="Generator")
331
-
332
- with gr.Column():
333
- output_image = gr.Gallery(
334
- label="Generated images",
335
- show_label=False,
336
- elem_id="gallery",
337
- ).style(grid=(1, 2))
338
-
339
- controlnet_seg_predict.click(
340
- fn=StableDiffusionControlNetSegGenerator().generate_image,
341
- inputs=[
342
- controlnet_seg_image_file,
343
- controlnet_seg_model_id,
344
- controlnet_seg_prompt,
345
- controlnet_seg_negative_prompt,
346
- controlnet_seg_num_images_per_prompt,
347
- controlnet_seg_guidance_scale,
348
- controlnet_seg_num_inference_step,
349
- controlnet_seg_scheduler,
350
- controlnet_seg_seed_generator,
351
- ],
352
- outputs=[output_image],
353
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_shuffle.py DELETED
@@ -1,176 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- from controlnet_aux import ContentShuffleDetector
4
- from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
5
- from diffusers.utils import load_image
6
-
7
- from diffusion_webui.utils.model_list import (
8
- controlnet_shuffle_model_list,
9
- stable_model_list,
10
- )
11
- from diffusion_webui.utils.scheduler_list import (
12
- SCHEDULER_LIST,
13
- get_scheduler_list,
14
- )
15
-
16
-
17
- class StableDiffusionControlNetShuffleGenerator:
18
- def __init__(self):
19
- self.pipe = None
20
-
21
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
22
- if self.pipe is None:
23
- controlnet = ControlNetModel.from_pretrained(
24
- controlnet_model_path, torch_dtype=torch.float16
25
- )
26
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
27
- pretrained_model_name_or_path=stable_model_path,
28
- controlnet=controlnet,
29
- safety_checker=None,
30
- torch_dtype=torch.float16,
31
- )
32
-
33
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
34
- self.pipe.to("cuda")
35
- self.pipe.enable_xformers_memory_efficient_attention()
36
-
37
- return self.pipe
38
-
39
- def controlnet_shuffle(
40
- self,
41
- image_path: str,
42
- ):
43
- image = load_image(image_path)
44
- control_image = ContentShuffleDetector()(image)
45
- return control_image
46
-
47
- def generate_image(
48
- self,
49
- image_path: str,
50
- stable_model_path: str,
51
- controlnet_model_path: str,
52
- prompt: str,
53
- negative_prompt: str,
54
- num_images_per_prompt: int,
55
- guidance_scale: int,
56
- num_inference_step: int,
57
- scheduler: str,
58
- seed_generator: int,
59
- ):
60
- pipe = self.load_model(
61
- stable_model_path=stable_model_path,
62
- controlnet_model_path=controlnet_model_path,
63
- scheduler=scheduler,
64
- )
65
-
66
- image = self.controlnet_shuffle(image_path)
67
-
68
- if seed_generator == 0:
69
- random_seed = torch.randint(0, 1000000, (1,))
70
- generator = torch.manual_seed(random_seed)
71
- else:
72
- generator = torch.manual_seed(seed_generator)
73
-
74
- output = pipe(
75
- prompt=prompt,
76
- image=image,
77
- negative_prompt=negative_prompt,
78
- num_images_per_prompt=num_images_per_prompt,
79
- num_inference_steps=num_inference_step,
80
- guidance_scale=guidance_scale,
81
- generator=generator,
82
- ).images
83
-
84
- return output
85
-
86
- def app():
87
- with gr.Blocks():
88
- with gr.Row():
89
- with gr.Column():
90
- controlnet_canny_image_file = gr.Image(
91
- type="filepath", label="Image"
92
- )
93
-
94
- controlnet_canny_prompt = gr.Textbox(
95
- lines=1,
96
- placeholder="Prompt",
97
- show_label=False,
98
- )
99
-
100
- controlnet_canny_negative_prompt = gr.Textbox(
101
- lines=1,
102
- placeholder="Negative Prompt",
103
- show_label=False,
104
- )
105
- with gr.Row():
106
- with gr.Column():
107
- controlnet_canny_stable_model_id = gr.Dropdown(
108
- choices=stable_model_list,
109
- value=stable_model_list[0],
110
- label="Stable Model Id",
111
- )
112
-
113
- controlnet_canny_guidance_scale = gr.Slider(
114
- minimum=0.1,
115
- maximum=15,
116
- step=0.1,
117
- value=7.5,
118
- label="Guidance Scale",
119
- )
120
- controlnet_canny_num_inference_step = gr.Slider(
121
- minimum=1,
122
- maximum=100,
123
- step=1,
124
- value=50,
125
- label="Num Inference Step",
126
- )
127
- controlnet_canny_num_images_per_prompt = gr.Slider(
128
- minimum=1,
129
- maximum=10,
130
- step=1,
131
- value=1,
132
- label="Number Of Images",
133
- )
134
- with gr.Row():
135
- with gr.Column():
136
- controlnet_canny_model_id = gr.Dropdown(
137
- choices=controlnet_shuffle_model_list,
138
- value=controlnet_shuffle_model_list[0],
139
- label="ControlNet Model Id",
140
- )
141
-
142
- controlnet_canny_scheduler = gr.Dropdown(
143
- choices=SCHEDULER_LIST,
144
- value=SCHEDULER_LIST[0],
145
- label="Scheduler",
146
- )
147
-
148
- controlnet_canny_seed_generator = gr.Number(
149
- value=0,
150
- label="Seed Generator",
151
- )
152
- controlnet_canny_predict = gr.Button(value="Generator")
153
-
154
- with gr.Column():
155
- output_image = gr.Gallery(
156
- label="Generated images",
157
- show_label=False,
158
- elem_id="gallery",
159
- ).style(grid=(1, 2))
160
-
161
- controlnet_canny_predict.click(
162
- fn=StableDiffusionControlNetShuffleGenerator().generate_image,
163
- inputs=[
164
- controlnet_canny_image_file,
165
- controlnet_canny_stable_model_id,
166
- controlnet_canny_model_id,
167
- controlnet_canny_prompt,
168
- controlnet_canny_negative_prompt,
169
- controlnet_canny_num_images_per_prompt,
170
- controlnet_canny_guidance_scale,
171
- controlnet_canny_num_inference_step,
172
- controlnet_canny_scheduler,
173
- controlnet_canny_seed_generator,
174
- ],
175
- outputs=[output_image],
176
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/controlnet/controlnet_softedge.py DELETED
@@ -1,179 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- from controlnet_aux import HEDdetector, PidiNetDetector
4
- from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
5
- from diffusers.utils import load_image
6
-
7
- from diffusion_webui.utils.model_list import (
8
- controlnet_softedge_model_list,
9
- stable_model_list,
10
- )
11
- from diffusion_webui.utils.scheduler_list import (
12
- SCHEDULER_LIST,
13
- get_scheduler_list,
14
- )
15
-
16
-
17
- class StableDiffusionControlNetSoftEdgeGenerator:
18
- def __init__(self):
19
- self.pipe = None
20
-
21
- def load_model(self, stable_model_path, controlnet_model_path, scheduler):
22
- if self.pipe is None:
23
- controlnet = ControlNetModel.from_pretrained(
24
- controlnet_model_path, torch_dtype=torch.float16
25
- )
26
- self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
27
- pretrained_model_name_or_path=stable_model_path,
28
- controlnet=controlnet,
29
- safety_checker=None,
30
- torch_dtype=torch.float16,
31
- )
32
-
33
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
34
- self.pipe.to("cuda")
35
- self.pipe.enable_xformers_memory_efficient_attention()
36
-
37
- return self.pipe
38
-
39
- def controlnet_softedge(
40
- self,
41
- image_path: str,
42
- ):
43
-
44
- image = load_image(image_path)
45
- processor = HEDdetector.from_pretrained("lllyasviel/Annotators")
46
- processor = PidiNetDetector.from_pretrained("lllyasviel/Annotators")
47
- control_image = processor(image, safe=True)
48
- return control_image
49
-
50
- def generate_image(
51
- self,
52
- image_path: str,
53
- stable_model_path: str,
54
- controlnet_model_path: str,
55
- prompt: str,
56
- negative_prompt: str,
57
- num_images_per_prompt: int,
58
- guidance_scale: int,
59
- num_inference_step: int,
60
- scheduler: str,
61
- seed_generator: int,
62
- ):
63
- pipe = self.load_model(
64
- stable_model_path=stable_model_path,
65
- controlnet_model_path=controlnet_model_path,
66
- scheduler=scheduler,
67
- )
68
-
69
- image = self.controlnet_softedge(image_path)
70
-
71
- if seed_generator == 0:
72
- random_seed = torch.randint(0, 1000000, (1,))
73
- generator = torch.manual_seed(random_seed)
74
- else:
75
- generator = torch.manual_seed(seed_generator)
76
-
77
- output = pipe(
78
- prompt=prompt,
79
- image=image,
80
- negative_prompt=negative_prompt,
81
- num_images_per_prompt=num_images_per_prompt,
82
- num_inference_steps=num_inference_step,
83
- guidance_scale=guidance_scale,
84
- generator=generator,
85
- ).images
86
-
87
- return output
88
-
89
- def app():
90
- with gr.Blocks():
91
- with gr.Row():
92
- with gr.Column():
93
- controlnet_canny_image_file = gr.Image(
94
- type="filepath", label="Image"
95
- )
96
-
97
- controlnet_canny_prompt = gr.Textbox(
98
- lines=1,
99
- placeholder="Prompt",
100
- show_label=False,
101
- )
102
-
103
- controlnet_canny_negative_prompt = gr.Textbox(
104
- lines=1,
105
- placeholder="Negative Prompt",
106
- show_label=False,
107
- )
108
- with gr.Row():
109
- with gr.Column():
110
- controlnet_canny_stable_model_id = gr.Dropdown(
111
- choices=stable_model_list,
112
- value=stable_model_list[0],
113
- label="Stable Model Id",
114
- )
115
-
116
- controlnet_canny_guidance_scale = gr.Slider(
117
- minimum=0.1,
118
- maximum=15,
119
- step=0.1,
120
- value=7.5,
121
- label="Guidance Scale",
122
- )
123
- controlnet_canny_num_inference_step = gr.Slider(
124
- minimum=1,
125
- maximum=100,
126
- step=1,
127
- value=50,
128
- label="Num Inference Step",
129
- )
130
- controlnet_canny_num_images_per_prompt = gr.Slider(
131
- minimum=1,
132
- maximum=10,
133
- step=1,
134
- value=1,
135
- label="Number Of Images",
136
- )
137
- with gr.Row():
138
- with gr.Column():
139
- controlnet_canny_model_id = gr.Dropdown(
140
- choices=controlnet_softedge_model_list,
141
- value=controlnet_softedge_model_list[0],
142
- label="ControlNet Model Id",
143
- )
144
-
145
- controlnet_canny_scheduler = gr.Dropdown(
146
- choices=SCHEDULER_LIST,
147
- value=SCHEDULER_LIST[0],
148
- label="Scheduler",
149
- )
150
-
151
- controlnet_canny_seed_generator = gr.Number(
152
- value=0,
153
- label="Seed Generator",
154
- )
155
- controlnet_canny_predict = gr.Button(value="Generator")
156
-
157
- with gr.Column():
158
- output_image = gr.Gallery(
159
- label="Generated images",
160
- show_label=False,
161
- elem_id="gallery",
162
- ).style(grid=(1, 2))
163
-
164
- controlnet_canny_predict.click(
165
- fn=StableDiffusionControlNetSoftEdgeGenerator().generate_image,
166
- inputs=[
167
- controlnet_canny_image_file,
168
- controlnet_canny_stable_model_id,
169
- controlnet_canny_model_id,
170
- controlnet_canny_prompt,
171
- controlnet_canny_negative_prompt,
172
- controlnet_canny_num_images_per_prompt,
173
- controlnet_canny_guidance_scale,
174
- controlnet_canny_num_inference_step,
175
- controlnet_canny_scheduler,
176
- controlnet_canny_seed_generator,
177
- ],
178
- outputs=[output_image],
179
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/stable_diffusion/__init__.py DELETED
@@ -1,9 +0,0 @@
1
- from diffusion_webui.diffusion_models.stable_diffusion.img2img_app import (
2
- StableDiffusionImage2ImageGenerator,
3
- )
4
- from diffusion_webui.diffusion_models.stable_diffusion.inpaint_app import (
5
- StableDiffusionInpaintGenerator,
6
- )
7
- from diffusion_webui.diffusion_models.stable_diffusion.text2img_app import (
8
- StableDiffusionText2ImageGenerator,
9
- )
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/stable_diffusion/img2img_app.py DELETED
@@ -1,153 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- from diffusers import StableDiffusionImg2ImgPipeline
4
- from PIL import Image
5
-
6
- from diffusion_webui.utils.model_list import stable_model_list
7
- from diffusion_webui.utils.scheduler_list import (
8
- SCHEDULER_LIST,
9
- get_scheduler_list,
10
- )
11
-
12
-
13
- class StableDiffusionImage2ImageGenerator:
14
- def __init__(self):
15
- self.pipe = None
16
-
17
- def load_model(self, model_path, scheduler):
18
- if self.pipe is None:
19
- self.pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
20
- model_path, safety_checker=None, torch_dtype=torch.float16
21
- )
22
-
23
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
24
- self.pipe.to("cuda")
25
- self.pipe.enable_xformers_memory_efficient_attention()
26
-
27
- return self.pipe
28
-
29
- def generate_image(
30
- self,
31
- image_path: str,
32
- model_path: str,
33
- prompt: str,
34
- negative_prompt: str,
35
- num_images_per_prompt: int,
36
- scheduler: str,
37
- guidance_scale: int,
38
- num_inference_step: int,
39
- seed_generator=0,
40
- ):
41
- pipe = self.load_model(
42
- model_path=model_path,
43
- scheduler=scheduler,
44
- )
45
-
46
- if seed_generator == 0:
47
- random_seed = torch.randint(0, 1000000, (1,))
48
- generator = torch.manual_seed(random_seed)
49
- else:
50
- generator = torch.manual_seed(seed_generator)
51
-
52
- image = Image.open(image_path)
53
- images = pipe(
54
- prompt,
55
- image=image,
56
- negative_prompt=negative_prompt,
57
- num_images_per_prompt=num_images_per_prompt,
58
- num_inference_steps=num_inference_step,
59
- guidance_scale=guidance_scale,
60
- generator=generator,
61
- ).images
62
-
63
- return images
64
-
65
- def app():
66
- with gr.Blocks():
67
- with gr.Row():
68
- with gr.Column():
69
- image2image_image_file = gr.Image(
70
- type="filepath", label="Image"
71
- ).style(height=260)
72
-
73
- image2image_prompt = gr.Textbox(
74
- lines=1,
75
- placeholder="Prompt",
76
- show_label=False,
77
- )
78
-
79
- image2image_negative_prompt = gr.Textbox(
80
- lines=1,
81
- placeholder="Negative Prompt",
82
- show_label=False,
83
- )
84
-
85
- with gr.Row():
86
- with gr.Column():
87
- image2image_model_path = gr.Dropdown(
88
- choices=stable_model_list,
89
- value=stable_model_list[0],
90
- label="Stable Model Id",
91
- )
92
-
93
- image2image_guidance_scale = gr.Slider(
94
- minimum=0.1,
95
- maximum=15,
96
- step=0.1,
97
- value=7.5,
98
- label="Guidance Scale",
99
- )
100
- image2image_num_inference_step = gr.Slider(
101
- minimum=1,
102
- maximum=100,
103
- step=1,
104
- value=50,
105
- label="Num Inference Step",
106
- )
107
- with gr.Row():
108
- with gr.Column():
109
- image2image_scheduler = gr.Dropdown(
110
- choices=SCHEDULER_LIST,
111
- value=SCHEDULER_LIST[0],
112
- label="Scheduler",
113
- )
114
- image2image_num_images_per_prompt = gr.Slider(
115
- minimum=1,
116
- maximum=30,
117
- step=1,
118
- value=1,
119
- label="Number Of Images",
120
- )
121
-
122
- image2image_seed_generator = gr.Slider(
123
- minimum=0,
124
- maximum=1000000,
125
- step=1,
126
- value=0,
127
- label="Seed(0 for random)",
128
- )
129
-
130
- image2image_predict_button = gr.Button(value="Generator")
131
-
132
- with gr.Column():
133
- output_image = gr.Gallery(
134
- label="Generated images",
135
- show_label=False,
136
- elem_id="gallery",
137
- ).style(grid=(1, 2))
138
-
139
- image2image_predict_button.click(
140
- fn=StableDiffusionImage2ImageGenerator().generate_image,
141
- inputs=[
142
- image2image_image_file,
143
- image2image_model_path,
144
- image2image_prompt,
145
- image2image_negative_prompt,
146
- image2image_num_images_per_prompt,
147
- image2image_scheduler,
148
- image2image_guidance_scale,
149
- image2image_num_inference_step,
150
- image2image_seed_generator,
151
- ],
152
- outputs=[output_image],
153
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/stable_diffusion/inpaint_app.py DELETED
@@ -1,148 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- from diffusers import DiffusionPipeline
4
-
5
- from diffusion_webui.utils.model_list import stable_inpiant_model_list
6
-
7
-
8
- class StableDiffusionInpaintGenerator:
9
- def __init__(self):
10
- self.pipe = None
11
-
12
- def load_model(self, model_path):
13
- if self.pipe is None:
14
- self.pipe = DiffusionPipeline.from_pretrained(
15
- model_path, revision="fp16", torch_dtype=torch.float16
16
- )
17
-
18
- self.pipe.to("cuda")
19
- self.pipe.enable_xformers_memory_efficient_attention()
20
-
21
- return self.pipe
22
-
23
- def generate_image(
24
- self,
25
- pil_image: str,
26
- model_path: str,
27
- prompt: str,
28
- negative_prompt: str,
29
- num_images_per_prompt: int,
30
- guidance_scale: int,
31
- num_inference_step: int,
32
- seed_generator=0,
33
- ):
34
- image = pil_image["image"].convert("RGB").resize((512, 512))
35
- mask_image = pil_image["mask"].convert("RGB").resize((512, 512))
36
- pipe = self.load_model(model_path)
37
-
38
- if seed_generator == 0:
39
- random_seed = torch.randint(0, 1000000, (1,))
40
- generator = torch.manual_seed(random_seed)
41
- else:
42
- generator = torch.manual_seed(seed_generator)
43
-
44
- output = pipe(
45
- prompt=prompt,
46
- image=image,
47
- mask_image=mask_image,
48
- negative_prompt=negative_prompt,
49
- num_images_per_prompt=num_images_per_prompt,
50
- num_inference_steps=num_inference_step,
51
- guidance_scale=guidance_scale,
52
- generator=generator,
53
- ).images
54
-
55
- return output
56
-
57
- def app():
58
- with gr.Blocks():
59
- with gr.Row():
60
- with gr.Column():
61
- stable_diffusion_inpaint_image_file = gr.Image(
62
- source="upload",
63
- tool="sketch",
64
- elem_id="image_upload",
65
- type="pil",
66
- label="Upload",
67
- ).style(height=260)
68
-
69
- stable_diffusion_inpaint_prompt = gr.Textbox(
70
- lines=1,
71
- placeholder="Prompt",
72
- show_label=False,
73
- )
74
-
75
- stable_diffusion_inpaint_negative_prompt = gr.Textbox(
76
- lines=1,
77
- placeholder="Negative Prompt",
78
- show_label=False,
79
- )
80
- stable_diffusion_inpaint_model_id = gr.Dropdown(
81
- choices=stable_inpiant_model_list,
82
- value=stable_inpiant_model_list[0],
83
- label="Inpaint Model Id",
84
- )
85
- with gr.Row():
86
- with gr.Column():
87
- stable_diffusion_inpaint_guidance_scale = gr.Slider(
88
- minimum=0.1,
89
- maximum=15,
90
- step=0.1,
91
- value=7.5,
92
- label="Guidance Scale",
93
- )
94
-
95
- stable_diffusion_inpaint_num_inference_step = (
96
- gr.Slider(
97
- minimum=1,
98
- maximum=100,
99
- step=1,
100
- value=50,
101
- label="Num Inference Step",
102
- )
103
- )
104
-
105
- with gr.Row():
106
- with gr.Column():
107
- stable_diffusion_inpiant_num_images_per_prompt = gr.Slider(
108
- minimum=1,
109
- maximum=10,
110
- step=1,
111
- value=1,
112
- label="Number Of Images",
113
- )
114
- stable_diffusion_inpaint_seed_generator = (
115
- gr.Slider(
116
- minimum=0,
117
- maximum=1000000,
118
- step=1,
119
- value=0,
120
- label="Seed(0 for random)",
121
- )
122
- )
123
-
124
- stable_diffusion_inpaint_predict = gr.Button(
125
- value="Generator"
126
- )
127
-
128
- with gr.Column():
129
- output_image = gr.Gallery(
130
- label="Generated images",
131
- show_label=False,
132
- elem_id="gallery",
133
- ).style(grid=(1, 2))
134
-
135
- stable_diffusion_inpaint_predict.click(
136
- fn=StableDiffusionInpaintGenerator().generate_image,
137
- inputs=[
138
- stable_diffusion_inpaint_image_file,
139
- stable_diffusion_inpaint_model_id,
140
- stable_diffusion_inpaint_prompt,
141
- stable_diffusion_inpaint_negative_prompt,
142
- stable_diffusion_inpiant_num_images_per_prompt,
143
- stable_diffusion_inpaint_guidance_scale,
144
- stable_diffusion_inpaint_num_inference_step,
145
- stable_diffusion_inpaint_seed_generator,
146
- ],
147
- outputs=[output_image],
148
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/diffusion_models/stable_diffusion/text2img_app.py DELETED
@@ -1,167 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- from diffusers import StableDiffusionPipeline
4
-
5
- from diffusion_webui.utils.model_list import stable_model_list
6
- from diffusion_webui.utils.scheduler_list import (
7
- SCHEDULER_LIST,
8
- get_scheduler_list,
9
- )
10
-
11
-
12
- class StableDiffusionText2ImageGenerator:
13
- def __init__(self):
14
- self.pipe = None
15
-
16
- def load_model(
17
- self,
18
- model_path,
19
- scheduler,
20
- ):
21
- if self.pipe is None:
22
- self.pipe = StableDiffusionPipeline.from_pretrained(
23
- model_path, safety_checker=None, torch_dtype=torch.float16
24
- )
25
-
26
- self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
27
- self.pipe.to("cuda")
28
- self.pipe.enable_xformers_memory_efficient_attention()
29
-
30
- return self.pipe
31
-
32
- def generate_image(
33
- self,
34
- model_path: str,
35
- prompt: str,
36
- negative_prompt: str,
37
- num_images_per_prompt: int,
38
- scheduler: str,
39
- guidance_scale: int,
40
- num_inference_step: int,
41
- height: int,
42
- width: int,
43
- seed_generator=0,
44
- ):
45
- pipe = self.load_model(
46
- model_path=model_path,
47
- scheduler=scheduler,
48
- )
49
- if seed_generator == 0:
50
- random_seed = torch.randint(0, 1000000, (1,))
51
- generator = torch.manual_seed(random_seed)
52
- else:
53
- generator = torch.manual_seed(seed_generator)
54
-
55
- images = pipe(
56
- prompt=prompt,
57
- height=height,
58
- width=width,
59
- negative_prompt=negative_prompt,
60
- num_images_per_prompt=num_images_per_prompt,
61
- num_inference_steps=num_inference_step,
62
- guidance_scale=guidance_scale,
63
- generator=generator,
64
- ).images
65
-
66
- return images
67
-
68
- def app():
69
- with gr.Blocks():
70
- with gr.Row():
71
- with gr.Column():
72
- text2image_prompt = gr.Textbox(
73
- lines=1,
74
- placeholder="Prompt",
75
- show_label=False,
76
- )
77
-
78
- text2image_negative_prompt = gr.Textbox(
79
- lines=1,
80
- placeholder="Negative Prompt",
81
- show_label=False,
82
- )
83
- with gr.Row():
84
- with gr.Column():
85
- text2image_model_path = gr.Dropdown(
86
- choices=stable_model_list,
87
- value=stable_model_list[0],
88
- label="Text-Image Model Id",
89
- )
90
-
91
- text2image_guidance_scale = gr.Slider(
92
- minimum=0.1,
93
- maximum=15,
94
- step=0.1,
95
- value=7.5,
96
- label="Guidance Scale",
97
- )
98
-
99
- text2image_num_inference_step = gr.Slider(
100
- minimum=1,
101
- maximum=100,
102
- step=1,
103
- value=50,
104
- label="Num Inference Step",
105
- )
106
- text2image_num_images_per_prompt = gr.Slider(
107
- minimum=1,
108
- maximum=30,
109
- step=1,
110
- value=1,
111
- label="Number Of Images",
112
- )
113
- with gr.Row():
114
- with gr.Column():
115
-
116
- text2image_scheduler = gr.Dropdown(
117
- choices=SCHEDULER_LIST,
118
- value=SCHEDULER_LIST[0],
119
- label="Scheduler",
120
- )
121
-
122
- text2image_height = gr.Slider(
123
- minimum=128,
124
- maximum=1280,
125
- step=32,
126
- value=512,
127
- label="Image Height",
128
- )
129
-
130
- text2image_width = gr.Slider(
131
- minimum=128,
132
- maximum=1280,
133
- step=32,
134
- value=512,
135
- label="Image Width",
136
- )
137
- text2image_seed_generator = gr.Slider(
138
- label="Seed(0 for random)",
139
- minimum=0,
140
- maximum=1000000,
141
- value=0,
142
- )
143
- text2image_predict = gr.Button(value="Generator")
144
-
145
- with gr.Column():
146
- output_image = gr.Gallery(
147
- label="Generated images",
148
- show_label=False,
149
- elem_id="gallery",
150
- ).style(grid=(1, 2), height=200)
151
-
152
- text2image_predict.click(
153
- fn=StableDiffusionText2ImageGenerator().generate_image,
154
- inputs=[
155
- text2image_model_path,
156
- text2image_prompt,
157
- text2image_negative_prompt,
158
- text2image_num_images_per_prompt,
159
- text2image_scheduler,
160
- text2image_guidance_scale,
161
- text2image_num_inference_step,
162
- text2image_height,
163
- text2image_width,
164
- text2image_seed_generator,
165
- ],
166
- outputs=output_image,
167
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/helpers.py DELETED
@@ -1,25 +0,0 @@
1
- from diffusion_webui.diffusion_models.stable_diffusion import (
2
- StableDiffusionText2ImageGenerator,
3
- StableDiffusionImage2ImageGenerator,
4
- StableDiffusionInpaintGenerator,
5
- )
6
-
7
- from diffusion_webui.diffusion_models.controlnet.controlnet_inpaint import (
8
- StableDiffusionControlNetInpaintCannyGenerator,
9
- StableDiffusionControlInpaintNetDepthGenerator,
10
- StableDiffusionControlNetInpaintHedGenerator,
11
- StableDiffusionControlNetInpaintMlsdGenerator,
12
- StableDiffusionControlNetInpaintPoseGenerator,
13
- StableDiffusionControlNetInpaintScribbleGenerator,
14
- StableDiffusionControlNetInpaintSegGenerator,
15
- )
16
-
17
- from diffusion_webui.diffusion_models.controlnet import (
18
- StableDiffusionControlNetCannyGenerator,
19
- StableDiffusionControlNetDepthGenerator,
20
- StableDiffusionControlNetHEDGenerator,
21
- StableDiffusionControlNetMLSDGenerator,
22
- StableDiffusionControlNetPoseGenerator,
23
- StableDiffusionControlNetScribbleGenerator,
24
- StableDiffusionControlNetSegGenerator,
25
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/upscaler_models/__init__.py DELETED
@@ -1,3 +0,0 @@
1
- from diffusion_webui.upscaler_models.codeformer_upscaler import (
2
- CodeformerUpscalerGenerator,
3
- )
 
 
 
 
diffusion_webui/upscaler_models/codeformer_upscaler.py DELETED
@@ -1,81 +0,0 @@
1
- import gradio as gr
2
-
3
-
4
- class CodeformerUpscalerGenerator:
5
- def generate_image(
6
- self,
7
- image_path: str,
8
- background_enhance: bool,
9
- face_upsample: bool,
10
- upscale: int,
11
- codeformer_fidelity: int,
12
- ):
13
- from codeformer.app import inference_app
14
-
15
- pipe = inference_app(
16
- image=image_path,
17
- background_enhance=background_enhance,
18
- face_upsample=face_upsample,
19
- upscale=upscale,
20
- codeformer_fidelity=codeformer_fidelity,
21
- )
22
-
23
- return [pipe]
24
-
25
- def app():
26
- with gr.Blocks():
27
- with gr.Row():
28
- with gr.Column():
29
- codeformer_upscale_image_file = gr.Image(
30
- type="filepath", label="Image"
31
- ).style(height=260)
32
-
33
- with gr.Row():
34
- with gr.Column():
35
- codeformer_face_upsample = gr.Checkbox(
36
- label="Face Upsample",
37
- value=True,
38
- )
39
- codeformer_upscale = gr.Slider(
40
- label="Upscale",
41
- minimum=1,
42
- maximum=4,
43
- step=1,
44
- value=2,
45
- )
46
- with gr.Row():
47
- with gr.Column():
48
- codeformer_background_enhance = gr.Checkbox(
49
- label="Background Enhance",
50
- value=True,
51
- )
52
- codeformer_upscale_fidelity = gr.Slider(
53
- label="Codeformer Fidelity",
54
- minimum=0.1,
55
- maximum=1.0,
56
- step=0.1,
57
- value=0.5,
58
- )
59
-
60
- codeformer_upscale_predict_button = gr.Button(
61
- value="Generator"
62
- )
63
-
64
- with gr.Column():
65
- output_image = gr.Gallery(
66
- label="Generated images",
67
- show_label=False,
68
- elem_id="gallery",
69
- ).style(grid=(1, 2))
70
-
71
- codeformer_upscale_predict_button.click(
72
- fn=CodeformerUpscalerGenerator().generate_image,
73
- inputs=[
74
- codeformer_upscale_image_file,
75
- codeformer_background_enhance,
76
- codeformer_face_upsample,
77
- codeformer_upscale,
78
- codeformer_upscale_fidelity,
79
- ],
80
- outputs=[output_image],
81
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/utils/__init__.py DELETED
File without changes
diffusion_webui/utils/data_utils.py DELETED
@@ -1,12 +0,0 @@
1
- from PIL import Image
2
-
3
-
4
- def image_grid(imgs, rows, cols):
5
- assert len(imgs) == rows * cols
6
-
7
- w, h = imgs[0].size
8
- grid = Image.new("RGB", size=(cols * w, rows * h))
9
-
10
- for i, img in enumerate(imgs):
11
- grid.paste(img, box=(i % cols * w, i // cols * h))
12
- return grid
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/utils/model_list.py DELETED
@@ -1,78 +0,0 @@
1
- stable_model_list = [
2
- "runwayml/stable-diffusion-v1-5",
3
- "stabilityai/stable-diffusion-2-1",
4
- "prompthero/openjourney-v4",
5
- "dreamlike-art/dreamlike-diffusion-1.0",
6
- "andite/anything-v4.0",
7
- "SG161222/Realistic_Vision_V2.0",
8
- ]
9
-
10
- controlnet_canny_model_list = [
11
- "lllyasviel/sd-controlnet-canny",
12
- "lllyasviel/control_v11p_sd15_canny",
13
- "thibaud/controlnet-sd21-canny-diffusers",
14
- ]
15
-
16
- controlnet_depth_model_list = [
17
- "lllyasviel/sd-controlnet-depth",
18
- "lllyasviel/control_v11f1p_sd15_depth",
19
- "thibaud/controlnet-sd21-depth-diffusers",
20
- ]
21
-
22
-
23
- controlnet_pose_model_list = [
24
- "lllyasviel/sd-controlnet-openpose",
25
- "lllyasviel/control_v11p_sd15_openpose",
26
- "thibaud/controlnet-sd21-openpose-diffusers",
27
- ]
28
-
29
- controlnet_hed_model_list = [
30
- "lllyasviel/sd-controlnet-hed",
31
- "thibaud/controlnet-sd21-hed-diffusers",
32
- ]
33
-
34
- controlnet_scribble_model_list = [
35
- "lllyasviel/sd-controlnet-scribble",
36
- "lllyasviel/control_v11p_sd15_scribble",
37
- "thibaud/controlnet-sd21-scribble-diffusers",
38
- ]
39
- stable_inpiant_model_list = [
40
- "stabilityai/stable-diffusion-2-inpainting",
41
- "runwayml/stable-diffusion-inpainting",
42
- "SG161222/Realistic_Vision_V2.0"
43
- ]
44
-
45
- controlnet_mlsd_model_list = [
46
- "lllyasviel/sd-controlnet-mlsd",
47
- "lllyasviel/control_v11p_sd15_mlsd",
48
- ]
49
-
50
- controlnet_normal_model_list = [
51
- "lllyasviel/sd-controlnet-normal",
52
- "lllyasviel/control_v11p_sd15_normalbae",
53
- ]
54
-
55
- controlnet_seg_model_list = [
56
- "lllyasviel/sd-controlnet-seg",
57
- "lllyasviel/control_v11p_sd15_seg",
58
- ]
59
-
60
- controlnet_shuffle_model_list = [
61
- "lllyasviel/control_v11e_sd15_shuffle",
62
- ]
63
-
64
- controlnet_pix2pix_model_list = [
65
- "lllyasviel/control_v11e_sd15_ip2p",
66
- ]
67
-
68
- controlnet_lineart_model_list = [
69
- "ControlNet-1-1-preview/control_v11p_sd15_lineart",
70
- ]
71
-
72
- controlnet_lineart_anime_model_list = [
73
- "lllyasviel/control_v11p_sd15s2_lineart_anime",
74
- ]
75
-
76
- controlnet_softedge_model_list = [
77
- "lllyasviel/control_v11p_sd15_softedge",
78
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffusion_webui/utils/scheduler_list.py DELETED
@@ -1,47 +0,0 @@
1
- from diffusers import (
2
- DDIMScheduler,
3
- EulerAncestralDiscreteScheduler,
4
- EulerDiscreteScheduler,
5
- HeunDiscreteScheduler,
6
- LMSDiscreteScheduler,
7
- UniPCMultistepScheduler,
8
- )
9
-
10
- SCHEDULER_LIST = [
11
- "DDIM",
12
- "EulerA",
13
- "Euler",
14
- "LMS",
15
- "Heun",
16
- "UniPC",
17
- ]
18
-
19
-
20
- def get_scheduler_list(pipe, scheduler):
21
- if scheduler == SCHEDULER_LIST[0]:
22
- pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
23
-
24
- elif scheduler == SCHEDULER_LIST[1]:
25
- pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(
26
- pipe.scheduler.config
27
- )
28
-
29
- elif scheduler == SCHEDULER_LIST[2]:
30
- pipe.scheduler = EulerDiscreteScheduler.from_config(
31
- pipe.scheduler.config
32
- )
33
-
34
- elif scheduler == SCHEDULER_LIST[3]:
35
- pipe.scheduler = LMSDiscreteScheduler.from_config(pipe.scheduler.config)
36
-
37
- elif scheduler == SCHEDULER_LIST[4]:
38
- pipe.scheduler = HeunDiscreteScheduler.from_config(
39
- pipe.scheduler.config
40
- )
41
-
42
- elif scheduler == SCHEDULER_LIST[5]:
43
- pipe.scheduler = UniPCMultistepScheduler.from_config(
44
- pipe.scheduler.config
45
- )
46
-
47
- return pipe