Kvikontent commited on
Commit
d9b1f1f
·
verified ·
1 Parent(s): e270cd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -51
app.py CHANGED
@@ -1,63 +1,36 @@
1
  import torch
2
  from PIL import Image
 
3
  from RealESRGAN import RealESRGAN
4
  import gradio as gr
5
 
6
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
7
- model_scales = {'2x': 2, '4x': 4, '8x': 8}
8
 
9
- # Load RealESRGAN models for different scales
10
- models = {scale: RealESRGAN(device, scale=scale) for scale in model_scales.values()}
11
 
12
- def inference(images, scale):
13
- results = []
 
14
 
15
- if images is None:
16
- raise gr.Error("No image uploaded. Please upload at least one image.")
 
17
 
18
- for image in images:
19
- width, height = image.size
20
- if width >= 5000 or height >= 5000:
21
- raise gr.Error("The image is too large.")
22
-
23
- if torch.cuda.is_available():
24
- torch.cuda.empty_cache()
25
-
26
- # Select the appropriate model based on the chosen scale
27
- model = models[model_scales[scale]]
28
- result = model.predict(image.convert('RGB'))
29
- print(f"Image size ({device}): {scale} ... OK")
30
- results.append(result)
31
-
32
- return results
33
 
34
- title = "✨ Real ESRGAN UpScale: 2x 4x 8x"
35
- description = (
36
- "This advanced demo for Real-ESRGAN allows you to upscale multiple images with different models and resolutions. \nChoose the scale and upload images for high-resolution enhancement."
37
- )
38
- article = (
39
- "<div style='text-align: center;'>Youtube "
40
- "<a href='https://youtube.com/@kvikontent' target='_blank'>KVI Kontent</a> | "
41
- "<a href='https://huggingface.co/sberbank-ai/Real-ESRGAN' target='_blank'>Model card</a></div>"
42
- )
43
 
44
- gr.Interface(
45
- inference,
46
- [
47
- gr.Image(type="pil", label="Upload Image"),
48
- gr.Radio(
49
- list(model_scales.keys()),
50
- type="value",
51
- value='2x',
52
- label='Resolution model',
53
- ),
54
- ],
55
- gr.Image(type="pil", label="Output"),
56
- title=title,
57
- description=description,
58
- article=article,
59
- examples=[['groot.jpg', '2x'], ['woman.jpg', '4x']],
60
- allow_flagging='never',
61
- cache_examples=False,
62
- theme="soft"
63
- ).launch()
 
1
  import torch
2
  from PIL import Image
3
+ import numpy as np
4
  from RealESRGAN import RealESRGAN
5
  import gradio as gr
6
 
7
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 
8
 
9
+ model = None
10
+ scale = 4
11
 
12
+ def upscale_image(image, scale):
13
+ global device
14
+ global model
15
 
16
+ if model is None or model.scale != scale:
17
+ model = RealESRGAN(device, scale=scale)
18
+ model.load_weights('weights/RealESRGAN_x{}.pth'.format(scale), download=True)
19
 
20
+ sr_image = model.predict(image)
21
+ return sr_image
22
+
23
+ inputs = [
24
+ gr.Image(label="Input Image", type="pil"),
25
+ gr.Slider(minimum=2, maximum=8, value=4, label="Upscale Scale", step=2)
26
+ ]
27
+
28
+ output = gr.Image(label="Upscaled Image")
 
 
 
 
 
 
29
 
30
+ examples = [
31
+ ['groot.jpg', '4'],
32
+ ['woman.jpg', '8']
33
+ ]
 
 
 
 
 
34
 
35
+ gr.Interface(fn=upscale_image, inputs=inputs, outputs=output, title="Image Upscaler",
36
+ examples=examples, theme="soft", server_name="0.0.0.0").launch()