tuan2308 commited on
Commit
06a3e99
Β·
verified Β·
1 Parent(s): 3c0356a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -26
app.py CHANGED
@@ -1,24 +1,15 @@
 
1
  import gradio as gr
2
  import cv2
3
  import numpy
4
  import os
5
  import random
6
- import torch
7
  from basicsr.archs.rrdbnet_arch import RRDBNet
8
  from basicsr.utils.download_util import load_file_from_url
 
9
  from realesrgan import RealESRGANer
10
  from realesrgan.archs.srvgg_arch import SRVGGNetCompact
11
- from torchvision.transforms.functional import rgb_to_grayscale
12
- import spaces
13
 
14
- # Enable HF Spaces GPU wrapper only when explicitly requested
15
- # Set env USE_SPACES_GPU=1 on Hugging Face Spaces to enable GPU decorator
16
- def maybe_spaces_gpu(fn):
17
- use_gpu_decorator = os.getenv("USE_SPACES_GPU", "0") == "1"
18
- if use_gpu_decorator:
19
- import spaces
20
- return spaces.GPU(duration=120)(fn)
21
- return fn
22
 
23
  last_file = None
24
  img_mode = "RGBA"
@@ -72,17 +63,16 @@ def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
72
  dni_weight = [denoise_strength, 1 - denoise_strength]
73
 
74
  # Restorer Class
75
- use_cuda = torch.cuda.is_available()
76
  upsampler = RealESRGANer(
77
  scale=netscale,
78
  model_path=model_path,
79
  dni_weight=dni_weight,
80
  model=model,
81
- tile=128,
82
  tile_pad=10,
83
  pre_pad=10,
84
- half=use_cuda,
85
- gpu_id=0 if use_cuda else None
86
  )
87
 
88
  # Use GFPGAN for face enhancement
@@ -109,10 +99,14 @@ def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
109
  print('Error', error)
110
  print('If you encounter CUDA out of memory, try to set --tile with a smaller number.')
111
  else:
112
- extension = 'jpg'
 
 
 
 
113
 
114
  out_filename = f"output_{rnd_string(8)}.{extension}"
115
- cv2.imwrite(out_filename, output, [int(cv2.IMWRITE_JPEG_QUALITY), 92])
116
  global last_file
117
  last_file = out_filename
118
  return out_filename
@@ -178,14 +172,10 @@ def image_properties(img):
178
 
179
  def main():
180
  # Gradio Interface
181
- with gr.Blocks(title="Real-ESRGAN Gradio Demo", theme=gr.themes.Soft()) as demo:
182
 
183
  gr.Markdown(
184
- """# <div align="center"> Ilaria Upscaler πŸ’– </div>
185
-
186
- Do not use images over 750x750 especially with 4x the resolution upscaling, it will give you an error.
187
-
188
- Hugginface port of [Real-ESRGAN](https://github.com/xinntao/Real-ESRGAN).
189
  """
190
  )
191
 
@@ -221,12 +211,11 @@ def main():
221
  # Undocumented method to clear a component's value using Javascript
222
 
223
  gr.Markdown(
224
- """Made with love by Ilaria πŸ’– | Support me on [Ko-Fi](https://ko-fi.com/ilariaowo) | Join [AI Hub](https://discord.gg/aihub)
225
  """
226
  )
227
 
228
- demo.queue(max_size=10)
229
- demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False, share=False, show_error=True)
230
 
231
 
232
  if __name__ == "__main__":
 
1
+ import spaces
2
  import gradio as gr
3
  import cv2
4
  import numpy
5
  import os
6
  import random
 
7
  from basicsr.archs.rrdbnet_arch import RRDBNet
8
  from basicsr.utils.download_util import load_file_from_url
9
+
10
  from realesrgan import RealESRGANer
11
  from realesrgan.archs.srvgg_arch import SRVGGNetCompact
 
 
12
 
 
 
 
 
 
 
 
 
13
 
14
  last_file = None
15
  img_mode = "RGBA"
 
63
  dni_weight = [denoise_strength, 1 - denoise_strength]
64
 
65
  # Restorer Class
 
66
  upsampler = RealESRGANer(
67
  scale=netscale,
68
  model_path=model_path,
69
  dni_weight=dni_weight,
70
  model=model,
71
+ tile=0,
72
  tile_pad=10,
73
  pre_pad=10,
74
+ half=False,
75
+ gpu_id=None
76
  )
77
 
78
  # Use GFPGAN for face enhancement
 
99
  print('Error', error)
100
  print('If you encounter CUDA out of memory, try to set --tile with a smaller number.')
101
  else:
102
+ # Save restored image and return it to the output Image component
103
+ if img_mode == 'RGBA': # RGBA images should be saved in png format
104
+ extension = 'png'
105
+ else:
106
+ extension = 'jpg'
107
 
108
  out_filename = f"output_{rnd_string(8)}.{extension}"
109
+ cv2.imwrite(out_filename, output)
110
  global last_file
111
  last_file = out_filename
112
  return out_filename
 
172
 
173
  def main():
174
  # Gradio Interface
175
+ with gr.Blocks(title="Real-ESRGAN Gradio Demo", theme="ParityError/Interstellar") as demo:
176
 
177
  gr.Markdown(
178
+ """ Image Upscaler
 
 
 
 
179
  """
180
  )
181
 
 
211
  # Undocumented method to clear a component's value using Javascript
212
 
213
  gr.Markdown(
214
+ """
215
  """
216
  )
217
 
218
+ demo.launch()
 
219
 
220
 
221
  if __name__ == "__main__":