yuan2023 kadirnar commited on
Commit
01800de
·
0 Parent(s):

Duplicate from ArtGAN/Stable-Diffusion-ControlNet-WebUI

Browse files

Co-authored-by: Kadir Nar <[email protected]>

.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Stable Diffusion ControlNet WebUI
3
+ emoji: 🔥
4
+ colorFrom: green
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 3.19
8
+ app_file: app.py
9
+ pinned: true
10
+ license: openrail
11
+ tags:
12
+ - making-demos
13
+ duplicated_from: ArtGAN/Stable-Diffusion-ControlNet-WebUI
14
+ ---
15
+
16
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from diffusion_webui.helpers import (
4
+ keras_stable_diffusion_app,
5
+ stable_diffusion_controlnet_canny_app,
6
+ stable_diffusion_controlnet_depth_app,
7
+ stable_diffusion_controlnet_hed_app,
8
+ stable_diffusion_controlnet_mlsd_app,
9
+ stable_diffusion_controlnet_pose_app,
10
+ stable_diffusion_controlnet_scribble_app,
11
+ stable_diffusion_controlnet_seg_app,
12
+ stable_diffusion_img2img_app,
13
+ stable_diffusion_inpaint_app,
14
+ stable_diffusion_text2img_app,
15
+ )
16
+
17
+ app = gr.Blocks()
18
+ with app:
19
+ gr.HTML(
20
+ """
21
+ <h1 style='text-align: center'>
22
+ Stable Diffusion + ControlNet + Keras Diffusion WebUI
23
+ </h1>
24
+ """
25
+ )
26
+ gr.Markdown(
27
+ """
28
+ <h4 style='text-align: center'>
29
+ Follow me for more!
30
+ <a href='https://twitter.com/kadirnar_ai' target='_blank'>Twitter</a> | <a href='https://github.com/kadirnar' target='_blank'>Github</a> | <a href='https://www.linkedin.com/in/kadir-nar/' target='_blank'>Linkedin</a>
31
+ </h4>
32
+ """
33
+ )
34
+ with gr.Row():
35
+ with gr.Column():
36
+ with gr.Tab("Text2Img"):
37
+ stable_diffusion_text2img_app()
38
+ with gr.Tab("Img2Img"):
39
+ stable_diffusion_img2img_app()
40
+ with gr.Tab("Inpaint"):
41
+ stable_diffusion_inpaint_app()
42
+
43
+ with gr.Tab("ControlNet"):
44
+ with gr.Tab("Canny"):
45
+ stable_diffusion_controlnet_canny_app()
46
+ with gr.Tab("Depth"):
47
+ stable_diffusion_controlnet_depth_app()
48
+ with gr.Tab("HED"):
49
+ stable_diffusion_controlnet_hed_app()
50
+ with gr.Tab("MLSD"):
51
+ stable_diffusion_controlnet_mlsd_app()
52
+ with gr.Tab("Pose"):
53
+ stable_diffusion_controlnet_pose_app()
54
+ with gr.Tab("Seg"):
55
+ stable_diffusion_controlnet_seg_app()
56
+ with gr.Tab("Scribble"):
57
+ stable_diffusion_controlnet_scribble_app()
58
+
59
+ with gr.Tab("Keras Diffusion"):
60
+ keras_diffusion_app = keras_stable_diffusion_app()
61
+
62
+ app.launch(debug=True)
data/bash.sh ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ black . --config pyproject.toml
2
+ isort .
data/test.png ADDED
diffusion_webui/__init__.py ADDED
File without changes
diffusion_webui/controlnet/__init__.py ADDED
File without changes
diffusion_webui/controlnet/controlnet_canny.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import gradio as gr
3
+ import numpy as np
4
+ import torch
5
+ from diffusers import (
6
+ ControlNetModel,
7
+ StableDiffusionControlNetPipeline,
8
+ UniPCMultistepScheduler,
9
+ )
10
+ from PIL import Image
11
+
12
+ stable_model_list = [
13
+ "runwayml/stable-diffusion-v1-5",
14
+ "stabilityai/stable-diffusion-2-1",
15
+ ]
16
+
17
+ controlnet_canny_model_list = [
18
+ "lllyasviel/sd-controlnet-canny",
19
+ "thibaud/controlnet-sd21-canny-diffusers",
20
+ ]
21
+
22
+
23
+ stable_prompt_list = ["a photo of a man.", "a photo of a girl."]
24
+
25
+ stable_negative_prompt_list = ["bad, ugly", "deformed"]
26
+
27
+ data_list = [
28
+ "data/test.png",
29
+ ]
30
+
31
+
32
+ def controlnet_canny(
33
+ image_path: str,
34
+ controlnet_model_path: str,
35
+ ):
36
+ image = Image.open(image_path)
37
+ image = np.array(image)
38
+
39
+ image = cv2.Canny(image, 100, 200)
40
+ image = image[:, :, None]
41
+ image = np.concatenate([image, image, image], axis=2)
42
+ image = Image.fromarray(image)
43
+
44
+ controlnet = ControlNetModel.from_pretrained(
45
+ controlnet_model_path, torch_dtype=torch.float16
46
+ )
47
+ return controlnet, image
48
+
49
+
50
+ def stable_diffusion_controlnet_canny(
51
+ image_path: str,
52
+ stable_model_path: str,
53
+ controlnet_model_path: str,
54
+ prompt: str,
55
+ negative_prompt: str,
56
+ guidance_scale: int,
57
+ num_inference_step: int,
58
+ ):
59
+
60
+ controlnet, image = controlnet_canny(
61
+ image_path=image_path, controlnet_model_path=controlnet_model_path
62
+ )
63
+
64
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
65
+ pretrained_model_name_or_path=stable_model_path,
66
+ controlnet=controlnet,
67
+ safety_checker=None,
68
+ torch_dtype=torch.float16,
69
+ )
70
+ pipe.to("cuda")
71
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
72
+ pipe.enable_xformers_memory_efficient_attention()
73
+
74
+ output = pipe(
75
+ prompt=prompt,
76
+ image=image,
77
+ negative_prompt=negative_prompt,
78
+ num_inference_steps=num_inference_step,
79
+ guidance_scale=guidance_scale,
80
+ ).images
81
+
82
+ return output[0]
83
+
84
+
85
+ def stable_diffusion_controlnet_canny_app():
86
+ with gr.Blocks():
87
+ with gr.Row():
88
+ with gr.Column():
89
+ controlnet_canny_image_file = gr.Image(
90
+ type="filepath", label="Image"
91
+ )
92
+
93
+ controlnet_canny_stable_model_id = gr.Dropdown(
94
+ choices=stable_model_list,
95
+ value=stable_model_list[0],
96
+ label="Stable Model Id",
97
+ )
98
+
99
+ controlnet_canny_model_id = gr.Dropdown(
100
+ choices=controlnet_canny_model_list,
101
+ value=controlnet_canny_model_list[0],
102
+ label="Controlnet Model Id",
103
+ )
104
+
105
+ controlnet_canny_prompt = gr.Textbox(
106
+ lines=1, value=stable_prompt_list[0], label="Prompt"
107
+ )
108
+
109
+ controlnet_canny_negative_prompt = gr.Textbox(
110
+ lines=1,
111
+ value=stable_negative_prompt_list[0],
112
+ label="Negative Prompt",
113
+ )
114
+
115
+ with gr.Accordion("Advanced Options", open=False):
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
+
124
+ controlnet_canny_num_inference_step = gr.Slider(
125
+ minimum=1,
126
+ maximum=100,
127
+ step=1,
128
+ value=50,
129
+ label="Num Inference Step",
130
+ )
131
+
132
+ controlnet_canny_predict = gr.Button(value="Generator")
133
+
134
+ with gr.Column():
135
+ output_image = gr.Image(label="Output")
136
+
137
+ gr.Examples(
138
+ fn=stable_diffusion_controlnet_canny,
139
+ examples=[
140
+ [
141
+ data_list[0],
142
+ stable_model_list[0],
143
+ controlnet_canny_model_list[0],
144
+ stable_prompt_list[0],
145
+ stable_negative_prompt_list[0],
146
+ 7.5,
147
+ 50,
148
+ ]
149
+ ],
150
+ inputs=[
151
+ controlnet_canny_image_file,
152
+ controlnet_canny_stable_model_id,
153
+ controlnet_canny_model_id,
154
+ controlnet_canny_prompt,
155
+ controlnet_canny_negative_prompt,
156
+ controlnet_canny_guidance_scale,
157
+ controlnet_canny_num_inference_step,
158
+ ],
159
+ outputs=[output_image],
160
+ cache_examples=False,
161
+ label="Controlnet Canny Example",
162
+ )
163
+
164
+ controlnet_canny_predict.click(
165
+ fn=stable_diffusion_controlnet_canny,
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_guidance_scale,
173
+ controlnet_canny_num_inference_step,
174
+ ],
175
+ outputs=[output_image],
176
+ )
diffusion_webui/controlnet/controlnet_depth.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import torch
4
+ from diffusers import (
5
+ ControlNetModel,
6
+ StableDiffusionControlNetPipeline,
7
+ UniPCMultistepScheduler,
8
+ )
9
+ from PIL import Image
10
+ from transformers import pipeline
11
+
12
+ stable_model_list = [
13
+ "runwayml/stable-diffusion-v1-5",
14
+ "stabilityai/stable-diffusion-2-1",
15
+ ]
16
+
17
+ controlnet_depth_model_list = [
18
+ "lllyasviel/sd-controlnet-depth",
19
+ "thibaud/controlnet-sd21-depth-diffusers",
20
+ ]
21
+
22
+
23
+ stable_prompt_list = ["a photo of a man.", "a photo of a girl."]
24
+
25
+ stable_negative_prompt_list = ["bad, ugly", "deformed"]
26
+
27
+ data_list = [
28
+ "data/test.png",
29
+ ]
30
+
31
+
32
+ def controlnet_depth(image_path: str, depth_model_path: str):
33
+ depth_estimator = pipeline("depth-estimation")
34
+
35
+ image = Image.open(image_path)
36
+ image = depth_estimator(image)["depth"]
37
+ image = np.array(image)
38
+ image = image[:, :, None]
39
+ image = np.concatenate([image, image, image], axis=2)
40
+ image = Image.fromarray(image)
41
+
42
+ controlnet = ControlNetModel.from_pretrained(
43
+ depth_model_path, torch_dtype=torch.float16
44
+ )
45
+
46
+ return controlnet, image
47
+
48
+
49
+ def stable_diffusion_controlnet_depth(
50
+ image_path: str,
51
+ stable_model_path: str,
52
+ depth_model_path: str,
53
+ prompt: str,
54
+ negative_prompt: str,
55
+ guidance_scale: int,
56
+ num_inference_step: int,
57
+ ):
58
+
59
+ controlnet, image = controlnet_depth(
60
+ image_path=image_path, depth_model_path=depth_model_path
61
+ )
62
+
63
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
64
+ pretrained_model_name_or_path=stable_model_path,
65
+ controlnet=controlnet,
66
+ safety_checker=None,
67
+ torch_dtype=torch.float16,
68
+ )
69
+
70
+ pipe.to("cuda")
71
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
72
+ pipe.enable_xformers_memory_efficient_attention()
73
+
74
+ output = pipe(
75
+ prompt=prompt,
76
+ image=image,
77
+ negative_prompt=negative_prompt,
78
+ num_inference_steps=num_inference_step,
79
+ guidance_scale=guidance_scale,
80
+ ).images
81
+
82
+ return output[0]
83
+
84
+
85
+ def stable_diffusion_controlnet_depth_app():
86
+ with gr.Blocks():
87
+ with gr.Row():
88
+ with gr.Column():
89
+ controlnet_depth_image_file = gr.Image(
90
+ type="filepath", label="Image"
91
+ )
92
+
93
+ controlnet_depth_stable_model_id = gr.Dropdown(
94
+ choices=stable_model_list,
95
+ value=stable_model_list[0],
96
+ label="Stable Model Id",
97
+ )
98
+
99
+ controlnet_depth_model_id = gr.Dropdown(
100
+ choices=controlnet_depth_model_list,
101
+ value=controlnet_depth_model_list[0],
102
+ label="ControlNet Model Id",
103
+ )
104
+
105
+ controlnet_depth_prompt = gr.Textbox(
106
+ lines=1, value=stable_prompt_list[0], label="Prompt"
107
+ )
108
+
109
+ controlnet_depth_negative_prompt = gr.Textbox(
110
+ lines=1,
111
+ value=stable_negative_prompt_list[0],
112
+ label="Negative Prompt",
113
+ )
114
+
115
+ with gr.Accordion("Advanced Options", open=False):
116
+ controlnet_depth_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
+
124
+ controlnet_depth_num_inference_step = gr.Slider(
125
+ minimum=1,
126
+ maximum=100,
127
+ step=1,
128
+ value=50,
129
+ label="Num Inference Step",
130
+ )
131
+
132
+ controlnet_depth_predict = gr.Button(value="Generator")
133
+
134
+ with gr.Column():
135
+ output_image = gr.Image(label="Output")
136
+
137
+ gr.Examples(
138
+ fn=stable_diffusion_controlnet_depth,
139
+ examples=[
140
+ [
141
+ data_list[0],
142
+ stable_model_list[0],
143
+ controlnet_depth_model_list[0],
144
+ stable_prompt_list[0],
145
+ stable_negative_prompt_list[0],
146
+ 7.5,
147
+ 50,
148
+ ]
149
+ ],
150
+ inputs=[
151
+ controlnet_depth_image_file,
152
+ controlnet_depth_stable_model_id,
153
+ controlnet_depth_model_id,
154
+ controlnet_depth_prompt,
155
+ controlnet_depth_negative_prompt,
156
+ controlnet_depth_guidance_scale,
157
+ controlnet_depth_num_inference_step,
158
+ ],
159
+ outputs=[output_image],
160
+ cache_examples=False,
161
+ label="ControlNet Depth Example",
162
+ )
163
+
164
+ controlnet_depth_predict.click(
165
+ fn=stable_diffusion_controlnet_depth,
166
+ inputs=[
167
+ controlnet_depth_image_file,
168
+ controlnet_depth_stable_model_id,
169
+ controlnet_depth_model_id,
170
+ controlnet_depth_prompt,
171
+ controlnet_depth_negative_prompt,
172
+ controlnet_depth_guidance_scale,
173
+ controlnet_depth_num_inference_step,
174
+ ],
175
+ outputs=output_image,
176
+ )
diffusion_webui/controlnet/controlnet_hed.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ stable_model_list = [
12
+ "runwayml/stable-diffusion-v1-5",
13
+ "stabilityai/stable-diffusion-2-1",
14
+ ]
15
+
16
+ controlnet_hed_model_list = [
17
+ "lllyasviel/sd-controlnet-hed",
18
+ "thibaud/controlnet-sd21-hed-diffusers",
19
+ ]
20
+
21
+ stable_prompt_list = ["a photo of a man.", "a photo of a girl."]
22
+
23
+ stable_negative_prompt_list = ["bad, ugly", "deformed"]
24
+
25
+ data_list = [
26
+ "data/test.png",
27
+ ]
28
+
29
+
30
+ def controlnet_hed(image_path: str, controlnet_hed_model_path: str):
31
+ hed = HEDdetector.from_pretrained("lllyasviel/ControlNet")
32
+
33
+ image = Image.open(image_path)
34
+ image = hed(image)
35
+
36
+ controlnet = ControlNetModel.from_pretrained(
37
+ controlnet_hed_model_path, torch_dtype=torch.float16
38
+ )
39
+ return controlnet, image
40
+
41
+
42
+ def stable_diffusion_controlnet_hed(
43
+ image_path: str,
44
+ stable_model_path: str,
45
+ controlnet_hed_model_path: str,
46
+ prompt: str,
47
+ negative_prompt: str,
48
+ guidance_scale: int,
49
+ num_inference_step: int,
50
+ ):
51
+
52
+ controlnet, image = controlnet_hed(
53
+ image_path=image_path,
54
+ controlnet_hed_model_path=controlnet_hed_model_path,
55
+ )
56
+
57
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
58
+ pretrained_model_name_or_path=stable_model_path,
59
+ controlnet=controlnet,
60
+ safety_checker=None,
61
+ torch_dtype=torch.float16,
62
+ )
63
+
64
+ pipe.to("cuda")
65
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
66
+ pipe.enable_xformers_memory_efficient_attention()
67
+
68
+ output = pipe(
69
+ prompt=prompt,
70
+ image=image,
71
+ negative_prompt=negative_prompt,
72
+ num_inference_steps=num_inference_step,
73
+ guidance_scale=guidance_scale,
74
+ ).images
75
+
76
+ return output[0]
77
+
78
+
79
+ def stable_diffusion_controlnet_hed_app():
80
+ with gr.Blocks():
81
+ with gr.Row():
82
+ with gr.Column():
83
+ controlnet_hed_image_file = gr.Image(
84
+ type="filepath", label="Image"
85
+ )
86
+
87
+ controlnet_hed_stable_model_id = gr.Dropdown(
88
+ choices=stable_model_list,
89
+ value=stable_model_list[0],
90
+ label="Stable Model Id",
91
+ )
92
+
93
+ controlnet_hed_model_id = gr.Dropdown(
94
+ choices=controlnet_hed_model_list,
95
+ value=controlnet_hed_model_list[1],
96
+ label="ControlNet Model Id",
97
+ )
98
+
99
+ controlnet_hed_prompt = gr.Textbox(
100
+ lines=1, value=stable_prompt_list[0], label="Prompt"
101
+ )
102
+
103
+ controlnet_hed_negative_prompt = gr.Textbox(
104
+ lines=1,
105
+ value=stable_negative_prompt_list[0],
106
+ label="Negative Prompt",
107
+ )
108
+
109
+ with gr.Accordion("Advanced Options", open=False):
110
+ controlnet_hed_guidance_scale = gr.Slider(
111
+ minimum=0.1,
112
+ maximum=15,
113
+ step=0.1,
114
+ value=7.5,
115
+ label="Guidance Scale",
116
+ )
117
+
118
+ controlnet_hed_num_inference_step = gr.Slider(
119
+ minimum=1,
120
+ maximum=100,
121
+ step=1,
122
+ value=50,
123
+ label="Num Inference Step",
124
+ )
125
+
126
+ controlnet_hed_predict = gr.Button(value="Generator")
127
+
128
+ with gr.Column():
129
+ output_image = gr.Image(label="Output")
130
+
131
+ gr.Examples(
132
+ fn=stable_diffusion_controlnet_hed,
133
+ examples=[
134
+ [
135
+ data_list[0],
136
+ stable_model_list[0],
137
+ controlnet_hed_model_list[0],
138
+ stable_prompt_list[0],
139
+ stable_negative_prompt_list[0],
140
+ 7.5,
141
+ 50,
142
+ ]
143
+ ],
144
+ inputs=[
145
+ controlnet_hed_image_file,
146
+ controlnet_hed_stable_model_id,
147
+ controlnet_hed_model_id,
148
+ controlnet_hed_prompt,
149
+ controlnet_hed_negative_prompt,
150
+ controlnet_hed_guidance_scale,
151
+ controlnet_hed_num_inference_step,
152
+ ],
153
+ outputs=[output_image],
154
+ cache_examples=False,
155
+ label="ControlNet HED Example",
156
+ )
157
+
158
+ controlnet_hed_predict.click(
159
+ fn=stable_diffusion_controlnet_hed,
160
+ inputs=[
161
+ controlnet_hed_image_file,
162
+ controlnet_hed_stable_model_id,
163
+ controlnet_hed_model_id,
164
+ controlnet_hed_prompt,
165
+ controlnet_hed_negative_prompt,
166
+ controlnet_hed_guidance_scale,
167
+ controlnet_hed_num_inference_step,
168
+ ],
169
+ outputs=[output_image],
170
+ )
diffusion_webui/controlnet/controlnet_mlsd.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from controlnet_aux import MLSDdetector
4
+ from diffusers import (
5
+ ControlNetModel,
6
+ StableDiffusionControlNetPipeline,
7
+ UniPCMultistepScheduler,
8
+ )
9
+ from PIL import Image
10
+
11
+ stable_model_list = [
12
+ "runwayml/stable-diffusion-v1-5",
13
+ ]
14
+
15
+ stable_prompt_list = ["a photo of a man.", "a photo of a girl."]
16
+
17
+ stable_negative_prompt_list = ["bad, ugly", "deformed"]
18
+
19
+ data_list = [
20
+ "data/test.png",
21
+ ]
22
+
23
+
24
+ def controlnet_mlsd(image_path: str):
25
+ mlsd = MLSDdetector.from_pretrained("lllyasviel/ControlNet")
26
+
27
+ image = Image.open(image_path)
28
+ image = mlsd(image)
29
+
30
+ controlnet = ControlNetModel.from_pretrained(
31
+ "lllyasviel/sd-controlnet-mlsd",
32
+ torch_dtype=torch.float16,
33
+ )
34
+
35
+ return controlnet, image
36
+
37
+
38
+ def stable_diffusion_controlnet_mlsd(
39
+ image_path: str,
40
+ model_path: str,
41
+ prompt: str,
42
+ negative_prompt: str,
43
+ guidance_scale: int,
44
+ num_inference_step: int,
45
+ ):
46
+
47
+ controlnet, image = controlnet_mlsd(image_path=image_path)
48
+
49
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
50
+ pretrained_model_name_or_path=model_path,
51
+ controlnet=controlnet,
52
+ safety_checker=None,
53
+ torch_dtype=torch.float16,
54
+ )
55
+
56
+ pipe.to("cuda")
57
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
58
+ pipe.enable_xformers_memory_efficient_attention()
59
+
60
+ output = pipe(
61
+ prompt=prompt,
62
+ image=image,
63
+ negative_prompt=negative_prompt,
64
+ num_inference_steps=num_inference_step,
65
+ guidance_scale=guidance_scale,
66
+ ).images
67
+
68
+ return output[0]
69
+
70
+
71
+ def stable_diffusion_controlnet_mlsd_app():
72
+ with gr.Blocks():
73
+ with gr.Row():
74
+ with gr.Column():
75
+ controlnet_mlsd_image_file = gr.Image(
76
+ type="filepath", label="Image"
77
+ )
78
+
79
+ controlnet_mlsd_model_id = gr.Dropdown(
80
+ choices=stable_model_list,
81
+ value=stable_model_list[0],
82
+ label="Stable Model Id",
83
+ )
84
+
85
+ controlnet_mlsd_prompt = gr.Textbox(
86
+ lines=1, value=stable_prompt_list[0], label="Prompt"
87
+ )
88
+
89
+ controlnet_mlsd_negative_prompt = gr.Textbox(
90
+ lines=1,
91
+ value=stable_negative_prompt_list[0],
92
+ label="Negative Prompt",
93
+ )
94
+
95
+ with gr.Accordion("Advanced Options", open=False):
96
+ controlnet_mlsd_guidance_scale = gr.Slider(
97
+ minimum=0.1,
98
+ maximum=15,
99
+ step=0.1,
100
+ value=7.5,
101
+ label="Guidance Scale",
102
+ )
103
+
104
+ controlnet_mlsd_num_inference_step = gr.Slider(
105
+ minimum=1,
106
+ maximum=100,
107
+ step=1,
108
+ value=50,
109
+ label="Num Inference Step",
110
+ )
111
+
112
+ controlnet_mlsd_predict = gr.Button(value="Generator")
113
+
114
+ with gr.Column():
115
+ output_image = gr.Image(label="Output")
116
+
117
+ gr.Examples(
118
+ fn=stable_diffusion_controlnet_mlsd,
119
+ examples=[
120
+ [
121
+ data_list[0],
122
+ stable_model_list[0],
123
+ stable_prompt_list[0],
124
+ stable_negative_prompt_list[0],
125
+ 7.5,
126
+ 50,
127
+ ]
128
+ ],
129
+ inputs=[
130
+ controlnet_mlsd_image_file,
131
+ controlnet_mlsd_model_id,
132
+ controlnet_mlsd_prompt,
133
+ controlnet_mlsd_negative_prompt,
134
+ controlnet_mlsd_guidance_scale,
135
+ controlnet_mlsd_num_inference_step,
136
+ ],
137
+ outputs=[output_image],
138
+ label="ControlNet-MLSD Example",
139
+ cache_examples=False,
140
+ )
141
+
142
+ controlnet_mlsd_predict.click(
143
+ fn=stable_diffusion_controlnet_mlsd,
144
+ inputs=[
145
+ controlnet_mlsd_image_file,
146
+ controlnet_mlsd_model_id,
147
+ controlnet_mlsd_prompt,
148
+ controlnet_mlsd_negative_prompt,
149
+ controlnet_mlsd_guidance_scale,
150
+ controlnet_mlsd_num_inference_step,
151
+ ],
152
+ outputs=output_image,
153
+ )
diffusion_webui/controlnet/controlnet_pose.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ stable_model_list = [
12
+ "runwayml/stable-diffusion-v1-5",
13
+ "stabilityai/stable-diffusion-2-1",
14
+ ]
15
+
16
+ controlnet_pose_model_list = [
17
+ "lllyasviel/sd-controlnet-openpose",
18
+ "thibaud/controlnet-sd21-openpose-diffusers",
19
+ ]
20
+
21
+ stable_prompt_list = ["a photo of a man.", "a photo of a girl."]
22
+
23
+ stable_negative_prompt_list = ["bad, ugly", "deformed"]
24
+
25
+ data_list = [
26
+ "data/test.png",
27
+ ]
28
+
29
+
30
+ def controlnet_pose(image_path: str, controlnet_pose_model_path: str):
31
+ openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
32
+
33
+ image = Image.open(image_path)
34
+ image = openpose(image)
35
+
36
+ controlnet = ControlNetModel.from_pretrained(
37
+ controlnet_pose_model_path, torch_dtype=torch.float16
38
+ )
39
+
40
+ return controlnet, image
41
+
42
+
43
+ def stable_diffusion_controlnet_pose(
44
+ image_path: str,
45
+ stable_model_path: str,
46
+ controlnet_pose_model_path: str,
47
+ prompt: str,
48
+ negative_prompt: str,
49
+ guidance_scale: int,
50
+ num_inference_step: int,
51
+ ):
52
+
53
+ controlnet, image = controlnet_pose(
54
+ image_path=image_path,
55
+ controlnet_pose_model_path=controlnet_pose_model_path,
56
+ )
57
+
58
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
59
+ pretrained_model_name_or_path=-stable_model_path,
60
+ controlnet=controlnet,
61
+ safety_checker=None,
62
+ torch_dtype=torch.float16,
63
+ )
64
+
65
+ pipe.to("cuda")
66
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
67
+ pipe.enable_xformers_memory_efficient_attention()
68
+
69
+ output = pipe(
70
+ prompt=prompt,
71
+ image=image,
72
+ negative_prompt=negative_prompt,
73
+ num_inference_steps=num_inference_step,
74
+ guidance_scale=guidance_scale,
75
+ ).images
76
+
77
+ return output[0]
78
+
79
+
80
+ def stable_diffusion_controlnet_pose_app():
81
+ with gr.Blocks():
82
+ with gr.Row():
83
+ with gr.Column():
84
+ controlnet_pose_image_file = gr.Image(
85
+ type="filepath", label="Image"
86
+ )
87
+
88
+ controlnet_pose_stable_model_id = gr.Dropdown(
89
+ choices=stable_model_list,
90
+ value=stable_model_list[0],
91
+ label="Stable Model Id",
92
+ )
93
+
94
+ controlnet_pose_model_id = gr.Dropdown(
95
+ choices=controlnet_pose_model_list,
96
+ value=controlnet_pose_model_list[0],
97
+ label="ControlNet Model Id",
98
+ )
99
+
100
+ controlnet_pose_prompt = gr.Textbox(
101
+ lines=1, value=stable_prompt_list[0], label="Prompt"
102
+ )
103
+
104
+ controlnet_pose_negative_prompt = gr.Textbox(
105
+ lines=1,
106
+ value=stable_negative_prompt_list[0],
107
+ label="Negative Prompt",
108
+ )
109
+
110
+ with gr.Accordion("Advanced Options", open=False):
111
+ controlnet_pose_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
+
119
+ controlnet_pose_num_inference_step = gr.Slider(
120
+ minimum=1,
121
+ maximum=100,
122
+ step=1,
123
+ value=50,
124
+ label="Num Inference Step",
125
+ )
126
+
127
+ controlnet_pose_predict = gr.Button(value="Generator")
128
+
129
+ with gr.Column():
130
+ output_image = gr.Image(label="Output")
131
+
132
+ gr.Examples(
133
+ fn=stable_diffusion_controlnet_pose,
134
+ examples=[
135
+ [
136
+ data_list[0],
137
+ stable_model_list[0],
138
+ controlnet_pose_model_list[0],
139
+ stable_prompt_list[0],
140
+ stable_negative_prompt_list[0],
141
+ 7.5,
142
+ 50,
143
+ ]
144
+ ],
145
+ inputs=[
146
+ controlnet_pose_image_file,
147
+ controlnet_pose_stable_model_id,
148
+ controlnet_pose_model_id,
149
+ controlnet_pose_prompt,
150
+ controlnet_pose_negative_prompt,
151
+ controlnet_pose_guidance_scale,
152
+ controlnet_pose_num_inference_step,
153
+ ],
154
+ outputs=[output_image],
155
+ label="ControlNet Pose Example",
156
+ cache_examples=False,
157
+ )
158
+ controlnet_pose_predict.click(
159
+ fn=stable_diffusion_controlnet_pose,
160
+ inputs=[
161
+ controlnet_pose_image_file,
162
+ controlnet_pose_stable_model_id,
163
+ controlnet_pose_model_id,
164
+ controlnet_pose_prompt,
165
+ controlnet_pose_negative_prompt,
166
+ controlnet_pose_guidance_scale,
167
+ controlnet_pose_num_inference_step,
168
+ ],
169
+ outputs=output_image,
170
+ )
diffusion_webui/controlnet/controlnet_scribble.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ stable_model_list = [
12
+ "runwayml/stable-diffusion-v1-5",
13
+ "stabilityai/stable-diffusion-2-1",
14
+ ]
15
+
16
+ controlnet_hed_model_list = [
17
+ "lllyasviel/sd-controlnet-scribble",
18
+ "thibaud/controlnet-sd21-scribble-diffusers",
19
+ ]
20
+
21
+ stable_prompt_list = ["a photo of a man.", "a photo of a girl."]
22
+
23
+ stable_negative_prompt_list = ["bad, ugly", "deformed"]
24
+
25
+ data_list = [
26
+ "data/test.png",
27
+ ]
28
+
29
+
30
+ def controlnet_scribble(image_path: str, controlnet_hed_model_path: str):
31
+ hed = HEDdetector.from_pretrained("lllyasviel/ControlNet")
32
+
33
+ image = Image.open(image_path)
34
+ image = hed(image, scribble=True)
35
+
36
+ controlnet = ControlNetModel.from_pretrained(
37
+ controlnet_hed_model_path, torch_dtype=torch.float16
38
+ )
39
+
40
+ return controlnet, image
41
+
42
+
43
+ def stable_diffusion_controlnet_scribble(
44
+ image_path: str,
45
+ stable_model_path: str,
46
+ controlnet_hed_model_path: str,
47
+ prompt: str,
48
+ negative_prompt: str,
49
+ guidance_scale: int,
50
+ num_inference_step: int,
51
+ ):
52
+
53
+ controlnet, image = controlnet_scribble(
54
+ image_path=image_path,
55
+ controlnet_hed_model_path=controlnet_hed_model_path,
56
+ )
57
+
58
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
59
+ pretrained_model_name_or_path=stable_model_path,
60
+ controlnet=controlnet,
61
+ safety_checker=None,
62
+ torch_dtype=torch.float16,
63
+ )
64
+
65
+ pipe.to("cuda")
66
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
67
+ pipe.enable_xformers_memory_efficient_attention()
68
+
69
+ output = pipe(
70
+ prompt=prompt,
71
+ image=image,
72
+ negative_prompt=negative_prompt,
73
+ num_inference_steps=num_inference_step,
74
+ guidance_scale=guidance_scale,
75
+ ).images
76
+
77
+ return output[0]
78
+
79
+
80
+ def stable_diffusion_controlnet_scribble_app():
81
+ with gr.Blocks():
82
+ with gr.Row():
83
+ with gr.Column():
84
+ controlnet_scribble_image_file = gr.Image(
85
+ type="filepath", label="Image"
86
+ )
87
+
88
+ controlnet_scribble_stable_model_id = gr.Dropdown(
89
+ choices=stable_model_list,
90
+ value=stable_model_list[0],
91
+ label="Stable v1.5 Model Id",
92
+ )
93
+
94
+ controlnet_scribble_model_id = gr.Dropdown(
95
+ choices=controlnet_hed_model_list,
96
+ value=controlnet_hed_model_list[1],
97
+ label="ControlNet Model Id",
98
+ )
99
+
100
+ controlnet_scribble_prompt = gr.Textbox(
101
+ lines=1, value=stable_prompt_list[0], label="Prompt"
102
+ )
103
+
104
+ controlnet_scribble_negative_prompt = gr.Textbox(
105
+ lines=1,
106
+ value=stable_negative_prompt_list[0],
107
+ label="Negative Prompt",
108
+ )
109
+
110
+ with gr.Accordion("Advanced Options", open=False):
111
+ controlnet_scribble_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
+
119
+ controlnet_scribble_num_inference_step = gr.Slider(
120
+ minimum=1,
121
+ maximum=100,
122
+ step=1,
123
+ value=50,
124
+ label="Num Inference Step",
125
+ )
126
+
127
+ controlnet_scribble_predict = gr.Button(value="Generator")
128
+
129
+ with gr.Column():
130
+ output_image = gr.Image(label="Output")
131
+
132
+ gr.Examples(
133
+ fn=stable_diffusion_controlnet_scribble,
134
+ examples=[
135
+ [
136
+ data_list[0],
137
+ stable_model_list[0],
138
+ controlnet_hed_model_list[0],
139
+ stable_prompt_list[0],
140
+ stable_negative_prompt_list[0],
141
+ 7.5,
142
+ 50,
143
+ ],
144
+ ],
145
+ inputs=[
146
+ controlnet_scribble_image_file,
147
+ controlnet_scribble_stable_model_id,
148
+ controlnet_scribble_model_id,
149
+ controlnet_scribble_prompt,
150
+ controlnet_scribble_negative_prompt,
151
+ controlnet_scribble_guidance_scale,
152
+ controlnet_scribble_num_inference_step,
153
+ ],
154
+ outputs=[output_image],
155
+ label="ControlNet Scribble Example",
156
+ cache_examples=False,
157
+ )
158
+ controlnet_scribble_predict.click(
159
+ fn=stable_diffusion_controlnet_scribble,
160
+ inputs=[
161
+ controlnet_scribble_image_file,
162
+ controlnet_scribble_stable_model_id,
163
+ controlnet_scribble_model_id,
164
+ controlnet_scribble_prompt,
165
+ controlnet_scribble_negative_prompt,
166
+ controlnet_scribble_guidance_scale,
167
+ controlnet_scribble_num_inference_step,
168
+ ],
169
+ outputs=output_image,
170
+ )
diffusion_webui/controlnet/controlnet_seg.py ADDED
@@ -0,0 +1,329 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import torch
4
+ from diffusers import (
5
+ ControlNetModel,
6
+ StableDiffusionControlNetPipeline,
7
+ UniPCMultistepScheduler,
8
+ )
9
+ from PIL import Image
10
+ from transformers import AutoImageProcessor, UperNetForSemanticSegmentation
11
+
12
+ stable_model_list = [
13
+ "runwayml/stable-diffusion-v1-5",
14
+ ]
15
+
16
+ stable_prompt_list = ["a photo of a man.", "a photo of a girl."]
17
+
18
+ stable_negative_prompt_list = ["bad, ugly", "deformed"]
19
+
20
+ data_list = [
21
+ "data/test.png",
22
+ ]
23
+
24
+
25
+ def ade_palette():
26
+ """ADE20K palette that maps each class to RGB values."""
27
+ return [
28
+ [120, 120, 120],
29
+ [180, 120, 120],
30
+ [6, 230, 230],
31
+ [80, 50, 50],
32
+ [4, 200, 3],
33
+ [120, 120, 80],
34
+ [140, 140, 140],
35
+ [204, 5, 255],
36
+ [230, 230, 230],
37
+ [4, 250, 7],
38
+ [224, 5, 255],
39
+ [235, 255, 7],
40
+ [150, 5, 61],
41
+ [120, 120, 70],
42
+ [8, 255, 51],
43
+ [255, 6, 82],
44
+ [143, 255, 140],
45
+ [204, 255, 4],
46
+ [255, 51, 7],
47
+ [204, 70, 3],
48
+ [0, 102, 200],
49
+ [61, 230, 250],
50
+ [255, 6, 51],
51
+ [11, 102, 255],
52
+ [255, 7, 71],
53
+ [255, 9, 224],
54
+ [9, 7, 230],
55
+ [220, 220, 220],
56
+ [255, 9, 92],
57
+ [112, 9, 255],
58
+ [8, 255, 214],
59
+ [7, 255, 224],
60
+ [255, 184, 6],
61
+ [10, 255, 71],
62
+ [255, 41, 10],
63
+ [7, 255, 255],
64
+ [224, 255, 8],
65
+ [102, 8, 255],
66
+ [255, 61, 6],
67
+ [255, 194, 7],
68
+ [255, 122, 8],
69
+ [0, 255, 20],
70
+ [255, 8, 41],
71
+ [255, 5, 153],
72
+ [6, 51, 255],
73
+ [235, 12, 255],
74
+ [160, 150, 20],
75
+ [0, 163, 255],
76
+ [140, 140, 140],
77
+ [250, 10, 15],
78
+ [20, 255, 0],
79
+ [31, 255, 0],
80
+ [255, 31, 0],
81
+ [255, 224, 0],
82
+ [153, 255, 0],
83
+ [0, 0, 255],
84
+ [255, 71, 0],
85
+ [0, 235, 255],
86
+ [0, 173, 255],
87
+ [31, 0, 255],
88
+ [11, 200, 200],
89
+ [255, 82, 0],
90
+ [0, 255, 245],
91
+ [0, 61, 255],
92
+ [0, 255, 112],
93
+ [0, 255, 133],
94
+ [255, 0, 0],
95
+ [255, 163, 0],
96
+ [255, 102, 0],
97
+ [194, 255, 0],
98
+ [0, 143, 255],
99
+ [51, 255, 0],
100
+ [0, 82, 255],
101
+ [0, 255, 41],
102
+ [0, 255, 173],
103
+ [10, 0, 255],
104
+ [173, 255, 0],
105
+ [0, 255, 153],
106
+ [255, 92, 0],
107
+ [255, 0, 255],
108
+ [255, 0, 245],
109
+ [255, 0, 102],
110
+ [255, 173, 0],
111
+ [255, 0, 20],
112
+ [255, 184, 184],
113
+ [0, 31, 255],
114
+ [0, 255, 61],
115
+ [0, 71, 255],
116
+ [255, 0, 204],
117
+ [0, 255, 194],
118
+ [0, 255, 82],
119
+ [0, 10, 255],
120
+ [0, 112, 255],
121
+ [51, 0, 255],
122
+ [0, 194, 255],
123
+ [0, 122, 255],
124
+ [0, 255, 163],
125
+ [255, 153, 0],
126
+ [0, 255, 10],
127
+ [255, 112, 0],
128
+ [143, 255, 0],
129
+ [82, 0, 255],
130
+ [163, 255, 0],
131
+ [255, 235, 0],
132
+ [8, 184, 170],
133
+ [133, 0, 255],
134
+ [0, 255, 92],
135
+ [184, 0, 255],
136
+ [255, 0, 31],
137
+ [0, 184, 255],
138
+ [0, 214, 255],
139
+ [255, 0, 112],
140
+ [92, 255, 0],
141
+ [0, 224, 255],
142
+ [112, 224, 255],
143
+ [70, 184, 160],
144
+ [163, 0, 255],
145
+ [153, 0, 255],
146
+ [71, 255, 0],
147
+ [255, 0, 163],
148
+ [255, 204, 0],
149
+ [255, 0, 143],
150
+ [0, 255, 235],
151
+ [133, 255, 0],
152
+ [255, 0, 235],
153
+ [245, 0, 255],
154
+ [255, 0, 122],
155
+ [255, 245, 0],
156
+ [10, 190, 212],
157
+ [214, 255, 0],
158
+ [0, 204, 255],
159
+ [20, 0, 255],
160
+ [255, 255, 0],
161
+ [0, 153, 255],
162
+ [0, 41, 255],
163
+ [0, 255, 204],
164
+ [41, 0, 255],
165
+ [41, 255, 0],
166
+ [173, 0, 255],
167
+ [0, 245, 255],
168
+ [71, 0, 255],
169
+ [122, 0, 255],
170
+ [0, 255, 184],
171
+ [0, 92, 255],
172
+ [184, 255, 0],
173
+ [0, 133, 255],
174
+ [255, 214, 0],
175
+ [25, 194, 194],
176
+ [102, 255, 0],
177
+ [92, 0, 255],
178
+ ]
179
+
180
+
181
+ def controlnet_mlsd(image_path: str):
182
+ image_processor = AutoImageProcessor.from_pretrained(
183
+ "openmmlab/upernet-convnext-small"
184
+ )
185
+ image_segmentor = UperNetForSemanticSegmentation.from_pretrained(
186
+ "openmmlab/upernet-convnext-small"
187
+ )
188
+
189
+ image = Image.open(image_path).convert("RGB")
190
+ pixel_values = image_processor(image, return_tensors="pt").pixel_values
191
+
192
+ with torch.no_grad():
193
+ outputs = image_segmentor(pixel_values)
194
+
195
+ seg = image_processor.post_process_semantic_segmentation(
196
+ outputs, target_sizes=[image.size[::-1]]
197
+ )[0]
198
+
199
+ color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
200
+ palette = np.array(ade_palette())
201
+
202
+ for label, color in enumerate(palette):
203
+ color_seg[seg == label, :] = color
204
+
205
+ color_seg = color_seg.astype(np.uint8)
206
+ image = Image.fromarray(color_seg)
207
+ controlnet = ControlNetModel.from_pretrained(
208
+ "lllyasviel/sd-controlnet-seg", torch_dtype=torch.float16
209
+ )
210
+
211
+ return controlnet, image
212
+
213
+
214
+ def stable_diffusion_controlnet_seg(
215
+ image_path: str,
216
+ model_path: str,
217
+ prompt: str,
218
+ negative_prompt: str,
219
+ guidance_scale: int,
220
+ num_inference_step: int,
221
+ ):
222
+
223
+ controlnet, image = controlnet_mlsd(image_path=image_path)
224
+
225
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
226
+ pretrained_model_name_or_path=model_path,
227
+ controlnet=controlnet,
228
+ safety_checker=None,
229
+ torch_dtype=torch.float16,
230
+ )
231
+
232
+ pipe.to("cuda")
233
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
234
+ pipe.enable_xformers_memory_efficient_attention()
235
+
236
+ output = pipe(
237
+ prompt=prompt,
238
+ image=image,
239
+ negative_prompt=negative_prompt,
240
+ num_inference_steps=num_inference_step,
241
+ guidance_scale=guidance_scale,
242
+ ).images
243
+
244
+ return output[0]
245
+
246
+
247
+ def stable_diffusion_controlnet_seg_app():
248
+ with gr.Blocks():
249
+ with gr.Row():
250
+ with gr.Column():
251
+ controlnet_seg_image_file = gr.Image(
252
+ type="filepath", label="Image"
253
+ )
254
+
255
+ controlnet_seg_model_id = gr.Dropdown(
256
+ choices=stable_model_list,
257
+ value=stable_model_list[0],
258
+ label="Stable Model Id",
259
+ )
260
+
261
+ controlnet_seg_prompt = gr.Textbox(
262
+ lines=1, value=stable_prompt_list[0], label="Prompt"
263
+ )
264
+
265
+ controlnet_seg_negative_prompt = gr.Textbox(
266
+ lines=1,
267
+ value=stable_negative_prompt_list[0],
268
+ label="Negative Prompt",
269
+ )
270
+
271
+ with gr.Accordion("Advanced Options", open=False):
272
+ controlnet_seg_guidance_scale = gr.Slider(
273
+ minimum=0.1,
274
+ maximum=15,
275
+ step=0.1,
276
+ value=7.5,
277
+ label="Guidance Scale",
278
+ )
279
+
280
+ controlnet_seg_num_inference_step = gr.Slider(
281
+ minimum=1,
282
+ maximum=100,
283
+ step=1,
284
+ value=50,
285
+ label="Num Inference Step",
286
+ )
287
+
288
+ controlnet_seg_predict = gr.Button(value="Generator")
289
+
290
+ with gr.Column():
291
+ output_image = gr.Image(label="Output")
292
+
293
+ gr.Examples(
294
+ fn=stable_diffusion_controlnet_seg,
295
+ examples=[
296
+ [
297
+ data_list[0],
298
+ stable_model_list[0],
299
+ stable_prompt_list[0],
300
+ stable_negative_prompt_list[0],
301
+ 7.5,
302
+ 50,
303
+ ],
304
+ ],
305
+ inputs=[
306
+ controlnet_seg_image_file,
307
+ controlnet_seg_model_id,
308
+ controlnet_seg_prompt,
309
+ controlnet_seg_negative_prompt,
310
+ controlnet_seg_guidance_scale,
311
+ controlnet_seg_num_inference_step,
312
+ ],
313
+ outputs=[output_image],
314
+ cache_examples=False,
315
+ label="ControlNet Segmentation Example",
316
+ )
317
+
318
+ controlnet_seg_predict.click(
319
+ fn=stable_diffusion_controlnet_seg,
320
+ inputs=[
321
+ controlnet_seg_image_file,
322
+ controlnet_seg_model_id,
323
+ controlnet_seg_prompt,
324
+ controlnet_seg_negative_prompt,
325
+ controlnet_seg_guidance_scale,
326
+ controlnet_seg_num_inference_step,
327
+ ],
328
+ outputs=[output_image],
329
+ )
diffusion_webui/helpers.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusion_webui.controlnet.controlnet_canny import (
2
+ stable_diffusion_controlnet_canny_app,
3
+ )
4
+ from diffusion_webui.controlnet.controlnet_depth import (
5
+ stable_diffusion_controlnet_depth_app,
6
+ )
7
+ from diffusion_webui.controlnet.controlnet_hed import (
8
+ stable_diffusion_controlnet_hed_app,
9
+ )
10
+ from diffusion_webui.controlnet.controlnet_mlsd import (
11
+ stable_diffusion_controlnet_mlsd_app,
12
+ )
13
+ from diffusion_webui.controlnet.controlnet_pose import (
14
+ stable_diffusion_controlnet_pose_app,
15
+ )
16
+ from diffusion_webui.controlnet.controlnet_scribble import (
17
+ stable_diffusion_controlnet_scribble_app,
18
+ )
19
+ from diffusion_webui.controlnet.controlnet_seg import (
20
+ stable_diffusion_controlnet_seg_app,
21
+ )
22
+ from diffusion_webui.stable_diffusion.img2img_app import (
23
+ stable_diffusion_img2img_app,
24
+ )
25
+ from diffusion_webui.stable_diffusion.inpaint_app import (
26
+ stable_diffusion_inpaint_app,
27
+ )
28
+ from diffusion_webui.stable_diffusion.keras_txt2img import (
29
+ keras_stable_diffusion_app,
30
+ )
31
+ from diffusion_webui.stable_diffusion.text2img_app import (
32
+ stable_diffusion_text2img_app,
33
+ )
diffusion_webui/stable_diffusion/__init__.py ADDED
File without changes
diffusion_webui/stable_diffusion/__pycache__/__init__.cpython-38.pyc ADDED
Binary file (191 Bytes). View file
 
