File size: 6,260 Bytes
4342954
6013cb0
eee6c85
 
4342954
 
 
 
 
 
 
e3e1936
ef0eb1c
e3e1936
4342954
 
ef0eb1c
4342954
e3e1936
7d60aec
4342954
 
 
 
 
 
 
237c7d9
4342954
 
 
 
6013cb0
4342954
 
 
6013cb0
 
 
 
 
 
9633dc9
 
 
 
 
 
103dcbe
9633dc9
 
 
 
 
 
 
 
6013cb0
 
 
eee6c85
4342954
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c515dbd
 
4342954
 
 
 
 
 
 
 
 
6013cb0
 
 
4342954
 
6013cb0
 
 
 
 
 
 
4342954
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c515dbd
4342954
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6013cb0
 
 
 
 
 
 
 
4342954
6013cb0
 
 
 
 
 
 
 
c515dbd
4342954
 
 
 
 
 
 
 
 
 
 
 
 
 
c515dbd
6013cb0
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
import torch

torch.jit.script = lambda f: f

import spaces
import numpy as np
from diffusers import (
    ControlNetModel,
    StableDiffusionControlNetPipeline,
    UniPCMultistepScheduler,
)
import gradio as gr
from huggingface_hub import hf_hub_download

from annotator.util import resize_image, HWC3
from annotator.midas import DepthDetector
from annotator.dsine_local import NormalDetector
from annotator.upernet import SegmDetector

controlnet_checkpoint = "kujiale-ai/controlnet-layout"
# Initialize pipeline
controlnet = ControlNetModel.from_pretrained(
    controlnet_checkpoint,
    subfolder="control_v1_sd15_layout_fp16",
    torch_dtype=torch.float16,
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
    "stablediffusionapi/realistic-vision-v51", controlnet=controlnet, torch_dtype=torch.float16
).to("cuda")
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)

apply_depth = DepthDetector()
apply_normal = NormalDetector(hf_hub_download("camenduru/DSINE", filename="dsine.pt"))
apply_segm = SegmDetector()


layout_examples = [
    [
        "examples/layout_input.jpg",
        "A modern bedroom",
        "examples/layout_output.jpg",
    ],
    [
        "examples/living_and_dining_room_input.jpg",
        "A modern living and dining room",
        "examples/living_and_dining_room_output.jpg",
    ],
    [
        "examples/living_room_input.png",
        "A living room",
        "examples/living_room_output.jpg",
    ],
    [
        "examples/kitchen_input.jpg",
        "A furnished kitchen",
        "examples/kitchen_output.jpg",
    ],
]


@spaces.GPU(duration=20)
def generate(
    input_image,
    prompt,
    a_prompt,
    n_prompt,
    num_samples,
    image_resolution,
    steps,
    strength,
    guidance_scale,
    seed,
):
    color_image = resize_image(HWC3(input_image), image_resolution)
    # set seed
    np.random.seed(seed)
    torch.manual_seed(seed)

    with torch.no_grad():
        depth_image = apply_depth(color_image)
        normal_image = apply_normal(color_image)
        segm_image = apply_segm(color_image)

        # Prepare Layout Control Image
        depth_image = np.array(depth_image, dtype=np.float32) / 255.0
        depth_image = torch.from_numpy(depth_image[:, :, None])[None].permute(
            0, 3, 1, 2
        )
        normal_image = np.array(normal_image, dtype=np.float32)
        normal_image = normal_image / 127.5 - 1.0
        normal_image = torch.from_numpy(normal_image)[None].permute(0, 3, 1, 2)
        segm_image = np.array(segm_image, dtype=np.float32) / 255.0
        segm_image = torch.from_numpy(segm_image)[None].permute(0, 3, 1, 2)
        control_image = torch.cat([depth_image, normal_image, segm_image], dim=1)

    generator = torch.Generator(device="cuda").manual_seed(seed)
    images = pipe(
        prompt + a_prompt,
        negative_prompt=n_prompt,
        num_images_per_prompt=num_samples,
        num_inference_steps=steps,
        image=control_image,
        generator=generator,
        guidance_scale=float(guidance_scale),
        controlnet_conditioning_scale=float(strength),
    ).images
    return images


block = gr.Blocks().queue()
with block:
    with gr.Row():
        gr.Markdown("## KuJiaLe Layout ControlNet Demo")
    with gr.Row():
        gr.Markdown(
            "### Checkout our released model at [kujiale-ai/controlnet-layout](https://huggingface.co/kujiale-ai/controlnet-layout)"
        )
    with gr.Row():
        with gr.Column():
            with gr.Row():
                input_image = gr.Image(
                    sources="upload", type="numpy", label="Input Image", height=512
                )

            prompt = gr.Textbox(label="Prompt")
            run_button = gr.Button(value="Run")
            with gr.Accordion("Advanced options", open=False):
                num_samples = gr.Slider(
                    label="Images", minimum=1, maximum=2, value=1, step=1
                )
                image_resolution = gr.Slider(
                    label="Image Resolution",
                    minimum=512,
                    maximum=768,
                    value=768,
                    step=64,
                )
                strength = gr.Slider(
                    label="Control Strength",
                    minimum=0.0,
                    maximum=2.0,
                    value=1.0,
                    step=0.1,
                )
                steps = gr.Slider(
                    label="Steps", minimum=1, maximum=50, value=25, step=1
                )
                guidance_scale = gr.Slider(
                    label="Guidance Scale",
                    minimum=0.1,
                    maximum=20.0,
                    value=7.5,
                    step=0.1,
                )
                seed = gr.Slider(
                    label="Seed", minimum=-1, maximum=2147483647, value=1, step=1
                )
                a_prompt = gr.Textbox(
                    label="Added Prompt", value="best quality, extremely detailed"
                )
                n_prompt = gr.Textbox(
                    label="Negative Prompt",
                    value="longbody, lowres, bad anatomy, human, extra digit, fewer digits, cropped, worst quality, low quality",
                )

        with gr.Column():
            image_gallery = gr.Gallery(
                label="Output",
                show_label=False,
                elem_id="gallery",
                height=512,
                object_fit="contain",
            )
    with gr.Row():
        dummy_image_for_outputs = gr.Image(visible=False, label="Result")
        gr.Examples(
            fn=lambda *args: [[args[-1]], args[-2]],
            examples=layout_examples,
            inputs=[input_image, prompt, dummy_image_for_outputs],
            outputs=[image_gallery, prompt],
            run_on_click=True,
            examples_per_page=1024,
        )

    ips = [
        input_image,
        prompt,
        a_prompt,
        n_prompt,
        num_samples,
        image_resolution,
        steps,
        strength,
        guidance_scale,
        seed,
    ]
    run_button.click(fn=generate, inputs=ips, outputs=[image_gallery])

block.launch(server_name="0.0.0.0")