leonelhs commited on
Commit
ba23456
·
verified ·
1 Parent(s): 50c01ef

Update app.py

Browse files

convert to app to API

Files changed (1) hide show
  1. app.py +69 -141
app.py CHANGED
@@ -1,162 +1,90 @@
1
- import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- import gradio as gr
4
  import torch
 
5
  from basicsr.archs.rrdbnet_arch import RRDBNet
6
- from basicsr.archs.srvgg_arch import SRVGGNetCompact
7
- from gfpgan.utils import GFPGANer
8
  from huggingface_hub import 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
-
17
- def showGPU():
18
- if torch.cuda.is_available():
19
- devices = torch.cuda.device_count()
20
- current = torch.cuda.current_device()
21
- return f"Running on GPU:{current} of {devices} total devices"
22
- return "Running on CPU"
23
-
24
 
25
- def download_model_gfpgan(file):
26
- return hf_hub_download(repo_id=GFPGAN_REPO_ID, filename=file)
 
 
 
27
 
28
-
29
- def download_model_realesrgan(file):
30
  return hf_hub_download(repo_id=REALESRGAN_REPO_ID, filename=file)
31
 
 
 
 
 
 
 
 
 
 
32
 
33
- def select_upsampler(version, netscale=4):
34
- model = None
35
- dni_weight = None
36
-
37
- version = version + ".pth"
38
- model_path = download_model_realesrgan(version)
39
-
40
- if version == 'RealESRGAN_x4plus.pth': # x4 RRDBNet model
41
- model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
42
-
43
- if version == 'RealESRNet_x4plus.pth': # x4 RRDBNet model
44
- model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
45
-
46
- if version == 'AI-Forever_x4plus.pth': # x4 RRDBNet model
47
- model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
48
-
49
- if version == 'RealESRGAN_x4plus_anime_6B.pth': # x4 RRDBNet model with 6 blocks
50
- model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
51
 
52
- if version == 'RealESRGAN_x2plus.pth': # x2 RRDBNet model
53
- model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
54
- netscale = 2 # This is
55
 
