ygtrfed commited on
Commit
1a1807c
1 Parent(s): ea23183

added upsacle function to upscale outputs automatically

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from diffusers import AutoencoderKL, UNet2DConditionModel, StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DPMSolverMultistepScheduler
2
  import gradio as gr
3
  import torch
4
  from PIL import Image
@@ -14,6 +14,12 @@ is_colab = utils.is_google_colab()
14
  state = None
15
  current_steps = 25
16
 
 
 
 
 
 
 
17
  class Model:
18
  def __init__(self, name, path="", prefix=""):
19
  self.name = name
@@ -119,7 +125,6 @@ models = [
119
  Model("Funko-Diffusion", "prompthero/funko-diffusion", "funko style"),
120
  Model("DreamShaper", "Lykon/DreamShaper", "dreamshaper"),
121
  Model("Realistic_Vision_V1.4", "SG161222/Realistic_Vision_V1.4", ""),
122
- Model("upscalerx4","stabilityai/stable-diffusion-x4-upscaler","")
123
 
124
 
125
 
@@ -260,7 +265,7 @@ def txt_to_img(model_path, prompt, n_images, neg_prompt, guidance, steps, width,
260
 
261
  # update_state(f"Done. Seed: {seed}")
262
 
263
- return replace_nsfw_images(result)
264
 
265
  def img_to_img(model_path, prompt, n_images, neg_prompt, img, strength, guidance, steps, width, height, generator, seed):
266
 
@@ -313,7 +318,7 @@ def img_to_img(model_path, prompt, n_images, neg_prompt, img, strength, guidance
313
 
314
  # update_state(f"Done. Seed: {seed}")
315
 
316
- return replace_nsfw_images(result)
317
 
318
  def replace_nsfw_images(results):
319
 
@@ -325,6 +330,11 @@ def replace_nsfw_images(results):
325
  results.images[i] = Image.open("nsfw.png")
326
  return results.images
327
 
 
 
 
 
 
328
  # css = """.finetuned-diffusion-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.finetuned-diffusion-div div h1{font-weight:900;margin-bottom:7px}.finetuned-diffusion-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem}
329
  # """
330
  with gr.Blocks(css="style.css") as demo:
 
1
+ from diffusers import AutoencoderKL, UNet2DConditionModel, StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DPMSolverMultistepScheduler, DiffusionPipeline
2
  import gradio as gr
3
  import torch
4
  from PIL import Image
 
14
  state = None
15
  current_steps = 25
16
 
17
+ UPSCALER = DiffusionPipeline.from_pretrained("stabilityai/sd-x2-latent-upscaler", torch_dtype=torch.float16)
18
+ if torch.cuda.is_available():
19
+ UPSCALER.to("cuda")
20
+ UPSCALER.enable_xformers_memory_efficient_attention()
21
+
22
+
23
  class Model:
24
  def __init__(self, name, path="", prefix=""):
25
  self.name = name
 
125
  Model("Funko-Diffusion", "prompthero/funko-diffusion", "funko style"),
126
  Model("DreamShaper", "Lykon/DreamShaper", "dreamshaper"),
127
  Model("Realistic_Vision_V1.4", "SG161222/Realistic_Vision_V1.4", ""),
 
128
 
129
 
130
 
 
265
 
266
  # update_state(f"Done. Seed: {seed}")
267
 
268
+ return replace_nsfw_images(upscale(result, prompt, neg_prompt, generator))
269
 
270
  def img_to_img(model_path, prompt, n_images, neg_prompt, img, strength, guidance, steps, width, height, generator, seed):
271
 
 
318
 
319
  # update_state(f"Done. Seed: {seed}")
320
 
321
+ return replace_nsfw_images(upscale(result, prompt, neg_prompt, generator))
322
 
323
  def replace_nsfw_images(results):
324
 
 
330
  results.images[i] = Image.open("nsfw.png")
331
  return results.images
332
 
333
+ def upscale(results, prompt, neg_prompt, generator):
334
+ for i in range(len(results.images)):
335
+ results.images.append(UPSCALER(prompt=prompt, negative_prompt=neg_prompt, image=results.images[i], num_inference_steps=20, guidance_scale=0, generator=generator).images[0])
336
+ return results
337
+
338
  # css = """.finetuned-diffusion-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.finetuned-diffusion-div div h1{font-weight:900;margin-bottom:7px}.finetuned-diffusion-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem}
339
  # """
340
  with gr.Blocks(css="style.css") as demo: