File size: 31,197 Bytes
3aa0ca8
 
 
eebec00
 
a2e978b
30f09bf
 
f3df6f1
30f09bf
4098b88
bb72424
ebd6751
 
19745bd
4f21463
cc8a350
eebec00
3aa0ca8
f734c44
3aa0ca8
50b52e7
3aa0ca8
 
f734c44
bb8619e
 
 
3aa0ca8
eebec00
6ca2d36
 
eebec00
 
01e0de2
eebec00
01e0de2
eebec00
 
3aa0ca8
6ca2d36
 
bd52be8
5adca2d
6ca2d36
 
 
 
fb53f82
b5ec975
6ca2d36
 
 
cc8a350
 
 
 
 
 
 
d8ecf0f
 
 
 
 
 
 
cc8a350
4f21463
6ca2d36
bd52be8
6ca2d36
05785e6
cc8a350
4f21463
cc8a350
6ca2d36
 
bd52be8
cc8a350
 
 
 
 
 
 
 
 
 
 
 
 
6ca2d36
cc8a350
 
 
 
 
 
 
 
 
 
3aa0ca8
 
 
 
 
 
 
 
 
 
 
 
 
bb8619e
 
 
 
 
 
 
 
 
b2c7457
bb72424
 
 
 
 
ebd6751
bb72424
ebd6751
 
bb72424
ebd6751
bb72424
 
 
ebd6751
b2c7457
 
 
ebd6751
 
 
 
 
 
b2c7457
 
 
ebd6751
b2c7457
ebd6751
 
 
 
b2c7457
 
 
89a84dc
b2c7457
4dba69f
19745bd
8845f6c
 
706a298
 
 
 
 
 
 
 
 
 
 
 
 
 
6ec9be2
5e66c32
bd52be8
5e66c32
 
 
 
 
4098b88
3aa0ca8
eebec00
3aa0ca8
 
bd52be8
 
 
3aa0ca8
802b807
4098b88
bb8619e
bd52be8
19745bd
 
 
155f3b3
6ca2d36
5ec4186
bd52be8
3aa0ca8
9302ea6
155f3b3
6ca2d36
4f21463
6ca2d36
4f21463
6ca2d36
9302ea6
 
 
4098b88
 
9302ea6
5e66c32
 
 
 
 
 
 
9302ea6
 
6ca2d36
1b8ab0f
6ca2d36
9302ea6
4098b88
 
 
 
 
 
 
 
 
 
 
 
1b8ab0f
4098b88
 
1b8ab0f
9302ea6
6ec9be2
 
 
 
 
 
 
 
 
 
 
bd52be8
 
 
 
 
 
9302ea6
6ca2d36
 
 
 
9863449
5ae6892
 
 
 
 
 
 
 
 
 
 
bd52be8
5ae6892
 
9863449
19745bd
 
 
6ca2d36
1b8ab0f
 
 
9302ea6
1b8ab0f
cc8a350
f734c44
bd52be8
 
9863449
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ca2d36
 
 
 
 
 
 
eebec00
90f8b0b
 
 
 
 
 
 
 
3aa0ca8
e5bd30a
5cca4db
 
4098b88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cca4db
3aa0ca8
4098b88
 
 
 
 
 
38a505c
4098b88
 
 
 
f5a9e7d
5e66c32
dbee31f
4098b88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee6a00
4098b88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b54767
24bbced
4098b88
 
 
 
 
8b84cd9
 
 
 
 
6a60d16
8b84cd9
24bbced
 
 
4098b88
24bbced
4098b88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00f7961
4098b88
 
 
 
00f7961
4098b88
 
 
00f7961
4098b88
 
 
 
8e871a8
4098b88
 
 
 
3c35602
 
 
4098b88
 
 
 
 
 
 
3c35602
4098b88
3c35602
4098b88
3c35602
4098b88
8e871a8
4098b88
 
 
 
