import os os.system("pip install gradio==2.9b23") import random import gradio as gr from PIL import Image import torch from random import randint import sys from subprocess import call import psutil # Remove the torch.hub download and instead ensure 'bear.jpg' is in your directory # Place bear.jpg and anime.png in your project directory manually def run_cmd(command): try: print(command) call(command, shell=True) except KeyboardInterrupt: print("Process interrupted") sys.exit(1) # Download model weights if they don't exist if not os.path.exists("RealESRGAN_x4plus.pth"): run_cmd("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P .") run_cmd("pip install basicsr") if not os.path.exists("RealESRGAN_x4plus_anime_6B.pth"): os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth -P .") def inference(img, mode): _id = randint(1, 10000) INPUT_DIR = f"/tmp/input_image{_id}/" OUTPUT_DIR = f"/tmp/output_image{_id}/" # Create directories safely os.makedirs(INPUT_DIR, exist_ok=True) os.makedirs(OUTPUT_DIR, exist_ok=True) # Resize image basewidth = 256 wpercent = (basewidth/float(img.size[0])) hsize = int((float(img.size[1])*float(wpercent))) img = img.resize((basewidth,hsize), Image.LANCZOS) input_path = os.path.join(INPUT_DIR, "1.jpg") img.save(input_path, "JPEG") if mode == "base": model_name = "RealESRGAN_x4plus" else: model_name = "RealESRGAN_x4plus_anime_6B" command = f"python inference_realesrgan.py -n {model_name} -i {INPUT_DIR} -o {OUTPUT_DIR}" run_cmd(command) output_path = os.path.join(OUTPUT_DIR, "1_out.jpg") # Cleanup temporary directories try: if os.path.exists(INPUT_DIR): os.system(f"rm -rf {INPUT_DIR}") if os.path.exists(OUTPUT_DIR): os.system(f"rm -rf {OUTPUT_DIR}") except Exception as e: print(f"Cleanup error: {e}") return output_path title = "Real-ESRGAN" description = "Gradio demo for Real-ESRGAN. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Please click submit only once" article = "

Real-ESRGAN: Training Real-World Blind Super-Resolution with Pure Synthetic Data | Github Repo

" # Create interface interface = gr.Interface( inference, [ gr.inputs.Image(type="pil", label="Input"), gr.inputs.Radio(["base", "anime"], type="value", default="base", label="model type") ], gr.outputs.Image(type="file", label="Output"), title=title, description=description, article=article, examples=[ ['bear.jpg', 'base'], ['anime.png', 'anime'] ] ) # Launch the interface interface.launch()