Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
import torch
|
| 5 |
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 6 |
from realesrgan import RealESRGANer
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
upscaler = RealESRGANer(
|
| 12 |
scale=4,
|
| 13 |
model_path='RealESRGAN_x4plus.pth',
|
| 14 |
-
model=
|
| 15 |
-
tile=
|
| 16 |
tile_pad=10,
|
| 17 |
pre_pad=0,
|
| 18 |
-
half=
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
-
def upscale_image(
|
| 22 |
try:
|
| 23 |
-
img = np.array(
|
| 24 |
-
output, _ =
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
return f"Error: {str(e)}"
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
import torch
|
| 7 |
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 8 |
from realesrgan import RealESRGANer
|
| 9 |
|
| 10 |
+
# Load Real-ESRGAN Model
|
| 11 |
+
model = RealESRGANer(
|
|
|
|
|
|
|
| 12 |
scale=4,
|
| 13 |
model_path='RealESRGAN_x4plus.pth',
|
| 14 |
+
model=RRDBNet(num_in_ch=3, num_out_ch=3, nf=64, nb=23, gc=32, scale=4, group=1, norm_type=None, act_type='leakyrelu', mode='NCHW', convtype='Conv'),
|
| 15 |
+
tile=0,
|
| 16 |
tile_pad=10,
|
| 17 |
pre_pad=0,
|
| 18 |
+
half=True,
|
| 19 |
+
gpu_id=None if not torch.cuda.is_available() else 0
|
| 20 |
)
|
| 21 |
|
| 22 |
+
def upscale_image(input_image, scale, file_type):
|
| 23 |
try:
|
| 24 |
+
img = np.array(input_image)
|
| 25 |
+
output, _ = model.enhance(img, outscale=scale)
|
| 26 |
+
output_pil = Image.fromarray(output)
|
| 27 |
+
|
| 28 |
+
output_path = "upscaled_output." + file_type.lower()
|
| 29 |
+
output_pil.save(output_path, dpi=(300, 300))
|
| 30 |
+
|
| 31 |
+
return output_path
|
| 32 |
except Exception as e:
|
| 33 |
return f"Error: {str(e)}"
|
| 34 |
|
| 35 |
+
with gr.Blocks() as demo:
|
| 36 |
+
gr.Markdown("## 🖼️ AI Image Upscaler (Real-ESRGAN Powered)")
|
| 37 |
+
|
| 38 |
+
with gr.Row():
|
| 39 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 40 |
+
scale_input = gr.Radio([2, 4, 6, 8], label="Upscale Factor", value=4)
|
| 41 |
+
format_input = gr.Dropdown(["PNG", "JPEG", "TIFF"], label="Export Format", value="PNG")
|
| 42 |
+
|
| 43 |
+
output_image = gr.File(label="Download Upscaled Image")
|
| 44 |
+
|
| 45 |
+
upscale_btn = gr.Button("Upscale & Download")
|
| 46 |
+
|
| 47 |
+
upscale_btn.click(fn=upscale_image, inputs=[image_input, scale_input, format_input], outputs=output_image)
|
| 48 |
+
|
| 49 |
+
demo.launch()
|