radames commited on
Commit
f734c44
1 Parent(s): 5353a0b

user SD locally for seed consistency

Browse files
Files changed (1) hide show
  1. app.py +30 -15
app.py CHANGED
@@ -2,19 +2,17 @@ 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,
11
  DDIMScheduler,
 
12
  )
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,
@@ -39,6 +37,17 @@ pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
39
  pipe.enable_model_cpu_offload()
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
42
  def resize_for_condition_image(input_image: Image.Image, resolution: int):
43
  input_image = input_image.convert("RGB")
44
  W, H = input_image.size
@@ -69,12 +78,20 @@ def inference(
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")
@@ -94,7 +111,6 @@ def inference(
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(
100
  prompt=prompt,
@@ -120,8 +136,7 @@ with gr.Blocks() as blocks:
120
  model: https://huggingface.co/DionTimmer/controlnet_qrcode-control_v1p_sd15
121
 
122
  <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">
123
- <img style="margin-bottom: 0em;display: inline;margin-top: -.25em;" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
124
- for longer sequences, more control and no queue.</p>
125
  """
126
  )
127
 
@@ -151,19 +166,19 @@ for longer sequences, more control and no queue.</p>
151
  guidance_scale = gr.Slider(
152
  minimum=0.0,
153
  maximum=50.0,
154
- step=0.1,
155
  value=10.0,
156
  label="Guidance Scale",
157
  )
158
  controlnet_conditioning_scale = gr.Slider(
159
  minimum=0.0,
160
  maximum=5.0,
161
- step=0.1,
162
  value=2.0,
163
  label="Controlnet Conditioning Scale",
164
  )
165
  strength = gr.Slider(
166
- minimum=0.0, maximum=1.0, step=0.1, value=0.8, label="Strength"
167
  )
168
  seed = gr.Slider(
169
  minimum=-1,
@@ -220,7 +235,7 @@ for longer sequences, more control and no queue.</p>
220
  None,
221
  None,
222
  "https://huggingface.co/spaces/huggingface-projects/AI-QR-code-generator",
223
- "beautiful sunset in san francisco",
224
  "ugly, disfigured, low quality, blurry, nsfw",
225
  10.0,
226
  2.7,
 
2
  import gradio as gr
3
  from PIL import Image
4
  import qrcode
 
5
  from pathlib import Path
6
 
7
  from diffusers import (
8
+ StableDiffusionPipeline,
9
  StableDiffusionControlNetImg2ImgPipeline,
10
  ControlNetModel,
11
  DDIMScheduler,
12
+ DPMSolverMultistepScheduler,
13
  )
 
 
 
14
 
15
+ from PIL import Image
16
 
17
  qrcode_generator = qrcode.QRCode(
18
  version=1,
 
37
  pipe.enable_model_cpu_offload()
38
 
39
 
40
+ sd_pipe = StableDiffusionPipeline.from_pretrained(
41
+ "stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16
42
+ )
43
+ sd_pipe.scheduler = DPMSolverMultistepScheduler.from_config(sd_pipe.scheduler.config)
44
+ sd_pipe = sd_pipe.to("cuda")
45
+
46
+
47
+ sd_pipe.enable_xformers_memory_efficient_attention()
48
+ sd_pipe.enable_model_cpu_offload()
49
+
50
+
51
  def resize_for_condition_image(input_image: Image.Image, resolution: int):
52
  input_image = input_image.convert("RGB")
53
  W, H = input_image.size
 
78
  if qrcode_image is None and qr_code_content is None:
79
  raise gr.Error("QR Code Image or QR Code Content is required")
80
 
81
+ generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
82
+
83
  if init_image is None:
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
 
96
  if qr_code_content is not None or qr_code_content != "":
97
  print("Generating QR Code from content")
 
111
  qrcode_image = resize_for_condition_image(qrcode_image, 768)
112
 
113
  init_image = resize_for_condition_image(init_image, 768)
 
114
 
115
  out = pipe(
116
  prompt=prompt,
 
136
  model: https://huggingface.co/DionTimmer/controlnet_qrcode-control_v1p_sd15
137
 
138
  <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">
139
+ <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>
 
140
  """
141
  )
142
 
 
166
  guidance_scale = gr.Slider(
167
  minimum=0.0,
168
  maximum=50.0,
169
+ step=0.01,
170
  value=10.0,
171
  label="Guidance Scale",
172
  )
173
  controlnet_conditioning_scale = gr.Slider(
174
  minimum=0.0,
175
  maximum=5.0,
176
+ step=0.01,
177
  value=2.0,
178
  label="Controlnet Conditioning Scale",
179
  )
180
  strength = gr.Slider(
181
+ minimum=0.0, maximum=1.0, step=0.01, value=0.8, label="Strength"
182
  )
183
  seed = gr.Slider(
184
  minimum=-1,
 
235
  None,
236
  None,
237
  "https://huggingface.co/spaces/huggingface-projects/AI-QR-code-generator",
238
+ "beautiful sunset in San Francisco with Golden Gate bridge in the background",
239
  "ugly, disfigured, low quality, blurry, nsfw",
240
  10.0,
241
  2.7,