File size: 24,016 Bytes
ad6358b
 
 
 
 
 
da1d929
 
 
 
 
 
 
 
 
ad6358b
 
6cc454a
ad6358b
6cc454a
ad6358b
6cc454a
 
 
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
1a92f6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad6358b
 
 
 
 
 
3e4dba7
ad6358b
 
 
 
6cc454a
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cc454a
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fdefc9
 
ad6358b
 
 
 
 
 
 
 
 
 
 
 
9fdefc9
 
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fdefc9
 
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fdefc9
 
ad6358b
9fdefc9
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a92f6a
ad6358b
 
9fdefc9
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e4dba7
ad6358b
 
 
 
 
 
 
9fdefc9
ad6358b
 
 
 
 
 
f5a76ac
ad6358b
 
 
 
 
 
 
6b68615
ad6358b
3e4dba7
 
ad6358b
 
 
 
 
 
 
 
 
 
3e4dba7
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a92f6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da1d929
 
 
 
 
 
 
 
 
 
 
 
 
 
ad6358b
 
 
 
da1d929
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
da1d929
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
da1d929
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
da1d929
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
da1d929
ad6358b
 
 
 
 
 
 
 
 
6b68615
 
 
 
da1d929
6b68615
 
 
 
 
 
 
 
 
ad6358b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a92f6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bad722e
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
import gradio as gr
import numpy as np
import time
import math
import random
import torch

try:
    import spaces
except:
    class spaces():
        def GPU(*args, **kwargs):
            def decorator(function):
                return lambda *dummy_args, **dummy_kwargs: function(*dummy_args, **dummy_kwargs)
            return decorator

from diffusers import StableDiffusionXLInpaintPipeline
from PIL import Image
import PIL.ImageOps
from pillow_heif import register_heif_opener

register_heif_opener()

max_64_bit_int = np.iinfo(np.int32).max

if torch.cuda.is_available():
    device = "cuda"
    floatType = torch.float16
    variant = "fp16"
else:
    device = "cpu"
    floatType = torch.float32
    variant = None

pipe = StableDiffusionXLInpaintPipeline.from_pretrained("diffusers/stable-diffusion-xl-1.0-inpainting-0.1", torch_dtype = floatType, variant = variant)
pipe = pipe.to(device)

default_local_storage = {
        "prompt":               "",
        "negative_prompt":      "Ugly, malformed, noise, blur, watermark",
        "num_inference_steps":  25,
        "guidance_scale":       7,
        "image_guidance_scale": 1.1,
        "strength":             0.99,
        "denoising_steps":      1000,
        "randomize_seed":       True,
        "seed":                 random.randint(0, max_64_bit_int),
        "debug_mode":           False
        }

def save_preferences_prompt(preferences, value):
    preferences["prompt"] = value
    return preferences

def save_preferences_negative_prompt(preferences, value):
    preferences["negative_prompt"] = value
    return preferences

def save_preferences_num_inference_steps(preferences, value):
    preferences["num_inference_steps"] = value
    return preferences

def save_preferences_guidance_scale(preferences, value):
    preferences["guidance_scale"] = value
    return preferences

def save_preferences_image_guidance_scale(preferences, value):
    preferences["image_guidance_scale"] = value
    return preferences

def save_preferences_strength(preferences, value):
    preferences["strength"] = value
    return preferences

def save_preferences_denoising_steps(preferences, value):
    preferences["denoising_steps"] = value
    return preferences

def save_preferences_randomize_seed(preferences, value):
    preferences["randomize_seed"] = value
    return preferences

def save_preferences_seed(preferences, value):
    preferences["seed"] = value
    return preferences

def save_preferences_debug_mode(preferences, value):
    preferences["debug_mode"] = value
    return preferences

def load_preferences(saved_prefs):
    saved_prefs = init_preferences(saved_prefs)
    return [
        saved_prefs["prompt"],
        saved_prefs["negative_prompt"],
        saved_prefs["num_inference_steps"],
        saved_prefs["guidance_scale"],
        saved_prefs["image_guidance_scale"],
        saved_prefs["strength"],
        saved_prefs["denoising_steps"],
        saved_prefs["randomize_seed"],
        saved_prefs["seed"],
        saved_prefs["debug_mode"]
    ]

def init_preferences(saved_prefs):
    if saved_prefs is None:
        saved_prefs = default_local_storage
    return saved_prefs

def update_seed(is_randomize_seed, seed):
    if is_randomize_seed:
        return random.randint(0, max_64_bit_int)
    return seed

