Spaces:
Running
on
L4
Running
on
L4
Update app.py
Browse filesAdded GPU or CPU functionality
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
|
7 |
model_id = "stabilityai/stable-diffusion-x4-upscaler"
|
8 |
-
|
9 |
-
|
|
|
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 =
|
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
|