Spaces:
Runtime error
Runtime error
Duplicate from huggingface-projects/QR-code-AI-art-generator
Browse filesCo-authored-by: Radamés Ajna <[email protected]>
- .gitattributes +37 -0
- .gitignore +3 -0
- README.md +15 -0
- app.py +285 -0
- examples/hack.png +3 -0
- examples/init.jpeg +3 -0
- examples/qrcode.png +3 -0
- requirements.txt +8 -0
.gitattributes
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
+
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
__pycache__
|
2 |
+
venv
|
3 |
+
gradio_cached_examples/
|
README.md
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: QR Code AI Art Generator
|
3 |
+
emoji: 📱🔲
|
4 |
+
colorFrom: MediumSeaGreen
|
5 |
+
colorTo: CornflowerBlue
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.35.2
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
suggested_hardware: t4-medium
|
11 |
+
startup_duration_timeout: 1h
|
12 |
+
duplicated_from: huggingface-projects/QR-code-AI-art-generator
|
13 |
+
---
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
12 |
+
from diffusers import (
|
13 |
+
StableDiffusionPipeline,
|
14 |
+
StableDiffusionControlNetImg2ImgPipeline,
|
15 |
+
ControlNetModel,
|
16 |
+
DDIMScheduler,
|
17 |
+
DPMSolverMultistepScheduler,
|
18 |
+
DEISMultistepScheduler,
|
19 |
+
HeunDiscreteScheduler,
|
20 |
+
EulerDiscreteScheduler,
|
21 |
+
)
|
22 |
+
|
23 |
+
qrcode_generator = qrcode.QRCode(
|
24 |
+
version=1,
|
25 |
+
error_correction=qrcode.ERROR_CORRECT_H,
|
26 |
+
box_size=10,
|
27 |
+
border=4,
|
28 |
+
)
|
29 |
+
|
30 |
+
controlnet = ControlNetModel.from_pretrained(
|
31 |
+
"DionTimmer/controlnet_qrcode-control_v1p_sd15", torch_dtype=torch.float16
|
32 |
+
)
|
33 |
+
|
34 |
+
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
35 |
+
"runwayml/stable-diffusion-v1-5",
|
36 |
+
controlnet=controlnet,
|
37 |
+
safety_checker=None,
|
38 |
+
torch_dtype=torch.float16,
|
39 |
+
).to("cuda")
|
40 |
+
pipe.enable_xformers_memory_efficient_attention()
|
41 |
+
|
42 |
+
|
43 |
+
def resize_for_condition_image(input_image: Image.Image, resolution: int):
|
44 |
+
input_image = input_image.convert("RGB")
|
45 |
+
W, H = input_image.size
|
46 |
+
k = float(resolution) / min(H, W)
|
47 |
+
H *= k
|
48 |
+
W *= k
|
49 |
+
H = int(round(H / 64.0)) * 64
|
50 |
+
W = int(round(W / 64.0)) * 64
|
51 |
+
img = input_image.resize((W, H), resample=Image.LANCZOS)
|
52 |
+
return img
|
53 |
+
|
54 |
+
|
55 |
+
SAMPLER_MAP = {
|
56 |
+
"DPM++ Karras SDE": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True, algorithm_type="sde-dpmsolver++"),
|
57 |
+
"DPM++ Karras": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True),
|
58 |
+
"Heun": lambda config: HeunDiscreteScheduler.from_config(config),
|
59 |
+
"Euler": lambda config: EulerDiscreteScheduler.from_config(config),
|
60 |
+
"DDIM": lambda config: DDIMScheduler.from_config(config),
|
61 |
+
"DEIS": lambda config: DEISMultistepScheduler.from_config(config),
|
62 |
+
}
|
63 |
+
|
64 |
+
|
65 |
+
def inference(
|
66 |
+
qr_code_content: str,
|
67 |
+
prompt: str,
|
68 |
+
negative_prompt: str,
|
69 |
+
guidance_scale: float = 10.0,
|
70 |
+
controlnet_conditioning_scale: float = 2.0,
|
71 |
+
strength: float = 0.8,
|
72 |
+
seed: int = -1,
|
73 |
+
init_image: Image.Image | None = None,
|
74 |
+
qrcode_image: Image.Image | None = None,
|
75 |
+
use_qr_code_as_init_image = True,
|
76 |
+
sampler = "DPM++ Karras SDE",
|
77 |
+
):
|
78 |
+
if prompt is None or prompt == "":
|
79 |
+
raise gr.Error("Prompt is required")
|
80 |
+
|
81 |
+
if qrcode_image is None and qr_code_content == "":
|
82 |
+
raise gr.Error("QR Code Image or QR Code Content is required")
|
83 |
+
|
84 |
+
pipe.scheduler = SAMPLER_MAP[sampler](pipe.scheduler.config)
|
85 |
+
|
86 |
+
generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
|
87 |
+
|
88 |
+
if qr_code_content != "" or qrcode_image.size == (1, 1):
|
89 |
+
print("Generating QR Code from content")
|
90 |
+
qr = qrcode.QRCode(
|
91 |
+
version=1,
|
92 |
+
error_correction=qrcode.constants.ERROR_CORRECT_H,
|
93 |
+
box_size=10,
|
94 |
+
border=4,
|
95 |
+
)
|
96 |
+
qr.add_data(qr_code_content)
|
97 |
+
qr.make(fit=True)
|
98 |
+
|
99 |
+
qrcode_image = qr.make_image(fill_color="black", back_color="white")
|
100 |
+
qrcode_image = resize_for_condition_image(qrcode_image, 768)
|
101 |
+
else:
|
102 |
+
print("Using QR Code Image")
|
103 |
+
qrcode_image = resize_for_condition_image(qrcode_image, 768)
|
104 |
+
|
105 |
+
# hack due to gradio examples
|
106 |
+
init_image = qrcode_image
|
107 |
+
|
108 |
+
out = pipe(
|
109 |
+
prompt=prompt,
|
110 |
+
negative_prompt=negative_prompt,
|
111 |
+
image=qrcode_image,
|
112 |
+
control_image=qrcode_image, # type: ignore
|
113 |
+
width=768, # type: ignore
|
114 |
+
height=768, # type: ignore
|
115 |
+
guidance_scale=float(guidance_scale),
|
116 |
+
controlnet_conditioning_scale=float(controlnet_conditioning_scale), # type: ignore
|
117 |
+
generator=generator,
|
118 |
+
strength=float(strength),
|
119 |
+
num_inference_steps=40,
|
120 |
+
)
|
121 |
+
return out.images[0] # type: ignore
|
122 |
+
|
123 |
+
|
124 |
+
with gr.Blocks() as blocks:
|
125 |
+
gr.Markdown(
|
126 |
+
"""
|
127 |
+
# QR Code AI Art Generator
|
128 |
+
|
129 |
+
## 💡 How to generate beautiful QR codes
|
130 |
+
|
131 |
+
We use the QR code image as the initial image **and** the control image, which allows you to generate
|
132 |
+
QR Codes that blend in **very naturally** with your provided prompt.
|
133 |
+
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.
|
134 |
+
Use a high strength value between 0.8 and 0.95 and choose a conditioning scale between 0.6 and 2.0.
|
135 |
+
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
|
136 |
+
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.
|
137 |
+
|
138 |
+
model: https://huggingface.co/DionTimmer/controlnet_qrcode-control_v1p_sd15
|
139 |
+
|
140 |
+
<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">
|
141 |
+
<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>
|
142 |
+
"""
|
143 |
+
)
|
144 |
+
|
145 |
+
with gr.Row():
|
146 |
+
with gr.Column():
|
147 |
+
qr_code_content = gr.Textbox(
|
148 |
+
label="QR Code Content",
|
149 |
+
info="QR Code Content or URL",
|
150 |
+
value="",
|
151 |
+
)
|
152 |
+
with gr.Accordion(label="QR Code Image (Optional)", open=False):
|
153 |
+
qr_code_image = gr.Image(
|
154 |
+
label="QR Code Image (Optional). Leave blank to automatically generate QR code",
|
155 |
+
type="pil",
|
156 |
+
)
|
157 |
+
|
158 |
+
prompt = gr.Textbox(
|
159 |
+
label="Prompt",
|
160 |
+
info="Prompt that guides the generation towards",
|
161 |
+
)
|
162 |
+
negative_prompt = gr.Textbox(
|
163 |
+
label="Negative Prompt",
|
164 |
+
value="ugly, disfigured, low quality, blurry, nsfw",
|
165 |
+
)
|
166 |
+
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")
|
167 |
+
|
168 |
+
with gr.Accordion(label="Init Images (Optional)", open=False, visible=False) as init_image_acc:
|
169 |
+
init_image = gr.Image(label="Init Image (Optional). Leave blank to generate image with SD 2.1", type="pil")
|
170 |
+
|
171 |
+
|
172 |
+
with gr.Accordion(
|
173 |
+
label="Params: The generated QR Code functionality is largely influenced by the parameters detailed below",
|
174 |
+
open=True,
|
175 |
+
):
|
176 |
+
controlnet_conditioning_scale = gr.Slider(
|
177 |
+
minimum=0.0,
|
178 |
+
maximum=5.0,
|
179 |
+
step=0.01,
|
180 |
+
value=1.1,
|
181 |
+
label="Controlnet Conditioning Scale",
|
182 |
+
)
|
183 |
+
strength = gr.Slider(
|
184 |
+
minimum=0.0, maximum=1.0, step=0.01, value=0.9, label="Strength"
|
185 |
+
)
|
186 |
+
guidance_scale = gr.Slider(
|
187 |
+
minimum=0.0,
|
188 |
+
maximum=50.0,
|
189 |
+
step=0.25,
|
190 |
+
value=7.5,
|
191 |
+
label="Guidance Scale",
|
192 |
+
)
|
193 |
+
sampler = gr.Dropdown(choices=list(SAMPLER_MAP.keys()), value="DPM++ Karras SDE")
|
194 |
+
seed = gr.Slider(
|
195 |
+
minimum=-1,
|
196 |
+
maximum=9999999999,
|
197 |
+
step=1,
|
198 |
+
value=2313123,
|
199 |
+
label="Seed",
|
200 |
+
randomize=True,
|
201 |
+
)
|
202 |
+
with gr.Row():
|
203 |
+
run_btn = gr.Button("Run")
|
204 |
+
with gr.Column():
|
205 |
+
result_image = gr.Image(label="Result Image")
|
206 |
+
run_btn.click(
|
207 |
+
inference,
|
208 |
+
inputs=[
|
209 |
+
qr_code_content,
|
210 |
+
prompt,
|
211 |
+
negative_prompt,
|
212 |
+
guidance_scale,
|
213 |
+
controlnet_conditioning_scale,
|
214 |
+
strength,
|
215 |
+
seed,
|
216 |
+
init_image,
|
217 |
+
qr_code_image,
|
218 |
+
use_qr_code_as_init_image,
|
219 |
+
sampler,
|
220 |
+
],
|
221 |
+
outputs=[result_image],
|
222 |
+
)
|
223 |
+
|
224 |
+
gr.Examples(
|
225 |
+
examples=[
|
226 |
+
[
|
227 |
+
"https://huggingface.co/",
|
228 |
+
"A sky view of a colorful lakes and rivers flowing through the desert",
|
229 |
+
"ugly, disfigured, low quality, blurry, nsfw",
|
230 |
+
7.5,
|
231 |
+
1.3,
|
232 |
+
0.9,
|
233 |
+
5392011833,
|
234 |
+
None,
|
235 |
+
None,
|
236 |
+
True,
|
237 |
+
"DPM++ Karras SDE",
|
238 |
+
],
|
239 |
+
[
|
240 |
+
"https://huggingface.co/",
|
241 |
+
"Bright sunshine coming through the cracks of a wet, cave wall of big rocks",
|
242 |
+
"ugly, disfigured, low quality, blurry, nsfw",
|
243 |
+
7.5,
|
244 |
+
1.11,
|
245 |
+
0.9,
|
246 |
+
2523992465,
|
247 |
+
None,
|
248 |
+
None,
|
249 |
+
True,
|
250 |
+
"DPM++ Karras SDE",
|
251 |
+
],
|
252 |
+
[
|
253 |
+
"https://huggingface.co/",
|
254 |
+
"Sky view of highly aesthetic, ancient greek thermal baths in beautiful nature",
|
255 |
+
"ugly, disfigured, low quality, blurry, nsfw",
|
256 |
+
7.5,
|
257 |
+
1.5,
|
258 |
+
0.9,
|
259 |
+
2523992465,
|
260 |
+
None,
|
261 |
+
None,
|
262 |
+
True,
|
263 |
+
"DPM++ Karras SDE",
|
264 |
+
],
|
265 |
+
],
|
266 |
+
fn=inference,
|
267 |
+
inputs=[
|
268 |
+
qr_code_content,
|
269 |
+
prompt,
|
270 |
+
negative_prompt,
|
271 |
+
guidance_scale,
|
272 |
+
controlnet_conditioning_scale,
|
273 |
+
strength,
|
274 |
+
seed,
|
275 |
+
init_image,
|
276 |
+
qr_code_image,
|
277 |
+
use_qr_code_as_init_image,
|
278 |
+
sampler,
|
279 |
+
],
|
280 |
+
outputs=[result_image],
|
281 |
+
cache_examples=True,
|
282 |
+
)
|
283 |
+
|
284 |
+
blocks.queue(concurrency_count=1, max_size=20)
|
285 |
+
blocks.launch(share=bool(os.environ.get("SHARE", False)))
|
examples/hack.png
ADDED
![]() |
Git LFS Details
|
examples/init.jpeg
ADDED
![]() |
Git LFS Details
|
examples/qrcode.png
ADDED
![]() |
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
diffusers
|
2 |
+
transformers
|
3 |
+
accelerate
|
4 |
+
torch
|
5 |
+
xformers
|
6 |
+
gradio
|
7 |
+
Pillow
|
8 |
+
qrcode
|