Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
from transformers import AutoImageProcessor, Swin2SRForImageSuperResolution
|
| 6 |
+
import gradio as gr # Import Gradio for creating the interface
|
| 7 |
+
|
| 8 |
+
# Function to upscale an image using Swin2SR
|
| 9 |
+
def upscale_image(image, model, processor, device):
|
| 10 |
+
# Convert the image to RGB format
|
| 11 |
+
image = image.convert("RGB")
|
| 12 |
+
|
| 13 |
+
# Process the image for the model
|
| 14 |
+
inputs = processor(image, return_tensors="pt")
|
| 15 |
+
|
| 16 |
+
# Move inputs to the same device as model
|
| 17 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 18 |
+
|
| 19 |
+
# Perform inference (upscale)
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
outputs = model(**inputs)
|
| 22 |
+
|
| 23 |
+
# Move output back to CPU for further processing
|
| 24 |
+
output = outputs.reconstruction.data.squeeze().cpu().clamp_(0, 1).numpy()
|
| 25 |
+
output = np.moveaxis(output, source=0, destination=-1)
|
| 26 |
+
output_image = (output * 255.0).round().astype(np.uint8) # Convert from float32 to uint8
|
| 27 |
+
|
| 28 |
+
# Remove 32 pixels from the bottom and right of the image
|
| 29 |
+
output_image = output_image[:-32, :-32]
|
| 30 |
+
|
| 31 |
+
return Image.fromarray(output_image)
|
| 32 |
+
|
| 33 |
+
@spaces.GPU
|
| 34 |
+
def main(image, save_as_jpg=True):
|
| 35 |
+
# Check if GPU is available and set the device accordingly
|
| 36 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 37 |
+
|
| 38 |
+
regular_model = "caidas/swin2SR-classical-sr-x4-64"
|
| 39 |
+
realworld_model = "caidas/swin2SR-realworld-sr-x4-64-bsrgan-psnr"
|
| 40 |
+
|
| 41 |
+
# Load the Swin2SR model and processor for 4x upscaling
|
| 42 |
+
processor = AutoImageProcessor.from_pretrained(realworld_model)
|
| 43 |
+
model = Swin2SRForImageSuperResolution.from_pretrained(realworld_model)
|
| 44 |
+
|
| 45 |
+
# Move the model to the device (GPU or CPU)
|
| 46 |
+
model.to(device)
|
| 47 |
+
|
| 48 |
+
# Upscale the image
|
| 49 |
+
upscaled_image = upscale_image(image, model, processor, device)
|
| 50 |
+
|
| 51 |
+
if save_as_jpg:
|
| 52 |
+
# Save the upscaled image as JPG with 98% compression
|
| 53 |
+
upscaled_image.save("upscaled_image.jpg", quality=98)
|
| 54 |
+
return "upscaled_image.jpg"
|
| 55 |
+
else:
|
| 56 |
+
# Save the upscaled image as PNG
|
| 57 |
+
upscaled_image.save("upscaled_image.png")
|
| 58 |
+
return "upscaled_image.png"
|
| 59 |
+
|
| 60 |
+
# Gradio interface
|
| 61 |
+
def gradio_interface(image, save_as_jpg):
|
| 62 |
+
return main(image, save_as_jpg)
|
| 63 |
+
|
| 64 |
+
# Create a Gradio interface
|
| 65 |
+
interface = gr.Interface(
|
| 66 |
+
fn=gradio_interface,
|
| 67 |
+
inputs=[
|
| 68 |
+
gr.inputs.Image(type="pil", label="Upload Image"),
|
| 69 |
+
gr.inputs.Checkbox(default=True, label="Save as JPEG"),
|
| 70 |
+
],
|
| 71 |
+
outputs=gr.outputs.File(label="Download Upscaled Image"),
|
| 72 |
+
title="Image Upscaler",
|
| 73 |
+
description="Upload an image, upscale it, and download the new image.",
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# Launch the interface
|
| 77 |
+
interface.launch()
|