L4Walk commited on
Commit
6f49b37
·
1 Parent(s): 3f587c6

初次提交

Browse files
Files changed (2) hide show
  1. app.py +293 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,293 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ raise gr.Error("请输入提示词")
81
+
82
+ if qrcode_image is None and qr_code_content == "":
83
+ #raise gr.Error("QR Code Image or QR Code Content is required")
84
+ raise gr.Error("请提供二维码图片或二维码内容。")
85
+
86
+ pipe.scheduler = SAMPLER_MAP[sampler](pipe.scheduler.config)
87
+
88
+ generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
89
+
90
+ if qr_code_content != "" or qrcode_image.size == (1, 1):
91
+ #print("Generating QR Code from content")
92
+ print("正在从文本生成艺术二维码")
93
+
94
+ qr = qrcode.QRCode(
95
+ version=1,
96
+ error_correction=qrcode.constants.ERROR_CORRECT_H,
97
+ box_size=10,
98
+ border=4,
99
+ )
100
+ qr.add_data(qr_code_content)
101
+ qr.make(fit=True)
102
+
103
+ qrcode_image = qr.make_image(fill_color="black", back_color="white")
104
+ qrcode_image = resize_for_condition_image(qrcode_image, 768)
105
+ else:
106
+ #print("Using QR Code Image")
107
+ print("正在使用二维码图片生成艺术二维码")
108
+ qrcode_image = resize_for_condition_image(qrcode_image, 768)
109
+
110
+ # hack due to gradio examples
111
+ init_image = qrcode_image
112
+
113
+ out = pipe(
114
+ prompt=prompt,
115
+ negative_prompt=negative_prompt,
116
+ image=qrcode_image,
117
+ control_image=qrcode_image, # type: ignore
118
+ width=768, # type: ignore
119
+ height=768, # type: ignore
120
+ guidance_scale=float(guidance_scale),
121
+ controlnet_conditioning_scale=float(
122
+ controlnet_conditioning_scale), # type: ignore
123
+ generator=generator,
124
+ strength=float(strength),
125
+ num_inference_steps=40,
126
+ )
127
+ return out.images[0] # type: ignore
128
+
129
+
130
+ with gr.Blocks() as blocks:
131
+ gr.Markdown(
132
+ """
133
+ # 蓝衫-艺术二维码生成器
134
+
135
+ ## 💡 如何去生成好看的艺术二维码?
136
+
137
+ We use the QR code image as the initial image **and** the control image, which allows you to generate
138
+ QR Codes that blend in **very naturally** with your provided prompt.
139
+ 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.
140
+ Use a high strength value between 0.8 and 0.95 and choose a conditioning scale between 0.6 and 2.0.
141
+ 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
142
+ 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.
143
+
144
+ model: https://huggingface.co/DionTimmer/controlnet_qrcode-control_v1p_sd15
145
+
146
+ <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">
147
+ <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>
148
+ """
149
+ )
150
+
151
+ with gr.Row():
152
+ with gr.Column():
153
+ qr_code_content = gr.Textbox(
154
+ label="QR Code Content",
155
+ info="QR Code Content or URL",
156
+ value="",
157
+ )
158
+ with gr.Accordion(label="QR Code Image (Optional)", open=False):
159
+ qr_code_image = gr.Image(
160
+ label="QR Code Image (Optional). Leave blank to automatically generate QR code",
161
+ type="pil",
162
+ )
163
+
164
+ prompt = gr.Textbox(
165
+ label="Prompt",
166
+ info="Prompt that guides the generation towards",
167
+ )
168
+ negative_prompt = gr.Textbox(
169
+ label="Negative Prompt",
170
+ value="ugly, disfigured, low quality, blurry, nsfw",
171
+ )
172
+ use_qr_code_as_init_image = gr.Checkbox(label="Use QR code as init image", value=True, interactive=False,
173
+ info="Whether init image should be QR code. Unclick to pass init image or generate init image with Stable Diffusion 2.1")
174
+
175
+ with gr.Accordion(label="Init Images (Optional)", open=False, visible=False) as init_image_acc:
176
+ init_image = gr.Image(
177
+ label="Init Image (Optional). Leave blank to generate image with SD 2.1", type="pil")
178
+
179
+ with gr.Accordion(
180
+ label="Params: The generated QR Code functionality is largely influenced by the parameters detailed below",
181
+ open=True,
182
+ ):
183
+ controlnet_conditioning_scale = gr.Slider(
184
+ minimum=0.0,
185
+ maximum=5.0,
186
+ step=0.01,
187
+ value=1.1,
188
+ label="Controlnet Conditioning Scale",
189
+ )
190
+ strength = gr.Slider(
191
+ minimum=0.0, maximum=1.0, step=0.01, value=0.9, label="Strength"
192
+ )
193
+ guidance_scale = gr.Slider(
194
+ minimum=0.0,
195
+ maximum=50.0,
196
+ step=0.25,
197
+ value=7.5,
198
+ label="Guidance Scale",
199
+ )
200
+ sampler = gr.Dropdown(choices=list(
201
+ SAMPLER_MAP.keys()), value="DPM++ Karras SDE", label="Sampler")
202
+ seed = gr.Slider(
203
+ minimum=-1,
204
+ maximum=9999999999,
205
+ step=1,
206
+ value=2313123,
207
+ label="Seed",
208
+ randomize=True,
209
+ )
210
+ with gr.Row():
211
+ run_btn = gr.Button("开始生成")
212
+ with gr.Column():
213
+ result_image = gr.Image(label="生成结果")
214
+ run_btn.click(
215
+ inference,
216
+ inputs=[
217
+ qr_code_content,
218
+ prompt,
219
+ negative_prompt,
220
+ guidance_scale,
221
+ controlnet_conditioning_scale,
222
+ strength,
223
+ seed,
224
+ init_image,
225
+ qr_code_image,
226
+ use_qr_code_as_init_image,
227
+ sampler,
228
+ ],
229
+ outputs=[result_image],
230
+ )
231
+
232
+ gr.Examples(
233
+ examples=[
234
+ [
235
+ "https://huggingface.co/",
236
+ "A sky view of a colorful lakes and rivers flowing through the desert",
237
+ "ugly, disfigured, low quality, blurry, nsfw",
238
+ 7.5,
239
+ 1.3,
240
+ 0.9,
241
+ 5392011833,
242
+ None,
243
+ None,
244
+ True,
245
+ "DPM++ Karras SDE",
246
+ ],
247
+ [
248
+ "https://huggingface.co/",
249
+ "Bright sunshine coming through the cracks of a wet, cave wall of big rocks",
250
+ "ugly, disfigured, low quality, blurry, nsfw",
251
+ 7.5,
252
+ 1.11,
253
+ 0.9,
254
+ 2523992465,
255
+ None,
256
+ None,
257
+ True,
258
+ "DPM++ Karras SDE",
259
+ ],
260
+ [
261
+ "https://huggingface.co/",
262
+ "Sky view of highly aesthetic, ancient greek thermal baths in beautiful nature",
263
+ "ugly, disfigured, low quality, blurry, nsfw",
264
+ 7.5,
265
+ 1.5,
266
+ 0.9,
267
+ 2523992465,
268
+ None,
269
+ None,
270
+ True,
271
+ "DPM++ Karras SDE",
272
+ ],
273
+ ],
274
+ fn=inference,
275
+ inputs=[
276
+ qr_code_content,
277
+ prompt,
278
+ negative_prompt,
279
+ guidance_scale,
280
+ controlnet_conditioning_scale,
281
+ strength,
282
+ seed,
283
+ init_image,
284
+ qr_code_image,
285
+ use_qr_code_as_init_image,
286
+ sampler,
287
+ ],
288
+ outputs=[result_image],
289
+ cache_examples=True,
290
+ )
291
+
292
+ blocks.queue(concurrency_count=1, max_size=20)
293
+ blocks.launch(share=bool(os.environ.get("SHARE", False)))
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ diffusers
2
+ transformers
3
+ accelerate
4
+ torch
5
+ xformers
6
+ gradio
7
+ Pillow
8
+ qrcode