3c35602
4098b88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e871a8
4098b88
 
 
 
 
 
 
 
 
00f7961
4098b88
 
 
 
00f7961
 
4098b88
 
 
 
 
 
5a1151f
4098b88
 
 
 
 
00f7961
4098b88
 
 
 
 
 
 
bb72424
bd52be8
 
 
 
 
 
 
 
 
 
 
bb72424
 
 
 
b2c7457
 
 
 
 
bb72424
8845f6c
 
 
 
 
bb72424
 
 
 
 
 
8845f6c
 
 
 
 
 
6ca2d36
 
 
 
 
 
cc8a350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd52be8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3aa0ca8
 
 
eebec00
3aa0ca8
 
 
 
 
 
802b807
4098b88
bb8619e
 
19745bd
 
 
6ca2d36
 
bd52be8
3aa0ca8
6ca2d36
c69d322
3aa0ca8
 
cc8a350
 
 
5adca2d
bd52be8
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
import torch
import gradio as gr
from PIL import Image
import qrcode
from pathlib import Path
from multiprocessing import cpu_count
import requests
import io
import os
from PIL import Image
import spaces
import numpy as np
import cv2
from pyzxing import BarCodeReader
from PIL import ImageOps
from huggingface_hub import hf_hub_download, snapshot_download
from PIL import ImageEnhance

from diffusers import (
    StableDiffusionPipeline,
    StableDiffusionControlNetImg2ImgPipeline,
    StableDiffusionControlNetPipeline,
    ControlNetModel,
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    DEISMultistepScheduler,
    HeunDiscreteScheduler,
    EulerDiscreteScheduler,
)



qrcode_generator = qrcode.QRCode(
    version=1,
    error_correction=qrcode.ERROR_CORRECT_H,
    box_size=10,
    border=4,
)


# Define available models
CONTROLNET_MODELS = {
    "QR Code Monster": "monster-labs/control_v1p_sd15_qrcode_monster/v2/",
    "QR Code": "DionTimmer/controlnet_qrcode-control_v1p_sd15",
    # Add more ControlNet models here
}

DIFFUSION_MODELS = {
    "GhostMix": "digiplay/GhostMixV1.2VAE",
    "Stable v1.5": "Jiali/stable-diffusion-1.5",
    # Add more diffusion models here
}

# Global variables to store loaded models
loaded_controlnet = None
loaded_pipe = None

def load_models_on_launch():
    global loaded_controlnet, loaded_pipe
    print("Loading models on launch...")
    
    # Download the main repository
    main_repo_path = snapshot_download("monster-labs/control_v1p_sd15_qrcode_monster")
    
    # Construct the path to the subfolder
    controlnet_path = os.path.join(main_repo_path, "v2")
    
    loaded_controlnet = ControlNetModel.from_pretrained(
        controlnet_path,
        torch_dtype=torch.float16
    ).to("mps")

    diffusion_path = snapshot_download(DIFFUSION_MODELS["GhostMix"])
    loaded_pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
        diffusion_path,
        controlnet=loaded_controlnet,
        torch_dtype=torch.float16,
        safety_checker=None,
    ).to("mps")
    print("Models loaded successfully!")

# Modify the load_models function to use global variables
def load_models(controlnet_model, diffusion_model):
    global loaded_controlnet, loaded_pipe
    if loaded_controlnet is None or loaded_pipe is None:
        load_models_on_launch()
    return loaded_pipe

# Add new functions for image adjustments
def adjust_image(image, brightness, contrast, saturation):
    if image is None:
        return None
    
    img = Image.fromarray(image) if isinstance(image, np.ndarray) else image
    
    if brightness != 1:
        img = ImageEnhance.Brightness(img).enhance(brightness)
    if contrast != 1:
        img = ImageEnhance.Contrast(img).enhance(contrast)
    if saturation != 1:
        img = ImageEnhance.Color(img).enhance(saturation)
    
    return np.array(img)