def toggle_debug(is_debug_mode):
    return [gr.update(visible = True)] + [gr.update(visible = is_debug_mode)] * 2

def check(
    source_img,
    prompt,
    uploaded_mask: Image.Image,
    negative_prompt,
    num_inference_steps,
    guidance_scale,
    image_guidance_scale,
    strength,
    denoising_steps,
    is_randomize_seed,
    seed,
    debug_mode,
    progress = gr.Progress()
):
    if source_img is None:
        raise gr.Error("Please provide an image.")

    if prompt is None or prompt == "":
        raise gr.Error("Please provide a prompt input.")

def inpaint(
    source_img,
    prompt,
    uploaded_mask: Image.Image,
    negative_prompt,
    num_inference_steps,
    guidance_scale,
    image_guidance_scale,
    strength,
    denoising_steps,
    is_randomize_seed,
    seed,
    debug_mode,
    progress = gr.Progress()
):
    check(
        source_img,
        prompt,
        uploaded_mask,
        negative_prompt,
        num_inference_steps,
        guidance_scale,
        image_guidance_scale,
        strength,
        denoising_steps,
        is_randomize_seed,
        seed,
        debug_mode
    )
    start = time.time()
    progress(0, desc = "Preparing data...")

    if negative_prompt is None:
        negative_prompt = ""

    if num_inference_steps is None:
        num_inference_steps = 25

    if guidance_scale is None:
        guidance_scale = 7

    if image_guidance_scale is None:
        image_guidance_scale = 1.1

    if strength is None:
        strength = 0.99

    if denoising_steps is None:
        denoising_steps = 1000

    if seed is None:
        seed = random.randint(0, max_64_bit_int)

    random.seed(seed)
    #pipe = pipe.manual_seed(seed)

    input_image = source_img["background"].convert("RGB")

    original_height, original_width, original_channel = np.array(input_image).shape
    output_width = original_width
    output_height = original_height

    if uploaded_mask is None:
        mask_image = source_img["layers"][0].convert("RGB")
    else:
        mask_image = uploaded_mask.convert("RGB")
        mask_image = mask_image.resize((original_width, original_height))

    # Limited to 1 million pixels
    if 1024 * 1024 < output_width * output_height:
        factor = ((1024 * 1024) / (output_width * output_height))**0.5
        process_width = math.floor(output_width * factor)
        process_height = math.floor(output_height * factor)

        limitation = " Due to technical limitation, the image have been downscaled and then upscaled.";
    else:
        process_width = output_width
        process_height = output_height

        limitation = "";

    # Width and height must be multiple of 8
    if (process_width % 8) != 0 or (process_height % 8) != 0:
        if ((process_width - (process_width % 8) + 8) * (process_height - (process_height % 8) + 8)) <= (1024 * 1024):
            process_width = process_width - (process_width % 8) + 8
            process_height = process_height - (process_height % 8) + 8
        elif (process_height % 8) <= (process_width % 8) and ((process_width - (process_width % 8) + 8) * process_height) <= (1024 * 1024):
            process_width = process_width - (process_width % 8) + 8
            process_height = process_height - (process_height % 8)
        elif (process_width % 8) <= (process_height % 8) and (process_width * (process_height - (process_height % 8) + 8)) <= (1024 * 1024):
            process_width = process_width - (process_width % 8)
            process_height = process_height - (process_height % 8) + 8
        else:
            process_width = process_width - (process_width % 8)
            process_height = process_height - (process_height % 8)

    if torch.cuda.is_available():
        progress(None, desc = "Searching a GPU...")
    output_image = inpaint_on_gpu(
        seed,
        process_width,
        process_height,
        prompt,
        negative_prompt,
        input_image,
        mask_image,
        num_inference_steps,
        guidance_scale,
        image_guidance_scale,
        strength,
        denoising_steps,
        progress
    )

    if limitation != "":
        output_image = output_image.resize((output_width, output_height))

    if debug_mode == False:
        input_image = None
        mask_image = None

    end = time.time()
    secondes = int(end - start)
    minutes = math.floor(secondes / 60)
    secondes = secondes - (minutes * 60)
    hours = math.floor(minutes / 60)
    minutes = minutes - (hours * 60)
    return [
        output_image,
        ("Start again to get a different result. " if is_randomize_seed else "") + "The image has been generated in " + ((str(hours) + " h, ") if hours != 0 else "") + ((str(minutes) + " min, ") if hours != 0 or minutes != 0 else "") + str(secondes) + " sec." + limitation,
        input_image,
        mask_image
    ]

