Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_imageslider import ImageSlider
|
| 3 |
-
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
-
|
| 7 |
from aura_sr import AuraSR
|
| 8 |
-
import spaces
|
| 9 |
import torch
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
if torch.cuda.is_available():
|
| 15 |
-
aura_sr.to("cuda")
|
| 16 |
-
|
| 17 |
-
@spaces.GPU
|
| 18 |
def process_image(input_image):
|
| 19 |
if input_image is None:
|
| 20 |
return None
|
| 21 |
-
|
| 22 |
-
#
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Upscale the image using AuraSR
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
# Convert result to numpy array
|
| 29 |
result_array = np.array(upscaled_image)
|
| 30 |
|
| 31 |
return [input_array, result_array]
|
|
@@ -34,7 +49,7 @@ with gr.Blocks() as demo:
|
|
| 34 |
gr.Markdown("# Image Upscaler using AuraSR")
|
| 35 |
with gr.Row():
|
| 36 |
with gr.Column(scale=1):
|
| 37 |
-
input_image = gr.Image(label="Input Image", type="
|
| 38 |
process_btn = gr.Button("Upscale Image")
|
| 39 |
with gr.Column(scale=1):
|
| 40 |
output_slider = ImageSlider(label="Before / After", type="numpy")
|
|
@@ -45,4 +60,5 @@ with gr.Blocks() as demo:
|
|
| 45 |
outputs=output_slider
|
| 46 |
)
|
| 47 |
|
|
|
|
| 48 |
demo.launch(debug=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_imageslider import ImageSlider
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
from aura_sr import AuraSR
|
|
|
|
| 6 |
import torch
|
| 7 |
|
| 8 |
+
# Initialize the AuraSR model
|
| 9 |
+
aura_sr = AuraSR.from_pretrained("fal-ai/AuraSR")
|
| 10 |
|
| 11 |
+
# Move the model to CUDA if available, otherwise keep it on CPU
|
| 12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
+
aura_sr.to(device)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def process_image(input_image):
|
| 16 |
if input_image is None:
|
| 17 |
return None
|
| 18 |
+
|
| 19 |
+
# Ensure input_image is a numpy array
|
| 20 |
+
input_array = np.array(input_image)
|
| 21 |
+
|
| 22 |
+
# Convert to PIL Image for resizing
|
| 23 |
+
pil_image = Image.fromarray(input_array)
|
| 24 |
+
|
| 25 |
+
# Resize the longest side to 256 while maintaining aspect ratio
|
| 26 |
+
width, height = pil_image.size
|
| 27 |
+
if width > height:
|
| 28 |
+
new_width = 256
|
| 29 |
+
new_height = int(height * (256 / width))
|
| 30 |
+
else:
|
| 31 |
+
new_height = 256
|
| 32 |
+
new_width = int(width * (256 / height))
|
| 33 |
+
|
| 34 |
+
resized_image = pil_image.resize((new_width, new_height), Image.LANCZOS)
|
| 35 |
+
|
| 36 |
+
# Convert back to numpy array
|
| 37 |
+
resized_array = np.array(resized_image)
|
| 38 |
|
| 39 |
# Upscale the image using AuraSR
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
upscaled_image = aura_sr.upscale_4x(resized_array)
|
| 42 |
|
| 43 |
+
# Convert result to numpy array if it's not already
|
| 44 |
result_array = np.array(upscaled_image)
|
| 45 |
|
| 46 |
return [input_array, result_array]
|
|
|
|
| 49 |
gr.Markdown("# Image Upscaler using AuraSR")
|
| 50 |
with gr.Row():
|
| 51 |
with gr.Column(scale=1):
|
| 52 |
+
input_image = gr.Image(label="Input Image", type="numpy")
|
| 53 |
process_btn = gr.Button("Upscale Image")
|
| 54 |
with gr.Column(scale=1):
|
| 55 |
output_slider = ImageSlider(label="Before / After", type="numpy")
|
|
|
|
| 60 |
outputs=output_slider
|
| 61 |
)
|
| 62 |
|
| 63 |
+
|
| 64 |
demo.launch(debug=True)
|