File size: 14,784 Bytes
2091d9d
 
 
 
 
 
5fc9884
2091d9d
 
 
 
 
 
 
 
 
 
 
31b607a
2091d9d
 
 
 
 
d51b2d2
2091d9d
 
 
 
d51b2d2
2091d9d
 
 
 
 
2bf41dd
2091d9d
 
 
d51b2d2
2091d9d
 
 
d51b2d2
2091d9d
 
d51b2d2
f6debc2
2091d9d
 
 
 
 
 
d51b2d2
 
 
 
 
2091d9d
 
d51b2d2
2091d9d
 
 
 
 
 
 
 
d51b2d2
2091d9d
 
a368971
 
 
 
 
 
 
 
 
 
 
2091d9d
 
 
9cdcb42
 
 
 
2091d9d
 
 
 
 
 
 
 
 
 
 
 
 
d51b2d2
 
2091d9d
 
 
 
 
 
 
 
 
 
 
 
 
 
bc10e06
d51b2d2
9cdcb42
d51b2d2
 
a28e570
d51b2d2
 
6bd8651
9cdcb42
6b09c44
 
 
6bd8651
 
 
 
9cdcb42
6b09c44
6bd8651
 
72e5969
6bd8651
 
 
9cdcb42
6bd8651
 
bad5e96
 
2091d9d
 
 
6bd8651
4e6b06e
466bbaf
 
4e6b06e
2091d9d
 
21fde4a
2091d9d
d51b2d2
2091d9d
 
 
 
 
 
 
 
 
 
 
00fc93f
2091d9d
 
 
21fde4a
466bbaf
21fde4a
2091d9d
 
 
 
 
 
e7039e8
2091d9d
 
 
 
 
 
 
 
 
 
 
 
 
d51b2d2
 
 
 
 
2091d9d
 
 
 
d51b2d2
 
 
 
 
2091d9d
 
 
a28e570
2091d9d
 
 
 
a28e570
2091d9d
 
 
a28e570
2091d9d
a81339e
2091d9d
d51b2d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2091d9d
 
 
 
 
 
 
 
 
 
 
 
d51b2d2
2091d9d
d51b2d2
 
2091d9d
 
 
 
 
ee83503
 
 
d51b2d2
 
2091d9d
6bd8651
2091d9d
 
 
bad5e96
 
 
 
6bd8651
6b09c44
6bd8651
bad5e96
6bd8651
 
6b09c44
 
2091d9d
 
 
d51b2d2
6bd8651
 
2091d9d
 
 
 
d51b2d2
2091d9d
ef8a4ee
2091d9d
 
 
 
54511f0
2091d9d
094fe55
31b607a
75d592d
6b7bef0
 
6bd8651
 
6b7bef0
d51b2d2
6b7bef0
d51b2d2
6b7bef0
d51b2d2
6bd8651
d51b2d2
 
 
 
 
 
 
6bd8651
d51b2d2
 
 
6bd8651
2091d9d
d51b2d2
 
 
 
 
6bd8651
d51b2d2
 
 
6bd8651
6b09c44
d51b2d2
 
 
46ea192
d51b2d2
 
 
 
 
 
46ea192
d51b2d2
 
 
 
 
 
 
6bd8651
d51b2d2
 
 
 
 
 
6bd8651
d51b2d2
 
 
 
 
 
46ea192
d51b2d2
2091d9d
46ea192
2091d9d
 
46ea192
2091d9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10356b1
 
094fe55
10356b1
094fe55
 
 
 
10356b1
 
 
 
 
 
 
 
2091d9d
 
 
6bd8651
2091d9d
 
d51b2d2
2091d9d
 
 
 
d51b2d2
 
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import cv2
import torch
import random
import tempfile
import numpy as np
from pathlib import Path
from PIL import Image
from diffusers import (
    ControlNetModel,
    StableDiffusionXLControlNetPipeline,
    UNet2DConditionModel,
    EulerDiscreteScheduler,
)
import spaces
import gradio as gr
from huggingface_hub import hf_hub_download, snapshot_download
from ip_adapter import IPAdapterXL
from safetensors.torch import load_file
from rembg import remove

snapshot_download(
    repo_id="h94/IP-Adapter", allow_patterns="sdxl_models/*", local_dir="."
)

# global variable
MAX_SEED = np.iinfo(np.int32).max
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16 if str(device).__contains__("cuda") else torch.float32

# initialization
base_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
image_encoder_path = "sdxl_models/image_encoder"
ip_ckpt = "sdxl_models/ip-adapter_sdxl.bin"

