Manjushri commited on
Commit
53af09b
1 Parent(s): 3ddb322

Update app.py

Browse files

Added GPU or CPU functionality

Files changed (1) hide show
  1. app.py +5 -4
app.py CHANGED
@@ -3,15 +3,16 @@ from PIL import Image
3
  from io import BytesIO
4
  from diffusers import StableDiffusionUpscalePipeline
5
  import gradio as gr
6
- # load model and scheduler
7
  model_id = "stabilityai/stable-diffusion-x4-upscaler"
8
- pipeline = StableDiffusionUpscalePipeline.from_pretrained(model_id)
9
- pipeline = pipeline.to("cpu")
 
10
  #define interface
11
  def upscale(low_res_img, prompt):
12
  low_res_img = Image.open(low_res_img).convert("RGB")
13
  low_res_img = low_res_img.resize((128, 128))
14
- upscaled_image = pipeline(prompt=prompt, image=low_res_img, guidance_scale=1, num_inference_steps=50).images[0]
15
  upscaled_image.save("upsampled.png")
16
  return upscaled_image
17
  #launch interface
 
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):
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, image=low_res_img, guidance_scale=1, num_inference_steps=50).images[0]
16
  upscaled_image.save("upsampled.png")
17
  return upscaled_image
18
  #launch interface