NickKolok commited on
Commit
b7258ad
·
verified ·
1 Parent(s): c78c8fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -24
app.py CHANGED
@@ -1,36 +1,33 @@
1
- import os
2
  import gradio as gr
3
  import torch
4
- from PIL import Image
5
- from torchvision.transforms import ToTensor, ToPILImage
6
- from torchvision import models
7
 
8
- # Load the R-ESRGAN Anime model (you need to set up this model properly)
9
- model = torch.hub.load('xinntao/Real-ESRGAN', 'restorer', model='R-ESRGAN_Anime_X6')
10
-
11
- def upscale_image(image, scale_factor):
12
- # Ensure image is in PIL format
13
- if not isinstance(image, Image.Image):
14
- image = Image.fromarray(image)
15
-
16
- # Upscale using the model
17
- with torch.no_grad():
18
- upscaled_image = model(image, scale=scale_factor)
19
 
 
 
 
 
 
 
 
 
 
 
20
  return upscaled_image
21
 
22
- # Create Gradio interface
23
  iface = gr.Interface(
24
  fn=upscale_image,
25
  inputs=[
26
- gr.inputs.Image(type="pil", label="Input Image"),
27
- gr.inputs.Slider(minimum=1, maximum=4, step=1, default=2, label="Scale Factor")
28
  ],
29
- outputs=gr.outputs.Image(type="pil", label="Upscaled Image"),
30
- title="R-ESRGAN Anime 6B Image Upscaler",
31
- description="Upload an image and select a scale factor to upscale the image using R-ESRGAN Anime 6B model."
32
  )
33
 
34
- # Launch the Gradio app
35
- if __name__ == "__main__":
36
- iface.launch()
 
 
1
  import gradio as gr
2
  import torch
3
+ from realesrgan import RealESRGAN
 
 
4
 
5
+ # Load the model
6
+ model = RealESRGAN('weights/RealESRGAN_x4plus_anime_6B.pth', scale=4) # Adjust scale if needed
 
 
 
 
 
 
 
 
 
7
 
8
+ def upscale_image(input_image, scale_factor):
9
+ # Ensure scale factor is an integer
10
+ scale_factor = int(scale_factor)
11
+
12
+ # Set the model scale
13
+ model.scale = scale_factor
14
+
15
+ # Perform the upscaling
16
+ upscaled_image = model.predict(input_image)
17
+
18
  return upscaled_image
19
 
20
+ # Create the Gradio interface
21
  iface = gr.Interface(
22
  fn=upscale_image,
23
  inputs=[
24
+ gr.inputs.Image(type="numpy", label="Input Image"),
25
+ gr.inputs.Slider(minimum=2, maximum=4, step=1, default=4, label="Scale Factor")
26
  ],
27
+ outputs=gr.outputs.Image(type="numpy", label="Upscaled Image"),
28
+ title="Image Upscaler using R-ESRGAN Anime 6B",
29
+ description="Upload an image and select a scale factor to upscale it using the R-ESRGAN Anime 6B model."
30
  )
31
 
32
+ # Launch the app
33
+ iface.launch()