def resize_for_condition_image(input_image: Image.Image, resolution: int):
    input_image = input_image.convert("RGB")
    W, H = input_image.size
    k = float(resolution) / min(H, W)
    H *= k
    W *= k
    H = int(round(H / 64.0)) * 64
    W = int(round(W / 64.0)) * 64
    img = input_image.resize((W, H), resample=Image.LANCZOS)
    return img


SAMPLER_MAP = {
    "DPM++ Karras SDE": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True, algorithm_type="sde-dpmsolver++"),
    "DPM++ Karras": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True),
    "Heun": lambda config: HeunDiscreteScheduler.from_config(config),
    "Euler": lambda config: EulerDiscreteScheduler.from_config(config),
    "DDIM": lambda config: DDIMScheduler.from_config(config),
    "DEIS": lambda config: DEISMultistepScheduler.from_config(config),
}

def scan_qr_code(image):
    # Convert gradio image to PIL Image if necessary
    if isinstance(image, np.ndarray):
        image = Image.fromarray(image)
    
    # Convert to grayscale
    gray_image = image.convert('L')
    
    # Convert to numpy array
    np_image = np.array(gray_image)
    
    # Method 1: Using qrcode library
    try:
        qr = qrcode.QRCode()
        qr.add_data('')
        qr.decode(gray_image)
        return qr.data.decode('utf-8')
    except Exception:
        pass
    
    # Method 2: Using OpenCV
    try:
        qr_detector = cv2.QRCodeDetector()
        retval, decoded_info, points, straight_qrcode = qr_detector.detectAndDecodeMulti(np_image)
        if retval:
            return decoded_info[0]
    except Exception:
        pass
    
    # Method 3: Fallback to zxing-cpp
    try:
        reader = BarCodeReader()
        results = reader.decode(np_image)
        if results:
            return results[0].parsed
    except Exception:
        pass
    
    return None

def invert_image(image):
    if image is None:
        return None
    if isinstance(image, np.ndarray):
        return 255 - image
    elif isinstance(image, Image.Image):
        return ImageOps.invert(image.convert('RGB'))
    else:
        raise ValueError("Unsupported image type")

def invert_displayed_image(image):
    if image is None:
        return None
    inverted = invert_image(image)
    if isinstance(inverted, np.ndarray):
        return Image.fromarray(inverted)
    return inverted


MAX_TOKENS = 78

def count_tokens(text):
    """Count the number of tokens in the text."""
    return len(text.split())

