Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,32 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
from image_gen_aux import UpscaleWithModel
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
UPSCALE_MODEL = None
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def get_upscaler():
|
| 11 |
global UPSCALE_MODEL
|
|
@@ -13,29 +34,147 @@ def get_upscaler():
|
|
| 13 |
UPSCALE_MODEL = UpscaleWithModel.from_pretrained(MODEL_ID).to("cuda")
|
| 14 |
return UPSCALE_MODEL
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
@spaces.GPU
|
| 17 |
-
def upscale(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if image is None:
|
| 19 |
-
return None
|
| 20 |
|
| 21 |
img = Image.fromarray(image).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
upscaler = get_upscaler()
|
| 23 |
-
out = upscaler(img, tiling=True, tile_width=1024, tile_height=1024)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
with gr.Blocks() as demo:
|
|
|
|
| 30 |
gr.Markdown("# Anime Upscale Alpha")
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
run_button = gr.Button("Upscale")
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
run_button.click(
|
| 36 |
fn=upscale,
|
| 37 |
-
inputs=
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
show_progress=True
|
| 40 |
)
|
| 41 |
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
from image_gen_aux import UpscaleWithModel
|
| 8 |
|
| 9 |
+
# -----------------------------
|
| 10 |
+
# Model configuration
|
| 11 |
+
# -----------------------------
|
| 12 |
+
|
| 13 |
+
MODEL_ID = "2xEvangelion_dat2"
|
| 14 |
UPSCALE_MODEL = None
|
| 15 |
+
|
| 16 |
+
# Aspect ratios
|
| 17 |
+
RATIO_MAP = {
|
| 18 |
+
"16:9": (16, 9),
|
| 19 |
+
"9:16": (9, 16),
|
| 20 |
+
"4:5": (4, 5),
|
| 21 |
+
"1:1": (1, 1),
|
| 22 |
+
"5:4": (5, 4),
|
| 23 |
+
"2:3": (2, 3),
|
| 24 |
+
"3:2": (3, 2),
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# -----------------------------
|
| 28 |
+
# Load model once (lazy load)
|
| 29 |
+
# -----------------------------
|
| 30 |
|
| 31 |
def get_upscaler():
|
| 32 |
global UPSCALE_MODEL
|
|
|
|
| 34 |
UPSCALE_MODEL = UpscaleWithModel.from_pretrained(MODEL_ID).to("cuda")
|
| 35 |
return UPSCALE_MODEL
|
| 36 |
|
| 37 |
+
# -----------------------------
|
| 38 |
+
# Resize image to selected ratio
|
| 39 |
+
# -----------------------------
|
| 40 |
+
|
| 41 |
+
def fit_to_ratio(image: Image.Image, ratio_name: str, long_side: int):
|
| 42 |
+
|
| 43 |
+
rw, rh = RATIO_MAP[ratio_name]
|
| 44 |
+
|
| 45 |
+
if rw >= rh:
|
| 46 |
+
width = long_side
|
| 47 |
+
height = int(long_side * rh / rw)
|
| 48 |
+
else:
|
| 49 |
+
height = long_side
|
| 50 |
+
width = int(long_side * rw / rh)
|
| 51 |
+
|
| 52 |
+
width -= width % 2
|
| 53 |
+
height -= height % 2
|
| 54 |
+
|
| 55 |
+
return image.resize((width, height), Image.LANCZOS)
|
| 56 |
+
|
| 57 |
+
# -----------------------------
|
| 58 |
+
# GPU upscale function
|
| 59 |
+
# -----------------------------
|
| 60 |
+
|
| 61 |
@spaces.GPU
|
| 62 |
+
def upscale(
|
| 63 |
+
image,
|
| 64 |
+
tile_width,
|
| 65 |
+
tile_height,
|
| 66 |
+
use_ratio,
|
| 67 |
+
ratio_name,
|
| 68 |
+
target_long_side
|
| 69 |
+
):
|
| 70 |
+
|
| 71 |
if image is None:
|
| 72 |
+
return None, None
|
| 73 |
|
| 74 |
img = Image.fromarray(image).convert("RGB")
|
| 75 |
+
|
| 76 |
+
# apply ratio preset if enabled
|
| 77 |
+
if use_ratio:
|
| 78 |
+
img = fit_to_ratio(img, ratio_name, int(target_long_side))
|
| 79 |
+
|
| 80 |
upscaler = get_upscaler()
|
|
|
|
| 81 |
|
| 82 |
+
out = upscaler(
|
| 83 |
+
img,
|
| 84 |
+
tiling=True,
|
| 85 |
+
tile_width=int(tile_width),
|
| 86 |
+
tile_height=int(tile_height)
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
if not isinstance(out, Image.Image):
|
| 90 |
+
out = Image.fromarray(out)
|
| 91 |
+
|
| 92 |
+
os.makedirs("/tmp/upscaled", exist_ok=True)
|
| 93 |
+
|
| 94 |
+
file_path = f"/tmp/upscaled/{uuid.uuid4().hex}.png"
|
| 95 |
+
|
| 96 |
+
out.save(
|
| 97 |
+
file_path,
|
| 98 |
+
format="PNG",
|
| 99 |
+
compress_level=0
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
return np.array(out), file_path
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# -----------------------------
|
| 106 |
+
# UI
|
| 107 |
+
# -----------------------------
|
| 108 |
|
| 109 |
with gr.Blocks() as demo:
|
| 110 |
+
|
| 111 |
gr.Markdown("# Anime Upscale Alpha")
|
| 112 |
+
|
| 113 |
+
input_image = gr.Image(
|
| 114 |
+
type="numpy",
|
| 115 |
+
label="Input Image",
|
| 116 |
+
height=400
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
with gr.Row():
|
| 120 |
+
|
| 121 |
+
tile_width = gr.Number(
|
| 122 |
+
value=1024,
|
| 123 |
+
label="Tile Width",
|
| 124 |
+
precision=0
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
tile_height = gr.Number(
|
| 128 |
+
value=1024,
|
| 129 |
+
label="Tile Height",
|
| 130 |
+
precision=0
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
with gr.Group():
|
| 134 |
+
|
| 135 |
+
use_ratio = gr.Checkbox(
|
| 136 |
+
label="Use Aspect Ratio Preset",
|
| 137 |
+
value=False
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
ratio_name = gr.Radio(
|
| 141 |
+
choices=["16:9", "9:16", "4:5", "1:1", "5:4", "2:3", "3:2"],
|
| 142 |
+
value="1:1",
|
| 143 |
+
label="Aspect Ratio"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
target_long_side = gr.Number(
|
| 147 |
+
value=2048,
|
| 148 |
+
label="Target Long Side (px)",
|
| 149 |
+
precision=0
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
run_button = gr.Button("Upscale")
|
| 153 |
+
|
| 154 |
+
output_image = gr.Image(
|
| 155 |
+
type="numpy",
|
| 156 |
+
label="Upscaled Preview",
|
| 157 |
+
height=400
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
download_file = gr.File(
|
| 161 |
+
label="Download Full PNG"
|
| 162 |
+
)
|
| 163 |
|
| 164 |
run_button.click(
|
| 165 |
fn=upscale,
|
| 166 |
+
inputs=[
|
| 167 |
+
input_image,
|
| 168 |
+
tile_width,
|
| 169 |
+
tile_height,
|
| 170 |
+
use_ratio,
|
| 171 |
+
ratio_name,
|
| 172 |
+
target_long_side
|
| 173 |
+
],
|
| 174 |
+
outputs=[
|
| 175 |
+
output_image,
|
| 176 |
+
download_file
|
| 177 |
+
],
|
| 178 |
show_progress=True
|
| 179 |
)
|
| 180 |
|