bsrgan / app.py
leonelhs's picture
upgrade interface
87e3905
#######################################################################################
#
# MIT License
#
# Copyright (c) [2025] [leonelhs@gmail.com]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#######################################################################################
#
# Source code is based on or inspired by several projects.
# For more details and proper attribution, please refer to the following resources:
#
# - [BSRGAN] - [https://github.com/cszn/BSRGAN]
# - [HF BSRGAN] - [https://huggingface.co/spaces/owsgfwnlgjuz/bsrgan]
# - [Self space] - [https://huggingface.co/spaces/leonelhs/bsrgan]
#
from itertools import islice
import gradio as gr
import numpy as np
import torch
from huggingface_hub import hf_hub_download
from torchvision.transforms import transforms
from models import RRDBNet
REPO_ID = "kadirnar/BSRGANx2"
pretrain_model_path = hf_hub_download(repo_id=REPO_ID, filename="BSRGANx2.pth")
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = RRDBNet(in_nc=3, out_nc=3, nf=64, nb=23, gc=32, sf=2)
model.load_state_dict(torch.load(pretrain_model_path), strict=True)
model.eval()
for k, v in model.named_parameters():
v.requires_grad = False
model = model.to(device)
transform = transforms.Compose([
transforms.ToTensor(), # converts to float32 and scales to [0,1]
])
def predict(image):
"""
Enhances the image face.
Parameters:
image (string): File path to the input image.
Returns:
image (string): paths for image enhanced.
"""
tensor = transform(image).unsqueeze(0).to(device)
tensor = model(tensor)
tensor = tensor.detach().squeeze().float().clamp(0, 1).cpu()
result = tensor.numpy()
if result.ndim == 3: # (C, H, W) -> (H, W, C)
result = np.transpose(result, (1, 2, 0))
return image, (result * 255.0).round().astype(np.uint8)
with gr.Blocks(title="BSRGAN") as app:
navbar = gr.Navbar(visible=True, main_page_name="Workspace")
gr.Markdown("## BSRGANx2")
with gr.Row():
with gr.Column(scale=1):
with gr.Row():
source_image = gr.Image(type="numpy", label="Image")
image_btn = gr.Button("Enhance image")
with gr.Column(scale=1):
with gr.Row():
output_image = gr.ImageSlider(label="Enhanced image", type="filepath")
# output_image = gr.Image(label="Enhanced faces", type="pil")
image_btn.click(fn=predict, inputs=[source_image], outputs=output_image)
with app.route("Readme", "/readme"):
with open("README.md") as f:
for line in islice(f, 15, None):
gr.Markdown(line.strip())
app.launch(share=False, debug=True, show_error=True, mcp_server=True, pwa=True)
app.queue()