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