radames commited on
Commit
eebec00
1 Parent(s): 15417c3

generate qr codes and images on the fly

Browse files
Files changed (2) hide show
  1. app.py +102 -10
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,6 +1,10 @@
1
  import torch
2
  import gradio as gr
3
  from PIL import Image
 
 
 
 
4
  from diffusers import (
5
  StableDiffusionControlNetImg2ImgPipeline,
6
  ControlNetModel,
@@ -9,6 +13,16 @@ from diffusers import (
9
  from diffusers.utils import load_image
10
  from PIL import Image
11
 
 
 
 
 
 
 
 
 
 
 
12
  controlnet = ControlNetModel.from_pretrained(
13
  "DionTimmer/controlnet_qrcode-control_v1p_sd15", torch_dtype=torch.float16
14
  )
@@ -40,6 +54,7 @@ def resize_for_condition_image(input_image: Image.Image, resolution: int):
40
  def inference(
41
  init_image: Image.Image,
42
  qrcode_image: Image.Image,
 
43
  prompt: str,
44
  negative_prompt: str,
45
  guidance_scale: float = 10.0,
@@ -48,9 +63,37 @@ def inference(
48
  seed: int = -1,
49
  num_inference_steps: int = 30,
50
  ):
51
- init_image = resize_for_condition_image(init_image, 768)
52
- qrcode_image = resize_for_condition_image(qrcode_image, 768)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
 
54
  generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
55
 
56
  out = pipe(
@@ -58,15 +101,15 @@ def inference(
58
  negative_prompt=negative_prompt,
59
  image=init_image,
60
  control_image=qrcode_image, # type: ignore
61
- width=768,
62
- height=768,
63
  guidance_scale=float(guidance_scale),
64
- controlnet_conditioning_scale=float(controlnet_conditioning_scale),
65
  generator=generator,
66
  strength=float(strength),
67
  num_inference_steps=num_inference_steps,
68
  )
69
- return out.images[0]
70
 
71
 
72
  with gr.Blocks() as blocks:
@@ -79,13 +122,26 @@ with gr.Blocks() as blocks:
79
 
80
  with gr.Row():
81
  with gr.Column():
82
- init_image = gr.Image(label="Init Image", type="pil")
83
- qr_code_image = gr.Image(label="QR Code Image", type="pil")
84
- prompt = gr.Textbox(label="Prompt")
 
 
 
 
 
 
85
  negative_prompt = gr.Textbox(
86
  label="Negative Prompt",
87
  value="ugly, disfigured, low quality, blurry, nsfw",
88
  )
 
 
 
 
 
 
 
89
  with gr.Accordion(label="Params"):
90
  guidance_scale = gr.Slider(
91
  minimum=0.0,
@@ -120,6 +176,7 @@ with gr.Blocks() as blocks:
120
  inputs=[
121
  init_image,
122
  qr_code_image,
 
123
  prompt,
124
  negative_prompt,
125
  guidance_scale,
@@ -135,18 +192,53 @@ with gr.Blocks() as blocks:
135
  [
136
  "./examples/init.jpeg",
137
  "./examples/qrcode.png",
 
138
  "crisp QR code prominently displayed on a billboard amidst the bustling skyline of New York City, with iconic landmarks subtly featured in the background.",
139
  "ugly, disfigured, low quality, blurry, nsfw",
140
  10.0,
141
  2.0,
142
  0.8,
143
  2313123,
144
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  ],
146
  fn=inference,
147
  inputs=[
148
  init_image,
149
  qr_code_image,
 
150
  prompt,
151
  negative_prompt,
152
  guidance_scale,
 
1
  import torch
2
  import gradio as gr
3
  from PIL import Image
4
+ import qrcode
5
+ from gradio_client import Client
6
+ from pathlib import Path
7
+
8
  from diffusers import (
9
  StableDiffusionControlNetImg2ImgPipeline,
10
  ControlNetModel,
 
13
  from diffusers.utils import load_image
14
  from PIL import Image
15
 
16
+
17
+ sd_client = Client("stabilityai/stable-diffusion")
18
+
19
+ qrcode_generator = qrcode.QRCode(
20
+ version=1,
21
+ error_correction=qrcode.constants.ERROR_CORRECT_H,
22
+ box_size=10,
23
+ border=0,
24
+ )
25
+
26
  controlnet = ControlNetModel.from_pretrained(
27
  "DionTimmer/controlnet_qrcode-control_v1p_sd15", torch_dtype=torch.float16
28
  )
 
54
  def inference(
55
  init_image: Image.Image,
56
  qrcode_image: Image.Image,
57
+ qr_code_content: str,
58
  prompt: str,
59
  negative_prompt: str,
60
  guidance_scale: float = 10.0,
 
63
  seed: int = -1,
64
  num_inference_steps: int = 30,
65
  ):
66
+ if prompt is None or prompt == "":
67
+ raise gr.Error("Prompt is required")
68
+
69
+ if qrcode_image is None and qr_code_content is None:
70
+ raise gr.Error("QR Code Image or QR Code Content is required")
71
+
72
+ if init_image is None:
73
+ print("Generating random image from prompt using Stable Diffusion")
74
+ # generate image from prompt
75
+ img_dir = sd_client.predict(prompt, negative_prompt, 7, fn_index=1)
76
+ images = Path(img_dir).rglob("*.jpg")
77
+ init_image = Image.open(next(images))
78
+
79
+ if qr_code_content is not None or qr_code_content != "":
80
+ print("Generating QR Code from content")
81
+ qr = qrcode.QRCode(
82
+ version=1,
83
+ error_correction=qrcode.constants.ERROR_CORRECT_H,
84
+ box_size=10,
85
+ border=4,
86
+ )
87
+ qr.add_data(qr_code_content)
88
+ qr.make(fit=True)
89
+
90
+ qrcode_image = qr.make_image(fill_color="black", back_color="white")
91
+ qrcode_image = resize_for_condition_image(qrcode_image, 768)
92
+ else:
93
+ print("Using QR Code Image")
94
+ qrcode_image = resize_for_condition_image(qrcode_image, 768)
95
 
96
+ init_image = resize_for_condition_image(init_image, 768)
97
  generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
98
 
99
  out = pipe(
 
101
  negative_prompt=negative_prompt,
102
  image=init_image,
103
  control_image=qrcode_image, # type: ignore
104
+ width=768, # type: ignore
105
+ height=768, # type: ignore
106
  guidance_scale=float(guidance_scale),
107
+ controlnet_conditioning_scale=float(controlnet_conditioning_scale), # type: ignore
108
  generator=generator,
109
  strength=float(strength),
110
  num_inference_steps=num_inference_steps,
111
  )
112
+ return out.images[0] # type: ignore
113
 
114
 
115
  with gr.Blocks() as blocks:
 
122
 
123
  with gr.Row():
124
  with gr.Column():
125
+ qr_code_content = gr.Textbox(
126
+ label="QR Code Content",
127
+ info="QR Code Content or URL",
128
+ value="",
129
+ )
130
+ prompt = gr.Textbox(
131
+ label="Prompt",
132
+ info="Prompt is required. If init image is not provided, then it will be generated from prompt using Stable Diffusion 2.1",
133
+ )
134
  negative_prompt = gr.Textbox(
135
  label="Negative Prompt",
136
  value="ugly, disfigured, low quality, blurry, nsfw",
137
  )
138
+ init_image = gr.Image(label="Init Image (Optional)", type="pil")
139
+
140
+ qr_code_image = gr.Image(
141
+ label="QR Code Image (Optional)",
142
+ type="pil",
143
+ )
144
+
145
  with gr.Accordion(label="Params"):
146
  guidance_scale = gr.Slider(
147
  minimum=0.0,
 
176
  inputs=[
177
  init_image,
178
  qr_code_image,
179
+ qr_code_content,
180
  prompt,
181
  negative_prompt,
182
  guidance_scale,
 
192
  [
193
  "./examples/init.jpeg",
194
  "./examples/qrcode.png",
195
+ "",
196
  "crisp QR code prominently displayed on a billboard amidst the bustling skyline of New York City, with iconic landmarks subtly featured in the background.",
197
  "ugly, disfigured, low quality, blurry, nsfw",
198
  10.0,
199
  2.0,
200
  0.8,
201
  2313123,
202
+ ],
203
+ [
204
+ "./examples/init.jpeg",
205
+ None,
206
+ "https://huggingface.co",
207
+ "crisp QR code prominently displayed on a billboard amidst the bustling skyline of New York City, with iconic landmarks subtly featured in the background.",
208
+ "ugly, disfigured, low quality, blurry, nsfw",
209
+ 10.0,
210
+ 2.0,
211
+ 0.8,
212
+ 2313123,
213
+ ],
214
+ [
215
+ None,
216
+ None,
217
+ "https://huggingface.co",
218
+ "crisp QR code prominently displayed on a 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
+ 10.0,
221
+ 2.0,
222
+ 0.8,
223
+ 2313123,
224
+ ],
225
+ [
226
+ None,
227
+ None,
228
+ "https://huggingface.co",
229
+ "A flying cat over a jungle",
230
+ "ugly, disfigured, low quality, blurry, nsfw",
231
+ 10.0,
232
+ 2.0,
233
+ 0.8,
234
+ 2313123,
235
+ ],
236
  ],
237
  fn=inference,
238
  inputs=[
239
  init_image,
240
  qr_code_image,
241
+ qr_code_content,
242
  prompt,
243
  negative_prompt,
244
  guidance_scale,
requirements.txt CHANGED
@@ -4,4 +4,5 @@ accelerate
4
  torch
5
  xformers
6
  gradio
7
- Pillow
 
 
4
  torch
5
  xformers
6
  gradio
7
+ Pillow
8
+ qrcode