56
- if version == 'AI-Forever_x2plus.pth': # x2 RRDBNet model
57
- model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
58
- netscale = 2 # This is
59
-
60
- if version == 'realesr-animevideov3.pth': # x4 VGG-style model (XS size)
61
- model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=16, upscale=4, act_type='prelu')
62
-
63
- if version == 'realesr-general-x4v3.pth': # x4 VGG-style model (S size)
64
- model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
65
- model_path = [
66
- download_model_realesrgan("realesr-general-x4v3.pth"),
67
- download_model_realesrgan("realesr-general-wdn-x4v3.pth")
68
- ]
69
- dni_weight = [0.2, 0.8]
70
-
71
- half = True if torch.cuda.is_available() else False
72
-
73
- return RealESRGANer(
74
- scale=netscale,
75
  model_path=model_path,
76
- dni_weight=dni_weight,
77
  model=model,
78
- tile=0,
79
- tile_pad=10,
80
- pre_pad=0,
81
- half=half,
82
- gpu_id=0)
83
-
84
-
85
- def select_face_enhancer(version, scale, upsampler):
86
- if 'v1.2' in version:
87
- model_path = download_model_gfpgan('GFPGANv1.2.pth')
88
- return GFPGANer(
89
- model_path=model_path, upscale=scale, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
90
- elif 'v1.3' in version:
91
- model_path = download_model_gfpgan('GFPGANv1.3.pth')
92
- return GFPGANer(
93
- model_path=model_path, upscale=scale, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
94
- elif 'v1.4' in version:
95
- model_path = download_model_gfpgan('GFPGANv1.4.pth')
96
- return GFPGANer(
97
- model_path=model_path, upscale=scale, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
98
- elif 'RestoreFormer' in version:
99
- model_path = download_model_gfpgan('RestoreFormer.pth')
100
- return GFPGANer(
101
- model_path=model_path, upscale=scale, arch='RestoreFormer', channel_multiplier=2, bg_upsampler=upsampler)
102
-
103
-
104
- def predict(image, version_upsampler, version_enhancer, scale):
105
- scale = int(scale)
106
-
107
- upsampler = select_upsampler(version_upsampler)
108
-
109
- if "No additional" not in version_enhancer:
110
- face_enhancer = select_face_enhancer(version_enhancer, scale, upsampler)
111
- _, _, output = face_enhancer.enhance(image, has_aligned=False, only_center_face=False, paste_back=True)
112
- else:
113
- output, _ = upsampler.enhance(image, outscale=scale)
114
-
115
- log = f"General enhance version: {version_upsampler}\n " \
116
- f"Face enhance version: {version_enhancer} \n " \
117
- f"Scale:{scale} \n {showGPU()}"
118
-
119
- return output, log
120
-
121
 
122
- title = "Super Face"
123
- description = r"""
124
- <b>Practical Image Restoration Algorithm based on Real-ESRGAN, GFPGAN</b>
125
- """
126
- article = r"""
127
- <center><span>xintao.wang@outlook.com or xintaowang@tencent.com</span></center>
128
- </br>
129
- <center><a href='https://github.com/TencentARC/GFPGAN' target='_blank'>Github Repo ⭐ </a> are welcome</center>
130
- """
131
 
132
- demo = gr.Interface(
133
  predict, [
134
- gr.Image(type="numpy", label="Input"),
135
- gr.Dropdown([
136
- 'RealESRGAN_x2plus',
137
- 'RealESRGAN_x4plus',
138
- 'RealESRNet_x4plus',
139
- 'AI-Forever_x2plus',
140
- 'AI-Forever_x4plus',
141
- 'RealESRGAN_x4plus_anime_6B',
142
- 'realesr-animevideov3',
143
- 'realesr-general-x4v3'],
144
- type="value", value='RealESRGAN_x4plus', label='General restoration algorithm', info="version"),
145
- gr.Dropdown([
146
- 'No additional face process',
147
- 'GFPGANv1.2',
148
- 'GFPGANv1.3',
149
- 'GFPGANv1.4',
150
- 'RestoreFormer'],
151
- type="value", value='No additional face process', label='Special face restoration algorithm',
152
- info="version"),
153
- gr.Dropdown(["1", "2", "3", "4"], value="2", label="Rescaling factor")
154
  ], [
155
- gr.Image(type="numpy", label="Output", interactive=False),
156
- gr.Textbox(label="log info")
157
  ],
158
- title=title,
159
- description=description,
160
- article=article)
161
 
162
- demo.queue().launch(share=False, debug=True)
 
 
1
+ #######################################################################################
2
+ #
3
+ # MIT License
4
+ #
5
+ # Copyright (c) [2025] [leonelhs@gmail.com]
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+ #
25
+ #######################################################################################
26
+
27
+ # This file implements an API endpoint RealESRGAN system.
28
+ # It provides functionality to enhances an image by upscaling it 4 times its original size .
29
+
30
+
31
+ # Source code is based on or inspired by several projects.
32
+ # For more details and proper attribution, please refer to the following resources:
33
+ #
34
+ # - [Real-ESRGAN] - [https://github.com/xinntao/Real-ESRGAN]
35
+
36
 
 
37
  import torch
38
+ import gradio as gr
39
  from basicsr.archs.rrdbnet_arch import RRDBNet
 
 
40
  from huggingface_hub import hf_hub_download
41
+ from realesrgan import RealESRGANer
42
 
43
  REALESRGAN_REPO_ID = 'leonelhs/realesrgan'
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
+ MODELS = {
46
+ 'RealESRGAN X4+':'RealESRGAN_x4plus.pth',
47
+ 'RealESRNet X4+':'RealESRNet_x4plus.pth',
48
+ 'AI-Forever X4+':'AI-Forever_x4plus.pth'
49
+ }
50
 
51
+ def download_model(file):
 
52
  return hf_hub_download(repo_id=REALESRGAN_REPO_ID, filename=file)
53
 
54
+ def predict(img, method="RealESRGAN_x4plus.pth"):
55
+ """
56
+ Enhances an image by upscaling it 4 times its original size.
57
+ Parameters:
58
+ img (string): Path to the input image file.
59
+ method (string): Upscaling method to use (RealESRGAN, RealESRNet, AI-Forever).
60
+ Returns:
61
+ path: File path to the enhanced, upscaled image.
62
+ """
63
 
64
+ model_path = download_model(method)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
+ # x4 RRDBNet model
67
+ model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
68
+ half = True if torch.cuda.is_available() else False
69
 
70
+ output , _ = RealESRGANer(
71
+ scale=4,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  model_path=model_path,
 
73
  model=model,
74
+ half=half).enhance(img)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
+ return output
 
 
 
 
 
 
 
 
77
 
78
+ app = gr.Interface(
79
  predict, [
80
+ gr.Image(type="numpy", label="Image input"),
81
+ gr.Dropdown(list(MODELS.items()),
82
+ type="value", value='RealESRGAN_x4plus.pth', label='Enhancer model'),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  ], [
84
+ gr.Image(type="numpy", label="Image enhanced")
 
85
  ],
86
+ title="Image enhancer",
87
+ description="RealESRGAN X4 plus")
 
88
 
89
+ app.launch(share=False, debug=True, show_error=True, mcp_server=True)
90
+ app.queue()