Manjushri commited on
Commit
31089e0
1 Parent(s): f778c96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -11
app.py CHANGED
@@ -1,19 +1,41 @@
1
  import torch #needed only for GPU
2
  from PIL import Image
3
  from io import BytesIO
4
- from diffusers import StableDiffusionUpscalePipeline
 
5
  import gradio as gr
 
 
6
  # load model for CPU or GPU
7
- model_id = "stabilityai/stable-diffusion-x4-upscaler"
 
 
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
- pipe = StableDiffusionUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16, revision="fp16") if torch.cuda.is_available() else StableDiffusionUpscalePipeline.from_pretrained(model_id)
10
- pipe = pipe.to(device)
 
11
  #define interface
12
- def upscale(low_res_img, prompt, negative_prompt, scale, steps):
13
- low_res_img = Image.open(low_res_img).convert("RGB")
14
- low_res_img = low_res_img.resize((128, 128))
15
- upscaled_image = pipe(prompt=prompt, negative_prompt=negative_prompt, image=low_res_img, guidance_scale=scale, num_inference_steps=steps).images[0]
16
- #upscaled_image.save("upsampled.png")
17
- return upscaled_image
 
 
 
 
 
18
  #launch interface
19
- gr.Interface(fn=upscale, inputs=[gr.Image(type='filepath', label='Low Resolution Image (less than 512x512, i.e. 128x128, 256x256, ect., ect..)'), gr.Textbox(label='Optional: Enter a Prompt to Slightly Guide the AI'), gr.Textbox(label='Experimental: Slightly influence What you do not want the AI to generate.'), gr.Slider(2, 15, 7, step=1, label='Guidance Scale: How much the AI influences the Upscaling.'), gr.Slider(10, 75, 50, step=1, label='Number of Iterations')], outputs=gr.Image(type='filepath'), title='SD 2.0 4x Upscaler', description='A 4x Low Resolution Upscaler using SD 2.0. <br>Expects a Lower than 512x512 image. <br><br><b>Warning: Images 512x512 or Higher Resolution WILL NOT BE UPSCALED and may result in Quality Loss!', article = "Code Monkey: <a href=\"https://huggingface.co/Manjushri\">Manjushri</a>").launch(max_threads=True, debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
1
  import torch #needed only for GPU
2
  from PIL import Image
3
  from io import BytesIO
4
+ import numpy as np
5
+ from diffusers import StableDiffusionLatentUpscalePipeline, StableDiffusionUpscalePipeline
6
  import gradio as gr
7
+ import modin.pandas as pd
8
+
9
  # load model for CPU or GPU
10
+
11
+ model_2x = "stabilityai/sd-x2-latent-upscaler"
12
+ model_4x = "stabilityai/stable-diffusion-x4-upscaler"
13
  device = "cuda" if torch.cuda.is_available() else "cpu"
14
+ upscaler2x = StableDiffusionLatentUpscalePipeline.from_pretrained(model_2x, torch_dtype=torch.float16, revision="fp16") if torch.cuda.is_available() else StableDiffusionLatentUpscalePipeline.from_pretrained(model_2x, safety_checker=None)
15
+ upscaler4x = StableDiffusionUpscalePipeline.from_pretrained(model_4x, torch_dtype=torch.float16, revision="fp16") if torch.cuda.is_available() else StableDiffusionUpscalePipeline.from_pretrained(model_4x)
16
+
17
  #define interface
18
+
19
+ def upscale(raw_img, model, prompt, negative_prompt, scale, steps):
20
+ generator = torch.manual_seed(999999)
21
+ low_res_img = Image.open(raw_img).convert("RGB")
22
+ if model == "Upscaler 4x":
23
+ low_res_img = low_res_img.resize((128, 128))
24
+ else:
25
+ low_res_img
26
+ image = upscaler2x(prompt=prompt, negative_prompt=negative_prompt, image=low_res_img, guidance_scale=scale, num_inference_steps=steps).images[0] if model == "Upscaler 2x" else upscaler4x(prompt=prompt, negative_prompt=negative_prompt, image=low_res_img, guidance_scale=scale, num_inference_steps=steps).images[0]
27
+ return image
28
+
29
  #launch interface
30
+
31
+ gr.Interface(fn=upscale, inputs=[
32
+ gr.Image(type="filepath", label='Lower Resolution Image'),
33
+ gr.Radio(['Upscaler 2x','Upscaler 4x'], label="Models"),
34
+ gr.Textbox(label="Optional: Enter a Prompt to Slightly Guide the AI's Enhancement"),
35
+ gr.Textbox(label='Experimental: Slightly influence What you do not want the AI to Enhance.'),
36
+ gr.Slider(2, 15, 7, step=1, label='Guidance Scale: How much the AI influences the Upscaling.'),
37
+ gr.Slider(5, 25, 10, step=1, label='Number of Iterations')],
38
+ outputs=gr.Image(type="filepath", label = 'Upscaled Image'),
39
+ title='SD Upscaler',
40
+ description='2x Latent Upscaler using SD 2.0 And 4x Upscaler using SD 2.1. This version runs on CPU or GPU and is currently running on a T4 GPU. For 4x Upscaling use images lower than 512x512. For 2x Upscaling use 512x512 to 768x768 images.<br><br><b>Notice: Largest Accepted Resolution is 768x768',
41
+ article = "Code Monkey: <a href=\"https://huggingface.co/Manjushri\">Manjushri</a>").launch(max_threads=True, debug=True)