Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,14 +6,17 @@ import numpy as np
|
|
| 6 |
from PIL import Image
|
| 7 |
from image_gen_aux import UpscaleWithModel
|
| 8 |
|
| 9 |
-
# -----------------------------
|
| 10 |
# Model configuration
|
| 11 |
-
# -----------------------------
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# Aspect ratios
|
| 17 |
RATIO_MAP = {
|
| 18 |
"16:9": (16, 9),
|
| 19 |
"9:16": (9, 16),
|
|
@@ -24,91 +27,83 @@ RATIO_MAP = {
|
|
| 24 |
"3:2": (3, 2),
|
| 25 |
}
|
| 26 |
|
| 27 |
-
# -----------------------------
|
| 28 |
-
#
|
| 29 |
-
# -----------------------------
|
| 30 |
|
| 31 |
-
def get_upscaler():
|
| 32 |
-
global
|
| 33 |
-
if UPSCALE_MODEL is None:
|
| 34 |
-
UPSCALE_MODEL = UpscaleWithModel.from_pretrained(MODEL_ID).to("cuda")
|
| 35 |
-
return UPSCALE_MODEL
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
|
|
|
|
|
|
|
| 43 |
rw, rh = RATIO_MAP[ratio_name]
|
| 44 |
|
| 45 |
if rw >= rh:
|
| 46 |
-
width = long_side
|
| 47 |
-
height =
|
| 48 |
else:
|
| 49 |
-
height = long_side
|
| 50 |
-
width =
|
| 51 |
|
| 52 |
-
width
|
| 53 |
-
height
|
| 54 |
|
| 55 |
return image.resize((width, height), Image.LANCZOS)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
#
|
| 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",
|
|
@@ -116,8 +111,13 @@ with gr.Blocks() as demo:
|
|
| 116 |
height=400
|
| 117 |
)
|
| 118 |
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
|
|
|
| 121 |
tile_width = gr.Number(
|
| 122 |
value=1024,
|
| 123 |
label="Tile Width",
|
|
@@ -131,7 +131,6 @@ with gr.Blocks() as demo:
|
|
| 131 |
)
|
| 132 |
|
| 133 |
with gr.Group():
|
| 134 |
-
|
| 135 |
use_ratio = gr.Checkbox(
|
| 136 |
label="Use Aspect Ratio Preset",
|
| 137 |
value=False
|
|
@@ -149,11 +148,11 @@ with gr.Blocks() as demo:
|
|
| 149 |
precision=0
|
| 150 |
)
|
| 151 |
|
| 152 |
-
run_button = gr.Button("
|
| 153 |
|
| 154 |
output_image = gr.Image(
|
| 155 |
type="numpy",
|
| 156 |
-
label="
|
| 157 |
height=400
|
| 158 |
)
|
| 159 |
|
|
@@ -165,15 +164,16 @@ with gr.Blocks() as demo:
|
|
| 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 |
)
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
from image_gen_aux import UpscaleWithModel
|
| 8 |
|
| 9 |
+
# ---------------------------------
|
| 10 |
# Model configuration
|
| 11 |
+
# ---------------------------------
|
| 12 |
|
| 13 |
+
MODEL_MAP = {
|
| 14 |
+
"Anime Enhancer": "OzzyGT/4xRemacri",
|
| 15 |
+
"Photo Enhancer": "OzzyGT/4xNomosWebPhoto_RealPLKSR",
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
MODEL_CACHE = {}
|
| 19 |
|
|
|
|
| 20 |
RATIO_MAP = {
|
| 21 |
"16:9": (16, 9),
|
| 22 |
"9:16": (9, 16),
|
|
|
|
| 27 |
"3:2": (3, 2),
|
| 28 |
}
|
| 29 |
|
| 30 |
+
# ---------------------------------
|
| 31 |
+
# Helpers
|
| 32 |
+
# ---------------------------------
|
| 33 |
|
| 34 |
+
def get_upscaler(enhancer_type: str):
|
| 35 |
+
global MODEL_CACHE
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
if enhancer_type not in MODEL_CACHE:
|
| 38 |
+
model_id = MODEL_MAP[enhancer_type]
|
| 39 |
+
MODEL_CACHE[enhancer_type] = UpscaleWithModel.from_pretrained(model_id).to("cuda")
|
| 40 |
|
| 41 |
+
return MODEL_CACHE[enhancer_type]
|
| 42 |
|
| 43 |
+
|
| 44 |
+
def fit_to_ratio(image: Image.Image, ratio_name: str, long_side: int) -> Image.Image:
|
| 45 |
rw, rh = RATIO_MAP[ratio_name]
|
| 46 |
|
| 47 |
if rw >= rh:
|
| 48 |
+
width = int(long_side)
|
| 49 |
+
height = round(long_side * rh / rw)
|
| 50 |
else:
|
| 51 |
+
height = int(long_side)
|
| 52 |
+
width = round(long_side * rw / rh)
|
| 53 |
|
| 54 |
+
width = max(2, width - (width % 2))
|
| 55 |
+
height = max(2, height - (height % 2))
|
| 56 |
|
| 57 |
return image.resize((width, height), Image.LANCZOS)
|
| 58 |
|
| 59 |
+
|
| 60 |
+
# ---------------------------------
|
| 61 |
+
# GPU function
|
| 62 |
+
# ---------------------------------
|
| 63 |
|
| 64 |
@spaces.GPU
|
| 65 |
def upscale(
|
| 66 |
image,
|
| 67 |
+
enhancer_type,
|
| 68 |
tile_width,
|
| 69 |
tile_height,
|
| 70 |
use_ratio,
|
| 71 |
ratio_name,
|
| 72 |
+
target_long_side,
|
| 73 |
):
|
|
|
|
| 74 |
if image is None:
|
| 75 |
return None, None
|
| 76 |
|
| 77 |
img = Image.fromarray(image).convert("RGB")
|
| 78 |
|
|
|
|
| 79 |
if use_ratio:
|
| 80 |
img = fit_to_ratio(img, ratio_name, int(target_long_side))
|
| 81 |
|
| 82 |
+
upscaler = get_upscaler(enhancer_type)
|
| 83 |
|
| 84 |
out = upscaler(
|
| 85 |
img,
|
| 86 |
tiling=True,
|
| 87 |
tile_width=int(tile_width),
|
| 88 |
+
tile_height=int(tile_height),
|
| 89 |
)
|
| 90 |
|
| 91 |
if not isinstance(out, Image.Image):
|
| 92 |
out = Image.fromarray(out)
|
| 93 |
|
| 94 |
os.makedirs("/tmp/upscaled", exist_ok=True)
|
|
|
|
| 95 |
file_path = f"/tmp/upscaled/{uuid.uuid4().hex}.png"
|
| 96 |
+
out.save(file_path, format="PNG", compress_level=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
return np.array(out), file_path
|
| 99 |
|
| 100 |
|
| 101 |
+
# ---------------------------------
|
| 102 |
# UI
|
| 103 |
+
# ---------------------------------
|
| 104 |
|
| 105 |
with gr.Blocks() as demo:
|
| 106 |
+
gr.Markdown("# Image Enhancer")
|
|
|
|
| 107 |
|
| 108 |
input_image = gr.Image(
|
| 109 |
type="numpy",
|
|
|
|
| 111 |
height=400
|
| 112 |
)
|
| 113 |
|
| 114 |
+
enhancer_type = gr.Radio(
|
| 115 |
+
choices=["Anime Enhancer", "Photo Enhancer"],
|
| 116 |
+
value="Anime Enhancer",
|
| 117 |
+
label="Enhancer Type"
|
| 118 |
+
)
|
| 119 |
|
| 120 |
+
with gr.Row():
|
| 121 |
tile_width = gr.Number(
|
| 122 |
value=1024,
|
| 123 |
label="Tile Width",
|
|
|
|
| 131 |
)
|
| 132 |
|
| 133 |
with gr.Group():
|
|
|
|
| 134 |
use_ratio = gr.Checkbox(
|
| 135 |
label="Use Aspect Ratio Preset",
|
| 136 |
value=False
|
|
|
|
| 148 |
precision=0
|
| 149 |
)
|
| 150 |
|
| 151 |
+
run_button = gr.Button("Enhance 4x")
|
| 152 |
|
| 153 |
output_image = gr.Image(
|
| 154 |
type="numpy",
|
| 155 |
+
label="Enhanced Preview",
|
| 156 |
height=400
|
| 157 |
)
|
| 158 |
|
|
|
|
| 164 |
fn=upscale,
|
| 165 |
inputs=[
|
| 166 |
input_image,
|
| 167 |
+
enhancer_type,
|
| 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 |
)
|