Img_enh / app.py
Kingoteam's picture
Update app.py
36c8786 verified
import gradio as gr
import numpy as np
from PIL import Image
import torch
from realesrgan import RealESRGANer
from basicsr.archs.rrdbnet_arch import RRDBNet
from huggingface_hub import hf_hub_download
# دانلود مدل x4
model_path = hf_hub_download(
repo_id="lllyasviel/Annotators",
filename="RealESRGAN_x4plus.pth"
)
# تعریف معماری RRDBNet (x4)
model = RRDBNet(
num_in_ch=3,
num_out_ch=3,
num_feat=64,
num_block=23,
num_grow_ch=32,
scale=4
)
# لود مدل
upsampler = RealESRGANer(
scale=4,
model_path=model_path,
model=model,
tile=0,
tile_pad=10,
pre_pad=0,
half=torch.cuda.is_available()
)
def upscale_image(img: Image.Image):
img = img.convert("RGB")
output, _ = upsampler.enhance(np.array(img), outscale=4)
return Image.fromarray(output)
demo = gr.Interface(
fn=upscale_image,
inputs=gr.Image(type="pil"),
outputs="image",
title="Real-ESRGAN x4plus",
description="افزایش کیفیت تصویر (Upscale ×4)"
)
if __name__ == "__main__":
demo.launch()