Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,26 +4,30 @@ import torch
|
|
4 |
from diffusers import StableDiffusionUpscalePipeline
|
5 |
|
6 |
# 모델 로드
|
7 |
-
pipe = StableDiffusionUpscalePipeline.from_pretrained("stabilityai/stable-diffusion-x4-upscaler")
|
8 |
|
9 |
# GPU가 사용 가능한지 확인한 후, 가능하면 GPU로 설정하고, 그렇지 않으면 CPU로 설정
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
-
pipe = pipe.to(device)
|
12 |
|
13 |
def upscale_image(image, scale_factor):
|
|
|
|
|
|
|
14 |
if scale_factor == 2:
|
15 |
# 2배 업스케일
|
16 |
-
low_res_image =
|
17 |
elif scale_factor == 4:
|
18 |
# 4배 업스케일
|
19 |
-
low_res_image =
|
20 |
else:
|
21 |
raise ValueError("지원하지 않는 배율입니다.")
|
22 |
|
23 |
-
#
|
24 |
prompt = "high quality, detailed"
|
25 |
with torch.no_grad(): # 메모리 최적화를 위해 no_grad 사용
|
26 |
upscaled_image = pipe(prompt=prompt, image=low_res_image).images[0]
|
|
|
27 |
return upscaled_image
|
28 |
|
29 |
# Gradio 인터페이스 설정
|
|
|
4 |
from diffusers import StableDiffusionUpscalePipeline
|
5 |
|
6 |
# 모델 로드
|
7 |
+
pipe = StableDiffusionUpscalePipeline.from_pretrained("stabilityai/stable-diffusion-x4-upscaler", torch_dtype=torch.float16)
|
8 |
|
9 |
# GPU가 사용 가능한지 확인한 후, 가능하면 GPU로 설정하고, 그렇지 않으면 CPU로 설정
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
pipe = pipe.to(device)
|
12 |
|
13 |
def upscale_image(image, scale_factor):
|
14 |
+
# 입력 이미지 크기 축소 (예: 가로/세로 각각 절반 크기)
|
15 |
+
reduced_image = image.resize((image.width // 2, image.height // 2), Image.BICUBIC)
|
16 |
+
|
17 |
if scale_factor == 2:
|
18 |
# 2배 업스케일
|
19 |
+
low_res_image = reduced_image.resize((reduced_image.width // 2, reduced_image.height // 2), Image.BICUBIC)
|
20 |
elif scale_factor == 4:
|
21 |
# 4배 업스케일
|
22 |
+
low_res_image = reduced_image.resize((reduced_image.width // 4, reduced_image.height // 4), Image.BICUBIC)
|
23 |
else:
|
24 |
raise ValueError("지원하지 않는 배율입니다.")
|
25 |
|
26 |
+
# 프롬프트 설정 및 업스케일링 수행
|
27 |
prompt = "high quality, detailed"
|
28 |
with torch.no_grad(): # 메모리 최적화를 위해 no_grad 사용
|
29 |
upscaled_image = pipe(prompt=prompt, image=low_res_image).images[0]
|
30 |
+
|
31 |
return upscaled_image
|
32 |
|
33 |
# Gradio 인터페이스 설정
|