Spaces:
Running
on
Zero
Running
on
Zero
File size: 8,259 Bytes
3aa0ca8 eebec00 3aa0ca8 eebec00 3aa0ca8 eebec00 3aa0ca8 15417c3 3aa0ca8 eebec00 3aa0ca8 eebec00 3aa0ca8 15417c3 3aa0ca8 eebec00 15417c3 eebec00 3aa0ca8 15417c3 3aa0ca8 15417c3 eebec00 3aa0ca8 e5bd30a 3aa0ca8 eebec00 3aa0ca8 eebec00 3aa0ca8 eebec00 3aa0ca8 eebec00 3aa0ca8 eebec00 8b618de eebec00 3aa0ca8 eebec00 3aa0ca8 |
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 |
import torch
import gradio as gr
from PIL import Image
import qrcode
from gradio_client import Client
from pathlib import Path
from diffusers import (
StableDiffusionControlNetImg2ImgPipeline,
ControlNetModel,
DDIMScheduler,
)
from diffusers.utils import load_image
from PIL import Image
sd_client = Client("stabilityai/stable-diffusion")
qrcode_generator = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=0,
)
controlnet = ControlNetModel.from_pretrained(
"DionTimmer/controlnet_qrcode-control_v1p_sd15", torch_dtype=torch.float16
)
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
controlnet=controlnet,
safety_checker=None,
torch_dtype=torch.float16,
)
pipe.enable_xformers_memory_efficient_attention()
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()
def resize_for_condition_image(input_image: Image.Image, resolution: int):
input_image = input_image.convert("RGB")
W, H = input_image.size
k = float(resolution) / min(H, W)
H *= k
W *= k
H = int(round(H / 64.0)) * 64
W = int(round(W / 64.0)) * 64
img = input_image.resize((W, H), resample=Image.LANCZOS)
return img
def inference(
init_image: Image.Image,
qrcode_image: Image.Image,
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,
num_inference_steps: int = 30,
):
if prompt is None or prompt == "":
raise gr.Error("Prompt is required")
if qrcode_image is None and qr_code_content is None:
raise gr.Error("QR Code Image or QR Code Content is required")
if init_image is None:
print("Generating random image from prompt using Stable Diffusion")
# generate image from prompt
img_dir = sd_client.predict(prompt, negative_prompt, 7, fn_index=1)
images = Path(img_dir).rglob("*.jpg")
init_image = Image.open(next(images))
if qr_code_content is not None or qr_code_content != "":
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 = resize_for_condition_image(qrcode_image, 768)
else:
print("Using QR Code Image")
qrcode_image = resize_for_condition_image(qrcode_image, 768)
init_image = resize_for_condition_image(init_image, 768)
generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
out = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
image=init_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=num_inference_steps,
)
return out.images[0] # type: ignore
with gr.Blocks() as blocks:
gr.Markdown(
"""
# AI QR Code Generator
model: https://huggingface.co/DionTimmer/controlnet_qrcode-control_v1p_sd15
<a href="https://huggingface.co/spaces/huggingface-projects/AI-QR-code-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 longer sequences, more control and no queue.</p>
"""
)
with gr.Row():
with gr.Column():
qr_code_content = gr.Textbox(
label="QR Code Content",
info="QR Code Content or URL",
value="",
)
prompt = gr.Textbox(
label="Prompt",
info="Prompt is required. If init image is not provided, then it will be generated from prompt using Stable Diffusion 2.1",
)
negative_prompt = gr.Textbox(
label="Negative Prompt",
value="ugly, disfigured, low quality, blurry, nsfw",
)
init_image = gr.Image(label="Init Image (Optional)", type="pil")
qr_code_image = gr.Image(
label="QR Code Image (Optional)",
type="pil",
)
with gr.Accordion(label="Params"):
guidance_scale = gr.Slider(
minimum=0.0,
maximum=50.0,
step=0.1,
value=10.0,
label="Guidance Scale",
)
controlnet_conditioning_scale = gr.Slider(
minimum=0.0,
maximum=5.0,
step=0.1,
value=2.0,
label="Controlnet Conditioning Scale",
)
strength = gr.Slider(
minimum=0.0, maximum=1.0, step=0.1, value=0.8, label="Strength"
)
seed = gr.Slider(
minimum=-1,
maximum=9999999999,
step=1,
value=2313123,
label="Seed",
randomize=True,
)
run_btn = gr.Button("Run")
with gr.Column():
result_image = gr.Image(label="Result Image")
run_btn.click(
inference,
inputs=[
init_image,
qr_code_image,
qr_code_content,
prompt,
negative_prompt,
guidance_scale,
controlnet_conditioning_scale,
strength,
seed,
],
outputs=[result_image],
)
gr.Examples(
examples=[
[
"./examples/init.jpeg",
"./examples/qrcode.png",
"",
"crisp QR code prominently displayed on a billboard amidst the bustling skyline of New York City, with iconic landmarks subtly featured in the background.",
"ugly, disfigured, low quality, blurry, nsfw",
10.0,
2.0,
0.8,
2313123,
],
[
"./examples/init.jpeg",
None,
"https://huggingface.co",
"crisp QR code prominently displayed on a billboard amidst the bustling skyline of New York City, with iconic landmarks subtly featured in the background.",
"ugly, disfigured, low quality, blurry, nsfw",
10.0,
2.0,
0.8,
2313123,
],
[
None,
None,
"https://huggingface.co",
"crisp QR code prominently displayed on a billboard amidst the bustling skyline of New York City, with iconic landmarks subtly featured in the background.",
"ugly, disfigured, low quality, blurry, nsfw",
10.0,
2.0,
0.8,
2313123,
],
[
None,
None,
"https://huggingface.co",
"A flying cat over a jungle",
"ugly, disfigured, low quality, blurry, nsfw",
10.0,
2.7,
0.8,
2313123,
],
],
fn=inference,
inputs=[
init_image,
qr_code_image,
qr_code_content,
prompt,
negative_prompt,
guidance_scale,
controlnet_conditioning_scale,
strength,
seed,
],
outputs=[result_image],
)
blocks.queue()
blocks.launch()
|