@spaces.GPU()
def inference(
    qr_code_content: str,
    prompt: str,
    negative_prompt: str,
    guidance_scale: float = 15.0,  
    controlnet_conditioning_scale: float = 1.5,  
    strength: float = 0.6,  
    seed: int = -1,
    init_image: Image.Image | None = None,
    qrcode_image: Image.Image | None = None,
    use_qr_code_as_init_image = True,
    sampler = "DPM++ Karras SDE",  
    bg_color: str = "white",
    qr_color: str = "black",
    invert_final_image: bool = False,
    invert_init_image: bool = False,
    controlnet_model: str = "QR Code Monster",
    diffusion_model: str = "GhostMix",
    reference_image_strength: float = 0.6,
):
    try:
        progress = gr.Progress()
        # Load models based on user selection
        progress(0, desc="Downloading models...")
        pipe = load_models(controlnet_model, diffusion_model)
        progress(0.5, desc="Models downloaded, preparing for inference...")
        
        if prompt is None or prompt == "":
            raise gr.Error("Prompt is required")

        if qrcode_image is None and qr_code_content == "":
            raise gr.Error("QR Code Image or QR Code Content is required")

        # Validate the length of the prompt
        if count_tokens(prompt) > MAX_TOKENS:
            raise gr.Error(f"Prompt exceeds the maximum allowed tokens of {MAX_TOKENS}")

        if count_tokens(negative_prompt) > MAX_TOKENS:
            raise gr.Error(f"Negative prompt exceeds the maximum allowed tokens of {MAX_TOKENS}")

        pipe.scheduler = SAMPLER_MAP[sampler](pipe.scheduler.config)

        if seed == -1:
            seed = torch.seed()  # Generate a truly random seed
        generator = torch.manual_seed(seed)

        if qr_code_content != "" or qrcode_image.size == (1, 1):
            print("Generating QR Code from content")
            qr = qrcode.QRCode(
                version=1,
                error_correction=qrcode.constants.ERROR_CORRECT_H,
                box_size=10,
                border=4,
            )
            qr.add_data(qr_code_content)
            qr.make(fit=True)

            qrcode_image = qr.make_image(fill_color=qr_color, back_color=bg_color)
            qrcode_image = resize_for_condition_image(qrcode_image, 1024)
        else:
            print("Using QR Code Image")
            qrcode_image = resize_for_condition_image(qrcode_image, 1024)

        # Determine which image to use as init_image and control_image
        if use_qr_code_as_init_image:
            init_image = qrcode_image
            control_image = qrcode_image
        else:
            control_image = qrcode_image
            if init_image is None:
                # If no init_image provided, set strength to 1.0 to generate a new image
                strength = 1.0

        # Adjust strength if using an init_image
        if init_image is not None and not use_qr_code_as_init_image:
            # Map the 0-5 range to 0-1 range for the strength parameter
            mapped_strength = min(reference_image_strength / 5.0, 1.0)
            strength = 1.0 - mapped_strength  # Invert the strength for img2img
        elif use_qr_code_as_init_image:
            strength = min(strength, 0.6)  # Cap strength at 0.6 when using QR code as init_image

        # Invert init_image if requested
        if invert_init_image and init_image is not None:
            init_image = invert_image(init_image)

        final_image = None
        out = pipe(
            prompt=prompt,  # Use the full prompt
            negative_prompt=negative_prompt,  # Use the full negative prompt
            image=init_image,
            control_image=control_image,
            width=1024,
            height=1024,
            guidance_scale=float(guidance_scale),
            controlnet_conditioning_scale=float(controlnet_conditioning_scale),
            generator=generator,
            strength=float(strength),
            num_inference_steps=50,
        )
        final_image = out.images[0] if final_image is None else final_image

        if invert_final_image:
            final_image = invert_image(final_image)
        
        return final_image, seed
    except gr.Error as e:
        print(f"Gradio error in inference: {str(e)}")
        return Image.new('RGB', (1024, 1024), color='white'), -1
    except Exception as e:
        print(f"Unexpected error in inference: {str(e)}")
        return Image.new('RGB', (1024, 1024), color='white'), -1



def split_prompt(prompt, max_length=77):
    """Split the prompt into chunks that do not exceed the max_length."""
    words = prompt.split()
    chunks = []
    current_chunk = []
    current_length = 0

    for word in words:
        if current_length + len(word) + 1 > max_length:
            chunks.append(" ".join(current_chunk))
            current_chunk = [word]
            current_length = len(word) + 1
        else:
            current_chunk.append(word)
            current_length += len(word) + 1

    if current_chunk:
        chunks.append(" ".join(current_chunk))

    return chunks

def invert_init_image_display(image):
    if image is None:
        return None
    inverted = invert_image(image)
    if isinstance(inverted, np.ndarray):
        return Image.fromarray(inverted)
    return inverted

css = """
h1 {
    text-align: center;
    display:block;
}
"""

