File size: 10,701 Bytes
e26724d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0dff2d
e26724d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63f34b6
e26724d
 
a0dff2d
 
e26724d
 
 
a0dff2d
e26724d
 
 
 
 
 
 
939851d
e26724d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
939851d
 
e26724d
939851d
e26724d
 
 
939851d
e26724d
a0dff2d
e26724d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0dff2d
e26724d
a0dff2d
e26724d
a0dff2d
e26724d
a0dff2d
 
e26724d
 
 
 
 
 
a0dff2d
 
e26724d
 
 
 
 
 
 
 
 
 
a0dff2d
e26724d
 
 
 
 
 
 
 
 
 
 
 
a0dff2d
 
 
 
e26724d
 
 
 
 
 
 
a0dff2d
e26724d
a0dff2d
 
e26724d
 
 
 
 
 
 
a0dff2d
e26724d
 
 
 
939851d
e26724d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
939851d
e26724d
 
 
 
 
 
 
 
a0dff2d
e26724d
 
 
 
 
 
 
 
939851d
e26724d
 
 
 
 
 
 
 
a0dff2d
e26724d
 
 
 
 
 
 
 
939851d
e26724d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a49bd51
 
a0dff2d
 
939851d
a0dff2d
939851d
a0dff2d
a49bd51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e26724d
a0dff2d
 
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
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

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

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

pipe = StableDiffusionControlNetPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    controlnet=controlnet,
    safety_checker=None,
    torch_dtype=torch.float16,
).to("cuda")
pipe.enable_xformers_memory_efficient_attention()

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 a": lambda config: EulerAncestralDiscreteScheduler.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 create_code(content: str):
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=16,
        border=0,
    )
    qr.add_data(content)
    qr.make(fit=True)
    img = qr.make_image(fill_color="black", back_color="white")

    # find smallest image size multiple of 256 that can fit qr
    offset_min = 8 * 16
    w, h = img.size
    w = (w + 255 + offset_min) // 256 * 256
    h = (h + 255 + offset_min) // 256 * 256
    if w > 1024:
        raise gr.Error("QR code is too large, please use a shorter content")
    bg = Image.new('L', (w, h), 128)

    # align on 16px grid
    coords = ((w - img.size[0]) // 2 // 16 * 16,
              (h - img.size[1]) // 2 // 16 * 16)
    bg.paste(img, coords)
    return bg


def inference(
    qr_code_content: str,
    prompt: str,
    negative_prompt: str,
    guidance_scale: float = 10.0,
    controlnet_conditioning_scale: float = 2.0,
    seed: int = -1,
    sampler="Euler a",
):
    if prompt is None or prompt == "":
        raise gr.Error("Prompt is required")

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

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

    generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()

    print("Generating QR Code from content")
    qrcode_image = create_code(qr_code_content)

    # hack due to gradio examples
    init_image = qrcode_image

    out = pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        image=qrcode_image,
        width=qrcode_image.width,
        height=qrcode_image.height,
        guidance_scale=float(guidance_scale),
        controlnet_conditioning_scale=float(controlnet_conditioning_scale),
        generator=generator,
        num_inference_steps=40,
    )
    return out.images[0]


css = """
#result_image {
    display: flex;
    place-content: center;
    align-items: center;
}
#result_image > img {
    height: auto;
    max-width: 100%;
    width: revert;
}
"""

