Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
from diffusers import StableDiffusionUpscalePipeline
|
5 |
+
|
6 |
+
# 모델 로드
|
7 |
+
pipe = StableDiffusionUpscalePipeline.from_pretrained("stabilityai/stable-diffusion-x4-upscaler", torch_dtype=torch.float16)
|
8 |
+
pipe = pipe.to("cuda") # GPU 사용
|
9 |
+
|
10 |
+
def upscale_image(image, scale_factor):
|
11 |
+
if scale_factor == 2:
|
12 |
+
# 2배 업스케일
|
13 |
+
low_res_image = image.resize((image.width // 2, image.height // 2), Image.BICUBIC)
|
14 |
+
elif scale_factor == 4:
|
15 |
+
# 4배 업스케일
|
16 |
+
low_res_image = image.resize((image.width // 4, image.height // 4), Image.BICUBIC)
|
17 |
+
else:
|
18 |
+
raise ValueError("지원하지 않는 배율입니다.")
|
19 |
+
|
20 |
+
upscaled_image = pipe(image=low_res_image).images[0]
|
21 |
+
return upscaled_image
|
22 |
+
|
23 |
+
# Gradio 인터페이스 설정
|
24 |
+
def main():
|
25 |
+
with gr.Blocks() as demo:
|
26 |
+
gr.Markdown("# 이미지 업스케일링 웹앱")
|
27 |
+
|
28 |
+
# 이미지 업로드
|
29 |
+
with gr.Row():
|
30 |
+
image_input = gr.Image(type="pil", label="이미지 업로드")
|
31 |
+
|
32 |
+
# 업스케일 배수 선택
|
33 |
+
with gr.Row():
|
34 |
+
scale_2x_btn = gr.Button("2배 업스케일")
|
35 |
+
scale_4x_btn = gr.Button("4배 업스케일")
|
36 |
+
|
37 |
+
# 결과 이미지 출력
|
38 |
+
output_image = gr.Image(type="pil", label="업스케일링된 이미지")
|
39 |
+
|
40 |
+
# 버튼 클릭 시 업스케일링 함수 연결
|
41 |
+
scale_2x_btn.click(fn=lambda img: upscale_image(img, 2), inputs=image_input, outputs=output_image)
|
42 |
+
scale_4x_btn.click(fn=lambda img: upscale_image(img, 4), inputs=image_input, outputs=output_image)
|
43 |
+
|
44 |
+
demo.launch()
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
main()
|