Files changed (2) hide show
  1. app.py +21 -26
  2. requirements.txt +4 -4
app.py CHANGED
@@ -5,22 +5,13 @@ import gradio as gr
5
  import torch
6
  from basicsr.archs.srvgg_arch import SRVGGNetCompact
7
  from gfpgan.utils import GFPGANer
 
8
  from realesrgan.utils import RealESRGANer
9
 
 
 
 
10
  os.system("pip freeze")
11
- # download weights
12
- if not os.path.exists('realesr-general-x4v3.pth'):
13
- os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
14
- if not os.path.exists('GFPGANv1.2.pth'):
15
- os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.2.pth -P .")
16
- if not os.path.exists('GFPGANv1.3.pth'):
17
- os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.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
- if not os.path.exists('RestoreFormer.pth'):
21
- os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth -P .")
22
- if not os.path.exists('CodeFormer.pth'):
23
- os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/CodeFormer.pth -P .")
24
 
25
  torch.hub.download_url_to_file(
26
  'https://upload.wikimedia.org/wikipedia/commons/thumb/a/ab/Abraham_Lincoln_O-77_matte_collodion_print.jpg/1024px-Abraham_Lincoln_O-77_matte_collodion_print.jpg',
@@ -37,7 +28,7 @@ torch.hub.download_url_to_file(
37
 
38
  # background enhancer with RealESRGAN
39
  model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
40
- model_path = 'realesr-general-x4v3.pth'
41
  half = True if torch.cuda.is_available() else False
42
  upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
43
 
@@ -65,28 +56,32 @@ def inference(img, version, scale):
65
  if h > 3500 or w > 3500:
66
  print('too large size')
67
  return None, None
68
-
69
  if h < 300:
70
  img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
71
 
 
 
 
72
  if version == 'v1.2':
 
73
  face_enhancer = GFPGANer(
74
- model_path='GFPGANv1.2.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
75
  elif version == 'v1.3':
 
76
  face_enhancer = GFPGANer(
77
- model_path='GFPGANv1.3.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
78
  elif version == 'v1.4':
 
79
  face_enhancer = GFPGANer(
80
- model_path='GFPGANv1.4.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
81
  elif version == 'RestoreFormer':
 
82
  face_enhancer = GFPGANer(
83
- model_path='RestoreFormer.pth', upscale=2, arch='RestoreFormer', channel_multiplier=2, bg_upsampler=upsampler)
84
- # elif version == 'CodeFormer':
85
- # face_enhancer = GFPGANer(
86
- # model_path='CodeFormer.pth', upscale=2, arch='CodeFormer', channel_multiplier=2, bg_upsampler=upsampler)
87
 
88
  try:
89
- # _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True, weight=weight)
90
  _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
91
  except RuntimeError as error:
92
  print('Error', error)
@@ -143,8 +138,8 @@ demo = gr.Interface(
143
  title=title,
144
  description=description,
145
  article=article,
146
- # examples=[['AI-generate.jpg', 'v1.4', 2, 50], ['lincoln.jpg', 'v1.4', 2, 50], ['Blake_Lively.jpg', 'v1.4', 2, 50],
147
- # ['10045.png', 'v1.4', 2, 50]]).launch()
148
- examples=[['AI-generate.jpg', 'v1.4', 2], ['lincoln.jpg', 'v1.4', 2], ['Blake_Lively.jpg', 'v1.4', 2],
149
  ['10045.png', 'v1.4', 2]])
150
  demo.queue().launch()
 
5
  import torch
6
  from basicsr.archs.srvgg_arch import SRVGGNetCompact
7
  from gfpgan.utils import GFPGANer
8
+ from huggingface_hub import snapshot_download, hf_hub_download
9
  from realesrgan.utils import RealESRGANer
10
 
11
+ REALESRGAN_REPO_ID = 'leonelhs/realesrgan'
12
+ GFPGAN_REPO_ID = 'leonelhs/gfpgan'
13
+
14
  os.system("pip freeze")
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  torch.hub.download_url_to_file(
17
  'https://upload.wikimedia.org/wikipedia/commons/thumb/a/ab/Abraham_Lincoln_O-77_matte_collodion_print.jpg/1024px-Abraham_Lincoln_O-77_matte_collodion_print.jpg',
 
28
 
29
  # background enhancer with RealESRGAN
30
  model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
31
+ model_path = hf_hub_download(repo_id=REALESRGAN_REPO_ID, filename='realesr-general-x4v3.pth')
32
  half = True if torch.cuda.is_available() else False
33
  upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
34
 
 
56
  if h > 3500 or w > 3500:
57
  print('too large size')
58
  return None, None
59
+
60
  if h < 300:
61
  img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
62
 
63
+ face_enhancer = None
64
+ snapshot_folder = snapshot_download(repo_id=GFPGAN_REPO_ID)
65
+
66
  if version == 'v1.2':
67
+ path = os.path.join(snapshot_folder, 'GFPGANv1.2.pth')
68
  face_enhancer = GFPGANer(
69
+ model_path=path, upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
70
  elif version == 'v1.3':
71
+ path = os.path.join(snapshot_folder, 'GFPGANv1.3.pth')
72
  face_enhancer = GFPGANer(
73
+ model_path=path, upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
74
  elif version == 'v1.4':
75
+ path = os.path.join(snapshot_folder, 'GFPGANv1.4.pth')
76
  face_enhancer = GFPGANer(
77
+ model_path=path, upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
78
  elif version == 'RestoreFormer':
79
+ path = os.path.join(snapshot_folder, 'RestoreFormer.pth')
80
  face_enhancer = GFPGANer(
81
+ model_path=path, upscale=2, arch='RestoreFormer', channel_multiplier=2,
82
+ bg_upsampler=upsampler)
 
 
83
 
84
  try:
 
85
  _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
86
  except RuntimeError as error:
87
  print('Error', error)
 
138
  title=title,
139
  description=description,
140
  article=article,
141
+ examples=[['AI-generate.jpg', 'v1.4', 2],
142
+ ['lincoln.jpg', 'v1.4', 2],
143
+ ['Blake_Lively.jpg', 'v1.4', 2],
144
  ['10045.png', 'v1.4', 2]])
145
  demo.queue().launch()
requirements.txt CHANGED
@@ -1,8 +1,8 @@
1
- torch>=1.7
2
  basicsr>=1.4.2
3
- facexlib>=0.2.5
4
- gfpgan>=1.3.7
5
- realesrgan>=0.2.5
6
  numpy
7
  opencv-python
8
  torchvision
 
1
+ torch>=2.0.1
2
  basicsr>=1.4.2
3
+ facexlib>=0.3.0
4
+ gfpgan>=1.3.8
5
+ realesrgan>=0.3.0
6
  numpy
7
  opencv-python
8
  torchvision