reedmayhew's picture
Update app.py
e2d6adc verified
raw
history blame
No virus
2.59 kB
# Import necessary libraries
from PIL import Image
import numpy as np
import torch
from transformers import AutoImageProcessor, Swin2SRForImageSuperResolution
import gradio as gr # Import Gradio for creating the interface
import spaces
# Function to upscale an image using Swin2SR
def upscale_image(image, model, processor, device):
# Convert the image to RGB format
image = image.convert("RGB")
# Process the image for the model
inputs = processor(image, return_tensors="pt")
# Move inputs to the same device as model
inputs = {k: v.to(device) for k, v in inputs.items()}
# Perform inference (upscale)
with torch.no_grad():
outputs = model(**inputs)
# Move output back to CPU for further processing
output = outputs.reconstruction.data.squeeze().cpu().clamp_(0, 1).numpy()
output = np.moveaxis(output, source=0, destination=-1)
output_image = (output * 255.0).round().astype(np.uint8) # Convert from float32 to uint8
# Remove 32 pixels from the bottom and right of the image
output_image = output_image[:-32, :-32]
return Image.fromarray(output_image)
@spaces.GPU
def main(image, save_as_jpg=True):
# Check if GPU is available and set the device accordingly
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
realworld_model = "caidas/swin2SR-realworld-sr-x4-64-bsrgan-psnr"
# Load the Swin2SR model and processor for 4x upscaling
processor = AutoImageProcessor.from_pretrained(realworld_model)
model = Swin2SRForImageSuperResolution.from_pretrained(realworld_model)
# Move the model to the device (GPU or CPU)
model.to(device)
# Upscale the image
upscaled_image = upscale_image(image, model, processor, device)
if save_as_jpg:
# Save the upscaled image as JPG with 98% compression
upscaled_image.save("upscaled_image.jpg", quality=98)
return "upscaled_image.jpg"
else:
# Save the upscaled image as PNG
upscaled_image.save("upscaled_image.png")
return "upscaled_image.png"
# Gradio interface
def gradio_interface(image, save_as_jpg):
return main(image, save_as_jpg)
# Create a Gradio interface
interface = gr.Interface(
fn=gradio_interface,
inputs=[
gr.Image(type="pil", label="Upload Image"),
gr.Checkbox(default=True, label="Save as JPEG"),
],
outputs=gr.File(label="Download Upscaled Image"),
title="Image Upscaler",
description="Upload an image, upscale it, and download the new image.",
)
# Launch the interface
interface.launch()