habulaj commited on
Commit
099787c
1 Parent(s): 57f42b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -276
app.py CHANGED
@@ -38,94 +38,31 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
38
  def generate(
39
  prompt: str,
40
  negative_prompt: str = "",
41
- prompt_2: str = "",
42
- negative_prompt_2: str = "",
43
  use_negative_prompt: bool = False,
44
- use_prompt_2: bool = False,
45
- use_negative_prompt_2: bool = False,
46
  seed: int = 0,
47
  width: int = 1024,
48
  height: int = 1024,
49
  guidance_scale_base: float = 5.0,
50
  num_inference_steps_base: int = 25,
51
- controlnet_conditioning_scale: float = 1,
52
- control_guidance_start: float = 0,
53
- control_guidance_end: float = 1,
54
- strength_img2img: float = 0.7,
55
  use_vae: bool = False,
56
  use_lora: bool = False,
57
  use_lora2: bool = False,
58
  model = 'stabilityai/stable-diffusion-xl-base-1.0',
59
- vaecall = 'madebyollin/sdxl-vae-fp16-fix',
60
  lora = '',
61
  lora2 = '',
62
- controlnet_model = 'diffusers/controlnet-canny-sdxl-1.0',
63
  lora_scale: float = 0.7,
64
  lora_scale2: float = 0.7,
65
  use_img2img: bool = False,
66
- use_controlnet: bool = False,
67
- use_controlnetimg2img: bool = False,
68
- controlnet_img = '',
69
- controlnet_img2img = '',
70
  ):
71
  if torch.cuda.is_available():
72
 
73
  if not use_img2img:
74
- scheduler = DPMSolverMultistepScheduler.from_pretrained(model, subfolder="scheduler")
75
- pipe = DiffusionPipeline.from_pretrained(model, scheduler=scheduler, torch_dtype=torch.float16)
76
-
77
- pipe.to(device)
78
 
79
  if use_vae:
80
- vae = AutoencoderKL.from_pretrained(vaecall, torch_dtype=torch.float16)
81
  pipe = DiffusionPipeline.from_pretrained(model, vae=vae, torch_dtype=torch.float16)
82
-
83
- pipe.to(device)
84
-
85
- if use_img2img:
86
- pipe = AutoPipelineForImage2Image.from_pretrained(model, torch_dtype=torch.float16)
87
-
88
- img = Image.open(url)
89
- img = img.resize((width,height))
90
- return img
91
-
92
- if use_vae:
93
- vae = AutoencoderKL.from_pretrained(vaecall, torch_dtype=torch.float16)
94
- pipe = AutoPipelineForImage2Image.from_pretrained(model, vae=vae, torch_dtype=torch.float16)
95
-
96
- if use_controlnet:
97
- controlnet = ControlNetModel.from_pretrained(controlnet_model, torch_dtype=torch.float16)
98
- pipe = StableDiffusionXLControlNetPipeline.from_pretrained(model, controlnet=controlnet, torch_dtype=torch.float16)
99
-
100
- image = load_image(controlnet_img)
101
-
102
- image = np.array(image)
103
- image = cv2.Canny(image, 100, 200)
104
- image = image[:, :, None]
105
- image = np.concatenate([image, image, image], axis=2)
106
- image = Image.fromarray(image)
107
-
108
- if use_vae:
109
- vae = AutoencoderKL.from_pretrained(vaecall, torch_dtype=torch.float16)
110
- pipe = StableDiffusionXLControlNetPipeline.from_pretrained(model, controlnet=controlnet, vae=vae, torch_dtype=torch.float16)
111
-
112
- if use_controlnetimg2img:
113
- controlnet = ControlNetModel.from_pretrained(controlnet_model, torch_dtype=torch.float16)
114
- pipe = StableDiffusionXLControlNetInpaintPipeline.from_pretrained(model, controlnet=controlnet, torch_dtype=torch.float16)
115
-
116
- image_start = load_image(controlnet_img)
117
- image = load_image(controlnet_img)
118
- image_mask = load_image(controlnet_img2img)
119
-
120
- image = np.array(image)
121
- image = cv2.Canny(image, 100, 200)
122
- image = image[:, :, None]
123
- image = np.concatenate([image, image, image], axis=2)
124
- image = Image.fromarray(image)
125
-
126
- if use_vae:
127
- vae = AutoencoderKL.from_pretrained(vaecall, torch_dtype=torch.float16)
128
- pipe = StableDiffusionXLControlNetInpaintPipeline.from_pretrained(model, controlnet=controlnet, vae=vae, torch_dtype=torch.float16)
129
 
