Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,19 @@ from transformers import AutoImageProcessor, Swin2SRForImageSuperResolution
|
|
| 5 |
import gradio as gr
|
| 6 |
import spaces
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def split_image(image, chunk_size=512):
|
| 9 |
width, height = image.size
|
| 10 |
chunks = []
|
|
@@ -30,8 +43,11 @@ def upscale_chunk(chunk, model, processor, device):
|
|
| 30 |
output_image = (output * 255.0).round().astype(np.uint8)
|
| 31 |
return Image.fromarray(output_image)
|
| 32 |
|
| 33 |
-
@spaces.GPU
|
| 34 |
def main(image, model_choice, save_as_jpg=True):
|
|
|
|
|
|
|
|
|
|
| 35 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 36 |
|
| 37 |
model_paths = {
|
|
@@ -87,7 +103,7 @@ interface = gr.Interface(
|
|
| 87 |
gr.Textbox(label="Error Message", visible=True)
|
| 88 |
],
|
| 89 |
title="Image Upscaler",
|
| 90 |
-
description="Upload an image, select a model, and upscale it. The image will be processed in 512x512 pixel chunks
|
| 91 |
)
|
| 92 |
|
| 93 |
interface.launch()
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import spaces
|
| 7 |
|
| 8 |
+
def resize_image(image, max_size=2048):
|
| 9 |
+
width, height = image.size
|
| 10 |
+
if width > max_size or height > max_size:
|
| 11 |
+
aspect_ratio = width / height
|
| 12 |
+
if width > height:
|
| 13 |
+
new_width = max_size
|
| 14 |
+
new_height = int(new_width / aspect_ratio)
|
| 15 |
+
else:
|
| 16 |
+
new_height = max_size
|
| 17 |
+
new_width = int(new_height * aspect_ratio)
|
| 18 |
+
image = image.resize((new_width, new_height), Image.LANCZOS)
|
| 19 |
+
return image
|
| 20 |
+
|
| 21 |
def split_image(image, chunk_size=512):
|
| 22 |
width, height = image.size
|
| 23 |
chunks = []
|
|
|
|
| 43 |
output_image = (output * 255.0).round().astype(np.uint8)
|
| 44 |
return Image.fromarray(output_image)
|
| 45 |
|
| 46 |
+
@spaces.GPU(duration=120)
|
| 47 |
def main(image, model_choice, save_as_jpg=True):
|
| 48 |
+
# Resize the input image
|
| 49 |
+
image = resize_image(image)
|
| 50 |
+
|
| 51 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 52 |
|
| 53 |
model_paths = {
|
|
|
|
| 103 |
gr.Textbox(label="Error Message", visible=True)
|
| 104 |
],
|
| 105 |
title="Image Upscaler",
|
| 106 |
+
description="Upload an image, select a model, and upscale it. Images larger than 2048x2048 will be resized while maintaining aspect ratio. The image will be processed in 512x512 pixel chunks for efficient handling.",
|
| 107 |
)
|
| 108 |
|
| 109 |
interface.launch()
|