Spaces:
Runtime error
Runtime error
app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 6 |
+
from realesrgan import RealESRGANer
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
# Ensure model weights exist
|
| 11 |
+
MODEL_URL = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth'
|
| 12 |
+
MODEL_PATH = 'weights/RealESRGAN_x4plus.pth'
|
| 13 |
+
|
| 14 |
+
os.makedirs('weights', exist_ok=True)
|
| 15 |
+
if not os.path.exists(MODEL_PATH):
|
| 16 |
+
print('Downloading model...')
|
| 17 |
+
response = requests.get(MODEL_URL, stream=True)
|
| 18 |
+
with open(MODEL_PATH, 'wb') as f:
|
| 19 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 20 |
+
f.write(chunk)
|
| 21 |
+
|
| 22 |
+
# Load Real-ESRGAN model
|
| 23 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 24 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 25 |
+
upscaler = RealESRGANer(scale=4, model_path=MODEL_PATH, model=model, tile=400, tile_pad=10, pre_pad=0, half=True)
|
| 26 |
+
|
| 27 |
+
# Image upscale function
|
| 28 |
+
def upscale_image(image):
|
| 29 |
+
img = np.array(image)
|
| 30 |
+
output, _ = upscaler.enhance(img, outscale=4)
|
| 31 |
+
return image, Image.fromarray(output)
|
| 32 |
+
|
| 33 |
+
# Gradio UI
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=upscale_image,
|
| 36 |
+
inputs=gr.Image(type='pil'),
|
| 37 |
+
outputs=[gr.Image(type='pil', label='Original Image'), gr.Image(type='pil', label='Upscaled Image')],
|
| 38 |
+
title='Image Upscaler',
|
| 39 |
+
description='Upload an image to see the before and after upscaling using Real-ESRGAN.'
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
if __name__ == '__main__':
|
| 43 |
+
iface.launch()
|