Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import numpy as np
|
|
| 4 |
from transformers import AutoImageProcessor, Swin2SRForImageSuperResolution
|
| 5 |
import gradio as gr
|
| 6 |
import spaces
|
|
|
|
| 7 |
|
| 8 |
def resize_image(image, max_size=2048):
|
| 9 |
width, height = image.size
|
|
@@ -44,11 +45,11 @@ def upscale_chunk(chunk, model, processor, device):
|
|
| 44 |
return Image.fromarray(output_image)
|
| 45 |
|
| 46 |
@spaces.GPU
|
| 47 |
-
def main(image, model_choice, save_as_jpg=True, use_tiling=True
|
| 48 |
# Resize the input image
|
| 49 |
image = resize_image(image)
|
| 50 |
|
| 51 |
-
device = torch.device("cuda" if torch.cuda.is_available()
|
| 52 |
|
| 53 |
model_paths = {
|
| 54 |
"Pixel Perfect": "caidas/swin2SR-classical-sr-x4-64",
|
|
@@ -77,16 +78,22 @@ def main(image, model_choice, save_as_jpg=True, use_tiling=True, auto_cpu=True):
|
|
| 77 |
# Process the entire image at once
|
| 78 |
upscaled_image = upscale_chunk(image, model, processor, device)
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if save_as_jpg:
|
| 81 |
-
|
| 82 |
-
|
| 83 |
else:
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
def gradio_interface(image, model_choice, save_as_jpg, use_tiling
|
| 88 |
try:
|
| 89 |
-
result = main(image, model_choice, save_as_jpg, use_tiling
|
| 90 |
return result, None
|
| 91 |
except Exception as e:
|
| 92 |
return None, str(e)
|
|
@@ -102,14 +109,13 @@ interface = gr.Interface(
|
|
| 102 |
),
|
| 103 |
gr.Checkbox(value=True, label="Save as JPEG"),
|
| 104 |
gr.Checkbox(value=True, label="Use Tiling"),
|
| 105 |
-
gr.Checkbox(value=True, label="Auto CPU"),
|
| 106 |
],
|
| 107 |
outputs=[
|
| 108 |
gr.File(label="Download Upscaled Image"),
|
| 109 |
gr.Textbox(label="Error Message", visible=True)
|
| 110 |
],
|
| 111 |
title="Image Upscaler",
|
| 112 |
-
description="Upload an image, select a model, and upscale it. Images larger than 2048x2048 will be resized while maintaining aspect ratio. Use tiling for efficient processing of large images.
|
| 113 |
)
|
| 114 |
|
| 115 |
interface.launch()
|
|
|
|
| 4 |
from transformers import AutoImageProcessor, Swin2SRForImageSuperResolution
|
| 5 |
import gradio as gr
|
| 6 |
import spaces
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
def resize_image(image, max_size=2048):
|
| 10 |
width, height = image.size
|
|
|
|
| 45 |
return Image.fromarray(output_image)
|
| 46 |
|
| 47 |
@spaces.GPU
|
| 48 |
+
def main(image, model_choice, save_as_jpg=True, use_tiling=True):
|
| 49 |
# Resize the input image
|
| 50 |
image = resize_image(image)
|
| 51 |
|
| 52 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 53 |
|
| 54 |
model_paths = {
|
| 55 |
"Pixel Perfect": "caidas/swin2SR-classical-sr-x4-64",
|
|
|
|
| 78 |
# Process the entire image at once
|
| 79 |
upscaled_image = upscale_chunk(image, model, processor, device)
|
| 80 |
|
| 81 |
+
# Generate output filename
|
| 82 |
+
original_filename = os.path.splitext(image.filename)[0] if image.filename else "image"
|
| 83 |
+
output_filename = f"{original_filename}_upscaled"
|
| 84 |
+
|
| 85 |
if save_as_jpg:
|
| 86 |
+
output_filename += ".jpg"
|
| 87 |
+
upscaled_image.save(output_filename, quality=95)
|
| 88 |
else:
|
| 89 |
+
output_filename += ".png"
|
| 90 |
+
upscaled_image.save(output_filename)
|
| 91 |
+
|
| 92 |
+
return output_filename
|
| 93 |
|
| 94 |
+
def gradio_interface(image, model_choice, save_as_jpg, use_tiling):
|
| 95 |
try:
|
| 96 |
+
result = main(image, model_choice, save_as_jpg, use_tiling)
|
| 97 |
return result, None
|
| 98 |
except Exception as e:
|
| 99 |
return None, str(e)
|
|
|
|
| 109 |
),
|
| 110 |
gr.Checkbox(value=True, label="Save as JPEG"),
|
| 111 |
gr.Checkbox(value=True, label="Use Tiling"),
|
|
|
|
| 112 |
],
|
| 113 |
outputs=[
|
| 114 |
gr.File(label="Download Upscaled Image"),
|
| 115 |
gr.Textbox(label="Error Message", visible=True)
|
| 116 |
],
|
| 117 |
title="Image Upscaler",
|
| 118 |
+
description="Upload an image, select a model, and upscale it. Images larger than 2048x2048 will be resized while maintaining aspect ratio. Use tiling for efficient processing of large images.",
|
| 119 |
)
|
| 120 |
|
| 121 |
interface.launch()
|