File size: 10,343 Bytes
cab0202
 
0c34bcf
cab0202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6a0569
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cab0202
 
 
b6a0569
cab0202
b6a0569
cab0202
 
 
 
 
 
 
 
 
b6a0569
fe8561f
 
b6a0569
 
 
 
 
cab0202
 
b6a0569
cab0202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08d3aea
 
2353791
 
860a671
 
 
08d3aea
 
2353791
cab0202
 
 
 
 
 
8d70627
cab0202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import gradio as gr
import torch
import cv2
from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
from PIL import Image

from diffusion_webui.diffusion_models.base_controlnet_pipeline import (
    ControlnetPipeline,
)
from diffusion_webui.utils.model_list import (
    controlnet_model_list,
    stable_model_list,
)
from diffusion_webui.utils.preprocces_utils import PREPROCCES_DICT
from diffusion_webui.utils.scheduler_list import (
    SCHEDULER_MAPPING,
    get_scheduler,
)


stable_model_list = [
    "runwayml/stable-diffusion-v1-5",
    "dreamlike-art/dreamlike-diffusion-1.0",
    "kadirnar/maturemalemix_v0",
    "kadirnar/DreamShaper_v6"
]

stable_inpiant_model_list = [
    "stabilityai/stable-diffusion-2-inpainting",
    "runwayml/stable-diffusion-inpainting",
    "saik0s/realistic_vision_inpainting",
]

controlnet_model_list = [
    "lllyasviel/control_v11p_sd15_canny",
    "lllyasviel/control_v11f1p_sd15_depth",
    "lllyasviel/control_v11p_sd15_openpose",
    "lllyasviel/control_v11p_sd15_scribble",
    "lllyasviel/control_v11p_sd15_mlsd",
    "lllyasviel/control_v11e_sd15_shuffle",
    "lllyasviel/control_v11e_sd15_ip2p",
    "lllyasviel/control_v11p_sd15_lineart",
    "lllyasviel/control_v11p_sd15s2_lineart_anime",
    "lllyasviel/control_v11p_sd15_softedge",
]

