TheLuckyDev's picture
Prioritize QR geometry in reference mode
2ec40c5 verified
Raw History Blame Contribute Delete
10.8 kB
import torch
import gradio as gr
from PIL import Image, ImageOps
import qrcode
import os
import spaces
from diffusers import (
StableDiffusionControlNetImg2ImgPipeline,
ControlNetModel,
DDIMScheduler,
DPMSolverMultistepScheduler,
DEISMultistepScheduler,
HeunDiscreteScheduler,
EulerDiscreteScheduler,
)
qrcode_generator = qrcode.QRCode(
version=1,
error_correction=qrcode.ERROR_CORRECT_H,
box_size=10,
border=4,
)
controlnet = ControlNetModel.from_pretrained(
"DionTimmer/controlnet_qrcode-control_v1p_sd15", torch_dtype=torch.float16
).to("cuda")
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
controlnet=controlnet,
torch_dtype=torch.float16,
).to("cuda")
# pipe.enable_xformers_memory_efficient_attention()
def fit_qr_image(input_image: Image.Image, resolution: int):
input_image = input_image.convert("RGB")
width, height = input_image.size
if width <= resolution and height <= resolution:
scale = max(1, min(resolution // width, resolution // height))
width *= scale
height *= scale
else:
scale = min(resolution / width, resolution / height)
width = max(1, int(width * scale))
height = max(1, int(height * scale))
qr_image = input_image.resize(
(width, height),
resample=Image.Resampling.NEAREST,
)
canvas = Image.new("RGB", (resolution, resolution), "white")
canvas.paste(qr_image, ((resolution - width) // 2, (resolution - height) // 2))
return canvas
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": lambda config: EulerDiscreteScheduler.from_config(config),
"DDIM": lambda config: DDIMScheduler.from_config(config),
"DEIS": lambda config: DEISMultistepScheduler.from_config(config),
}
@spaces.GPU()
def inference(
qr_code_content: str,
prompt: str,
negative_prompt: str,
guidance_scale: float = 10.0,
controlnet_conditioning_scale: float = 2.0,
strength: float = 0.8,
seed: int = -1,
init_image: Image.Image | None = None,
qrcode_image: Image.Image | None = None,
use_qr_code_as_init_image = True,
sampler = "DPM++ Karras SDE",
):
if prompt is None or prompt == "":
raise gr.Error("Prompt is required")
if qrcode_image is None and qr_code_content == "":
raise gr.Error("QR Code Image or QR Code Content is required")
pipe.scheduler = SAMPLER_MAP[sampler](pipe.scheduler.config)
generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
if qr_code_content != "" or qrcode_image is None or qrcode_image.size == (1, 1):
print("Generating QR Code from content")
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(qr_code_content)
qr.make(fit=True)
qrcode_image = qr.make_image(fill_color="black", back_color="white")
qrcode_image = fit_qr_image(qrcode_image, 768)
else:
print("Using QR Code Image")
qrcode_image = fit_qr_image(qrcode_image, 768)
if use_qr_code_as_init_image or init_image is None:
initial_image = qrcode_image
else:
print("Blending uploaded reference artwork with QR structure")
reference_image = ImageOps.fit(
init_image.convert("RGB"),
qrcode_image.size,
method=Image.Resampling.LANCZOS,
)
initial_image = Image.blend(
reference_image,
qrcode_image.convert("RGB"),
alpha=0.80,
)
out = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
image=initial_image,
control_image=qrcode_image, # type: ignore
width=768, # type: ignore
height=768, # type: ignore
guidance_scale=float(guidance_scale),
controlnet_conditioning_scale=float(controlnet_conditioning_scale), # type: ignore
generator=generator,
strength=float(strength),
num_inference_steps=40,
)
return out.images[0] # type: ignore
with gr.Blocks() as blocks:
gr.Markdown(
"""
# QR Code AI Art Generator
## 💡 How to generate beautiful QR codes
We use the QR code as the ControlNet guide. You can either use it as the initial
image or upload reference artwork to blend its composition into the result.
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.
Use a high strength value between 0.8 and 0.95 and choose a conditioning scale between 0.6 and 2.0.
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
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.
model: https://huggingface.co/DionTimmer/controlnet_qrcode-control_v1p_sd15
<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">
<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",
info="QR Code Content or URL",
value="",
)
with gr.Accordion(label="QR Code Image (Optional)", open=False):
qr_code_image = gr.Image(
label="QR Code Image (Optional). Leave blank to automatically generate QR code",
type="pil",
)
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",
)
use_qr_code_as_init_image = gr.Checkbox(
label="Use QR code as initial image",
value=True,
interactive=True,
info="Turn this off to use the uploaded reference artwork as the initial image.",
)
with gr.Accordion(label="Reference artwork (Optional)", open=False):
init_image = gr.Image(
label="Reference artwork",
type="pil",
)
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.0,
maximum=5.0,
step=0.01,
value=1.1,
label="Controlnet Conditioning Scale",
)
strength = gr.Slider(
minimum=0.0, maximum=1.0, step=0.01, value=0.9, label="Strength"
)
guidance_scale = gr.Slider(
minimum=0.0,
maximum=50.0,
step=0.25,
value=7.5,
label="Guidance Scale",
)
sampler = gr.Dropdown(choices=list(SAMPLER_MAP.keys()), value="DPM++ Karras SDE", label="Sampler")
seed = gr.Slider(
minimum=-1,
maximum=9999999999,
step=1,
value=2313123,
label="Seed",
randomize=True,
)
with gr.Row():
run_btn = gr.Button("Run")
with gr.Column():
result_image = gr.Image(label="Result Image")
run_btn.click(
inference,
inputs=[
qr_code_content,
prompt,
negative_prompt,
guidance_scale,
controlnet_conditioning_scale,
strength,
seed,
init_image,
qr_code_image,
use_qr_code_as_init_image,
sampler,
],
outputs=[result_image],
concurrency_limit=1,
api_name="generate",
)
gr.Examples(
examples=[
[
"https://huggingface.co/",
"A sky view of a colorful lakes and rivers flowing through the desert",
"ugly, disfigured, low quality, blurry, nsfw",
7.5,
1.3,
0.9,
5392011833,
None,
None,
True,
"DPM++ Karras SDE",
],
[
"https://huggingface.co/",
"Bright sunshine coming through the cracks of a wet, cave wall of big rocks",
"ugly, disfigured, low quality, blurry, nsfw",
7.5,
1.11,
0.9,
2523992465,
None,
None,
True,
"DPM++ Karras SDE",
],
[
"https://huggingface.co/",
"Sky view of highly aesthetic, ancient greek thermal baths in beautiful nature",
"ugly, disfigured, low quality, blurry, nsfw",
7.5,
1.5,
0.9,
2523992465,
None,
None,
True,
"DPM++ Karras SDE",
],
],
fn=inference,
inputs=[
qr_code_content,
prompt,
negative_prompt,
guidance_scale,
controlnet_conditioning_scale,
strength,
seed,
init_image,
qr_code_image,
use_qr_code_as_init_image,
sampler,
],
outputs=[result_image],
cache_examples=False,
)
blocks.queue(max_size=20, api_open=True)
blocks.launch(share=bool(os.environ.get("SHARE", False)))