qbration21 commited on
Commit
e1ed3fd
·
1 Parent(s): 9c26cfc

qr code generator

Browse files
Files changed (2) hide show
  1. app.py +301 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,301 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from PIL import Image
4
+ import qrcode
5
+ from pathlib import Path
6
+ from multiprocessing import cpu_count
7
+ import requests
8
+ import io
9
+ import os
10
+ from PIL import Image
11
+ import spaces
12
+
13
+ from diffusers import (
14
+ StableDiffusionPipeline,
15
+ StableDiffusionControlNetImg2ImgPipeline,
16
+ ControlNetModel,
17
+ DDIMScheduler,
18
+ DPMSolverMultistepScheduler,
19
+ DEISMultistepScheduler,
20
+ HeunDiscreteScheduler,
21
+ EulerDiscreteScheduler,
22
+ )
23
+
24
+ qrcode_generator = qrcode.QRCode(
25
+ version=1,
26
+ error_correction=qrcode.ERROR_CORRECT_H,
27
+ box_size=10,
28
+ border=4,
29
+ )
30
+
31
+ controlnet = ControlNetModel.from_pretrained(
32
+ "DionTimmer/controlnet_qrcode-control_v1p_sd15", torch_dtype=torch.float16
33
+ ).to("cuda")
34
+
35
+ pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
36
+ "runwayml/stable-diffusion-v1-5",
37
+ controlnet=controlnet,
38
+ safety_checker=None,
39
+ torch_dtype=torch.float16,
40
+ ).to("cuda")
41
+ # pipe.enable_xformers_memory_efficient_attention()
42
+
43
+
44
+ def resize_for_condition_image(input_image: Image.Image, resolution: int):
45
+ input_image = input_image.convert("RGB")
46
+ W, H = input_image.size
47
+ k = float(resolution) / min(H, W)
48
+ H *= k
49
+ W *= k
50
+ H = int(round(H / 64.0)) * 64
51
+ W = int(round(W / 64.0)) * 64
52
+ img = input_image.resize((W, H), resample=Image.LANCZOS)
53
+ return img
54
+
55
+
56
+ SAMPLER_MAP = {
57
+ "DPM++ Karras SDE": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True, algorithm_type="sde-dpmsolver++"),
58
+ "DPM++ Karras": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True),
59
+ "Heun": lambda config: HeunDiscreteScheduler.from_config(config),
60
+ "Euler": lambda config: EulerDiscreteScheduler.from_config(config),
61
+ "DDIM": lambda config: DDIMScheduler.from_config(config),
62
+ "DEIS": lambda config: DEISMultistepScheduler.from_config(config),
63
+ }
64
+
65
+ @spaces.GPU()
66
+ def inference(
67
+ qr_code_content: str,
68
+ prompt: str,
69
+ negative_prompt: str,
70
+ guidance_scale: float = 10.0,
71
+ controlnet_conditioning_scale: float = 2.0,
72
+ strength: float = 0.8,
73
+ seed: int = -1,
74
+ init_image: Image.Image | None = None,
75
+ custom_image: Image.Image | None = None,
76
+ use_qr_code_as_init_image = True,
77
+ sampler = "DPM++ Karras SDE",
78
+ ):
79
+ if prompt is None or prompt == "":
80
+ raise gr.Error("Prompt is required")
81
+
82
+ if qr_code_content == "":
83
+ raise gr.Error("QR Code Image or QR Code Content is required")
84
+
85
+ pipe.scheduler = SAMPLER_MAP[sampler](pipe.scheduler.config)
86
+
87
+ generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
88
+
89
+
90
+ print("Generating QR Code from content")
91
+ qr = qrcode.QRCode(
92
+ version=1,
93
+ error_correction=qrcode.constants.ERROR_CORRECT_H,
94
+ box_size=10,
95
+ border=4,
96
+ )
97
+ qr.add_data(qr_code_content)
98
+ qr.make(fit=True)
99
+
100
+ qrcode_image = qr.make_image(fill_color="black", back_color="white")
101
+ qrcode_image = resize_for_condition_image(qrcode_image, 768)
102
+ if custom_image is None:
103
+ print("Using QR Code Image")
104
+ custom_image = qrcode_image
105
+
106
+ out = pipe(
107
+ prompt=prompt,
108
+ negative_prompt=negative_prompt,
109
+ image=init_image,
110
+ control_image=qrcode_image, # type: ignore
111
+ width=768, # type: ignore
112
+ height=768, # type: ignore
113
+ guidance_scale=float(guidance_scale),
114
+ controlnet_conditioning_scale=float(controlnet_conditioning_scale), # type: ignore
115
+ generator=generator,
116
+ strength=float(strength),
117
+ num_inference_steps=40,
118
+ )
119
+ return out.images[0] # type: ignore
120
+
121
+
122
+ with gr.Blocks() as blocks:
123
+ gr.Markdown(
124
+ """
125
+ # QR Code AI Art Generator
126
+ ## 💡 How to generate beautiful QR codes
127
+ We use the QR code image as the initial image **and** the control image, which allows you to generate
128
+ QR Codes that blend in **very naturally** with your provided prompt.
129
+ The strength parameter defines how much noise is added to your QR code and the noisy QR code is then guided towards both your prompt and the QR code image via Controlnet.
130
+ Use a high strength value between 0.8 and 0.95 and choose a conditioning scale between 0.6 and 2.0.
131
+ This mode arguably achieves the asthetically most appealing QR code images, but also requires more tuning of the controlnet conditioning scale and the strength value. If the generated image
132
+ looks way to much like the original QR code, make sure to gently increase the *strength* value and reduce the *conditioning* scale. Also check out the examples below.
133
+ model: https://huggingface.co/DionTimmer/controlnet_qrcode-control_v1p_sd15
134
+ <a href="https://huggingface.co/spaces/huggingface-projects/QR-code-AI-art-generator?duplicate=true" style="display: inline-block;margin-top: .5em;margin-right: .25em;" target="_blank">
135
+ <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>
136
+ """
137
+ )
138
+
139
+ with gr.Row():
140
+ with gr.Column():
141
+ qr_code_content = gr.Textbox(
142
+ label="QR Code Content",
143
+ info="QR Code Content or URL",
144
+ value="",
145
+ )
146
+ with gr.Accordion(label="init Image (Optional)"):#, open=False):
147
+ custom_image = gr.Image(
148
+ label="Init Code Image Leave blank to automatically generate QR code",
149
+ type="pil",
150
+ )
151
+
152
+ prompt = gr.Textbox(
153
+ label="Prompt",
154
+ info="Prompt that guides the generation towards",
155
+ )
156
+ negative_prompt = gr.Textbox(
157
+ label="Negative Prompt",
158
+ value="ugly, disfigured, low quality, blurry, nsfw",
159
+ )
160
+ use_qr_code_as_init_image = gr.Checkbox(label="Use QR code as init image", value=True, interactive=False, info="Whether init image should be QR code. Unclick to pass init image or generate init image with Stable Diffusion 2.1")
161
+
162
+ with gr.Accordion(label="Init Images (Optional)", open=False, visible=False) as init_image_acc:
163
+ init_image = gr.Image(label="Init Image (Optional). Leave blank to generate image with SD 2.1", type="pil")
164
+
165
+
166
+ with gr.Accordion(
167
+ label="Params: The generated QR Code functionality is largely influenced by the parameters detailed below",
168
+ open=True,
169
+ ):
170
+ controlnet_conditioning_scale = gr.Slider(
171
+ minimum=0.0,
172
+ maximum=5.0,
173
+ step=0.01,
174
+ value=1.1,
175
+ label="Controlnet Conditioning Scale",
176
+ )
177
+ steps = gr.Slider(
178
+ minimum=20,
179
+ maximum=50,
180
+ step=1,
181
+ value=20,
182
+ label="Steps",
183
+ )
184
+ strength = gr.Slider(
185
+ minimum=0.0, maximum=1.0, step=0.01, value=0.9, label="Strength"
186
+ )
187
+ guidance_scale = gr.Slider(
188
+ minimum=0.0,
189
+ maximum=50.0,
190
+ step=0.25,
191
+ value=7.5,
192
+ label="Guidance Scale",
193
+ )
194
+ sampler = gr.Dropdown(choices=list(SAMPLER_MAP.keys()), value="DPM++ Karras SDE", label="Sampler")
195
+ control_guidance_start = gr.Slider(
196
+ minimum=0,
197
+ maximum=1,
198
+ step=0.01,
199
+ value=0,
200
+ label="control_guidance_start",
201
+ )
202
+ control_guidance_end = gr.Slider(
203
+ minimum=0,
204
+ maximum=1,
205
+ step=0.01,
206
+ value=1,
207
+ label="control_guidance_end",
208
+ )
209
+ seed = gr.Slider(
210
+ minimum=-1,
211
+ maximum=9999999999,
212
+ step=1,
213
+ value=2313123,
214
+ label="Seed",
215
+ randomize=True,
216
+ )
217
+ with gr.Row():
218
+ run_btn = gr.Button("Run")
219
+ with gr.Column():
220
+ result_image = gr.Image(label="Result Image")
221
+ run_btn.click(
222
+ inference,
223
+ inputs=[
224
+ qr_code_content,
225
+ prompt,
226
+ negative_prompt,
227
+ guidance_scale,
228
+ controlnet_conditioning_scale,
229
+ strength,
230
+ seed,
231
+ init_image,
232
+ custom_image,
233
+ use_qr_code_as_init_image,
234
+ sampler,
235
+ ],
236
+ outputs=[result_image],
237
+ concurrency_limit=1
238
+ )
239
+
240
+ gr.Examples(
241
+ examples=[
242
+ [
243
+ "https://huggingface.co/",
244
+ "A sky view of a colorful lakes and rivers flowing through the desert",
245
+ "ugly, disfigured, low quality, blurry, nsfw",
246
+ 7.5,
247
+ 1.3,
248
+ 0.9,
249
+ 5392011833,
250
+ None,
251
+ None,
252
+ True,
253
+ "DPM++ Karras SDE",
254
+ ],
255
+ [
256
+ "https://huggingface.co/",
257
+ "Bright sunshine coming through the cracks of a wet, cave wall of big rocks",
258
+ "ugly, disfigured, low quality, blurry, nsfw",
259
+ 7.5,
260
+ 1.11,
261
+ 0.9,
262
+ 2523992465,
263
+ None,
264
+ None,
265
+ True,
266
+ "DPM++ Karras SDE",
267
+ ],
268
+ [
269
+ "https://huggingface.co/",
270
+ "Sky view of highly aesthetic, ancient greek thermal baths in beautiful nature",
271
+ "ugly, disfigured, low quality, blurry, nsfw",
272
+ 7.5,
273
+ 1.5,
274
+ 0.9,
275
+ 2523992465,
276
+ None,
277
+ None,
278
+ True,
279
+ "DPM++ Karras SDE",
280
+ ],
281
+ ],
282
+ fn=inference,
283
+ inputs=[
284
+ qr_code_content,
285
+ prompt,
286
+ negative_prompt,
287
+ guidance_scale,
288
+ controlnet_conditioning_scale,
289
+ strength,
290
+ seed,
291
+ init_image,
292
+ custom_image,
293
+ use_qr_code_as_init_image,
294
+ sampler,
295
+ ],
296
+ outputs=[result_image],
297
+ cache_examples=True,
298
+ )
299
+
300
+ blocks.queue(max_size=20,api_open=False)
301
+ blocks.launch(share=bool(os.environ.get("SHARE", False)), show_api=False)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ diffusers
2
+ transformers
3
+ accelerate
4
+ torch
5
+ xformers
6
+ gradio
7
+ Pillow
8
+ qrcode
9
+ gradio==4.8.0