Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import uuid
|
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
from image_gen_aux import UpscaleWithModel
|
| 7 |
|
| 8 |
# ---------------------------------
|
|
@@ -11,16 +12,29 @@ from image_gen_aux import UpscaleWithModel
|
|
| 11 |
|
| 12 |
BASE_TMP_DIR = "/tmp/image_enhancer"
|
| 13 |
ENHANCED_DIR = os.path.join(BASE_TMP_DIR, "enhanced")
|
|
|
|
|
|
|
| 14 |
os.makedirs(ENHANCED_DIR, exist_ok=True)
|
|
|
|
| 15 |
|
| 16 |
# ---------------------------------
|
| 17 |
# Model configuration
|
| 18 |
# ---------------------------------
|
| 19 |
-
# Swap only these repo IDs if you want to test different models later.
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
MODEL_CACHE = {}
|
| 26 |
|
|
@@ -35,9 +49,7 @@ RATIO_MAP = {
|
|
| 35 |
}
|
| 36 |
|
| 37 |
MODE_CHOICES = [
|
| 38 |
-
"2x High Fidelity",
|
| 39 |
"4x High Fidelity",
|
| 40 |
-
"4x Fast / Medium Fidelity",
|
| 41 |
"8x Multi-Pass (Drift Likely)",
|
| 42 |
]
|
| 43 |
|
|
@@ -53,11 +65,23 @@ REDUCTION_DISCLAIMER = (
|
|
| 53 |
# Helpers
|
| 54 |
# ---------------------------------
|
| 55 |
|
| 56 |
-
def get_model(
|
| 57 |
global MODEL_CACHE
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
def get_tile_dimensions(ratio_name: str, tile_preset: str):
|
|
@@ -109,17 +133,19 @@ def build_stats_markdown(
|
|
| 109 |
mode_name: str,
|
| 110 |
reduction_choice: str,
|
| 111 |
reduction_applied: bool,
|
|
|
|
| 112 |
):
|
| 113 |
reduction_status = reduction_choice if reduction_applied else "Ignored / Not Applied"
|
| 114 |
|
| 115 |
return (
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
f"**Original Dimensions:** {original_width} × {original_height}px \n"
|
| 117 |
f"**Original Megapixels:** {format_megapixels(original_width, original_height)} \n\n"
|
| 118 |
f"**Enhanced Dimensions:** {enhanced_width} × {enhanced_height}px \n"
|
| 119 |
f"**Enhanced Megapixels:** {format_megapixels(enhanced_width, enhanced_height)} \n\n"
|
| 120 |
-
f"**Mode:** {mode_name} \n"
|
| 121 |
-
f"**Export Format:** {export_format} \n"
|
| 122 |
-
f"**Hi-Fi Output Reduction:** {reduction_status} \n"
|
| 123 |
f"**Saved File Size:** {format_file_size(file_size_bytes)}"
|
| 124 |
)
|
| 125 |
|
|
@@ -150,8 +176,8 @@ def apply_output_reduction(img: Image.Image, reduction_choice: str):
|
|
| 150 |
return img.resize((new_width, new_height), Image.LANCZOS)
|
| 151 |
|
| 152 |
|
| 153 |
-
def upscale_once(img: Image.Image,
|
| 154 |
-
upscaler = get_model(
|
| 155 |
out = upscaler(
|
| 156 |
img,
|
| 157 |
tiling=True,
|
|
@@ -167,33 +193,20 @@ def upscale_once(img: Image.Image, model_id: str, tile_width: int, tile_height:
|
|
| 167 |
|
| 168 |
def run_mode_pipeline(
|
| 169 |
img: Image.Image,
|
| 170 |
-
|
| 171 |
mode_name: str,
|
| 172 |
tile_width: int,
|
| 173 |
tile_height: int,
|
| 174 |
):
|
| 175 |
-
# Conservative Hi-Fi branch uses true 2x model.
|
| 176 |
-
if mode_name == "2x High Fidelity":
|
| 177 |
-
return upscale_once(img, ANIME_HIFI_2X_MODEL, tile_width, tile_height)
|
| 178 |
-
|
| 179 |
if mode_name == "4x High Fidelity":
|
| 180 |
-
|
| 181 |
-
second = upscale_once(first, ANIME_HIFI_2X_MODEL, tile_width, tile_height)
|
| 182 |
-
return second
|
| 183 |
-
|
| 184 |
-
# Rough capability / direct 4x branch
|
| 185 |
-
if mode_name == "4x Fast / Medium Fidelity":
|
| 186 |
-
if enhancer_type == "Photo Enhancer":
|
| 187 |
-
return upscale_once(img, FAST_PHOTO_4X_MODEL, tile_width, tile_height)
|
| 188 |
-
return upscale_once(img, FAST_ANIME_4X_MODEL, tile_width, tile_height)
|
| 189 |
|
| 190 |
if mode_name == "8x Multi-Pass (Drift Likely)":
|
| 191 |
-
first = upscale_once(img,
|
| 192 |
-
second =
|
| 193 |
-
|
| 194 |
-
return third
|
| 195 |
|
| 196 |
-
return upscale_once(img,
|
| 197 |
|
| 198 |
|
| 199 |
def save_output_image(output_img: Image.Image, export_format: str):
|
|
@@ -210,10 +223,6 @@ def save_output_image(output_img: Image.Image, export_format: str):
|
|
| 210 |
return path
|
| 211 |
|
| 212 |
|
| 213 |
-
def is_hifi_mode(mode_name: str):
|
| 214 |
-
return mode_name in {"2x High Fidelity", "4x High Fidelity"}
|
| 215 |
-
|
| 216 |
-
|
| 217 |
# ---------------------------------
|
| 218 |
# GPU function
|
| 219 |
# ---------------------------------
|
|
@@ -221,7 +230,7 @@ def is_hifi_mode(mode_name: str):
|
|
| 221 |
@spaces.GPU
|
| 222 |
def enhance_image(
|
| 223 |
reduction_choice,
|
| 224 |
-
|
| 225 |
mode_name,
|
| 226 |
ratio_name,
|
| 227 |
tile_preset,
|
|
@@ -238,14 +247,14 @@ def enhance_image(
|
|
| 238 |
|
| 239 |
enhanced_img = run_mode_pipeline(
|
| 240 |
img=original_img,
|
| 241 |
-
|
| 242 |
mode_name=mode_name,
|
| 243 |
tile_width=tile_width,
|
| 244 |
tile_height=tile_height,
|
| 245 |
)
|
| 246 |
|
| 247 |
reduction_applied = False
|
| 248 |
-
if
|
| 249 |
enhanced_img = apply_output_reduction(enhanced_img, reduction_choice)
|
| 250 |
reduction_applied = True
|
| 251 |
|
|
@@ -264,6 +273,7 @@ def enhance_image(
|
|
| 264 |
mode_name=mode_name,
|
| 265 |
reduction_choice=reduction_choice,
|
| 266 |
reduction_applied=reduction_applied,
|
|
|
|
| 267 |
)
|
| 268 |
|
| 269 |
return enhanced_img, output_path, stats_markdown
|
|
@@ -287,17 +297,17 @@ with gr.Blocks() as demo:
|
|
| 287 |
label="Reduction Amount"
|
| 288 |
)
|
| 289 |
|
| 290 |
-
# 1.
|
| 291 |
with gr.Group():
|
| 292 |
-
|
| 293 |
-
choices=["
|
| 294 |
-
value="
|
| 295 |
-
label="
|
| 296 |
)
|
| 297 |
|
| 298 |
mode_name = gr.Radio(
|
| 299 |
choices=MODE_CHOICES,
|
| 300 |
-
value="
|
| 301 |
label="Processing Mode"
|
| 302 |
)
|
| 303 |
|
|
@@ -307,7 +317,7 @@ with gr.Blocks() as demo:
|
|
| 307 |
|
| 308 |
ratio_name = gr.Radio(
|
| 309 |
choices=["16:9", "9:16", "4:5", "1:1", "5:4", "2:3", "3:2"],
|
| 310 |
-
value="
|
| 311 |
label="Aspect Ratio"
|
| 312 |
)
|
| 313 |
|
|
@@ -318,7 +328,7 @@ with gr.Blocks() as demo:
|
|
| 318 |
)
|
| 319 |
|
| 320 |
tile_display = gr.Markdown(
|
| 321 |
-
value=update_tile_display("
|
| 322 |
)
|
| 323 |
|
| 324 |
# 2.5 Output Settings
|
|
@@ -380,7 +390,7 @@ with gr.Blocks() as demo:
|
|
| 380 |
fn=enhance_image,
|
| 381 |
inputs=[
|
| 382 |
reduction_choice,
|
| 383 |
-
|
| 384 |
mode_name,
|
| 385 |
ratio_name,
|
| 386 |
tile_preset,
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
from PIL import Image
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
from image_gen_aux import UpscaleWithModel
|
| 8 |
|
| 9 |
# ---------------------------------
|
|
|
|
| 12 |
|
| 13 |
BASE_TMP_DIR = "/tmp/image_enhancer"
|
| 14 |
ENHANCED_DIR = os.path.join(BASE_TMP_DIR, "enhanced")
|
| 15 |
+
MODEL_DIR = os.path.join(BASE_TMP_DIR, "models")
|
| 16 |
+
|
| 17 |
os.makedirs(ENHANCED_DIR, exist_ok=True)
|
| 18 |
+
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 19 |
|
| 20 |
# ---------------------------------
|
| 21 |
# Model configuration
|
| 22 |
# ---------------------------------
|
|
|
|
| 23 |
|
| 24 |
+
MODEL_SPECS = {
|
| 25 |
+
"AnimeSharp": {
|
| 26 |
+
"repo_id": "Kim2091/AnimeSharp",
|
| 27 |
+
"filename": "4x-AnimeSharp.pth",
|
| 28 |
+
},
|
| 29 |
+
"UltraSharp": {
|
| 30 |
+
"repo_id": "Kim2091/UltraSharp",
|
| 31 |
+
"filename": "4x-UltraSharp.pth",
|
| 32 |
+
},
|
| 33 |
+
"UltraMix Balanced": {
|
| 34 |
+
"repo_id": "LykosAI/Upscalers",
|
| 35 |
+
"filename": "UltraMix/4x-UltraMix_Balanced.pth",
|
| 36 |
+
},
|
| 37 |
+
}
|
| 38 |
|
| 39 |
MODEL_CACHE = {}
|
| 40 |
|
|
|
|
| 49 |
}
|
| 50 |
|
| 51 |
MODE_CHOICES = [
|
|
|
|
| 52 |
"4x High Fidelity",
|
|
|
|
| 53 |
"8x Multi-Pass (Drift Likely)",
|
| 54 |
]
|
| 55 |
|
|
|
|
| 65 |
# Helpers
|
| 66 |
# ---------------------------------
|
| 67 |
|
| 68 |
+
def get_model(model_name: str):
|
| 69 |
global MODEL_CACHE
|
| 70 |
+
|
| 71 |
+
if model_name in MODEL_CACHE:
|
| 72 |
+
return MODEL_CACHE[model_name]
|
| 73 |
+
|
| 74 |
+
spec = MODEL_SPECS[model_name]
|
| 75 |
+
|
| 76 |
+
local_path = hf_hub_download(
|
| 77 |
+
repo_id=spec["repo_id"],
|
| 78 |
+
filename=spec["filename"],
|
| 79 |
+
local_dir=MODEL_DIR,
|
| 80 |
+
local_dir_use_symlinks=False,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
MODEL_CACHE[model_name] = UpscaleWithModel.from_pretrained(local_path).to("cuda")
|
| 84 |
+
return MODEL_CACHE[model_name]
|
| 85 |
|
| 86 |
|
| 87 |
def get_tile_dimensions(ratio_name: str, tile_preset: str):
|
|
|
|
| 133 |
mode_name: str,
|
| 134 |
reduction_choice: str,
|
| 135 |
reduction_applied: bool,
|
| 136 |
+
model_name: str,
|
| 137 |
):
|
| 138 |
reduction_status = reduction_choice if reduction_applied else "Ignored / Not Applied"
|
| 139 |
|
| 140 |
return (
|
| 141 |
+
f"**Model:** {model_name} \n"
|
| 142 |
+
f"**Mode:** {mode_name} \n"
|
| 143 |
+
f"**Export Format:** {export_format} \n"
|
| 144 |
+
f"**Hi-Fi Output Reduction:** {reduction_status} \n\n"
|
| 145 |
f"**Original Dimensions:** {original_width} × {original_height}px \n"
|
| 146 |
f"**Original Megapixels:** {format_megapixels(original_width, original_height)} \n\n"
|
| 147 |
f"**Enhanced Dimensions:** {enhanced_width} × {enhanced_height}px \n"
|
| 148 |
f"**Enhanced Megapixels:** {format_megapixels(enhanced_width, enhanced_height)} \n\n"
|
|
|
|
|
|
|
|
|
|
| 149 |
f"**Saved File Size:** {format_file_size(file_size_bytes)}"
|
| 150 |
)
|
| 151 |
|
|
|
|
| 176 |
return img.resize((new_width, new_height), Image.LANCZOS)
|
| 177 |
|
| 178 |
|
| 179 |
+
def upscale_once(img: Image.Image, model_name: str, tile_width: int, tile_height: int):
|
| 180 |
+
upscaler = get_model(model_name)
|
| 181 |
out = upscaler(
|
| 182 |
img,
|
| 183 |
tiling=True,
|
|
|
|
| 193 |
|
| 194 |
def run_mode_pipeline(
|
| 195 |
img: Image.Image,
|
| 196 |
+
model_name: str,
|
| 197 |
mode_name: str,
|
| 198 |
tile_width: int,
|
| 199 |
tile_height: int,
|
| 200 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
if mode_name == "4x High Fidelity":
|
| 202 |
+
return upscale_once(img, model_name, tile_width, tile_height)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
if mode_name == "8x Multi-Pass (Drift Likely)":
|
| 205 |
+
first = upscale_once(img, model_name, tile_width, tile_height)
|
| 206 |
+
second = first.resize((img.width * 8, img.height * 8), Image.LANCZOS)
|
| 207 |
+
return second.convert("RGB")
|
|
|
|
| 208 |
|
| 209 |
+
return upscale_once(img, model_name, tile_width, tile_height)
|
| 210 |
|
| 211 |
|
| 212 |
def save_output_image(output_img: Image.Image, export_format: str):
|
|
|
|
| 223 |
return path
|
| 224 |
|
| 225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
# ---------------------------------
|
| 227 |
# GPU function
|
| 228 |
# ---------------------------------
|
|
|
|
| 230 |
@spaces.GPU
|
| 231 |
def enhance_image(
|
| 232 |
reduction_choice,
|
| 233 |
+
model_name,
|
| 234 |
mode_name,
|
| 235 |
ratio_name,
|
| 236 |
tile_preset,
|
|
|
|
| 247 |
|
| 248 |
enhanced_img = run_mode_pipeline(
|
| 249 |
img=original_img,
|
| 250 |
+
model_name=model_name,
|
| 251 |
mode_name=mode_name,
|
| 252 |
tile_width=tile_width,
|
| 253 |
tile_height=tile_height,
|
| 254 |
)
|
| 255 |
|
| 256 |
reduction_applied = False
|
| 257 |
+
if mode_name == "4x High Fidelity" and reduction_choice != "Off":
|
| 258 |
enhanced_img = apply_output_reduction(enhanced_img, reduction_choice)
|
| 259 |
reduction_applied = True
|
| 260 |
|
|
|
|
| 273 |
mode_name=mode_name,
|
| 274 |
reduction_choice=reduction_choice,
|
| 275 |
reduction_applied=reduction_applied,
|
| 276 |
+
model_name=model_name,
|
| 277 |
)
|
| 278 |
|
| 279 |
return enhanced_img, output_path, stats_markdown
|
|
|
|
| 297 |
label="Reduction Amount"
|
| 298 |
)
|
| 299 |
|
| 300 |
+
# 1. Model / Mode box
|
| 301 |
with gr.Group():
|
| 302 |
+
model_name = gr.Radio(
|
| 303 |
+
choices=["AnimeSharp", "UltraSharp", "UltraMix Balanced"],
|
| 304 |
+
value="AnimeSharp",
|
| 305 |
+
label="Reconstruction Model"
|
| 306 |
)
|
| 307 |
|
| 308 |
mode_name = gr.Radio(
|
| 309 |
choices=MODE_CHOICES,
|
| 310 |
+
value="4x High Fidelity",
|
| 311 |
label="Processing Mode"
|
| 312 |
)
|
| 313 |
|
|
|
|
| 317 |
|
| 318 |
ratio_name = gr.Radio(
|
| 319 |
choices=["16:9", "9:16", "4:5", "1:1", "5:4", "2:3", "3:2"],
|
| 320 |
+
value="2:3",
|
| 321 |
label="Aspect Ratio"
|
| 322 |
)
|
| 323 |
|
|
|
|
| 328 |
)
|
| 329 |
|
| 330 |
tile_display = gr.Markdown(
|
| 331 |
+
value=update_tile_display("2:3", "768")
|
| 332 |
)
|
| 333 |
|
| 334 |
# 2.5 Output Settings
|
|
|
|
| 390 |
fn=enhance_image,
|
| 391 |
inputs=[
|
| 392 |
reduction_choice,
|
| 393 |
+
model_name,
|
| 394 |
mode_name,
|
| 395 |
ratio_name,
|
| 396 |
tile_preset,
|