controlnet_path = "diffusers/controlnet-canny-sdxl-1.0"
controlnet = ControlNetModel.from_pretrained(
    controlnet_path, use_safetensors=False, torch_dtype=torch.float16
).to(device)

# load SDXL lightnining

pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
    base_model_path,
    controlnet=controlnet,
    torch_dtype=torch.float16,
    variant="fp16",
    add_watermarker=False,
).to(device)
pipe.set_progress_bar_config(disable=True)
pipe.scheduler = EulerDiscreteScheduler.from_config(
    pipe.scheduler.config, timestep_spacing="trailing", prediction_type="epsilon"
)
pipe.unet.load_state_dict(
    load_file(
        hf_hub_download(
            "ByteDance/SDXL-Lightning", "sdxl_lightning_2step_unet.safetensors"
        ),
        device="cuda",
    )
)

# load ip-adapter
# target_blocks=["block"] for original IP-Adapter
# target_blocks=["up_blocks.0.attentions.1"] for style blocks only
# target_blocks = ["up_blocks.0.attentions.1", "down_blocks.2.attentions.1"] # for style+layout blocks
ip_model = IPAdapterXL(
    pipe,
    image_encoder_path,
    ip_ckpt,
    device,
    target_blocks=["up_blocks.0.attentions.1"],
)

def segment(input_image):
    # Convert PIL image to NumPy array
    image_np = np.array(input_image)
    
    # Remove background
    image_np = remove(image_np)
    
    # Convert back to PIL image
    input_image = Image.fromarray(image_np)
    
    return input_image

