File size: 4,031 Bytes
82eb08c
9fe04a3
82eb08c
 
 
 
 
9fe04a3
82eb08c
 
9fe04a3
82eb08c
 
9fe04a3
82eb08c
 
d4e868f
f20165d
 
 
9fe04a3
82eb08c
 
f20165d
82eb08c
 
9fe04a3
82eb08c
 
 
 
f20165d
82eb08c
 
9fe04a3
82eb08c
 
 
9fe04a3
 
 
82eb08c
9fe04a3
 
 
 
82eb08c
 
9fe04a3
82eb08c
 
 
 
 
9fe04a3
82eb08c
 
 
 
 
 
 
 
 
 
9fe04a3
 
 
82eb08c
 
 
 
 
9fe04a3
 
 
 
 
 
 
 
 
 
 
d4e868f
9fe04a3
82eb08c
 
 
 
 
 
 
 
9fe04a3
 
 
 
 
 
 
 
 
 
 
82eb08c
9fe04a3
 
82eb08c
9fe04a3
 
82eb08c
9fe04a3
 
82eb08c
9fe04a3
82eb08c
 
9fe04a3
 
 
 
82eb08c
9fe04a3
 
 
82eb08c
9fe04a3
 
 
 
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
import torch
import gradio as gr
from diffusers import (
    StableDiffusionControlNetPipeline,
    ControlNetModel,
    EulerAncestralDiscreteScheduler,
)
import random
from types import SimpleNamespace
from qr_creator import generate_qr_art

cache_dir = '.cache'
model = 'stable-diffusion-v1-5/stable-diffusion-v1-5'

# @spaces.GPU #[uncomment to use ZeroGPU]
device = 'cuda' if torch.cuda.is_available() else 'cpu'
if device == 'cuda':
    torch_dtype=torch.float16
else:
    torch_dtype='auto'

controlnet = ControlNetModel.from_pretrained(
    "monster-labs/control_v1p_sd15_qrcode_monster",
    torch_dtype=torch_dtype,
    cache_dir=cache_dir,
)

pipe = StableDiffusionControlNetPipeline.from_pretrained(
    model,
    controlnet=controlnet,
    safety_checker=None,
    torch_dtype=torch_dtype,
    cache_dir=cache_dir,
).to(device)

pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(
    pipe.scheduler.config)
# pipe.enable_xformers_memory_efficient_attention() # Commented out to fix ModuleNotFoundError


def infer(
    content,
    prompt,
    negative_prompt,
    progress=gr.Progress(track_tqdm=True),
):
    args = SimpleNamespace(
        content=content,
        prompt=prompt,
        negative=negative_prompt,
        scale=round(random.uniform(5, 9), 1),
        control=round(random.uniform(0.9, 1.2), 1),
        seed=random.randint(1, 12345),
    )

    img = generate_qr_art(
        pipe,
        args.content,
        args.prompt,
        args.negative,
        args.scale,
        args.control,
        args.seed,
    )
    return img


examples = [
    [
        'https://sabitech.co.jp/',
        'A peaceful Japanese countryside in anime style, with traditional wooden houses, cherry blossom trees, stone paths, and soft sunlight. The QR code is seamlessly formed by natural elements of the scene: intertwined sakura branches, wooden planks, rice field patterns, and gently curved roof tiles. The QR pattern remains fully intact and scannable, but blends harmoniously into the environment as if it naturally belongs there. Warm color palette, soft anime shading, clean linework, gentle breeze moving the leaves, serene rural atmosphere. High resolution, detailed textures, balanced contrast, smooth integration between QR structure and Japanese countryside elements.',
        'Unscannable QR code, distorted QR pattern, broken squares, missing modules, chaotic branches, overly abstract shapes, blurry, low contrast, messy composition, unnatural geometry, disconnected elements, glitch artifacts, low resolution.',
    ],
]

css = """
#col-container {
    margin: 0 auto;
    max-width: 640px;
}
"""

with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown(" # Create QR Code fingerprints")

        with gr.Row():
            content = gr.Text(
                label="Content",
                show_label=False,
                max_lines=1,
                placeholder="Enter your content",
                container=False,
            )
        with gr.Row():
            prompt = gr.Text(
                label="Prompt",
                show_label=False,
                max_lines=1,
                placeholder="Enter your prompt",
                container=False,
            )

            run_button = gr.Button("Run", scale=0, variant="primary")

        with gr.Row():
            negative_prompt = gr.Text(
                label="Negative prompt",
                show_label=False,
                max_lines=1,
                placeholder="Enter a negative prompt",
                container=False,
            )

        result = gr.Image(label="Result", show_label=False)

        gr.Examples(examples=examples, inputs=[
                    content, prompt, negative_prompt])
    gr.on(
        triggers=[run_button.click, prompt.submit],
        fn=infer,
        inputs=[
            content,
            prompt,
            negative_prompt,
        ],
        outputs=[result],
    )

if __name__ == "__main__":
    demo.launch()