diffusion_webui/stable_diffusion/__pycache__/img2img_app.cpython-38.pyc ADDED
Binary file (2.55 kB). View file
 
diffusion_webui/stable_diffusion/__pycache__/inpaint_app.cpython-38.pyc ADDED
Binary file (2.42 kB). View file
 
diffusion_webui/stable_diffusion/__pycache__/keras_txt2img.cpython-38.pyc ADDED
Binary file (2.79 kB). View file
 
diffusion_webui/stable_diffusion/__pycache__/text2img_app.cpython-38.pyc ADDED
Binary file (2.74 kB). View file
 
diffusion_webui/stable_diffusion/img2img_app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import DDIMScheduler, StableDiffusionImg2ImgPipeline
4
+ from PIL import Image
5
+
6
+ stable_model_list = [
7
+ "runwayml/stable-diffusion-v1-5",
8
+ "stabilityai/stable-diffusion-2-1",
9
+ ]
10
+
11
+ stable_prompt_list = ["a photo of a man.", "a photo of a girl."]
12
+
13
+ stable_negative_prompt_list = ["bad, ugly", "deformed"]
14
+
15
+ data_list = [
16
+ "data/test.png",
17
+ ]
18
+
19
+
20
+ def stable_diffusion_img2img(
21
+ image_path: str,
22
+ model_path: str,
23
+ prompt: str,
24
+ negative_prompt: str,
25
+ guidance_scale: int,
26
+ num_inference_step: int,
27
+ ):
28
+
29
+ image = Image.open(image_path)
30
+
31
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
32
+ model_path, safety_checker=None, torch_dtype=torch.float16
33
+ )
34
+ pipe.to("cuda")
35
+ pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
36
+ pipe.enable_xformers_memory_efficient_attention()
37
+
38
+ output = pipe(
39
+ prompt=prompt,
40
+ image=image,
41
+ negative_prompt=negative_prompt,
42
+ num_inference_steps=num_inference_step,
43
+ guidance_scale=guidance_scale,
44
+ ).images
45
+
46
+ return output[0]
47
+
48
+
49
+ def stable_diffusion_img2img_app():
50
+ with gr.Blocks():
51
+ with gr.Row():
52
+ with gr.Column():
53
+ image2image2_image_file = gr.Image(
54
+ type="filepath", label="Image"
55
+ )
56
+
57
+ image2image_model_path = gr.Dropdown(
58
+ choices=stable_model_list,
59
+ value=stable_model_list[0],
60
+ label="Image-Image Model Id",
61
+ )
62
+
63
+ image2image_prompt = gr.Textbox(
64
+ lines=1, value=stable_prompt_list[0], label="Prompt"
65
+ )
66
+
67
+ image2image_negative_prompt = gr.Textbox(
68
+ lines=1,
69
+ value=stable_negative_prompt_list[0],
70
+ label="Negative Prompt",
71
+ )
72
+
73
+ with gr.Accordion("Advanced Options", open=False):
74
+ image2image_guidance_scale = gr.Slider(
75
+ minimum=0.1,
76
+ maximum=15,
77
+ step=0.1,
78
+ value=7.5,
79
+ label="Guidance Scale",
80
+ )
81
+
82
+ image2image_num_inference_step = gr.Slider(
83
+ minimum=1,
84
+ maximum=100,
85
+ step=1,
86
+ value=50,
87
+ label="Num Inference Step",
88
+ )
89
+
90
+ image2image_predict = gr.Button(value="Generator")
91
+
92
+ with gr.Column():
93
+ output_image = gr.Image(label="Output")
94
+
95
+ gr.Examples(
96
+ fn=stable_diffusion_img2img,
97
+ examples=[
98
+ [
99
+ data_list[0],
100
+ stable_model_list[0],
101
+ stable_prompt_list[0],
102
+ stable_negative_prompt_list[0],
103
+ 7.5,
104
+ 50,
105
+ ],
106
+ ],
107
+ inputs=[
108
+ image2image2_image_file,
109
+ image2image_model_path,
110
+ image2image_prompt,
111
+ image2image_negative_prompt,
112
+ image2image_guidance_scale,
113
+ image2image_num_inference_step,
114
+ ],
115
+ outputs=[output_image],
116
+ cache_examples=False,
117
+ label="Image-Image Generator",
118
+ )
119
+
120
+ image2image_predict.click(
121
+ fn=stable_diffusion_img2img,
122
+ inputs=[
123
+ image2image2_image_file,
124
+ image2image_model_path,
125
+ image2image_prompt,
126
+ image2image_negative_prompt,
127
+ image2image_guidance_scale,
128
+ image2image_num_inference_step,
129
+ ],
130
+ outputs=[output_image],
131
+ )
diffusion_webui/stable_diffusion/inpaint_app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import DDIMScheduler, DiffusionPipeline
4
+
5
+ stable_inpiant_model_list = [
6
+ "stabilityai/stable-diffusion-2-inpainting",
7
+ "runwayml/stable-diffusion-inpainting",
8
+ ]
9
+
10
+ stable_prompt_list = ["a photo of a man.", "a photo of a girl."]
11
+
12
+ stable_negative_prompt_list = ["bad, ugly", "deformed"]
13
+
14
+
15
+ def stable_diffusion_inpaint(
16
+ dict: str,
17
+ model_path: str,
18
+ prompt: str,
19
+ negative_prompt: str,
20
+ guidance_scale: int,
21
+ num_inference_step: int,
22
+ ):
23
+
24
+ image = dict["image"].convert("RGB").resize((512, 512))
25
+ mask_image = dict["mask"].convert("RGB").resize((512, 512))
26
+ pipe = DiffusionPipeline.from_pretrained(
27
+ model_path,
28
+ revision="fp16",
29
+ torch_dtype=torch.float16,
30
+ )
31
+ pipe.to("cuda")
32
+ pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
33
+ pipe.enable_xformers_memory_efficient_attention()
34
+
35
+ output = pipe(
36
+ prompt=prompt,
37
+ image=image,
38
+ mask_image=mask_image,
39
+ negative_prompt=negative_prompt,
40
+ num_inference_steps=num_inference_step,
41
+ guidance_scale=guidance_scale,
42
+ ).images
43
+
44
+ return output[0]
45
+
46
+
47
+ def stable_diffusion_inpaint_app():
48
+ with gr.Blocks():
49
+ with gr.Row():
50
+ with gr.Column():
51
+ inpaint_image_file = gr.Image(
52
+ source="upload",
53
+ tool="sketch",
54
+ elem_id="image_upload",
55
+ type="pil",
56
+ label="Upload",
57
+ )
58
+
59
+ inpaint_model_id = gr.Dropdown(
60
+ choices=stable_inpiant_model_list,
61
+ value=stable_inpiant_model_list[0],
62
+ label="Inpaint Model Id",
63
+ )
64
+
65
+ inpaint_prompt = gr.Textbox(
66
+ lines=1, value=stable_prompt_list[0], label="Prompt"
67
+ )
68
+
69
+ inpaint_negative_prompt = gr.Textbox(
70
+ lines=1,
71
+ value=stable_negative_prompt_list[0],
72
+ label="Negative Prompt",
73
+ )
74
+
75
+ with gr.Accordion("Advanced Options", open=False):
76
+ inpaint_guidance_scale = gr.Slider(
77
+ minimum=0.1,
78
+ maximum=15,
79
+ step=0.1,
80
+ value=7.5,
81
+ label="Guidance Scale",
82
+ )
83
+
84
+ inpaint_num_inference_step = gr.Slider(
85
+ minimum=1,
86
+ maximum=100,
87
+ step=1,
88
+ value=50,
89
+ label="Num Inference Step",
90
+ )
91
+
92
+ inpaint_predict = gr.Button(value="Generator")
93
+
94
+ with gr.Column():
95
+ output_image = gr.Gallery(label="Outputs")
96
+
97
+ inpaint_predict.click(
98
+ fn=stable_diffusion_inpaint,
99
+ inputs=[
100
+ inpaint_image_file,
101
+ inpaint_model_id,
102
+ inpaint_prompt,
103
+ inpaint_negative_prompt,
104
+ inpaint_guidance_scale,
105
+ inpaint_num_inference_step,
106
+ ],
107
+ outputs=output_image,
108
+ )
diffusion_webui/stable_diffusion/keras_txt2img.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ from huggingface_hub import from_pretrained_keras
4
+ from keras_cv import models
5
+ from tensorflow import keras
6
+
7
+ keras_model_list = [
8
+ "keras-dreambooth/keras_diffusion_lowpoly_world",
9
+ "keras-dreambooth/pink-floyd-division-bell",
10
+ "keras-dreambooth/dreambooth_diffusion_model",
11
+ ]
12
+
13
+ stable_prompt_list = [
14
+ "a photo of lowpoly_world",
15
+ "Flower vase inspired by pink floyd division bell",
16
+ ]
17
+
18
+ stable_negative_prompt_list = ["bad, ugly", "deformed"]
19
+
20
+
21
+ def keras_stable_diffusion(
22
+ model_path: str,
23
+ prompt: str,
24
+ negative_prompt: str,
25
+ guidance_scale: int,
26
+ num_inference_step: int,
27
+ height: int,
28
+ width: int,
29
+ ):
30
+
31
+ with tf.device("/GPU:0"):
32
+ keras.mixed_precision.set_global_policy("mixed_float16")
33
+
34
+ sd_dreambooth_model = models.StableDiffusion(
35
+ img_width=height, img_height=width
36
+ )
37
+
38
+ db_diffusion_model = from_pretrained_keras(model_path)
39
+ sd_dreambooth_model._diffusion_model = db_diffusion_model
40
+
41
+ generated_images = sd_dreambooth_model.text_to_image(
42
+ prompt=prompt,
43
+ negative_prompt=negative_prompt,
44
+ num_steps=num_inference_step,
45
+ unconditional_guidance_scale=guidance_scale,
46
+ )
47
+
48
+ return generated_images
49
+
50
+
51
+ def keras_stable_diffusion_app():
52
+ with gr.Blocks():
53
+ with gr.Row():
54
+ with gr.Column():
55
+ keras_text2image_model_path = gr.Dropdown(
56
+ choices=keras_model_list,
57
+ value=keras_model_list[0],
58
+ label="Text-Image Model Id",
59
+ )
60
+
61
+ keras_text2image_prompt = gr.Textbox(
62
+ lines=1, value=stable_prompt_list[0], label="Prompt"
63
+ )
64
+
65
+ keras_text2image_negative_prompt = gr.Textbox(
66
+ lines=1,
67
+ value=stable_negative_prompt_list[0],
68
+ label="Negative Prompt",
69
+ )
70
+
71
+ with gr.Accordion("Advanced Options", open=False):
72
+ keras_text2image_guidance_scale = gr.Slider(
73
+ minimum=0.1,
74
+ maximum=15,
75
+ step=0.1,
76
+ value=7.5,
77
+ label="Guidance Scale",
78
+ )
79
+
80
+ keras_text2image_num_inference_step = gr.Slider(
81
+ minimum=1,
82
+ maximum=100,
83
+ step=1,
84
+ value=50,
85
+ label="Num Inference Step",
86
+ )
87
+
88
+ keras_text2image_height = gr.Slider(
89
+ minimum=128,
90
+ maximum=1280,
91
+ step=32,
92
+ value=512,
93
+ label="Image Height",
94
+ )
95
+
96
+ keras_text2image_width = gr.Slider(
97
+ minimum=128,
98
+ maximum=1280,
99
+ step=32,
100
+ value=512,
101
+ label="Image Height",
102
+ )
103
+
104
+ keras_text2image_predict = gr.Button(value="Generator")
105
+
106
+ with gr.Column():
107
+ output_image = gr.Gallery(label="Output")
108
+
109
+ gr.Examples(
110
+ fn=keras_stable_diffusion,
111
+ inputs=[
112
+ keras_text2image_model_path,
113
+ keras_text2image_prompt,
114
+ keras_text2image_negative_prompt,
115
+ keras_text2image_guidance_scale,
116
+ keras_text2image_num_inference_step,
117
+ keras_text2image_height,
118
+ keras_text2image_width,
119
+ ],
120
+ outputs=[output_image],
121
+ examples=[
122
+ [
123
+ keras_model_list[0],
124
+ stable_prompt_list[0],
125
+ stable_negative_prompt_list[0],
126
+ 7.5,
127
+ 50,
128
+ 512,
129
+ 512,
130
+ ],
131
+ ],
132
+ label="Keras Stable Diffusion Example",
133
+ cache_examples=False,
134
+ )
135
+
136
+ keras_text2image_predict.click(
137
+ fn=keras_stable_diffusion,
138
+ inputs=[
139
+ keras_text2image_model_path,
140
+ keras_text2image_prompt,
141
+ keras_text2image_negative_prompt,
142
+ keras_text2image_guidance_scale,
143
+ keras_text2image_num_inference_step,
144
+ keras_text2image_height,
145
+ keras_text2image_width,
146
+ ],
147
+ outputs=output_image,
148
+ )
diffusion_webui/stable_diffusion/text2img_app.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import DDIMScheduler, StableDiffusionPipeline
4
+
5
+ stable_model_list = [
6
+ "runwayml/stable-diffusion-v1-5",
7
+ "stabilityai/stable-diffusion-2-1",
8
+ "sd-dreambooth-library/disco-diffusion-style",
9
+ "prompthero/openjourney-v2",
10
+ "andite/anything-v4.0",
11
+ "Lykon/DreamShaper",
12
+ "nitrosocke/Nitro-Diffusion",
13
+ "dreamlike-art/dreamlike-diffusion-1.0",
14
+ ]
15
+
16
+ stable_prompt_list = ["a photo of a man.", "a photo of a girl."]
17
+
18
+ stable_negative_prompt_list = ["bad, ugly", "deformed"]
19
+
20
+
21
+ def stable_diffusion_text2img(
22
+ model_path: str,
23
+ prompt: str,
24
+ negative_prompt: str,
25
+ guidance_scale: int,
26
+ num_inference_step: int,
27
+ height: int,
28
+ width: int,
29
+ ):
30
+
31
+ pipe = StableDiffusionPipeline.from_pretrained(
32
+ model_path, safety_checker=None, torch_dtype=torch.float16
33
+ ).to("cuda")
34
+
35
+ pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
36
+ pipe.enable_xformers_memory_efficient_attention()
37
+
38
+ images = pipe(
39
+ prompt,
40
+ height=height,
41
+ width=width,
42
+ negative_prompt=negative_prompt,
43
+ num_inference_steps=num_inference_step,
44
+ guidance_scale=guidance_scale,
45
+ ).images
46
+
47
+ return images[0]
48
+
49
+
50
+ def stable_diffusion_text2img_app():
51
+ with gr.Blocks():
52
+ with gr.Row():
53
+ with gr.Column():
54
+ text2image_model_path = gr.Dropdown(
55
+ choices=stable_model_list,
56
+ value=stable_model_list[0],
57
+ label="Text-Image Model Id",
58
+ )
59
+
60
+ text2image_prompt = gr.Textbox(
61
+ lines=1, value=stable_prompt_list[0], label="Prompt"
62
+ )
63
+
64
+ text2image_negative_prompt = gr.Textbox(
65
+ lines=1,
66
+ value=stable_negative_prompt_list[0],
67
+ label="Negative Prompt",
68
+ )
69
+
70
+ with gr.Accordion("Advanced Options", open=False):
71
+ text2image_guidance_scale = gr.Slider(
72
+ minimum=0.1,
73
+ maximum=15,
74
+ step=0.1,
75
+ value=7.5,
76
+ label="Guidance Scale",
77
+ )
78
+
79
+ text2image_num_inference_step = gr.Slider(
80
+ minimum=1,
81
+ maximum=100,
82
+ step=1,
83
+ value=50,
84
+ label="Num Inference Step",
85
+ )
86
+
87
+ text2image_height = gr.Slider(
88
+ minimum=128,
89
+ maximum=1280,
90
+ step=32,
91
+ value=512,
92
+ label="Image Height",
93
+ )
94
+
95
+ text2image_width = gr.Slider(
96
+ minimum=128,
97
+ maximum=1280,
98
+ step=32,
99
+ value=768,
100
+ label="Image Width",
101
+ )
102
+
103
+ text2image_predict = gr.Button(value="Generator")
104
+
105
+ with gr.Column():
106
+ output_image = gr.Image(label="Output")
107
+
108
+ gr.Examples(
109
+ examples=[
110
+ [
111
+ stable_model_list[0],
112
+ stable_prompt_list[0],
113
+ stable_negative_prompt_list[0],
114
+ 7.5,
115
+ 50,
116
+ 512,
117
+ 768,
118
+ ]
119
+ ],
120
+ inputs=[
121
+ text2image_model_path,
122
+ text2image_prompt,
123
+ text2image_negative_prompt,
124
+ text2image_guidance_scale,
125
+ text2image_num_inference_step,
126
+ text2image_height,
127
+ text2image_width,
128
+ ],
129
+ outputs=[output_image],
130
+ cache_examples=False,
131
+ fn=stable_diffusion_text2img,
132
+ label="Text2Image Example",
133
+ )
134
+ text2image_predict.click(
135
+ fn=stable_diffusion_text2img,
136
+ inputs=[
137
+ text2image_model_path,
138
+ text2image_prompt,
139
+ text2image_negative_prompt,
140
+ text2image_guidance_scale,
141
+ text2image_num_inference_step,
142
+ text2image_height,
143
+ text2image_width,
144
+ ],
145
+ outputs=output_image,
146
+ )
pyproject.toml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ [tool.black]
2
+ line-length = 80
3
+
4
+ [tool.isort]
5
+ line_length = 80
6
+ profile = "black"
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ transformers
2
+ bitsandbytes==0.35.0
3
+ xformers
4
+ controlnet_aux
5
+ diffusers
6
+ imageio
7
+ gradio
8
+ triton
9
+ tensorflow
10
+ huggingface-hub
11
+ keras-cv
12
+ pycocotools
script/code_formatter.sh ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ black . --config pyproject.toml
2
+ isort .