Kims12 commited on
Commit
2bf72a1
ยท
verified ยท
1 Parent(s): 60d6181

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -115
app.py CHANGED
@@ -1,103 +1,3 @@
1
- import os
2
- import sys
3
- from torchvision.transforms import functional
4
- sys.modules["torchvision.transforms.functional_tensor"] = functional
5
-
6
- from basicsr.archs.srvgg_arch import SRVGGNetCompact
7
- from gfpgan.utils import GFPGANer
8
- from realesrgan.utils import RealESRGANer
9
-
10
- import torch
11
- import cv2
12
- import gradio as gr
13
-
14
- # ํ•„์š”ํ•œ ๋ชจ๋ธ ๋‹ค์šด๋กœ๋“œ
15
- def download_upscaler_models():
16
- if not os.path.exists('realesr-general-x4v3.pth'):
17
- os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
18
- if not os.path.exists('GFPGANv1.4.pth'):
19
- os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
20
-
21
- # ์—…์Šค์ผ€์ผ๋Ÿฌ ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
22
- def init_upscaler_model():
23
- model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
24
- model_path = 'realesr-general-x4v3.pth'
25
- half = True if torch.cuda.is_available() else False
26
- upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
27
-
28
- return upsampler
29
-
30
- # ์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ๋ง ํ•จ์ˆ˜
31
- def upscale_image(img):
32
- try:
33
- upsampler = init_upscaler_model()
34
-
35
- # ์ด๋ฏธ์ง€ ํŒŒ์ผ ์ฝ๊ธฐ
36
- img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
37
- if len(img.shape) == 3 and img.shape[2] == 4:
38
- img_mode = 'RGBA'
39
- elif len(img.shape) == 2:
40
- img_mode = None
41
- img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
42
- else:
43
- img_mode = None
44
-
45
- # ์ž‘์€ ์ด๋ฏธ์ง€ ํฌ๊ธฐ ์กฐ์ •
46
- h, w = img.shape[0:2]
47
- if h < 300:
48
- img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
49
-
50
- # ๊ณ ์ •๋œ GFPGANv1.4 ๋ชจ๋ธ ์‚ฌ์šฉ
51
- face_enhancer = GFPGANer(
52
- model_path='GFPGANv1.4.pth',
53
- upscale=2,
54
- arch='clean',
55
- channel_multiplier=2,
56
- bg_upsampler=upsampler
57
- )
58
-
59
- # ์ด๋ฏธ์ง€ ํ–ฅ์ƒ
60
- try:
61
- _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
62
- except RuntimeError as error:
63
- print('์˜ค๋ฅ˜', error)
64
- return None
65
-
66
- # RGB๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
67
- output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
68
- return output
69
- except Exception as error:
70
- print('๊ธ€๋กœ๋ฒŒ ์˜ˆ์™ธ', error)
71
- return None
72
-
73
- # ์—…์Šค์ผ€์ผ๋Ÿฌ ํƒญ UI ์ƒ์„ฑ ํ•จ์ˆ˜
74
- def create_upscaler_tab():
75
- # ํ•„์š”ํ•œ ๋ชจ๋ธ ๋‹ค์šด๋กœ๋“œ
76
- download_upscaler_models()
77
-
78
- with gr.Tab("์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ๋Ÿฌ"):
79
- gr.Markdown(
80
- """
81
- # ์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ๋Ÿฌ
82
-
83
- ์—…๋กœ๋“œํ•œ ์ด๋ฏธ์ง€๋ฅผ ๊ณ ํ•ด์ƒ๋„๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ์ธ๋ฌผ ์‚ฌ์ง„์˜ ์–ผ๊ตด ๋””ํ…Œ์ผ์„ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.
84
- """
85
- )
86
-
87
- with gr.Row():
88
- with gr.Column():
89
- input_image = gr.Image(type="filepath", label="์ž…๋ ฅ ์ด๋ฏธ์ง€")
90
- upscale_btn = gr.Button("์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ", variant="primary")
91
-
92
- with gr.Column():
93
- output_image = gr.Image(type="numpy", label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง€")
94
-
95
- upscale_btn.click(
96
- fn=upscale_image,
97
- inputs=[input_image],
98
- outputs=[output_image]
99
- )
100
-
101
  import os
102
  import tempfile
103
  from PIL import Image
@@ -130,6 +30,13 @@ load_dotenv()
130
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
131
  logger = logging.getLogger(__name__)
132
 
 
 
 
 
 
 
 
133
  def save_binary_file(file_name, data):
134
  with open(file_name, "wb") as f:
135
  f.write(data)
@@ -411,13 +318,7 @@ def generate_multiple_images(image1, image2, image3, prompt, progress=gr.Progres
411
 
412
  return results[0], results[1], results[2], results[3], combined_status, combined_prompts
413
 
414
- # ์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ๋Ÿฌ ๊ธฐ๋Šฅ
415
- def download_upscaler_models():
416
- if not os.path.exists('realesr-general-x4v3.pth'):
417
- os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
418
- if not os.path.exists('GFPGANv1.4.pth'):
419
- os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
420
-
421
  def upscale_image(img):
422
  try:
423
  # ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
@@ -464,6 +365,9 @@ def upscale_image(img):
464
  print('๊ธ€๋กœ๋ฒŒ ์˜ˆ์™ธ', error)
465
  return None
466
 
 
 
 
467
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค
468
  with gr.Blocks() as demo:
469
  gr.HTML(
@@ -475,7 +379,7 @@ with gr.Blocks() as demo:
475
  """
476
  )
477
 
478
- with gr.Tabs():
479
  # ์ฒซ ๋ฒˆ์งธ ํƒญ: ์ด๋ฏธ์ง€ ์ƒ์„ฑ๊ธฐ
480
  with gr.Tab("์ด๋ฏธ์ง€ ์ƒ์„ฑ๊ธฐ"):
481
  with gr.Row():
@@ -532,8 +436,7 @@ with gr.Blocks() as demo:
532
 
533
  gr.Examples(
534
  examples=examples,
535
- inputs=[image1_input, image2_input, image3_input, prompt_input],
536
- elem_id="examples-grid"
537
  )
538
 
539
  # ์˜ค๋ฅธ์ชฝ ์ถœ๋ ฅ ์˜์—ญ
@@ -571,7 +474,7 @@ with gr.Blocks() as demo:
571
  outputs=prompt_input
572
  )
573
  clothes_change_btn1.click(
574
- fn=lambda: "(#1์˜ ์—ฌ์„ฑ๋ชจ๊ฒ”)์ด ์‹ ์ฒด ๋น„์œจ๊ณผ ํฌ์ฆˆ๋Š” ์œ ์น˜ํ•œ ์ฒด (#2์˜ ์„ ๊ธ€๋ผ์Šค)์™€ (#3์˜ ์ฒญ๋ฐ”์ง€)๋ฅผ ์ง์ ‘ ๋ชจ๋ธ์ด ์ฐฉ์šฉํ•œ๊ฒƒ ์ฒ˜๋Ÿผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜๋ผ.",
575
  inputs=[],
576
  outputs=prompt_input
577
  )
@@ -604,9 +507,6 @@ with gr.Blocks() as demo:
604
 
605
  # ๋‘ ๋ฒˆ์งธ ํƒญ: ์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ๋Ÿฌ
606
  with gr.Tab("์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ๋Ÿฌ"):
607
- # ํ•„์š”ํ•œ ๋ชจ๋ธ ๋‹ค์šด๋กœ๋“œ ํ˜ธ์ถœ
608
- download_upscaler_models()
609
-
610
  gr.Markdown(
611
  """
612
  # ์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ๋Ÿฌ
@@ -630,4 +530,4 @@ with gr.Blocks() as demo:
630
  )
631
 
632
  if __name__ == "__main__":
633
- demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import tempfile
3
  from PIL import Image
 
30
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
31
  logger = logging.getLogger(__name__)
32
 
33
+ # ํ•„์š”ํ•œ ๋ชจ๋ธ ๋‹ค์šด๋กœ๋“œ
34
+ def download_upscaler_models():
35
+ if not os.path.exists('realesr-general-x4v3.pth'):
36
+ os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
37
+ if not os.path.exists('GFPGANv1.4.pth'):
38
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
39
+
40
  def save_binary_file(file_name, data):
41
  with open(file_name, "wb") as f:
42
  f.write(data)
 
318
 
319
  return results[0], results[1], results[2], results[3], combined_status, combined_prompts
320
 
321
+ # ์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ ํ•จ์ˆ˜
 
 
 
 
 
 
322
  def upscale_image(img):
323
  try:
324
  # ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
 
365
  print('๊ธ€๋กœ๋ฒŒ ์˜ˆ์™ธ', error)
366
  return None
367
 
368
+ # ์‹œ์ž‘ ์‹œ ์—…์Šค์ผ€์ผ๋Ÿฌ ๋ชจ๋ธ ๋‹ค์šด๋กœ๋“œ
369
+ download_upscaler_models()
370
+
371
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค
372
  with gr.Blocks() as demo:
373
  gr.HTML(
 
379
  """
380
  )
381
 
382
+ with gr.Tabs() as tabs:
383
  # ์ฒซ ๋ฒˆ์งธ ํƒญ: ์ด๋ฏธ์ง€ ์ƒ์„ฑ๊ธฐ
384
  with gr.Tab("์ด๋ฏธ์ง€ ์ƒ์„ฑ๊ธฐ"):
385
  with gr.Row():
 
436
 
437
  gr.Examples(
438
  examples=examples,
439
+ inputs=[image1_input, image2_input, image3_input, prompt_input]
 
440
  )
441
 
442
  # ์˜ค๋ฅธ์ชฝ ์ถœ๋ ฅ ์˜์—ญ
 
474
  outputs=prompt_input
475
  )
476
  clothes_change_btn1.click(
477
+ fn=lambda: "(#1์˜ ์—ฌ์„ฑ๋ชจ๋ธ)์ด ์‹ ์ฒด ๋น„์œจ๊ณผ ํฌ์ฆˆ๋Š” ์œ ์น˜ํ•œ ์ฒด (#2์˜ ์„ ๊ธ€๋ผ์Šค)์™€ (#3์˜ ์ฒญ๋ฐ”์ง€)๋ฅผ ์ง์ ‘ ๋ชจ๋ธ์ด ์ฐฉ์šฉํ•œ๊ฒƒ ์ฒ˜๋Ÿผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜๋ผ.",
478
  inputs=[],
479
  outputs=prompt_input
480
  )
 
507
 
508
  # ๋‘ ๋ฒˆ์งธ ํƒญ: ์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ๋Ÿฌ
509
  with gr.Tab("์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ๋Ÿฌ"):
 
 
 
510
  gr.Markdown(
511
  """
512
  # ์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ๋Ÿฌ
 
530
  )
531
 
532
  if __name__ == "__main__":
533
+ demo.launch(share=True)์„ ๊ธ€๋ผ์Šค)์™€ (#3์˜ ์ฒญ๋ฐ”์ง€)๋ฅผ ์ง์ ‘ ๋ชจ๋ธ์ด ์ฐฉ์šฉํ•œ๊ฒƒ ์ฒ˜๋Ÿผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ด๋ฏธ