def inpaint_on_gpu2(
        seed,
        process_width,
        process_height,
        prompt,
        negative_prompt,
        input_image,
        mask_image,
        num_inference_steps,
        guidance_scale,
        image_guidance_scale,
        strength,
        denoising_steps,
        progress
):
    return input_image

@spaces.GPU(duration=420)
def inpaint_on_gpu(
        seed,
        process_width,
        process_height,
        prompt,
        negative_prompt,
        input_image,
        mask_image,
        num_inference_steps,
        guidance_scale,
        image_guidance_scale,
        strength,
        denoising_steps,
        progress
):
    progress(None, desc = "Processing...")
    return pipe(
        seeds = [seed],
        width = process_width,
        height = process_height,
        prompt = prompt,
        negative_prompt = negative_prompt,
        image = input_image,
        mask_image = mask_image,
        num_inference_steps = num_inference_steps,
        guidance_scale = guidance_scale,
        image_guidance_scale = image_guidance_scale,
        strength = strength,
        denoising_steps = denoising_steps,
        show_progress_bar = True
    ).images[0]

with gr.Blocks() as interface:
    local_storage = gr.BrowserState(default_local_storage)
    gr.HTML(
        """
        <h1 style="text-align: center;">Inpaint / Outpaint</h1>
        <p style="text-align: center;">Modifies one detail of your image, at any resolution, freely, without account, without watermark, without installation, which can be downloaded</p>
        <br/>
        <br/>
        ✨ Powered by <i>SDXL 1.0</i> artificial intellingence. For illustration purpose, not information purpose. The new content is not based on real information but imagination.
        <br/>
        <ul>
        <li>To change the <b>view angle</b> of your image, I recommend to use <i>Zero123</i>,</li>
        <li>To <b>upscale</b> your image, I recommend to use <i><a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR">SUPIR</a></i>,</li>
        <li>To <b>slightly change</b> your image, I recommend to use <i>Image-to-Image SDXL</i>,</li>
        <li>If you need to enlarge the <b>viewpoint</b> of your image, I recommend you to use <i>Uncrop</i>,</li>
        <li>To remove the <b>background</b> of your image, I recommend to use <i>BRIA</i>,</li>
        <li>To make a <b>tile</b> of your image, I recommend to use <i>Make My Image Tile</i>,</li>
        <li>To modify <b>anything else</b> on your image, I recommend to use <i>Instruct Pix2Pix</i>.</li>
        </ul>
        <br/>
        """ + ("🏃‍♀️ Estimated time: few minutes. Current device: GPU." if torch.cuda.is_available() else "🐌 Slow process... ~1 hour. Current device: CPU.") + """
        You can duplicate this space on a free account, it's designed to work on CPU, GPU and ZeroGPU.<br/>
        <a href='https://huggingface.co/spaces/Fabrice-TIERCELIN/Inpaint?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14'></a>
        <br/>
        ⚖️ You can use, modify and share the generated images but not for commercial uses.

        """
    )
    with gr.Column():
        source_img = gr.ImageMask(label = "Your image (click on the landscape 🌄 to upload your image; click on the pen 🖌️ to draw the mask)", type = "pil", brush=gr.Brush(colors=["#FFFFFF80"], color_mode="fixed"))
        prompt = gr.Textbox(label = "Prompt", info = "Describe the subject, the background and the style of image; 77 token limit", placeholder = "Describe what you want to see in the entire image", lines = 2)
        with gr.Accordion("Upload a mask", open = False):
             uploaded_mask = gr.Image(label = "Already made mask (black pixels will be preserved, white pixels will be redrawn)", sources = ["upload"], type = "pil")
        with gr.Accordion("Advanced options", open = False):
             negative_prompt = gr.Textbox(label = "Negative prompt", placeholder = "Describe what you do NOT want to see in the entire image", value = "Ugly, malformed, noise, blur, watermark")
             num_inference_steps = gr.Slider(minimum = 10, maximum = 100, value = 25, step = 1, label = "Number of inference steps", info = "lower=faster, higher=image quality")
             guidance_scale = gr.Slider(minimum = 1, maximum = 13, value = 7, step = 0.1, label = "Guidance Scale", info = "lower=image quality, higher=follow the prompt")
             image_guidance_scale = gr.Slider(minimum = 1, value = 1.1, step = 0.1, label = "Image Guidance Scale", info = "lower=image quality, higher=follow the image")
             strength = gr.Slider(value = 0.99, minimum = 0.01, maximum = 1.0, step = 0.01, label = "Strength", info = "lower=follow the original area, higher=redraw from scratch")
             denoising_steps = gr.Number(minimum = 0, value = 1000, step = 1, label = "Denoising", info = "lower=irrelevant result, higher=relevant result")
             randomize_seed = gr.Checkbox(label = "\U0001F3B2 Randomize seed", value = True, info = "If checked, result is always different")
             seed = gr.Slider(minimum = 0, maximum = max_64_bit_int, step = 1, randomize = True, label = "Seed")
             debug_mode = gr.Checkbox(label = "Debug mode", value = False, info = "Show intermediate results")

        submit = gr.Button("🚀 Inpaint/Outpaint", variant = "primary")

        warning = gr.HTML(value = "<center><span style='color: red;''>Your computer must not enter into standby mode.</span> On Chrome, you can force to keep a tab alive in <code>chrome://discards/</code> The generation time may vary on the number of steps and the resolution of the image.</center>", visible = False)

        inpainted_image = gr.Image(label = "Inpainted image")
        information = gr.HTML()
        original_image = gr.Image(label = "Original image", visible = False)
        mask_image = gr.Image(label = "Mask image", visible = False)

    submit.click(update_seed, inputs = [
        randomize_seed, seed
    ], outputs = [
        seed
    ], queue = False, show_progress = False).then(toggle_debug, debug_mode, [
        warning,
        original_image,
        mask_image
    ], queue = False, show_progress = False).then(check, inputs = [
        source_img,
        prompt,
        uploaded_mask,
        negative_prompt,
        num_inference_steps,
        guidance_scale,
        image_guidance_scale,
        strength,
        denoising_steps,
        randomize_seed,
        seed,
        debug_mode
    ], outputs = [], queue = False, show_progress = False).success(inpaint, inputs = [
        source_img,
        prompt,
        uploaded_mask,
        negative_prompt,
        num_inference_steps,
        guidance_scale,
        image_guidance_scale,
        strength,
        denoising_steps,
        randomize_seed,
        seed,
        debug_mode
    ], outputs = [
        inpainted_image,
        information,
        original_image,
        mask_image
    ], scroll_to_output = True)

    prompt.change(fn = save_preferences_prompt, inputs = [
        local_storage,
        prompt,
    ], outputs = [
        local_storage
    ])

    negative_prompt.change(fn = save_preferences_negative_prompt, inputs = [
        local_storage,
        negative_prompt,
    ], outputs = [
        local_storage
    ])

    num_inference_steps.change(fn = save_preferences_num_inference_steps, inputs = [
        local_storage,
        num_inference_steps,
    ], outputs = [
        local_storage
    ])

    guidance_scale.change(fn = save_preferences_guidance_scale, inputs = [
        local_storage,
        guidance_scale,
    ], outputs = [
        local_storage
    ])

    image_guidance_scale.change(fn = save_preferences_image_guidance_scale, inputs = [
        local_storage,
        image_guidance_scale,
    ], outputs = [
        local_storage
    ])

    strength.change(fn = save_preferences_strength, inputs = [
        local_storage,
        strength,
    ], outputs = [
        local_storage
    ])

    denoising_steps.change(fn = save_preferences_denoising_steps, inputs = [
        local_storage,
        denoising_steps,
    ], outputs = [
        local_storage
    ])

    randomize_seed.change(fn = save_preferences_randomize_seed, inputs = [
        local_storage,
        randomize_seed,
    ], outputs = [
        local_storage
    ])

    seed.change(fn = save_preferences_seed, inputs = [
        local_storage,
        seed,
    ], outputs = [
        local_storage
    ])

    debug_mode.change(fn = save_preferences_debug_mode, inputs = [
        local_storage,
        debug_mode,
    ], outputs = [
        local_storage
    ])

    gr.Examples(
        fn = inpaint,
	    inputs = [
            source_img,
            prompt,
            uploaded_mask,
            negative_prompt,
            num_inference_steps,
            guidance_scale,
            image_guidance_scale,
            strength,
            denoising_steps,
            randomize_seed,
            seed,
            debug_mode
        ],
	    outputs = [
            inpainted_image,
            information,
            original_image,
            mask_image
        ],
        examples = [
                [
                    "./Examples/Example7.png",
                    "A birthday cake with lit candles",
                    "./Examples/Mask7.png",
                    "Ugly, malformed, painting, drawing, cartoon, anime, 3d, noise, blur, watermark, text, error, logo, username, sitename, URL",
                    10,
                    7,
                    1.1,
                    0.99,
                    1000,
                    False,
                    42,
                    False
                ],
                [
                    "./Examples/Example1.png",
                    "A deer, in a forest landscape, ultrarealistic, realistic, photorealistic, 8k",
                    "./Examples/Mask1.webp",
                    "Ugly, malformed, painting, drawing, cartoon, anime, 3d, noise, blur, watermark, text, error, logo, username, sitename, URL",
                    25,
                    7,
                    1.1,
                    0.99,
                    1000,
                    False,
                    42,
                    False
                ],
                [
                    "./Examples/Example3.jpg",
                    "An angry old woman, ultrarealistic, realistic, photorealistic, 8k",
                    "./Examples/Mask3.gif",
                    "Ugly, malformed, painting, drawing, cartoon, anime, 3d, noise, blur, watermark, text, error, logo, username, sitename, URL",
                    25,
                    7,
                    1.5,
                    0.99,
                    1000,
                    False,
                    42,
                    False
                ],
                [
                    "./Examples/Example4.gif",
                    "A laptop, ultrarealistic, realistic, photorealistic, 8k",
                    "./Examples/Mask4.bmp",
                    "Ugly, malformed, painting, drawing, cartoon, anime, 3d, noise, blur, watermark, text, error, logo, username, sitename, URL",
                    25,
                    7,
                    1.1,
                    0.99,
                    1000,
                    False,
                    42,
                    False
                ],
                [
                    "./Examples/Example5.bmp",
                    "A sand castle, ultrarealistic, realistic, photorealistic, 8k",
                    "./Examples/Mask5.png",
                    "Ugly, malformed, painting, drawing, cartoon, anime, 3d, noise, blur, watermark, text, error, logo, username, sitename, URL",
                    50,
                    7,
                    1.5,
                    0.5,
                    1000,
                    False,
                    42,
                    False
                ],
                [
                    "./Examples/Example2.webp",
                    "A cat, ultrarealistic, realistic, photorealistic, 8k",
                    "./Examples/Mask2.png",
                    "Ugly, malformed, painting, drawing, cartoon, anime, 3d, noise, blur, watermark, text, error, logo, username, sitename, URL",
                    25,
                    7,
                    1.1,
                    0.99,
                    1000,
                    False,
                    42,
                    False
                ],
                [
                    "./Examples/Example6.webp",
                    "A car, in the street, in a city, photorealistic, realistic, extremely detailled, 8k",
                    "./Examples/Mask6.webp",
                    "Forest, wood, trees, ugly, malformed, painting, drawing, cartoon, anime, 3d, noise, blur, watermark, text, error, logo, username, sitename, URL",
                    25,
                    7,
                    1.1,
                    0.99,
                    1000,
                    False,
                    42,
                    False
                ],
            ],
        cache_examples = False,
    )
    
    gr.Markdown(
        """
        ## How to prompt your image

        To easily read your prompt, start with the subject, then describ the pose or action, then secondary elements, then the background, then the graphical style, then the image quality:
        ```
        A Vietnamese woman, red clothes, walking, smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
        ```

        You can use round brackets to increase the importance of a part:
        ```
        A Vietnamese woman, (red clothes), walking, smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
        ```

        You can use several levels of round brackets to even more increase the importance of a part:
        ```
        A Vietnamese woman, ((red clothes)), (walking), smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
        ```

        You can use number instead of several round brackets:
        ```
        A Vietnamese woman, (red clothes:1.5), (walking), smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
        ```

        You can do the same thing with square brackets to decrease the importance of a part:
        ```
        A [Vietnamese] woman, (red clothes:1.5), (walking), smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
        ```

        To easily read your negative prompt, organize it the same way as your prompt (not important for the AI):
        ```
        man, boy, hat, running, tree, bicycle, forest, drawing, painting, cartoon, 3d, monochrome, blurry, noisy, bokeh
        ```
        """
    )

    # Load saved preferences when the page loads
    interface.load(
        fn=load_preferences, inputs = [
        local_storage
    ], outputs = [
       prompt,
       negative_prompt,
       num_inference_steps,
       guidance_scale,
       image_guidance_scale,
       strength,
       denoising_steps,
       randomize_seed,
       seed,
       debug_mode
    ]
    )

    interface.queue().launch(mcp_server=True)