with gr.Blocks(css=css) as blocks:
    gr.Markdown(
        """
# QR Code Monster v1.0
## QR Code AI Art Generator

Model used: https://huggingface.co./monster-labs/control_v1p_sd15_qrcode_monster

Try our more powerful v2 here: https://qrcodemonster.art!

<a href="https://huggingface.co./spaces/monster-labs/Controlnet-QRCode-Monster-V1?duplicate=true" style="display: inline-block;margin-top: .5em;margin-right: .25em;" target="_blank">
<img style="margin-bottom: 0em;display: inline;margin-top: -.25em;" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a> for no queue on your own hardware.</p>
"""
    )

    with gr.Row():
        with gr.Column():
            qr_code_content = gr.Textbox(
                label="QR Code Content or URL",
                info="The text you want to encode into the QR code",
                value="",
            )

            prompt = gr.Textbox(
                label="Prompt",
                info="Prompt that guides the generation towards",
            )
            negative_prompt = gr.Textbox(
                label="Negative Prompt",
                value="ugly, disfigured, low quality, blurry, nsfw",
                info="Prompt that guides the generation away from",
            )

            with gr.Accordion(
                label="Params: The generated QR Code functionality is largely influenced by the parameters detailed below",
                open=True,
            ):
                controlnet_conditioning_scale = gr.Slider(
                    minimum=0.5,
                    maximum=2.5,
                    step=0.01,
                    value=1.5,
                    label="Controlnet Conditioning Scale",
                    info="""Controls the readability/creativity of the QR code.
                    High values: The generated QR code will be more readable.
                    Low values: The generated QR code will be more creative.
                    """
                )
                guidance_scale = gr.Slider(
                    minimum=0.0,
                    maximum=25.0,
                    step=0.25,
                    value=7,
                    label="Guidance Scale",
                    info="Controls the amount of guidance the text prompt guides the image generation"
                )
                sampler = gr.Dropdown(choices=list(
                    SAMPLER_MAP.keys()), value="Euler a", label="Sampler")
                seed = gr.Number(
                    minimum=-1,
                    maximum=9999999999,
                    step=1,
                    value=2313123,
                    label="Seed",
                    randomize=True,
                    info="Seed for the random number generator. Set to -1 for a random seed"
                )
            with gr.Row():
                run_btn = gr.Button("Run")
        with gr.Column():
            result_image = gr.Image(label="Result Image", elem_id="result_image")
    run_btn.click(
        inference,
        inputs=[
            qr_code_content,
            prompt,
            negative_prompt,
            guidance_scale,
            controlnet_conditioning_scale,
            seed,
            sampler,
        ],
        outputs=[result_image],
    )

    gr.Examples(
        examples=[
            [
                "test",
                "Baroque rococo architecture, architectural photography, post apocalyptic New York, hyperrealism, [roots], hyperrealistic, octane render, cinematic, hyper detailed, 8K",
                "",
                7,
                1.6,
                2592353769,
                "Euler a",
            ],
            [
                "https://qrcodemonster.art",
                "a centered render of an ancient tree covered in bio - organic micro organisms growing in a mystical setting, cinematic, beautifully lit, by tomasz alen kopera and peter mohrbacher and craig mullins, 3d, trending on artstation, octane render, 8k",
                "",
                7,
                1.57,
                259235398,
                "Euler a",
            ],
            [
                "test",
                "3 cups of coffee with coffee beans around",
                "",
                7,
                1.95,
                1889601353,
                "Euler a",
            ],
            [
                "https://huggingface.co.",
                "A top view picture of a sandy beach with a sand castle, beautiful lighting, 8k, highly detailed",
                "sky",
                7,
                1.15,
                46200,
                "Euler a",
            ],
            [
                "test",
                "A top view picture of a sandy beach, organic shapes, beautiful lighting, bumps and shadows, 8k, highly detailed",
                "sky, water, squares",
                7,
                1.25,
                46220,
                "Euler a",
            ],
        ],
        fn=inference,
        inputs=[
            qr_code_content,
            prompt,
            negative_prompt,
            guidance_scale,
            controlnet_conditioning_scale,
            seed,
            sampler,
        ],
        outputs=[result_image],
        cache_examples=True,
    )
    gr.Markdown(
        """
## Notes

* The generated QR codes may not always be easily readable and may require adjusting the parameters.
* The prompt affects the quality of the generated QR code.
* The scan may work better if the phone is held further away from the screen, or if the page is zoomed out.

## Parameters

- **Input Text:** The text you want to encode into the QR code
- **Prompt:** Input a prompt to guide the QR code generation process, allowing you to control the appearance and style of the generated QR codes. Some are easier than others to generate readable QR codes.
- **Controlnet Control Scale:** Raise the control scale value to increase the readability of the QR codes or lower it to make the QR codes more creative and distinctive.

The generated QR codes might not always be easily readable. It might take a few tries with different parameters to find the right balance. This often depends on the prompt, which can be more or less suitable for QR code generation.


## How to Use

1. Input your text: Pass the text you'd like to encode into the QR code as input. Bigger text means bigger codes, which are less likely to give good results (will ressemble qr codes too much).
2. Set your prompt: Choose a prompt to guide the generation process (use all the SD tricks you like: styles, adjectives...).
3. Adjust the Controlnet Control Scale: The higher the control scale, the more readable the QR code will be, while a lower control scale leads to a more creative QR code.
4. Generate multiple codes: Since not all generated codes may be readable, you'll need to create a few codes with the same parameters to determine if any adjustments are needed.
5. Test the generated QR codes: Scan the generated QR codes to make sure they are readable and meet your requirements.
"""
    )

blocks.queue(concurrency_count=1, max_size=20, api_open=False)
blocks.launch(share=bool(os.environ.get("SHARE", False)), show_api=False)