130
  if use_lora:
131
  pipe.load_lora_weights(lora)
@@ -140,74 +77,19 @@ def generate(
140
 
141
  if not use_negative_prompt:
142
  negative_prompt = None # type: ignore
143
- if not use_prompt_2:
144
- prompt_2 = None # type: ignore
145
- if not use_negative_prompt_2:
146
- negative_prompt_2 = None # type: ignore
147
-
148
- if use_controlnetimg2img:
149
- image = pipe(
150
- prompt=prompt,
151
- strength=strength_img2img,
152
- controlnet_conditioning_scale=controlnet_conditioning_scale,
153
- eta=0.0,
154
- mask_image=image_mask,
155
- image=image_start,
156
- control_image=image,
157
- negative_prompt=negative_prompt,
158
- width=width,
159
- height=height,
160
- guidance_scale=guidance_scale_base,
161
- num_inference_steps=num_inference_steps_base,
162
- generator=generator,
163
- ).images[0]
164
- return image
165
- if use_controlnet:
166
- image = pipe(
167
- prompt=prompt,
168
- controlnet_conditioning_scale=controlnet_conditioning_scale,
169
- control_guidance_start=control_guidance_start,
170
- control_guidance_end=control_guidance_end,
171
- image=image,
172
- negative_prompt=negative_prompt,
173
- prompt_2=prompt_2,
174
- width=width,
175
- height=height,
176
- negative_prompt_2=negative_prompt_2,
177
- guidance_scale=guidance_scale_base,
178
- num_inference_steps=num_inference_steps_base,
179
- generator=generator,
180
- ).images[0]
181
- return image
182
- elif use_img2img:
183
- images = pipe(
184
- prompt=prompt,
185
- image=img,
186
- strength=strength_img2img,
187
- negative_prompt=negative_prompt,
188
- prompt_2=prompt_2,
189
- negative_prompt_2=negative_prompt_2,
190
- width=width,
191
- height=height,
192
- guidance_scale=guidance_scale_base,
193
- num_inference_steps=num_inference_steps_base,
194
- generator=generator,
195
- output_type="pil",
196
- ).images[0]
197
- return images
198
- else:
199
- return pipe(
200
- prompt=prompt,
201
- negative_prompt=negative_prompt,
202
- prompt_2=prompt_2,
203
- negative_prompt_2=negative_prompt_2,
204
- width=width,
205
- height=height,
206
- guidance_scale=guidance_scale_base,
207
- num_inference_steps=num_inference_steps_base,
208
- generator=generator,
209
- output_type="pil",
210
- ).images[0]
211
 
212
  theme = gr.themes.Monochrome(
213
  text_size=gr.themes.Size(lg="18px", md="15px", sm="13px", xl="22px", xs="12px", xxl="24px", xxs="9px"),
@@ -227,10 +109,22 @@ with gr.Blocks(theme=theme, css="style.css") as demo:
227
 
228
  with gr.Group():
229
  model = gr.Text(label='Model', value='stabilityai/stable-diffusion-xl-base-1.0', placeholder='e.g. stabilityai/stable-diffusion-xl-base-1.0', info='Enter the HUB path for this model! The model must be converted to diffusers to work. We recommend SG161222/RealVisXL_V4.0 for realistic generations, for example. By default, SDXL 1.0 (stabilityai/stable-diffusion-xl-base-1.0) will be used.')
230
- vaecall = gr.Text(label='VAE', placeholder='e.g. madebyollin/sdxl-vae-fp16-fix', info='You can use the VAE that suits you best! Using an unfamiliar VAE or one that is not from the same base model mentioned above will result in errors or distorted images. You must enable "Use VAE" in advanced settings if you want to use a VAE.')
231
- controlnet_model = gr.Text(label='Controlnet', placeholder='e.g diffusers/controlnet-canny-sdxl-1.0')
232
- controlnet_img = gr.Text(label='URL (Controlnet)', placeholder='e.g https://example.com/image.png')
233
- controlnet_img2img = gr.Text(label='URL (Controlnet - IMG2IMG)', placeholder='e.g https://example.com/image.png')
 
 
 
 
 
 
 
 
 
 
 
 
234
  num_inference_steps_base = gr.Slider(
235
  info="Number of denoising steps",
236
  label="Number of inference steps",
@@ -251,15 +145,10 @@ with gr.Blocks(theme=theme, css="style.css") as demo:
251
  result = gr.Image(label="Result", show_label=False)
252
  with gr.Accordion("Advanced options", open=False):
253
  with gr.Row():
254
- use_controlnet = gr.Checkbox(label='Use Controlnet', value=False)
255
- use_controlnetimg2img = gr.Checkbox(label='Use Controlnet Img2Img', value=False)
256
- use_img2img = gr.Checkbox(label='Use Img2Img', value=False)
257
  use_vae = gr.Checkbox(label='Use VAE', value=False)
258
  use_lora = gr.Checkbox(label='Use Lora 1', value=False)
259
  use_lora2 = gr.Checkbox(label='Use Lora 2', value=False)
260
  use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False)
261
- use_prompt_2 = gr.Checkbox(label="Use prompt 2", value=False)
262
- use_negative_prompt_2 = gr.Checkbox(label="Use negative prompt 2", value=False)
263
  with gr.Group():
264
  gr.Markdown("""### LoRAs
265
  <small>To activate the use of LoRAs, you should check only the 'Use 1 LoRA' checkbox if you want to use just one LoRA, and only the 'Use 2 LoRAs' checkbox if you want to use 2 LoRAs (i.e., blend styles)! It's important that all LoRAs are from the same base version of the selected model. For example, if the model is SDXL 1.0, the LoRA must also be.</small>"""
@@ -284,11 +173,6 @@ with gr.Blocks(theme=theme, css="style.css") as demo:
284
  value=0.7,
285
  visible=False,
286
  )
287
- with gr.Group():
288
- gr.Markdown("""### Img2Img
289
- <small>To activate the use of Img2Img and submit an image as a reference for generation, you must check the 'Use Img2Img' checkbox.</small>"""
290
- )
291
- url = gr.Image(label="Target Image", type="filepath")
292
  with gr.Group():
293
  negative_prompt = gr.Text(
294
  placeholder="Input Negative Prompt",
@@ -296,19 +180,6 @@ with gr.Blocks(theme=theme, css="style.css") as demo:
296
  max_lines=1,
297
  visible=False,
298
  )
299
- prompt_2 = gr.Text(
300
- placeholder="Input Prompt 2",
301
- label="Prompt 2",
302
- max_lines=1,
303
- visible=False,
304
- )
305
- negative_prompt_2 = gr.Text(
306
- placeholder="Input Negative Prompt 2",
307
- label="Negative prompt 2",
308
- max_lines=1,
309
- visible=False,
310
- )
311
-
312
  seed = gr.Slider(
313
  label="Seed",
314
  minimum=0,
@@ -317,23 +188,7 @@ with gr.Blocks(theme=theme, css="style.css") as demo:
317
  value=0,
318
  )
319
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
320
- with gr.Row():
321
- width = gr.Slider(
322
- label="Width",
323
- minimum=256,
324
- maximum=MAX_IMAGE_SIZE,
325
- step=32,
326
- value=1024,
327
- )
328
- height = gr.Slider(
329
- label="Height",
330
- minimum=256,
331
- maximum=MAX_IMAGE_SIZE,
332
- step=32,
333
- value=1024,
334
- )
335
-
336
- with gr.Row():
337
  guidance_scale_base = gr.Slider(
338
  info="Scale for classifier-free guidance",
339
  label="Guidance scale",
@@ -342,43 +197,7 @@ with gr.Blocks(theme=theme, css="style.css") as demo:
342
  step=0.1,
343
  value=5.0,
344
  )
345
- with gr.Row():
346
- controlnet_conditioning_scale = gr.Slider(
347
- info="controlnet_conditioning_scale",
348
- label="controlnet_conditioning_scale",
349
- minimum=0.01,
350
- maximum=2,
351
- step=0.01,
352
- value=1,
353
- )
354
- with gr.Row():
355
- control_guidance_start = gr.Slider(
356
- info="control_guidance_start",
357
- label="control_guidance_start",
358
- minimum=0.01,
359
- maximum=1,
360
- step=0.01,
361
- value=0,
362
- )
363
- with gr.Row():
364
- control_guidance_end = gr.Slider(
365
- info="control_guidance_end",
366
- label="control_guidance_end",
367
- minimum=0.01,
368
- maximum=1,
369
- step=0.01,
370
- value=1,
371
- )
372
- with gr.Row():
373
- strength_img2img = gr.Slider(
374
- info="Strength for Img2Img",
375
- label="Strength",
376
- minimum=0,
377
- maximum=1,
378
- step=0.01,
379
- value=0.7,
380
- )
381
-
382
  use_negative_prompt.change(
383
  fn=lambda x: gr.update(visible=x),
384
  inputs=use_negative_prompt,
@@ -386,59 +205,24 @@ with gr.Blocks(theme=theme, css="style.css") as demo:
386
  queue=False,
387
  api_name=False,
388
  )
389
- use_prompt_2.change(
390
- fn=lambda x: gr.update(visible=x),
391
- inputs=use_prompt_2,
392
- outputs=prompt_2,
393
- queue=False,
394
- api_name=False,
395
- )
396
- use_negative_prompt_2.change(
397
- fn=lambda x: gr.update(visible=x),
398
- inputs=use_negative_prompt_2,
399
- outputs=negative_prompt_2,
400
- queue=False,
401
- api_name=False,
402
- )
403
  use_vae.change(
404
  fn=lambda x: gr.update(visible=x),
405
  inputs=use_vae,
406
- outputs=vaecall,
407
  queue=False,
408
  api_name=False,
409
  )
410
  use_lora.change(
411
- fn=lambda x: [gr.update(visible=x), gr.update(visible=x)],
412
- inputs=use_lora,
413
- outputs=[lora, lora_scale],
414
- queue=False,
415
- api_name=False,
416
- )
417
- use_lora2.change(
418
- fn=lambda x: [gr.update(visible=x), gr.update(visible=x), gr.update(visible=x), gr.update(visible=x)],
419
- inputs=use_lora2,
420
- outputs=[lora, lora2, lora_scale, lora_scale2],
421
- queue=False,
422
- api_name=False,
423
- )
424
- use_img2img.change(
425
- fn=lambda x: gr.update(visible=x),
426
- inputs=use_img2img,
427
- outputs=url,
428
  queue=False,
429
  api_name=False,
430
  )
431
- use_controlnet.change(
432
- fn=lambda x: gr.update(visible=x),
433
- inputs=use_controlnet,
434
- outputs=controlnet_img,
435
- queue=False,
436
- api_name=False,
437
- )
438
- use_controlnetimg2img.change(
439
- fn=lambda x: gr.update(visible=x),
440
- inputs=use_controlnetimg2img,
441
- outputs=controlnet_img2img,
442
  queue=False,
443
  api_name=False,
444
  )
@@ -447,8 +231,6 @@ with gr.Blocks(theme=theme, css="style.css") as demo:
447
  triggers=[
448
  prompt.submit,
449
  negative_prompt.submit,
450
- prompt_2.submit,
451
- negative_prompt_2.submit,
452
  run_button.click,
453
  ],
454
  fn=randomize_seed_fn,
@@ -461,36 +243,21 @@ with gr.Blocks(theme=theme, css="style.css") as demo:
461
  inputs=[
462
  prompt,
463
  negative_prompt,
464
- prompt_2,
465
- negative_prompt_2,
466
  use_negative_prompt,
467
- use_prompt_2,
468
- use_negative_prompt_2,
469
  seed,
470
  width,
471
  height,
472
  guidance_scale_base,
473
  num_inference_steps_base,
474
- controlnet_conditioning_scale,
475
- control_guidance_start,
476
- control_guidance_end,
477
- strength_img2img,
478
  use_vae,
479
  use_lora,
480
  use_lora2,
481
  model,
482
- vaecall,
483
  lora,
484
  lora2,
485
- controlnet_model,
486
  lora_scale,
487
  lora_scale2,
488
- use_img2img,
489
- use_controlnet,
490
- use_controlnetimg2img,
491
- url,
492
- controlnet_img,
493
- controlnet_img2img,
494
  ],
495
  outputs=result,
496
  api_name="run",
 
38
  def generate(
39
  prompt: str,
40
  negative_prompt: str = "",
 
 
41
  use_negative_prompt: bool = False,
 
 
42
  seed: int = 0,
43
  width: int = 1024,
44
  height: int = 1024,
45
  guidance_scale_base: float = 5.0,
46
  num_inference_steps_base: int = 25,
 
 
 
 
47
  use_vae: bool = False,
48
  use_lora: bool = False,
49
  use_lora2: bool = False,
50
  model = 'stabilityai/stable-diffusion-xl-base-1.0',
51
+ vaemodel = 'madebyollin/sdxl-vae-fp16-fix',
52
  lora = '',
53
  lora2 = '',
 
54
  lora_scale: float = 0.7,
55
  lora_scale2: float = 0.7,
56
  use_img2img: bool = False,
 
 
 
 
57
  ):
58
  if torch.cuda.is_available():
59
 
60
  if not use_img2img:
61
+ pipe = DiffusionPipeline.from_pretrained(model, torch_dtype=torch.float16)
 
 
 
62
 
63
  if use_vae:
64
+ vae = AutoencoderKL.from_pretrained(vaemodel, torch_dtype=torch.float16)
65
  pipe = DiffusionPipeline.from_pretrained(model, vae=vae, torch_dtype=torch.float16)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  if use_lora:
68
  pipe.load_lora_weights(lora)
 
77
 
78
  if not use_negative_prompt:
79
  negative_prompt = None # type: ignore
80
+
81
+ return pipe(
82
+ prompt=prompt,
83
+ negative_prompt=negative_prompt,
84
+ prompt_2=prompt_2,
85
+ negative_prompt_2=negative_prompt_2,
86
+ width=width,
87
+ height=height,
88
+ guidance_scale=guidance_scale_base,
89
+ num_inference_steps=num_inference_steps_base,
90
+ generator=generator,
91
+ output_type="pil",
92
+ ).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
  theme = gr.themes.Monochrome(
95
  text_size=gr.themes.Size(lg="18px", md="15px", sm="13px", xl="22px", xs="12px", xxl="24px", xxs="9px"),
 
109
 
110
  with gr.Group():
111
  model = gr.Text(label='Model', value='stabilityai/stable-diffusion-xl-base-1.0', placeholder='e.g. stabilityai/stable-diffusion-xl-base-1.0', info='Enter the HUB path for this model! The model must be converted to diffusers to work. We recommend SG161222/RealVisXL_V4.0 for realistic generations, for example. By default, SDXL 1.0 (stabilityai/stable-diffusion-xl-base-1.0) will be used.')
112
+ vaemodel = gr.Text(label='VAE', placeholder='e.g. madebyollin/sdxl-vae-fp16-fix', info='You can use the VAE that suits you best! Using an unfamiliar VAE or one that is not from the same base model mentioned above will result in errors or distorted images. You must enable "Use VAE" in advanced settings if you want to use a VAE.')
113
+ with gr.Row():
114
+ width = gr.Slider(
115
+ label="Width",
116
+ minimum=256,
117
+ maximum=MAX_IMAGE_SIZE,
118
+ step=32,
119
+ value=1024,
120
+ )
121
+ height = gr.Slider(
122
+ label="Height",
123
+ minimum=256,
124
+ maximum=MAX_IMAGE_SIZE,
125
+ step=32,
126
+ value=1024,
127
+ )
128
  num_inference_steps_base = gr.Slider(
129
  info="Number of denoising steps",
130
  label="Number of inference steps",
 
145
  result = gr.Image(label="Result", show_label=False)
146
  with gr.Accordion("Advanced options", open=False):
147
  with gr.Row():
 
 
 
148
  use_vae = gr.Checkbox(label='Use VAE', value=False)
149
  use_lora = gr.Checkbox(label='Use Lora 1', value=False)
150
  use_lora2 = gr.Checkbox(label='Use Lora 2', value=False)
151
  use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False)
 
 
152
  with gr.Group():
153
  gr.Markdown("""### LoRAs
154
  <small>To activate the use of LoRAs, you should check only the 'Use 1 LoRA' checkbox if you want to use just one LoRA, and only the 'Use 2 LoRAs' checkbox if you want to use 2 LoRAs (i.e., blend styles)! It's important that all LoRAs are from the same base version of the selected model. For example, if the model is SDXL 1.0, the LoRA must also be.</small>"""
 
173
  value=0.7,
174
  visible=False,
175
  )
 
 
 
 
 
176
  with gr.Group():
177
  negative_prompt = gr.Text(
178
  placeholder="Input Negative Prompt",
 
180
  max_lines=1,
181
  visible=False,
182
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  seed = gr.Slider(
184
  label="Seed",
185
  minimum=0,
 
188
  value=0,
189
  )
190
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
191
+ with gr.Group():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  guidance_scale_base = gr.Slider(
193
  info="Scale for classifier-free guidance",
194
  label="Guidance scale",
 
197
  step=0.1,
198
  value=5.0,
199
  )
200
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  use_negative_prompt.change(
202
  fn=lambda x: gr.update(visible=x),
203
  inputs=use_negative_prompt,
 
205
  queue=False,
206
  api_name=False,
207
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  use_vae.change(
209
  fn=lambda x: gr.update(visible=x),
210
  inputs=use_vae,
211
+ outputs=vaemodel,
212
  queue=False,
213
  api_name=False,
214
  )
215
  use_lora.change(
216
+ fn=lambda x: [gr.update(visible=x), gr.update(visible=x)],
217
+ inputs=use_lora,
218
+ outputs=[lora, lora_scale],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
  queue=False,
220
  api_name=False,
221
  )
222
+ use_lora2.change(
223
+ fn=lambda x: [gr.update(visible=x), gr.update(visible=x), gr.update(visible=x), gr.update(visible=x)],
224
+ inputs=use_lora2,
225
+ outputs=[lora, lora2, lora_scale, lora_scale2],
 
 
 
 
 
 
 
226
  queue=False,
227
  api_name=False,
228
  )
 
231
  triggers=[
232
  prompt.submit,
233
  negative_prompt.submit,
 
 
234
  run_button.click,
235
  ],
236
  fn=randomize_seed_fn,
 
243
  inputs=[
244
  prompt,
245
  negative_prompt,
 
 
246
  use_negative_prompt,
 
 
247
  seed,
248
  width,
249
  height,
250
  guidance_scale_base,
251
  num_inference_steps_base,
 
 
 
 
252
  use_vae,
253
  use_lora,
254
  use_lora2,
255
  model,
256
+ vaemodel,
257
  lora,
258
  lora2,
 
259
  lora_scale,
260
  lora_scale2,
 
 
 
 
 
 
261
  ],
262
  outputs=result,
263
  api_name="run",