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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
```python
|
| 2 |
+
# please install ppdiffusers develop
|
| 3 |
+
import paddle
|
| 4 |
+
import os
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import qrcode
|
| 8 |
+
|
| 9 |
+
from ppdiffusers import (
|
| 10 |
+
StableDiffusionPipeline,
|
| 11 |
+
DiffusionPipeline,
|
| 12 |
+
ControlNetModel,
|
| 13 |
+
DDIMScheduler,
|
| 14 |
+
DPMSolverMultistepScheduler,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
from PIL import Image
|
| 18 |
+
|
| 19 |
+
qrcode_generator = qrcode.QRCode(
|
| 20 |
+
version=1,
|
| 21 |
+
error_correction=qrcode.ERROR_CORRECT_H,
|
| 22 |
+
box_size=10,
|
| 23 |
+
border=4,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
controlnet = ControlNetModel.from_pretrained(
|
| 27 |
+
"DionTimmer/controlnet_qrcode-control_v1p_sd15", paddle_dtype=paddle.float16
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 31 |
+
"runwayml/stable-diffusion-v1-5",
|
| 32 |
+
controlnet=controlnet,
|
| 33 |
+
safety_checker=None,
|
| 34 |
+
paddle_dtype=paddle.float16,
|
| 35 |
+
custom_pipeline="junnyu/stable_diffusion_controlnet_img2img",
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 39 |
+
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
sd_pipe = StableDiffusionPipeline.from_pretrained(
|
| 43 |
+
"stabilityai/stable-diffusion-2-1",
|
| 44 |
+
paddle_dtype=paddle.float16,
|
| 45 |
+
safety_checker=None,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
sd_pipe.scheduler = DPMSolverMultistepScheduler.from_config(sd_pipe.scheduler.config)
|
| 49 |
+
sd_pipe.enable_xformers_memory_efficient_attention()
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def resize_for_condition_image(input_image: Image.Image, resolution: int):
|
| 53 |
+
input_image = input_image.convert("RGB")
|
| 54 |
+
W, H = input_image.size
|
| 55 |
+
k = float(resolution) / min(H, W)
|
| 56 |
+
H *= k
|
| 57 |
+
W *= k
|
| 58 |
+
H = int(round(H / 64.0)) * 64
|
| 59 |
+
W = int(round(W / 64.0)) * 64
|
| 60 |
+
img = input_image.resize((W, H), resample=Image.LANCZOS)
|
| 61 |
+
return img
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def inference(
|
| 65 |
+
qr_code_content: str,
|
| 66 |
+
prompt: str,
|
| 67 |
+
negative_prompt: str,
|
| 68 |
+
guidance_scale: float = 10.0,
|
| 69 |
+
controlnet_conditioning_scale: float = 2.0,
|
| 70 |
+
strength: float = 0.8,
|
| 71 |
+
seed: int = -1,
|
| 72 |
+
init_image: Image.Image = None,
|
| 73 |
+
qrcode_image: Image.Image = None,
|
| 74 |
+
):
|
| 75 |
+
if prompt is None or prompt == "":
|
| 76 |
+
raise gr.Error("Prompt is required")
|
| 77 |
+
|
| 78 |
+
if qrcode_image is None and qr_code_content == "":
|
| 79 |
+
raise gr.Error("QR Code Image or QR Code Content is required")
|
| 80 |
+
|
| 81 |
+
generator = paddle.Generator().manual_seed(seed) if seed != -1 else None
|
| 82 |
+
|
| 83 |
+
# hack due to gradio examples
|
| 84 |
+
if init_image is None or init_image.size == (1, 1):
|
| 85 |
+
print("Generating random image from prompt using Stable Diffusion")
|
| 86 |
+
# generate image from prompt
|
| 87 |
+
out = sd_pipe(
|
| 88 |
+
prompt=prompt,
|
| 89 |
+
negative_prompt=negative_prompt,
|
| 90 |
+
generator=generator,
|
| 91 |
+
num_inference_steps=25,
|
| 92 |
+
num_images_per_prompt=1,
|
| 93 |
+
) # type: ignore
|
| 94 |
+
|
| 95 |
+
init_image = out.images[0]
|
| 96 |
+
else:
|
| 97 |
+
print("Using provided init image")
|
| 98 |
+
init_image = resize_for_condition_image(init_image, 768)
|
| 99 |
+
|
| 100 |
+
if qr_code_content != "" or qrcode_image.size == (1, 1):
|
| 101 |
+
print("Generating QR Code from content")
|
| 102 |
+
qr = qrcode.QRCode(
|
| 103 |
+
version=1,
|
| 104 |
+
error_correction=qrcode.constants.ERROR_CORRECT_H,
|
| 105 |
+
box_size=10,
|
| 106 |
+
border=4,
|
| 107 |
+
)
|
| 108 |
+
qr.add_data(qr_code_content)
|
| 109 |
+
qr.make(fit=True)
|
| 110 |
+
|
| 111 |
+
qrcode_image = qr.make_image(fill_color="black", back_color="white")
|
| 112 |
+
qrcode_image = resize_for_condition_image(qrcode_image, 768)
|
| 113 |
+
else:
|
| 114 |
+
print("Using QR Code Image")
|
| 115 |
+
qrcode_image = resize_for_condition_image(qrcode_image, 768)
|
| 116 |
+
|
| 117 |
+
out = pipe(
|
| 118 |
+
prompt=prompt,
|
| 119 |
+
negative_prompt=negative_prompt,
|
| 120 |
+
image=init_image,
|
| 121 |
+
control_image=qrcode_image, # type: ignore
|
| 122 |
+
width=768, # type: ignore
|
| 123 |
+
height=768, # type: ignore
|
| 124 |
+
guidance_scale=float(guidance_scale),
|
| 125 |
+
controlnet_conditioning_scale=float(controlnet_conditioning_scale), # type: ignore
|
| 126 |
+
generator=generator,
|
| 127 |
+
strength=float(strength),
|
| 128 |
+
num_inference_steps=40,
|
| 129 |
+
)
|
| 130 |
+
return out.images[0] # type: ignore
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
with gr.Blocks() as blocks:
|
| 134 |
+
gr.Markdown(
|
| 135 |
+
"""
|
| 136 |
+
# QR Code AI Art Generator
|
| 137 |
+
model: https://huggingface.co/DionTimmer/controlnet_qrcode-control_v1p_sd15
|
| 138 |
+
<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">
|
| 139 |
+
<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>
|
| 140 |
+
"""
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
with gr.Row():
|
| 144 |
+
with gr.Column():
|
| 145 |
+
qr_code_content = gr.Textbox(
|
| 146 |
+
label="QR Code Content",
|
| 147 |
+
info="QR Code Content or URL",
|
| 148 |
+
value="",
|
| 149 |
+
)
|
| 150 |
+
prompt = gr.Textbox(
|
| 151 |
+
label="Prompt",
|
| 152 |
+
info="Prompt is required. If init image is not provided, then it will be generated from prompt using Stable Diffusion 2.1",
|
| 153 |
+
)
|
| 154 |
+
negative_prompt = gr.Textbox(
|
| 155 |
+
label="Negative Prompt",
|
| 156 |
+
value="ugly, disfigured, low quality, blurry, nsfw",
|
| 157 |
+
)
|
| 158 |
+
with gr.Accordion(label="Init Images (Optional)", open=False):
|
| 159 |
+
init_image = gr.Image(label="Init Image (Optional)", type="pil")
|
| 160 |
+
|
| 161 |
+
qr_code_image = gr.Image(
|
| 162 |
+
label="QR Code Image (Optional)",
|
| 163 |
+
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=False,
|
| 169 |
+
):
|
| 170 |
+
guidance_scale = gr.Slider(
|
| 171 |
+
minimum=0.0,
|
| 172 |
+
maximum=50.0,
|
| 173 |
+
step=0.01,
|
| 174 |
+
value=10.0,
|
| 175 |
+
label="Guidance Scale",
|
| 176 |
+
)
|
| 177 |
+
controlnet_conditioning_scale = gr.Slider(
|
| 178 |
+
minimum=0.0,
|
| 179 |
+
maximum=5.0,
|
| 180 |
+
step=0.01,
|
| 181 |
+
value=2.0,
|
| 182 |
+
label="Controlnet Conditioning Scale",
|
| 183 |
+
)
|
| 184 |
+
strength = gr.Slider(
|
| 185 |
+
minimum=0.0, maximum=1.0, step=0.01, value=0.8, label="Strength"
|
| 186 |
+
)
|
| 187 |
+
seed = gr.Slider(
|
| 188 |
+
minimum=-1,
|
| 189 |
+
maximum=9999999999,
|
| 190 |
+
step=1,
|
| 191 |
+
value=2313123,
|
| 192 |
+
label="Seed",
|
| 193 |
+
randomize=True,
|
| 194 |
+
)
|
| 195 |
+
with gr.Row():
|
| 196 |
+
run_btn = gr.Button("Run")
|
| 197 |
+
with gr.Column():
|
| 198 |
+
result_image = gr.Image(label="Result Image")
|
| 199 |
+
run_btn.click(
|
| 200 |
+
inference,
|
| 201 |
+
inputs=[
|
| 202 |
+
qr_code_content,
|
| 203 |
+
prompt,
|
| 204 |
+
negative_prompt,
|
| 205 |
+
guidance_scale,
|
| 206 |
+
controlnet_conditioning_scale,
|
| 207 |
+
strength,
|
| 208 |
+
seed,
|
| 209 |
+
init_image,
|
| 210 |
+
qr_code_image,
|
| 211 |
+
],
|
| 212 |
+
outputs=[result_image],
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
gr.Examples(
|
| 216 |
+
examples=[
|
| 217 |
+
[
|
| 218 |
+
"https://huggingface.co/spaces/huggingface-projects/QR-code-AI-art-generator",
|
| 219 |
+
"billboard amidst the bustling skyline of New York City, with iconic landmarks subtly featured in the background.",
|
| 220 |
+
"ugly, disfigured, low quality, blurry, nsfw",
|
| 221 |
+
13.37,
|
| 222 |
+
2.81,
|
| 223 |
+
0.68,
|
| 224 |
+
2313123,
|
| 225 |
+
"./examples/hack.png",
|
| 226 |
+
"./examples/hack.png",
|
| 227 |
+
],
|
| 228 |
+
[
|
| 229 |
+
"https://huggingface.co/spaces/huggingface-projects/QR-code-AI-art-generator",
|
| 230 |
+
"beautiful sunset in San Francisco with Golden Gate bridge in the background",
|
| 231 |
+
"ugly, disfigured, low quality, blurry, nsfw",
|
| 232 |
+
11.01,
|
| 233 |
+
2.61,
|
| 234 |
+
0.66,
|
| 235 |
+
1423585430,
|
| 236 |
+
"./examples/hack.png",
|
| 237 |
+
"./examples/hack.png",
|
| 238 |
+
],
|
| 239 |
+
[
|
| 240 |
+
"https://huggingface.co",
|
| 241 |
+
"A flying cat over a jungle",
|
| 242 |
+
"ugly, disfigured, low quality, blurry, nsfw",
|
| 243 |
+
13,
|
| 244 |
+
2.81,
|
| 245 |
+
0.66,
|
| 246 |
+
2702246671,
|
| 247 |
+
"./examples/hack.png",
|
| 248 |
+
"./examples/hack.png",
|
| 249 |
+
],
|
| 250 |
+
[
|
| 251 |
+
"",
|
| 252 |
+
"crisp QR code prominently displayed on a billboard amidst the bustling skyline of New York City, with iconic landmarks subtly featured in the background.",
|
| 253 |
+
"ugly, disfigured, low quality, blurry, nsfw",
|
| 254 |
+
10.0,
|
| 255 |
+
2.0,
|
| 256 |
+
0.8,
|
| 257 |
+
2313123,
|
| 258 |
+
"./examples/init.jpeg",
|
| 259 |
+
"./examples/qrcode.png",
|
| 260 |
+
],
|
| 261 |
+
],
|
| 262 |
+
fn=inference,
|
| 263 |
+
inputs=[
|
| 264 |
+
qr_code_content,
|
| 265 |
+
prompt,
|
| 266 |
+
negative_prompt,
|
| 267 |
+
guidance_scale,
|
| 268 |
+
controlnet_conditioning_scale,
|
| 269 |
+
strength,
|
| 270 |
+
seed,
|
| 271 |
+
init_image,
|
| 272 |
+
qr_code_image,
|
| 273 |
+
],
|
| 274 |
+
outputs=[result_image],
|
| 275 |
+
cache_examples=True,
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
blocks.queue(concurrency_count=1, max_size=20)
|
| 279 |
+
blocks.launch(server_name="0.0.0.0", server_port=8235)
|
| 280 |
+
|
| 281 |
+
```
|