class StableDiffusionControlNetGenerator(ControlnetPipeline):
    def __init__(self):
        self.pipe = None
        
    def load_model(self, stable_model_path, controlnet_model_path, scheduler):
        if self.pipe is None or self.pipe.model_name != stable_model_path or self.pipe.scheduler_name != scheduler:
            controlnet = ControlNetModel.from_pretrained(
                controlnet_model_path, torch_dtype=torch.float16
            )
            self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
                pretrained_model_name_or_path=stable_model_path,
                controlnet=controlnet,
                safety_checker=None,
                torch_dtype=torch.float16,
            )
            self.pipe.model_name = stable_model_path
            self.pipe.scheduler_name = scheduler
            
            self.pipe = get_scheduler(pipe=self.pipe, scheduler=scheduler)
            self.pipe.scheduler_name = scheduler
            self.pipe.to("cuda")
            self.pipe.enable_xformers_memory_efficient_attention()
    
        return self.pipe


    def controlnet_preprocces(
        self,
        read_image: str,
        preprocces_type: str,
    ):
        processed_image = PREPROCCES_DICT[preprocces_type](read_image)
        return processed_image

    def generate_image(
        self,
        image_path: str,
        stable_model_path: str,
        controlnet_model_path: str,
        height: int,
        width: int,
        guess_mode: bool,
        controlnet_conditioning_scale: int,
        prompt: str,
        negative_prompt: str,
        num_images_per_prompt: int,
        guidance_scale: int,
        num_inference_step: int,
        scheduler: str,
        seed_generator: int,
        preprocces_type: str,
    ):
        pipe = self.load_model(
            stable_model_path=stable_model_path,
            controlnet_model_path=controlnet_model_path,
            scheduler=scheduler,
        )
        if preprocces_type== "ScribbleXDOG":
            read_image = cv2.imread(image_path)
            controlnet_image = self.controlnet_preprocces(read_image=read_image, preprocces_type=preprocces_type)[0]
            controlnet_image = Image.fromarray(controlnet_image)
            
        elif preprocces_type== "None":
            controlnet_image = self.controlnet_preprocces(read_image=image_path, preprocces_type=preprocces_type)
        else:
            read_image = Image.open(image_path)
            controlnet_image = self.controlnet_preprocces(read_image=read_image, preprocces_type=preprocces_type)

        if seed_generator == 0:
            random_seed = torch.randint(0, 1000000, (1,))
            generator = torch.manual_seed(random_seed)
        else:
            generator = torch.manual_seed(seed_generator)
        

        output = pipe(
            prompt=prompt,
            height=height,
            width=width,
            controlnet_conditioning_scale=float(controlnet_conditioning_scale),
            guess_mode=guess_mode,
            image=controlnet_image,
            negative_prompt=negative_prompt,
            num_images_per_prompt=num_images_per_prompt,
            num_inference_steps=num_inference_step,
            guidance_scale=guidance_scale,
            generator=generator,
        ).images

        return output

    def app():
        with gr.Blocks():
            with gr.Row():
                with gr.Column():
                    controlnet_image_path = gr.Image(
                        type="filepath", label="Image"
                    ).style(height=260)
                    controlnet_prompt = gr.Textbox(
                        lines=1, placeholder="Prompt", show_label=False
                    )
                    controlnet_negative_prompt = gr.Textbox(
                        lines=1, placeholder="Negative Prompt", show_label=False
                    )

                    with gr.Row():
                        with gr.Column():
                            controlnet_stable_model_path = gr.Dropdown(
                                choices=stable_model_list,
                                value=stable_model_list[0],
                                label="Stable Model Path",
                            )
                            controlnet_preprocces_type = gr.Dropdown(
                                choices=list(PREPROCCES_DICT.keys()),
                                value=list(PREPROCCES_DICT.keys())[0],
                                label="Preprocess Type",
                            )
                            controlnet_conditioning_scale = gr.Slider(
                                minimum=0.0,
                                maximum=1.0,
                                step=0.1,
                                value=1.0,
                                label="ControlNet Conditioning Scale",
                            )
                            controlnet_guidance_scale = gr.Slider(
                                minimum=0.1,
                                maximum=15,
                                step=0.1,
                                value=7.5,
                                label="Guidance Scale",
                            )
                            controlnet_height = gr.Slider(
                                minimum=128,
                                maximum=1280,
                                step=32,
                                value=512,
                                label="Height",
                            )
                            controlnet_width = gr.Slider(
                                minimum=128,
                                maximum=1280,
                                step=32,
                                value=512,
                                label="Width",
                            )

                        with gr.Row():
                            with gr.Column():
                                controlnet_model_path = gr.Dropdown(
                                    choices=controlnet_model_list,
                                    value=controlnet_model_list[0],
                                    label="ControlNet Model Path",
                                )
                                controlnet_scheduler = gr.Dropdown(
                                    choices=list(SCHEDULER_MAPPING.keys()),
                                    value=list(SCHEDULER_MAPPING.keys())[0],
                                    label="Scheduler",
                                )
                                controlnet_num_inference_step = gr.Slider(
                                    minimum=1,
                                    maximum=150,
                                    step=1,
                                    value=30,
                                    label="Num Inference Step",
                                )

                                controlnet_num_images_per_prompt = gr.Slider(
                                    minimum=1,
                                    maximum=4,
                                    step=1,
                                    value=1,
                                    label="Number Of Images",
                                )
                                controlnet_seed_generator = gr.Slider(
                                    minimum=0,
                                    maximum=1000000,
                                    step=1,
                                    value=0,
                                    label="Seed(0 for random)",
                                )
                                controlnet_guess_mode = gr.Checkbox(
                                    label="Guess Mode"
                                )

                    # Button to generate the image
                    predict_button = gr.Button(value="Generate Image")

                with gr.Column():
                    # Gallery to display the generated images
                    output_image = gr.Gallery(
                        label="Generated images",
                        show_label=False,
                        elem_id="gallery",
                    ).style(grid=(1, 2))

        predict_button.click(
            fn=StableDiffusionControlNetGenerator().generate_image,
            inputs=[
                controlnet_image_path,
                controlnet_stable_model_path,
                controlnet_model_path,
                controlnet_height,
                controlnet_width,
                controlnet_guess_mode,
                controlnet_conditioning_scale,
                controlnet_prompt,
                controlnet_negative_prompt,
                controlnet_num_images_per_prompt,
                controlnet_guidance_scale,
                controlnet_num_inference_step,
                controlnet_scheduler,
                controlnet_seed_generator,
                controlnet_preprocces_type,
            ],
            outputs=[output_image],
        )