with gr.Blocks(theme='Hev832/Applio', css=css) as blocks:
    gr.Markdown(
        """
        ![Yamamoto Logo](/static-proxy?url=https%3A%2F%2Fcdn-uploads.huggingface.co%2Fproduction%2Fuploads%2F64740cf7485a7c8e1bd51ac9%2F_VyYxp5qE_nRZ_LJqBxmL.webp)%3C%2Fspan%3E
        # 🎨 Yamamoto QR Code Art Generator
        ## Transform Your QR Codes into Brand Masterpieces
        Welcome to Yamamoto's innovative QR Code Art Generator! This cutting-edge tool empowers our creative team to craft 
        visually stunning, on-brand QR codes that perfectly blend functionality with artistic expression.
        ## 🚀 How It Works:
        1. **Enter Your QR Code Content**: Start by inputting the URL or text for your QR code.
        2. **Craft Your Prompt**: Describe the artistic style or theme you envision for your QR code.
        3. **Fine-tune with Advanced Settings**: Adjust parameters to perfect your creation (see tips below).
        4. **Generate and Iterate**: Click 'Run' to create your art, then refine as needed.
        ## 🌟 Tips for Spectacular Results:
        - **Artistic Freedom**: Set between 0.8 and 0.95 for a balance of creativity and scannability.
        - **QR Code Visibility**: Aim for 0.6 to 2.0 to ensure your code is both artistic and functional.
        - **Prompt Crafting**: Use vivid, specific descriptions that align with your brand identity.
        - **Experimentation**: Don't hesitate to try different settings and prompts to find your perfect style!
        ## 🎭 Prompt Ideas to Spark Your Creativity:
        - "A serene Japanese garden with cherry blossoms and a koi pond"
        - "A futuristic cityscape with neon lights and flying cars"
        - "An abstract painting with swirling colors and geometric shapes"
        - "A vintage-style travel poster featuring iconic landmarks"
        Remember, the magic lies in the details of your prompt and the fine-tuning of your settings. 
        Happy creating!
        """
    )
    with gr.Row():
        with gr.Column():
            qr_code_content = gr.Textbox(
                label="QR Code Content",
                placeholder="Enter URL or text for your QR code",
                info="This is what your QR code will link to or display when scanned.",
                value="https://theunderground.digital/",
            )

            prompt = gr.Textbox(
                label="Artistic Prompt",
                placeholder="Describe the style or theme for your QR code art (For best results, keep the prompt to 75 characters or less as seen below)",
                value="A high-res, photo-realistic minimalist rendering of Mount Fuji as a sharp, semi-realistic silhouette on the horizon. The mountain conveys strength and motion with clean, crisp lines and natural flow. Features detailed snow textures, subtle ridge highlights, and a powerful yet serene atmosphere. Emphasizes strength with clarity and precision in texture and light. (Sharp outlines:1.5), (Photo-realistic:1.4), (Detailed textures:1.3), (Minimalist:1.3), (Semi-realistic:1.3), (Monochrome contrast:1.2), (Crisp detail:1.2), (Evoking strength:1.2). Inspired by traditional Japanese woodblock prints, nature photography, and minimalist design principles.",
                info="Describe the style or theme for your QR code art (For best results, keep the prompt to 75 characters or less as seen in the example)",
            )
            negative_prompt = gr.Textbox(
                label="Elements to Avoid",
                placeholder="Describe what you don't want in the image",
                value="ugly, disfigured, low quality, blurry, nsfw, bad_pictures, (bad_prompt_version2:0.8), EasyNegative, 3d, cartoon, anime, sketches, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), poorly drawn, distorted, overexposed, flat shading, bad proportions, deformed, pixelated, messy details, lack of contrast, unrealistic textures, bad anatomy, rough edges, low resolution, text artifacts.",
                info="List elements or styles you want to avoid in your QR code art.",
            )

            run_btn = gr.Button("🎨 Create Your QR Art", variant="primary")  
            
            with gr.Accordion(label="Use Your Own Image as a Reference", open=True, visible=True) as init_image_acc:
                init_image = gr.Image(label="Reference Image", type="pil")
                with gr.Row():
                    use_qr_code_as_init_image = gr.Checkbox(
                        label="Uncheck to use your own image for generation", 
                        value=True, 
                        interactive=True,
                        info="Allows you to use your own image for generation, otherwise a generic QR Code is created automatically as the base image"
                    )
                    invert_init_image_button = gr.Button("Invert Init Image")
            
            with gr.Accordion(label="QR Code Image (Optional)", open=False, visible=False):
                qr_code_image = gr.Image(
                    label="QR Code Image (Optional). Leave blank to automatically generate QR code",
                    type="pil",
                )

                
            with gr.Accordion("Set Custom QR Code Colors", open=False):
                bg_color = gr.ColorPicker(
                    label="Background Color", 
                    value="#FFFFFF",
                    info="Choose the background color for the QR code"
                )
                qr_color = gr.ColorPicker(
                    label="QR Code Color", 
                    value="#000000",
                    info="Choose the color for the QR code pattern"
                )
                invert_final_image = gr.Checkbox(
                    label="Invert Final Image", 
                    value=False,
                    info="Check this to invert the colors of the final image",
                    visible=False,
                )
            with gr.Accordion("AI Model Selection", open=False):
                controlnet_model_dropdown = gr.Dropdown(
                    choices=list(CONTROLNET_MODELS.keys()),
                    value="QR Code Monster",
                    label="ControlNet Model",
                    info="Select the ControlNet model for QR code generation"
                )
                diffusion_model_dropdown = gr.Dropdown(
                    choices=list(DIFFUSION_MODELS.keys()),
                    value="GhostMix",
                    label="Diffusion Model",
                    info="Select the main diffusion model for image generation"
                )
                
        with gr.Column():
            result_image = gr.Image(label="Your Artistic QR Code", show_download_button=True, show_fullscreen_button=True)

            
            with gr.Row(visible=False):
                scan_button = gr.Button("Verify QR Code Works")
                scan_result = gr.Textbox(label="Validation Result of QR Code", interactive=False)

            
            with gr.Row():
                brightness = gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Brightness")
                contrast = gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Contrast")
                saturation = gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Saturation")
            with gr.Row():
                invert_button = gr.Button("Invert Image")
                
            used_seed = gr.Number(label="Seed Used", interactive=False)
            
            gr.Markdown(
            
                """
                ### 🔍 Analyzing Your Creation
                - Is the QR code scannable? Check with your phone camera to see if it can scan it.
                - If not scannable, use the Brightness, Contrast, and Saturation sliders to optimize the QR code for scanning.
                - Does the art style match your prompt? If not, try adjusting the 'Prompt Adherence'.
                - Want more artistic flair? Increase the 'Artistic Freedom'.
                - Need a clearer QR code? Raise the 'QR Code Visibility'.
                """
            )

            with gr.Accordion("Advanced Art Controls", open=True):
                with gr.Row():
                    controlnet_conditioning_scale = gr.Slider(
                        minimum=0.0,
                        maximum=5.0,
                        step=0.01,
                        value=2,
                        label="QR Code Visibility in Image",
                    )
                    with gr.Accordion("How Much QR Code Visibility is in Final Image (Click For Explanation)", open=False):
                        gr.Markdown(
                            """
                            **QR Code Visibility** determines how much the QR code itself stands out in the final design. Think of it like balancing between how "artistic" the image looks and how "functional" the QR code is.
                    
                            - **Low settings (0.0-1)**: If you choose a lower value, the QR code will blend more into the art, and it might be hard to scan with a phone. This setting is great if you want the image to look amazing, but you might lose some of the scannability. Try this if you care more about art and less about the QR code being easily recognized.
                            
                            - **Medium settings (1-3)**: This is the sweet spot where the QR code remains clearly visible while still blending in with the art. You can still scan it easily with a phone, but it looks more creative. For most users, setting it around **1.1** is a great start to balance both art and function.
                    
                            - **High settings (3-5.0)**: If you need to make sure that the QR code is super easy to scan, even if it means the image looks less like art and more like a regular QR code, then choose a higher value. This is ideal when functionality is the main goal, and the artistic side can take a backseat.
                            
                            Start with **1.3** if you're unsure, and adjust up or down depending on whether you want the QR code to be more artistic or more functional.
                            """
                        )
                
                with gr.Row():
                    strength = gr.Slider(
                        minimum=0.0, 
                        maximum=5, 
                        step=0.10, 
                        value=2, 
                        label="Artistic Freedom for the AI When Generating",
                    )
                    with gr.Accordion("How Much Artistic Freedom the AI has When Generating Image (Click For Explanation)", open=False):
                        gr.Markdown(
                            """
                            **Artistic Freedom** controls how much the AI is allowed to change the QR code's look to match your description. It's like telling the AI how creative it can get with your QR code:
                    
                            - **Low settings (0.10-2)**: If you set this low, the AI will make small changes and your QR code will look more like a regular, plain QR code. This is useful if you want something that is still creative but not too wild, keeping it simple and easy to scan.
                    
                            - **Medium settings (2-3)**: Here, the AI will add more artistic touches but keep the QR code recognizable. You get the best of both worlds—your QR code will have some creative flair, but it will still be easy to scan. For most cases, setting it to **0.6** is a great way to keep the code functional and artistic.
                    
                            - **High settings (3-5)**: If you set this high, the AI will go all-out creative. The QR code will look amazing, but it might be difficult to scan because the art can start to take over the code. This setting is perfect if you're aiming for a highly creative piece of art and don't mind if it's a bit harder to scan. Start at **0.9** to explore creative but functional designs.
                            """
                        )
                
                with gr.Row():
                    guidance_scale = gr.Slider(
                        minimum=0.0,
                        maximum=100.0,
                        step=0.25,
                        value=7.5,
                        label="How Closely the AI Follows the Prompt",
 
                    )
                    with gr.Accordion("How Closely the AI Follows the Prompt (Click For Explanation)", open=False):
                        gr.Markdown(
                            """
                            **Follow the Prompt** tells the AI how closely it should follow your description when creating the QR code art. Think of it like giving the AI instructions on how strict or flexible it can be with your design ideas:
                    
                            - **Low settings (0-5)**: If you choose a lower value, the AI has more freedom to get creative on its own and may not stick too closely to your description. This is great if you want to see how the AI interprets your ideas in unexpected ways.
                    
                            - **Medium settings (5-15)**: This is a good balance where the AI will mostly follow your prompt but will also add some of its own creative touches. If you want to see some surprises but still want the design to look like what you described, start at around **7.5**.
                    
                            - **High settings (15+)**: If you choose a higher value, the AI will stick very closely to what you wrote in the description. This is good if you have a very specific idea and don't want the AI to change much. Just keep in mind that this might limit the AI's creativity.
                            
                            Start at **7.5** for a balanced approach where the AI follows your ideas but still adds some artistic flair.
                            """
                    )
                
                with gr.Row():
                    sampler = gr.Dropdown(
                        choices=list(SAMPLER_MAP.keys()), 
                        value="DPM++ Karras SDE", 
                        label="Art Style Used to Create the Image",

                    )
                    with gr.Accordion("Details on Art Style the AI Uses to Create the Image (Click For Explanation)", open=False):
                        gr.Markdown(
                            """
                            **Art Style** changes how the AI creates the image, using different methods (or "samplers"). Each method has a different effect on how detailed or artistic the final QR code looks:
                    
                            - **DPM++ Karras SDE**: This is a great all-around option for creating high-quality, detailed images. It's a good place to start if you want a balance of sharpness and creativity.
                    
                            - **Euler**: This method creates very sharp, detailed images, making the QR code look crisp and clear. Choose this if you want a precise, well-defined design.
                    
                            - **DDIM**: This method is better if you want the QR code to have a more artistic, abstract style. It's great for when you want the QR code to look like a piece of modern art.
                            
                            Feel free to experiment with different samplers to see what works best for the look you're going for!
                            """
                        )
                
                with gr.Row():
                    seed = gr.Slider(
                        minimum=-1,
                        maximum=9999999999,
                        step=1,
                        value=-1,
                        label="Creative Seed for the Image Generation",                 
                    )
                    with gr.Accordion("How Creative Seed Works for Generating New and Unique Images (Click For Explanation)", open=False):
                        gr.Markdown(
                            """
                            **Creative Seed** controls whether the AI creates a completely new design each time or sticks to a specific design. Think of it like a recipe: with the same seed number, you get the same "recipe" for your QR code every time.
                    
                            - **-1**: This setting makes the AI create something completely new every time you run it. Use this if you want to explore different design ideas with each attempt.
                    
                            - **Any other number**: If you set a specific number, the AI will always create the same image based on that number. This is useful if you find a design you like and want to recreate it exactly.
                    
                            Try **-1** if you want to explore and generate different designs. If you find something you really love, write down the seed number and use it again to recreate the same design.
                            """
                        )

                with gr.Row():
                    reference_image_strength = gr.Slider(
                        minimum=0.0,
                        maximum=5.0,
                        step=0.05,
                        value=0.6,
                        label="Reference Image Influence",
                        info="Controls how much the reference image influences the final result (0 = ignore, 5 = copy exactly)",
                        visible=False  # We'll make this visible when a reference image is uploaded
                    )

    def scan_and_display(image):
        if image is None:
            return "No image to scan"
        
        scanned_text = scan_qr_code(image)
        if scanned_text:
            return f"Scanned successfully: {scanned_text}"
        else:
            return "Failed to scan QR code. Try adjusting the settings for better visibility."

    def invert_displayed_image(image):
        if image is None:
            return None
        return invert_image(image)

    scan_button.click(
        scan_and_display,
        inputs=[result_image],
        outputs=[scan_result]
    )

    invert_button.click(
        invert_displayed_image,
        inputs=[result_image],
        outputs=[result_image]
    )

    invert_init_image_button.click(
        invert_init_image_display,
        inputs=[init_image],
        outputs=[init_image]
    )

    brightness.change(
        adjust_image,
        inputs=[result_image, brightness, contrast, saturation],
        outputs=[result_image]
    )
    contrast.change(
        adjust_image,
        inputs=[result_image, brightness, contrast, saturation],
        outputs=[result_image]
    )
    saturation.change(
        adjust_image,
        inputs=[result_image, brightness, contrast, saturation],
        outputs=[result_image]
    )

    # Add logic to show/hide the reference_image_strength slider
    def update_reference_image_strength_visibility(init_image, use_qr_code_as_init_image):
        return gr.update(visible=init_image is not None and not use_qr_code_as_init_image)

    init_image.change(
        update_reference_image_strength_visibility,
        inputs=[init_image, use_qr_code_as_init_image],
        outputs=[reference_image_strength]
    )

    use_qr_code_as_init_image.change(
        update_reference_image_strength_visibility,
        inputs=[init_image, use_qr_code_as_init_image],
        outputs=[reference_image_strength]
    )

    run_btn.click(
        inference,
        inputs=[
            qr_code_content,
            prompt,
            negative_prompt,
            guidance_scale,
            controlnet_conditioning_scale,
            strength,
            seed,
            init_image,
            qr_code_image,
            use_qr_code_as_init_image,
            sampler,
            bg_color,
            qr_color,
            invert_final_image,
            controlnet_model_dropdown,
            diffusion_model_dropdown,
            reference_image_strength,
        ],
        outputs=[result_image, used_seed],
        concurrency_limit=20
    )

# Load models on launch
load_models_on_launch()

blocks.queue(max_size=20)
blocks.launch(share=True, show_api=True)