def resize_img(
    input_image,
    # max_side=1280,
    # min_side=1024,
    max_side=1024,
    min_side=512,
    size=None,
    pad_to_max_side=False,
    mode=Image.BILINEAR,
    base_pixel_number=64,
):
    w, h = input_image.size
    if size is not None:
        w_resize_new, h_resize_new = size
    else:
        ratio = min_side / min(h, w)
        w, h = round(ratio * w), round(ratio * h)
        ratio = max_side / max(h, w)
        input_image = input_image.resize([round(ratio * w), round(ratio * h)], mode)
        w_resize_new = (round(ratio * w) // base_pixel_number) * base_pixel_number
        h_resize_new = (round(ratio * h) // base_pixel_number) * base_pixel_number
    input_image = input_image.resize([w_resize_new, h_resize_new], mode)

    if pad_to_max_side:
        res = np.ones([max_side, max_side, 3], dtype=np.uint8) * 255
        offset_x = (max_side - w_resize_new) // 2
        offset_y = (max_side - h_resize_new) // 2
        res[offset_y : offset_y + h_resize_new, offset_x : offset_x + w_resize_new] = (
            np.array(input_image)
        )
        input_image = Image.fromarray(res)
    return input_image

examples = [
    [
        "./asset/0.jpg",
        None,
        "3d render, 3d model, clean 3d style, cute space monster on mars, isolated clean white background, cinematic lighting",
        1.0,
        0.0,
        0.0,
    ],
    [
        "./asset/zeichnung1.jpg",
        "./asset/zeichnung1mask.png",
        "3d render, 3d model, clean 3d style, space ship, isolated clean white background, cinematic lighting",
        0.95,
        0.5,
        0.5,
    ],
    [   
        "./asset/zeichnung2.jpg",
        "./asset/zeichnung2mask.png",
        "3d render, 3d model, clean 3d style, space station on mars, isolated clean white background, cinematic lighting",
        1.0,
        0.5,
        0.3,
    ],
    [   
        "./asset/zeichnung3.jpg",
        "./asset/zeichnung3mask.png",
        "3d render, 3d model, clean 3d style, cute space astronaut on mars, isolated clean white background, cinematic lighting",
        1.0,
        0.45,
        1.5,
    ],
]

def run_for_examples(style_image, source_image, prompt, scale, control_scale, guidance_scale):
    # added
    # if source_image is not None:
    #     source_image = segment(source_image)
    #/added
    return create_image(
        image_pil=style_image,
        input_image=source_image,
        prompt=prompt,
        n_prompt="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
        scale=scale,
        control_scale=control_scale,
        guidance_scale=0.0,
        num_inference_steps=2,
        seed=42,
        target="Load only style blocks",
        neg_content_prompt="",
        neg_content_scale=0,
    )


@spaces.GPU
def create_image(
    image_pil,
    input_image,
    # added
    # src_image_pil,
    #/added
    prompt,
    n_prompt,
    scale,
    control_scale,
    guidance_scale,
    num_inference_steps,
    seed,
    target="Load only style blocks",
    neg_content_prompt=None,
    neg_content_scale=0,
):
    seed = random.randint(0, MAX_SEED) if seed == -1 else seed
    if target == "Load original IP-Adapter":
        # target_blocks=["blocks"] for original IP-Adapter
        ip_model = IPAdapterXL(
            pipe, image_encoder_path, ip_ckpt, device, target_blocks=["blocks"]
        )
    elif target == "Load only style blocks":
        # target_blocks=["up_blocks.0.attentions.1"] for style blocks only
        ip_model = IPAdapterXL(
            pipe,
            image_encoder_path,
            ip_ckpt,
            device,
            target_blocks=["up_blocks.0.attentions.1"],
        )
    elif target == "Load style+layout block":
        # target_blocks = ["up_blocks.0.attentions.1", "down_blocks.2.attentions.1"] # for style+layout blocks
        ip_model = IPAdapterXL(
            pipe,
            image_encoder_path,
            ip_ckpt,
            device,
            target_blocks=["up_blocks.0.attentions.1", "down_blocks.2.attentions.1"],
        )

    if input_image is not None:
        input_image = resize_img(input_image, max_side=1024)
        cv_input_image = pil_to_cv2(input_image)
        detected_map = cv2.Canny(cv_input_image, 50, 200)
        canny_map = Image.fromarray(cv2.cvtColor(detected_map, cv2.COLOR_BGR2RGB))
    else:
        canny_map = Image.new("RGB", (1024, 1024), color=(255, 255, 255))
        control_scale = 0

    if float(control_scale) == 0:
        canny_map = canny_map.resize((1024, 1024))

    if len(neg_content_prompt) > 0 and neg_content_scale != 0:
        images = ip_model.generate(
            pil_image=image_pil,
            prompt=prompt,
            negative_prompt=n_prompt,
            scale=scale,
            guidance_scale=guidance_scale,
            num_samples=1,
            num_inference_steps=num_inference_steps,
            seed=seed,
            image=canny_map,
            controlnet_conditioning_scale=float(control_scale),
            neg_content_prompt=neg_content_prompt,
            neg_content_scale=neg_content_scale,
        )
    else:
        images = ip_model.generate(
            pil_image=image_pil,
            prompt=prompt,
            negative_prompt=n_prompt,
            scale=scale,
            guidance_scale=guidance_scale,
            num_samples=1,
            num_inference_steps=num_inference_steps,
            seed=seed,
            image=canny_map,
            controlnet_conditioning_scale=float(control_scale),
        )
    image = images[0]
    with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmpfile:
        image.save(tmpfile, "JPEG", quality=80, optimize=True, progressive=True)
        return Path(tmpfile.name)


def pil_to_cv2(image_pil):
    image_np = np.array(image_pil)
    image_cv2 = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
    return image_cv2

def segment(image_pil):
    return remove(image_pil)


# Description
title = r"""
<h1 align="center">Pipeline 1: 2.5D( image to image )</h1>
"""

description = r"""
<b>ARM <3 GoldExtra KI-Demo #1<b/>
<p>Bei der finalen Version läuft dieser Teil auf einer dedicated GPU! Bitte beachten: Bei der Demo mit einer "Zero" GPU kann es zu kurzen Wartezeiten 
und Leistungsbeschränkungen kommen, da Zero's günstig zum Testen sind, jedoch von vielen Menschen gleichzeitig verwendet werden. Im Fall der Fälle bitte 
kurz warten, dann geht sie wieder.</p><br>
<p>Normalerweise sollte ein <b>Scale<b/> von '1' die besten Ergebnisse liefern. Das ist ein normierter Wert, den wir erreichen wollen. Bei '1.x' bleibt 
die KI näher beim Bild und bei '0.x' fängt die KI an Bildinhalte dazu zu erfinden.<br>
Man kann auch die Details öffnen und an den feineren Rädchend drehen. Die fürs Projekt spannenden Einstellungen sind der <b>ControlNet Scale</b>, der verändert,
wie wichtig der KI das Input Bild ist und der <b>Guidance Scale</b>, bei dem es um die Wichtigkeit des Text-Prompts geht. Die restlichen Einstellungen könnt ihr ignorieren.
Das Source-Image ist sowas wie eine Malvorlage für die KI. D.h. sie wird versuchen die Inhalte der Vorlage so genau wie möglich bei der Bild-Synthese zu verwenden.
Bei der finalen Version, wird hier automatisch eine Vorlage der Zeichnung erstellt, damit die Ergebnisse der 2.5D-Synthese möglichst nah (Farbe, Form, Komposition)
der Kidnerzeichnung ist, und gleichzeitig ein Bild generiert, das vergleichsweise gute Ergebnisse bei der 3D Synthese erziehlt. Der zeichnerische, leicht verwaschene Stil
der 2.5D Synthese ist erwünscht und wird für die 3D Synthese gebraucht.</p>
"""

article = r"""
<br>
Bei technischen Fragen schickt bitte ein <a href="mailto:hideo@artificialmuseum.com">Mail an Hidéo</a> oder bei allgemeinen Fragen schickt
<a href="mailto:team@artificialmuseum.com">Mail an das ARM-Team</a>.
"""

block = gr.Blocks()
with block:
    # description
    gr.Markdown(title)
    gr.Markdown(description)

    with gr.Tabs():
        with gr.Row():
            with gr.Column():
                with gr.Row():
                    with gr.Column():
                        with gr.Row():
#                            with gr.Column():
                            image_pil = gr.Image(label="Style Image", type="pil")
                    with gr.Column():
                        prompt = gr.Textbox(
                            label="Text-Prompts",
                            value="<How (Guidance)>, <What (Objekt)>, <Where (Location)>, <With (Qualität)>",
                        )

                        scale = gr.Slider(
                            minimum=0, maximum=2.0, step=0.01, value=1.0, label="Scale"
                        )

                with gr.Accordion(open=False, label="Motorhaube (optional)"):
                    target = gr.Radio(
                        [
                            "Load only style blocks",
                            "Load style+layout block",
                            "Load original IP-Adapter",
                        ],
                        value="Load only style blocks",
                        label="Adapter Modus",
                    )
                    with gr.Column():
                        src_image_pil = gr.Image(
                            label="Source Image (optional/wird generiert)", type="pil"
                        )
                    control_scale = gr.Slider(
                        minimum=0,
                        maximum=1.0,
                        step=0.01,
                        value=0.5,
                        label="ControlNet Scale (Relevanz: Input-Bild)",
                    )

                    n_prompt = gr.Textbox(
                        label="Negative Prompt (Ignore this!)",
                        value="text, watermark, lowres, low quality, worst quality, deformed, detached, broken, glitch, low contrast, noisy, saturation, blurry, blur",
                    )

                    neg_content_prompt = gr.Textbox(
                        label="Negative Content Prompt (Ignore this!)", value=""
                    )
                    neg_content_scale = gr.Slider(
                        minimum=0,
                        maximum=1.0,
                        step=0.01,
                        value=0.5,
                        label="NCS (Ignore this!) // neg_content_scale",
                    )

                    guidance_scale = gr.Slider(
                        minimum=0,
                        maximum=10.0,
                        step=0.01,
                        value=0.0,
                        label="Guidance Scale (Relevanz: Text-Prompt)",
                    )
                    num_inference_steps = gr.Slider(
                        minimum=2,
                        maximum=50.0,
                        step=1.0,
                        value=2,
                        label="Inference Steps (Stärke der Bildsynthese)",
                    )
                    seed = gr.Slider(
                        minimum=-1,
                        maximum=MAX_SEED,
                        value=-1,
                        step=1,
                        label="Seed Value (Seed-Proof) // -1 == random",
                    )

                generate_button = gr.Button("Simsalabim")

            with gr.Column():
                generated_image = gr.Image(label="Magix uWu")

    inputs = [
        image_pil,
        src_image_pil,
        prompt,
        n_prompt,
        scale,
        control_scale,
        guidance_scale,
        num_inference_steps,
        seed,
        target,
        neg_content_prompt,
        neg_content_scale,
    ]
    outputs = [generated_image]

    gr.on(
        triggers=[
#            prompt.input,
            generate_button.click,
#            guidance_scale.input,
#            scale.input,
#            control_scale.input,
#            seed.input,
        ],
        fn=create_image,
        inputs=inputs,
        outputs=outputs,
        show_progress="minimal",
        show_api=False,
        trigger_mode="always_last",
    )

    gr.Examples(
        examples=examples,
        inputs=[image_pil, src_image_pil, prompt, scale, control_scale, guidance_scale],
        fn=run_for_examples,
        outputs=[generated_image],
        cache_examples=True,
    )

    gr.Markdown(article)

block.queue(api_open=False)
block.launch(show_api=False)