tuan2308 commited on
Commit
2269064
Β·
verified Β·
1 Parent(s): 53b24c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -1,19 +1,28 @@
1
  import gradio as gr
2
- import spaces
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
  from realesrgan import RealESRGANer
10
  from realesrgan.archs.srvgg_arch import SRVGGNetCompact
11
  from torchvision.transforms.functional import rgb_to_grayscale
12
 
 
 
 
 
 
 
 
 
 
13
  last_file = None
14
  img_mode = "RGBA"
15
 
16
- @spaces.GPU(duration=120)
17
  def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
18
  """Real-ESRGAN function to restore (and upscale) images.
19
  """
@@ -62,16 +71,17 @@ def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
62
  dni_weight = [denoise_strength, 1 - denoise_strength]
63
 
64
  # Restorer Class
 
65
  upsampler = RealESRGANer(
66
  scale=netscale,
67
  model_path=model_path,
68
  dni_weight=dni_weight,
69
  model=model,
70
- tile=0,
71
  tile_pad=10,
72
  pre_pad=10,
73
- half=False,
74
- gpu_id=None
75
  )
76
 
77
  # Use GFPGAN for face enhancement
@@ -167,7 +177,7 @@ def image_properties(img):
167
 
168
  def main():
169
  # Gradio Interface
170
- with gr.Blocks(title="Real-ESRGAN Gradio Demo", theme="dark") as demo:
171
 
172
  gr.Markdown(
173
  """# <div align="center"> Ilaria Upscaler πŸ’– </div>
 
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
 
13
+ # Enable HF Spaces GPU wrapper only when explicitly requested
14
+ # Set env USE_SPACES_GPU=1 on Hugging Face Spaces to enable GPU decorator
15
+ def maybe_spaces_gpu(fn):
16
+ use_gpu_decorator = os.getenv("USE_SPACES_GPU", "0") == "1"
17
+ if use_gpu_decorator:
18
+ import spaces
19
+ return spaces.GPU(duration=120)(fn)
20
+ return fn
21
+
22
  last_file = None
23
  img_mode = "RGBA"
24
 
25
+ @maybe_spaces_gpu
26
  def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
27
  """Real-ESRGAN function to restore (and upscale) images.
28
  """
 
71
  dni_weight = [denoise_strength, 1 - denoise_strength]
72
 
73
  # Restorer Class
74
+ use_cuda = torch.cuda.is_available()
75
  upsampler = RealESRGANer(
76
  scale=netscale,
77
  model_path=model_path,
78
  dni_weight=dni_weight,
79
  model=model,
80
+ tile=128,
81
  tile_pad=10,
82
  pre_pad=10,
83
+ half=use_cuda,
84
+ gpu_id=0 if use_cuda else None
85
  )
86
 
87
  # Use GFPGAN for face enhancement
 
177
 
178
  def main():
179
  # Gradio Interface
180
+ with gr.Blocks(title="Real-ESRGAN Gradio Demo", theme=gr.themes.Soft()) as demo:
181
 
182
  gr.Markdown(
183
  """# <div align="center"> Ilaria Upscaler πŸ’– </div>