Spaces:
Running on Zero
Running on Zero
Add FLUX.2 VAE latent space (Steppe-Flux.2, default) with VAE selector
Browse files- .ruff_cache/.gitignore +2 -0
- .ruff_cache/0.17.6/6528528170386474551 +0 -0
- .ruff_cache/CACHEDIR.TAG +1 -0
- README.md +17 -8
- __pycache__/app.cpython-312.pyc +0 -0
- app.py +127 -42
- requirements.txt +2 -2
.ruff_cache/.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Automatically created by ruff.
|
| 2 |
+
*
|
.ruff_cache/0.17.6/6528528170386474551
ADDED
|
Binary file (148 Bytes). View file
|
|
|
.ruff_cache/CACHEDIR.TAG
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Signature: 8a477f597d28d172789f06886806bc55
|
README.md
CHANGED
|
@@ -7,32 +7,41 @@ sdk: gradio
|
|
| 7 |
sdk_version: 6.32.0
|
| 8 |
app_file: app.py
|
| 9 |
python_version: "3.12"
|
| 10 |
-
short_description: Generative 2x / 4x upscaling in Wan 2.1 VAE
|
| 11 |
license: mit
|
| 12 |
models:
|
|
|
|
| 13 |
- TensorForger/Steppe-Wan2.1
|
|
|
|
| 14 |
- Wan-AI/Wan2.1-T2V-1.3B-Diffusers
|
| 15 |
tags:
|
| 16 |
- image-upscaling
|
| 17 |
- super-resolution
|
| 18 |
- latent-upscaler
|
|
|
|
| 19 |
- wan
|
| 20 |
---
|
| 21 |
|
| 22 |
-
# Steppe latent upscaler (Wan 2.1 VAE)
|
| 23 |
|
| 24 |
-
Demo of
|
| 25 |
-
Wan 2.1 VAE (Wan 2.1, Wan 2.2 14B, Qwen-Image, ...). The image is encoded with the Wan 2.1 VAE, upscaled in latent
|
| 26 |
-
space by the 200M single step model and decoded directly by the VAE. 4x chains two 2x passes in latent space.
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
[ComfyUI nodes](https://github.com/TensorForger/ComfyUI-Steppe).
|
| 30 |
|
| 31 |
-
Settings (same as the `Steppe Upscale Latent` node):
|
| 32 |
|
| 33 |
- **hff**: target sharpness / fine detail [0...1]; -1 = the model decides itself. Default 0.9.
|
| 34 |
- **guidance**: distilled guidance, 1 = off (default), 2 also works well.
|
| 35 |
- **seed**: noise seed (the second 4x pass uses seed + 1).
|
| 36 |
- **noise_scale**: initial noise temperature, -1 = trained default (1.0).
|
| 37 |
|
| 38 |
-
Example images were generated with Krea 2. License: MIT (Wan 2.1 VAE: Apache-2.0).
|
|
|
|
| 7 |
sdk_version: 6.32.0
|
| 8 |
app_file: app.py
|
| 9 |
python_version: "3.12"
|
| 10 |
+
short_description: Generative 2x / 4x upscaling in FLUX.2 / Wan 2.1 VAE latents
|
| 11 |
license: mit
|
| 12 |
models:
|
| 13 |
+
- TensorForger/Steppe-Flux.2
|
| 14 |
- TensorForger/Steppe-Wan2.1
|
| 15 |
+
- black-forest-labs/FLUX.2-klein-4B
|
| 16 |
- Wan-AI/Wan2.1-T2V-1.3B-Diffusers
|
| 17 |
tags:
|
| 18 |
- image-upscaling
|
| 19 |
- super-resolution
|
| 20 |
- latent-upscaler
|
| 21 |
+
- flux2
|
| 22 |
- wan
|
| 23 |
---
|
| 24 |
|
| 25 |
+
# Steppe latent upscaler (FLUX.2 VAE / Wan 2.1 VAE)
|
| 26 |
|
| 27 |
+
Demo of the Steppe generative 2x latent upscalers:
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
- [Steppe-Flux.2](https://huggingface.co/TensorForger/Steppe-Flux.2) for the FLUX.2 VAE (FLUX.2 [dev] / [klein],
|
| 30 |
+
MOSS-Image, ...). **Default**, best quality.
|
| 31 |
+
- [Steppe-Wan2.1](https://huggingface.co/TensorForger/Steppe-Wan2.1) for the Wan 2.1 VAE (Wan 2.1, Wan 2.2 14B,
|
| 32 |
+
Qwen-Image, Krea 2, ...).
|
| 33 |
+
|
| 34 |
+
Select the **VAE latent space**: the image is encoded with that VAE, upscaled in latent space by its 200M single step
|
| 35 |
+
model and decoded directly by the VAE. 4x chains two 2x passes in latent space.
|
| 36 |
+
|
| 37 |
+
Images only; for video (spatial and 2x frame rate, Wan 2.1 VAE) and the other models use the
|
| 38 |
[ComfyUI nodes](https://github.com/TensorForger/ComfyUI-Steppe).
|
| 39 |
|
| 40 |
+
Settings (same as the `Steppe Upscale Latent` node, same defaults for both latent spaces):
|
| 41 |
|
| 42 |
- **hff**: target sharpness / fine detail [0...1]; -1 = the model decides itself. Default 0.9.
|
| 43 |
- **guidance**: distilled guidance, 1 = off (default), 2 also works well.
|
| 44 |
- **seed**: noise seed (the second 4x pass uses seed + 1).
|
| 45 |
- **noise_scale**: initial noise temperature, -1 = trained default (1.0).
|
| 46 |
|
| 47 |
+
Example images were generated with Krea 2. License: MIT (FLUX.2 VAE and Wan 2.1 VAE: Apache-2.0).
|
__pycache__/app.cpython-312.pyc
DELETED
|
Binary file (13.4 kB)
|
|
|
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
"""
|
| 2 |
-
Steppe Space: generative 2x / chained 4x image upscaling in the Wan 2.1 VAE latent space.
|
| 3 |
|
| 4 |
-
image ->
|
| 5 |
The latents are upscaled directly; no extra denoising pass of a diffusion model is needed.
|
| 6 |
"""
|
| 7 |
|
|
@@ -15,23 +15,22 @@ import math # noqa: E402
|
|
| 15 |
import random # noqa: E402
|
| 16 |
import tempfile # noqa: E402
|
| 17 |
import time # noqa: E402
|
|
|
|
|
|
|
| 18 |
|
| 19 |
import gradio as gr # noqa: E402
|
| 20 |
import numpy as np # noqa: E402
|
| 21 |
import torch # noqa: E402
|
| 22 |
import torch.nn.functional as functional # noqa: E402
|
| 23 |
-
from diffusers import AutoencoderKLWan # noqa: E402
|
| 24 |
from PIL import Image # noqa: E402
|
| 25 |
-
from steppe import UpscaleSettings, load_upscaler # noqa: E402
|
| 26 |
|
| 27 |
-
MODEL_FILE = "steppe_wan_2_1_200M_1step.safetensors"
|
| 28 |
-
VAE_REPOSITORY = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
|
| 29 |
MAX_INPUT_PIXELS = 1280 * 1024
|
| 30 |
MAX_SEED = 2**31 - 1
|
| 31 |
-
VAE_MULTIPLE = 8
|
| 32 |
-
VAE_TILE = 1024
|
| 33 |
-
VAE_TILE_STRIDE = 896
|
| 34 |
FACTORS = {"2x": 1, "4x (2x → 2x)": 2}
|
|
|
|
|
|
|
| 35 |
|
| 36 |
HFF_INFO = (
|
| 37 |
"Target sharpness / fine detail of the result, [0...1]. -1 = no hff, the model decides itself."
|
|
@@ -39,10 +38,79 @@ HFF_INFO = (
|
|
| 39 |
GUIDANCE_INFO = "Distilled guidance (trained on 1-3, no extra cost). 1 = off (default, safe and stable), 2 also works well."
|
| 40 |
NOISE_SCALE_INFO = "Initial noise temperature. -1 = trained default (1.0)."
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
def limit_size(image: Image.Image) -> tuple[Image.Image, str]:
|
|
@@ -59,26 +127,26 @@ def limit_size(image: Image.Image) -> tuple[Image.Image, str]:
|
|
| 59 |
return image.resize(size, Image.LANCZOS), note
|
| 60 |
|
| 61 |
|
| 62 |
-
def to_pixels(image: Image.Image) -> torch.Tensor:
|
| 63 |
"""
|
| 64 |
returns:
|
| 65 |
-
pixels: [1, 3,
|
| 66 |
"""
|
| 67 |
array = torch.from_numpy(np.asarray(image, dtype=np.float32) / 127.5 - 1.0)
|
| 68 |
pixels = array.permute(2, 0, 1)[None]
|
| 69 |
-
pad_height = -pixels.shape[2] %
|
| 70 |
-
pad_width = -pixels.shape[3] %
|
| 71 |
if pad_height or pad_width:
|
| 72 |
pixels = functional.pad(pixels, (0, pad_width, 0, pad_height), mode="replicate")
|
| 73 |
-
return pixels
|
| 74 |
|
| 75 |
|
| 76 |
def to_image(pixels: torch.Tensor, width: int, height: int) -> Image.Image:
|
| 77 |
"""
|
| 78 |
args:
|
| 79 |
-
pixels: [1, 3,
|
| 80 |
"""
|
| 81 |
-
array = ((pixels[0, :,
|
| 82 |
return Image.fromarray(array.permute(1, 2, 0).cpu().numpy())
|
| 83 |
|
| 84 |
|
|
@@ -88,7 +156,7 @@ def estimate_duration(image=None, scale="2x", *args, **kwargs) -> int:
|
|
| 88 |
pixels = min(image.width * image.height, MAX_INPUT_PIXELS)
|
| 89 |
passes = FACTORS.get(scale, 1)
|
| 90 |
output_megapixels = pixels * 4**passes / 1e6
|
| 91 |
-
return int(min(60,
|
| 92 |
|
| 93 |
|
| 94 |
@spaces.GPU(duration=estimate_duration)
|
|
@@ -100,10 +168,11 @@ def upscale(
|
|
| 100 |
seed: int = 0,
|
| 101 |
randomize_seed: bool = True,
|
| 102 |
noise_scale: float = -1.0,
|
|
|
|
| 103 |
) -> tuple[tuple[Image.Image, Image.Image], str, int, str]:
|
| 104 |
"""
|
| 105 |
-
Upscales an image 2x or 4x (two chained 2x passes) with the Steppe generative latent upscaler in the
|
| 106 |
-
latent space (encode, upscale the latents in one step, decode).
|
| 107 |
|
| 108 |
Args:
|
| 109 |
image: input image (RGB). Larger than 1.3 MP is downscaled first.
|
|
@@ -113,6 +182,7 @@ def upscale(
|
|
| 113 |
seed: random seed of the generative upscaler (the second 4x pass uses seed + 1).
|
| 114 |
randomize_seed: pick a random seed instead of `seed`.
|
| 115 |
noise_scale: initial noise temperature, -1 = trained default (1.0).
|
|
|
|
| 116 |
|
| 117 |
Returns:
|
| 118 |
(bicubic, steppe) image pair, path of the full resolution PNG, the used seed, status text.
|
|
@@ -122,6 +192,9 @@ def upscale(
|
|
| 122 |
if randomize_seed:
|
| 123 |
seed = random.randint(0, MAX_SEED)
|
| 124 |
seed = int(seed)
|
|
|
|
|
|
|
|
|
|
| 125 |
passes = FACTORS[scale]
|
| 126 |
factor = 2**passes
|
| 127 |
started = time.perf_counter()
|
|
@@ -129,7 +202,7 @@ def upscale(
|
|
| 129 |
image, note = limit_size(image.convert("RGB"))
|
| 130 |
width, height = image.size
|
| 131 |
with torch.inference_mode():
|
| 132 |
-
latents =
|
| 133 |
for index in range(passes):
|
| 134 |
settings = UpscaleSettings(
|
| 135 |
spatial=True,
|
|
@@ -139,8 +212,8 @@ def upscale(
|
|
| 139 |
noise_scale=None if noise_scale < 0 else float(noise_scale),
|
| 140 |
seed=seed + index,
|
| 141 |
)
|
| 142 |
-
latents = upscaler.upscale(latents, settings, device="cuda").to("cuda")
|
| 143 |
-
pixels =
|
| 144 |
result = to_image(pixels, width * factor, height * factor)
|
| 145 |
del latents, pixels
|
| 146 |
|
|
@@ -148,34 +221,45 @@ def upscale(
|
|
| 148 |
path = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
|
| 149 |
result.save(path)
|
| 150 |
elapsed = time.perf_counter() - started
|
| 151 |
-
status = f"{note}{width}×{height} → {result.width}×{result.height} ({factor}x) in {elapsed:.1f} s, seed {seed}."
|
| 152 |
return (bicubic, result), path, seed, status
|
| 153 |
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
CSS = """
|
| 156 |
#col-container { max-width: 1200px; margin: 0 auto; }
|
| 157 |
.dark .gradio-container { color: var(--body-text-color); }
|
| 158 |
"""
|
| 159 |
|
| 160 |
INTRO = """
|
| 161 |
-
# Steppe: generative latent
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
4x chains two 2x passes in latent space.
|
| 166 |
|
| 167 |
-
[
|
|
|
|
|
|
|
| 168 |
"""
|
|
|
|
| 169 |
|
| 170 |
EXAMPLES_DIRECTORY = os.path.join(os.path.dirname(os.path.abspath(__file__)), "examples")
|
| 171 |
EXAMPLES = [
|
| 172 |
-
[os.path.join(EXAMPLES_DIRECTORY, name), scale]
|
| 173 |
-
for name, scale in (
|
| 174 |
-
("cat.png", "4x (2x → 2x)"),
|
| 175 |
-
("anime.png", "4x (2x → 2x)"),
|
| 176 |
-
("portrait.png", "2x"),
|
| 177 |
-
("cloth.png", "4x (2x → 2x)"),
|
| 178 |
-
("leaves.png", "2x"),
|
|
|
|
| 179 |
)
|
| 180 |
]
|
| 181 |
|
|
@@ -186,6 +270,7 @@ with gr.Blocks(title="Steppe latent upscaler") as demo:
|
|
| 186 |
with gr.Column(scale=2):
|
| 187 |
input_image = gr.Image(label="Input image", type="pil", image_mode="RGB", height=420)
|
| 188 |
scale = gr.Radio(list(FACTORS), value="2x", label="Upscale")
|
|
|
|
| 189 |
run = gr.Button("Upscale", variant="primary")
|
| 190 |
with gr.Accordion("Settings", open=False):
|
| 191 |
hff = gr.Slider(-1.0, 1.0, value=0.9, step=0.01, label="hff", info=HFF_INFO)
|
|
@@ -201,16 +286,16 @@ with gr.Blocks(title="Steppe latent upscaler") as demo:
|
|
| 201 |
|
| 202 |
gr.Examples(
|
| 203 |
examples=EXAMPLES,
|
| 204 |
-
inputs=[input_image, scale],
|
| 205 |
outputs=[comparison, download, seed, status],
|
| 206 |
-
fn=
|
| 207 |
cache_examples=True,
|
| 208 |
cache_mode="lazy",
|
| 209 |
)
|
| 210 |
|
| 211 |
run.click(
|
| 212 |
upscale,
|
| 213 |
-
inputs=[input_image, scale, hff, guidance, seed, randomize_seed, noise_scale],
|
| 214 |
outputs=[comparison, download, seed, status],
|
| 215 |
api_name="upscale",
|
| 216 |
)
|
|
|
|
| 1 |
"""
|
| 2 |
+
Steppe Space: generative 2x / chained 4x image upscaling in the FLUX.2 VAE (default) or Wan 2.1 VAE latent space.
|
| 3 |
|
| 4 |
+
image -> VAE encode -> Steppe 200M 1-step of that latent space (once for 2x, twice for 4x) -> VAE decode.
|
| 5 |
The latents are upscaled directly; no extra denoising pass of a diffusion model is needed.
|
| 6 |
"""
|
| 7 |
|
|
|
|
| 15 |
import random # noqa: E402
|
| 16 |
import tempfile # noqa: E402
|
| 17 |
import time # noqa: E402
|
| 18 |
+
from dataclasses import dataclass # noqa: E402
|
| 19 |
+
from typing import Callable # noqa: E402
|
| 20 |
|
| 21 |
import gradio as gr # noqa: E402
|
| 22 |
import numpy as np # noqa: E402
|
| 23 |
import torch # noqa: E402
|
| 24 |
import torch.nn.functional as functional # noqa: E402
|
| 25 |
+
from diffusers import AutoencoderKLFlux2, AutoencoderKLWan # noqa: E402
|
| 26 |
from PIL import Image # noqa: E402
|
| 27 |
+
from steppe import LATENT_SPACES, SteppeUpscaler, UpscaleSettings, load_upscaler # noqa: E402
|
| 28 |
|
|
|
|
|
|
|
| 29 |
MAX_INPUT_PIXELS = 1280 * 1024
|
| 30 |
MAX_SEED = 2**31 - 1
|
|
|
|
|
|
|
|
|
|
| 31 |
FACTORS = {"2x": 1, "4x (2x → 2x)": 2}
|
| 32 |
+
FLUX_2 = "FLUX.2 VAE"
|
| 33 |
+
WAN_2_1 = "Wan 2.1 VAE"
|
| 34 |
|
| 35 |
HFF_INFO = (
|
| 36 |
"Target sharpness / fine detail of the result, [0...1]. -1 = no hff, the model decides itself."
|
|
|
|
| 38 |
GUIDANCE_INFO = "Distilled guidance (trained on 1-3, no extra cost). 1 = off (default, safe and stable), 2 also works well."
|
| 39 |
NOISE_SCALE_INFO = "Initial noise temperature. -1 = trained default (1.0)."
|
| 40 |
|
| 41 |
+
|
| 42 |
+
@dataclass
|
| 43 |
+
class Backend:
|
| 44 |
+
"""
|
| 45 |
+
A VAE latent space with its Steppe model.
|
| 46 |
+
encode: pixels [1, 3, H, W] (values [-1...1], H and W multiples of `multiple`) -> latents as Steppe expects them
|
| 47 |
+
decode: latents -> pixels [1, 3, H, W], values [-1...1]
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
upscaler: SteppeUpscaler
|
| 51 |
+
encode: Callable[[torch.Tensor], torch.Tensor]
|
| 52 |
+
decode: Callable[[torch.Tensor], torch.Tensor]
|
| 53 |
+
multiple: int
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def load_wan_2_1() -> Backend:
|
| 57 |
+
"""
|
| 58 |
+
Wan 2.1 VAE: raw latents [1, 16, 1, H / 8, W / 8], fp32 VAE with tiling as in the first version of the Space.
|
| 59 |
+
"""
|
| 60 |
+
upscaler = load_upscaler("steppe_wan_2_1_200M_1step.safetensors", dtype=torch.bfloat16)
|
| 61 |
+
upscaler.model.to("cuda")
|
| 62 |
+
repository = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
|
| 63 |
+
vae = AutoencoderKLWan.from_pretrained(repository, subfolder="vae", torch_dtype=torch.float32).to("cuda").eval()
|
| 64 |
+
vae.enable_tiling(1024, 1024, 896, 896)
|
| 65 |
+
|
| 66 |
+
def encode(pixels: torch.Tensor) -> torch.Tensor:
|
| 67 |
+
return vae.encode(pixels[:, :, None].float()).latent_dist.mode()
|
| 68 |
+
|
| 69 |
+
def decode(latents: torch.Tensor) -> torch.Tensor:
|
| 70 |
+
return vae.decode(latents.to("cuda", torch.float32)).sample[:, :, 0]
|
| 71 |
+
|
| 72 |
+
return Backend(upscaler, encode, decode, 8)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def load_flux_2() -> Backend:
|
| 76 |
+
"""
|
| 77 |
+
FLUX.2 VAE (Apache-2.0 copy in FLUX.2 [klein] 4B, identical for all FLUX.2 models): latents 2x2 packed to
|
| 78 |
+
[1, 128, H / 16, W / 16] and batch norm normalized with the VAE's running statistics, as the diffusers FLUX.2
|
| 79 |
+
pipelines and ComfyUI hand them over. bf16 VAE as in the benchmark. Decoded in one piece: the diffusers tiled
|
| 80 |
+
decode costs ~3 dB PSNR with this VAE (per tile group norm / attention statistics). The largest output
|
| 81 |
+
(5120 x 4096) needs ~45 GB, which ZeroGPU has; tiles of 2048 px are only a fallback when the memory runs out.
|
| 82 |
+
"""
|
| 83 |
+
upscaler = load_upscaler("steppe_flux_2_200M_1step.safetensors", dtype=torch.bfloat16)
|
| 84 |
+
upscaler.model.to("cuda")
|
| 85 |
+
repository = "black-forest-labs/FLUX.2-klein-4B"
|
| 86 |
+
vae = AutoencoderKLFlux2.from_pretrained(repository, subfolder="vae", torch_dtype=torch.bfloat16).to("cuda").eval()
|
| 87 |
+
mean = vae.bn.running_mean.float()[None, :, None, None]
|
| 88 |
+
std = torch.sqrt(vae.bn.running_var.float() + vae.config.batch_norm_eps)[None, :, None, None]
|
| 89 |
+
space = LATENT_SPACES["flux_2"]
|
| 90 |
+
|
| 91 |
+
def encode(pixels: torch.Tensor) -> torch.Tensor:
|
| 92 |
+
latents = vae.encode(pixels.to(torch.bfloat16)).latent_dist.mode().float()
|
| 93 |
+
return (space.pack(latents[:, :, None])[:, :, 0] - mean) / std
|
| 94 |
+
|
| 95 |
+
def decode(latents: torch.Tensor) -> torch.Tensor:
|
| 96 |
+
latents = space.unpack((latents.to("cuda", torch.float32) * std + mean)[:, :, None])[:, :, 0]
|
| 97 |
+
latents = latents.to(torch.bfloat16)
|
| 98 |
+
try:
|
| 99 |
+
return vae.decode(latents).sample.float()
|
| 100 |
+
except torch.OutOfMemoryError:
|
| 101 |
+
torch.cuda.empty_cache()
|
| 102 |
+
vae.enable_tiling()
|
| 103 |
+
vae.tile_sample_min_size = 2048
|
| 104 |
+
vae.tile_latent_min_size = 2048 // 8
|
| 105 |
+
try:
|
| 106 |
+
return vae.decode(latents).sample.float()
|
| 107 |
+
finally:
|
| 108 |
+
vae.disable_tiling()
|
| 109 |
+
|
| 110 |
+
return Backend(upscaler, encode, decode, 16)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
BACKENDS = {FLUX_2: load_flux_2(), WAN_2_1: load_wan_2_1()}
|
| 114 |
|
| 115 |
|
| 116 |
def limit_size(image: Image.Image) -> tuple[Image.Image, str]:
|
|
|
|
| 127 |
return image.resize(size, Image.LANCZOS), note
|
| 128 |
|
| 129 |
|
| 130 |
+
def to_pixels(image: Image.Image, multiple: int) -> torch.Tensor:
|
| 131 |
"""
|
| 132 |
returns:
|
| 133 |
+
pixels: [1, 3, H_padded, W_padded], values: [-1...1], replicate padded to a multiple of `multiple`
|
| 134 |
"""
|
| 135 |
array = torch.from_numpy(np.asarray(image, dtype=np.float32) / 127.5 - 1.0)
|
| 136 |
pixels = array.permute(2, 0, 1)[None]
|
| 137 |
+
pad_height = -pixels.shape[2] % multiple
|
| 138 |
+
pad_width = -pixels.shape[3] % multiple
|
| 139 |
if pad_height or pad_width:
|
| 140 |
pixels = functional.pad(pixels, (0, pad_width, 0, pad_height), mode="replicate")
|
| 141 |
+
return pixels
|
| 142 |
|
| 143 |
|
| 144 |
def to_image(pixels: torch.Tensor, width: int, height: int) -> Image.Image:
|
| 145 |
"""
|
| 146 |
args:
|
| 147 |
+
pixels: [1, 3, H, W], values: [-1...1]
|
| 148 |
"""
|
| 149 |
+
array = ((pixels[0, :, :height, :width].float().clamp(-1, 1) + 1.0) * 127.5).round().to(torch.uint8)
|
| 150 |
return Image.fromarray(array.permute(1, 2, 0).cpu().numpy())
|
| 151 |
|
| 152 |
|
|
|
|
| 156 |
pixels = min(image.width * image.height, MAX_INPUT_PIXELS)
|
| 157 |
passes = FACTORS.get(scale, 1)
|
| 158 |
output_megapixels = pixels * 4**passes / 1e6
|
| 159 |
+
return int(min(60, 15 + output_megapixels))
|
| 160 |
|
| 161 |
|
| 162 |
@spaces.GPU(duration=estimate_duration)
|
|
|
|
| 168 |
seed: int = 0,
|
| 169 |
randomize_seed: bool = True,
|
| 170 |
noise_scale: float = -1.0,
|
| 171 |
+
latent_space: str = FLUX_2,
|
| 172 |
) -> tuple[tuple[Image.Image, Image.Image], str, int, str]:
|
| 173 |
"""
|
| 174 |
+
Upscales an image 2x or 4x (two chained 2x passes) with the Steppe generative latent upscaler in the FLUX.2 VAE
|
| 175 |
+
or Wan 2.1 VAE latent space (encode, upscale the latents in one step, decode).
|
| 176 |
|
| 177 |
Args:
|
| 178 |
image: input image (RGB). Larger than 1.3 MP is downscaled first.
|
|
|
|
| 182 |
seed: random seed of the generative upscaler (the second 4x pass uses seed + 1).
|
| 183 |
randomize_seed: pick a random seed instead of `seed`.
|
| 184 |
noise_scale: initial noise temperature, -1 = trained default (1.0).
|
| 185 |
+
latent_space: "FLUX.2 VAE" (default, best quality) or "Wan 2.1 VAE"; the VAE and Steppe model to use.
|
| 186 |
|
| 187 |
Returns:
|
| 188 |
(bicubic, steppe) image pair, path of the full resolution PNG, the used seed, status text.
|
|
|
|
| 192 |
if randomize_seed:
|
| 193 |
seed = random.randint(0, MAX_SEED)
|
| 194 |
seed = int(seed)
|
| 195 |
+
if latent_space not in BACKENDS:
|
| 196 |
+
raise gr.Error(f"Unknown latent space {latent_space!r}, choose one of: {', '.join(BACKENDS)}.")
|
| 197 |
+
backend = BACKENDS[latent_space]
|
| 198 |
passes = FACTORS[scale]
|
| 199 |
factor = 2**passes
|
| 200 |
started = time.perf_counter()
|
|
|
|
| 202 |
image, note = limit_size(image.convert("RGB"))
|
| 203 |
width, height = image.size
|
| 204 |
with torch.inference_mode():
|
| 205 |
+
latents = backend.encode(to_pixels(image, backend.multiple).to("cuda"))
|
| 206 |
for index in range(passes):
|
| 207 |
settings = UpscaleSettings(
|
| 208 |
spatial=True,
|
|
|
|
| 212 |
noise_scale=None if noise_scale < 0 else float(noise_scale),
|
| 213 |
seed=seed + index,
|
| 214 |
)
|
| 215 |
+
latents = backend.upscaler.upscale(latents, settings, device="cuda").to("cuda")
|
| 216 |
+
pixels = backend.decode(latents)
|
| 217 |
result = to_image(pixels, width * factor, height * factor)
|
| 218 |
del latents, pixels
|
| 219 |
|
|
|
|
| 221 |
path = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
|
| 222 |
result.save(path)
|
| 223 |
elapsed = time.perf_counter() - started
|
| 224 |
+
status = f"{note}{width}×{height} → {result.width}×{result.height} ({factor}x, {latent_space}) in {elapsed:.1f} s, seed {seed}."
|
| 225 |
return (bicubic, result), path, seed, status
|
| 226 |
|
| 227 |
|
| 228 |
+
def upscale_example(image: Image.Image, scale: str, latent_space: str):
|
| 229 |
+
"""Examples pass only image, scale and latent space; the other settings keep their defaults."""
|
| 230 |
+
return upscale(image, scale, latent_space=latent_space)
|
| 231 |
+
|
| 232 |
+
|
| 233 |
CSS = """
|
| 234 |
#col-container { max-width: 1200px; margin: 0 auto; }
|
| 235 |
.dark .gradio-container { color: var(--body-text-color); }
|
| 236 |
"""
|
| 237 |
|
| 238 |
INTRO = """
|
| 239 |
+
# Steppe: generative latent upscalers for FLUX.2 VAE and Wan 2.1 VAE
|
| 240 |
+
|
| 241 |
+
2x upscaling of VAE latents in a single step; the result is decoded directly by the VAE, no extra diffusion pass.
|
| 242 |
+
Here: image → VAE encode → Steppe 200M 1-step → VAE decode. 4x chains two 2x passes in latent space.
|
| 243 |
|
| 244 |
+
- **FLUX.2 VAE** (default, best quality): FLUX.2 [dev] / [klein], MOSS-Image, ...
|
| 245 |
+
- **Wan 2.1 VAE**: Wan 2.1, Wan 2.2 14B, Qwen-Image, Krea 2, ...
|
|
|
|
| 246 |
|
| 247 |
+
Models: [Steppe-Flux.2](https://huggingface.co/TensorForger/Steppe-Flux.2) ·
|
| 248 |
+
[Steppe-Wan2.1](https://huggingface.co/TensorForger/Steppe-Wan2.1) ·
|
| 249 |
+
[ComfyUI nodes](https://github.com/TensorForger/ComfyUI-Steppe)
|
| 250 |
"""
|
| 251 |
+
LATENT_SPACE_INFO = "VAE whose latent space is upscaled, with its Steppe model. FLUX.2 gives the best quality."
|
| 252 |
|
| 253 |
EXAMPLES_DIRECTORY = os.path.join(os.path.dirname(os.path.abspath(__file__)), "examples")
|
| 254 |
EXAMPLES = [
|
| 255 |
+
[os.path.join(EXAMPLES_DIRECTORY, name), scale, latent_space]
|
| 256 |
+
for name, scale, latent_space in (
|
| 257 |
+
("cat.png", "4x (2x → 2x)", FLUX_2),
|
| 258 |
+
("anime.png", "4x (2x → 2x)", FLUX_2),
|
| 259 |
+
("portrait.png", "2x", FLUX_2),
|
| 260 |
+
("cloth.png", "4x (2x → 2x)", FLUX_2),
|
| 261 |
+
("leaves.png", "2x", FLUX_2),
|
| 262 |
+
("cat.png", "4x (2x → 2x)", WAN_2_1),
|
| 263 |
)
|
| 264 |
]
|
| 265 |
|
|
|
|
| 270 |
with gr.Column(scale=2):
|
| 271 |
input_image = gr.Image(label="Input image", type="pil", image_mode="RGB", height=420)
|
| 272 |
scale = gr.Radio(list(FACTORS), value="2x", label="Upscale")
|
| 273 |
+
latent_space = gr.Radio(list(BACKENDS), value=FLUX_2, label="VAE latent space", info=LATENT_SPACE_INFO)
|
| 274 |
run = gr.Button("Upscale", variant="primary")
|
| 275 |
with gr.Accordion("Settings", open=False):
|
| 276 |
hff = gr.Slider(-1.0, 1.0, value=0.9, step=0.01, label="hff", info=HFF_INFO)
|
|
|
|
| 286 |
|
| 287 |
gr.Examples(
|
| 288 |
examples=EXAMPLES,
|
| 289 |
+
inputs=[input_image, scale, latent_space],
|
| 290 |
outputs=[comparison, download, seed, status],
|
| 291 |
+
fn=upscale_example,
|
| 292 |
cache_examples=True,
|
| 293 |
cache_mode="lazy",
|
| 294 |
)
|
| 295 |
|
| 296 |
run.click(
|
| 297 |
upscale,
|
| 298 |
+
inputs=[input_image, scale, hff, guidance, seed, randomize_seed, noise_scale, latent_space],
|
| 299 |
outputs=[comparison, download, seed, status],
|
| 300 |
api_name="upscale",
|
| 301 |
)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
-
diffusers>=0.
|
| 2 |
-
steppe @ git+https://github.com/tensorforger/ComfyUI-Steppe@
|
|
|
|
| 1 |
+
diffusers>=0.36
|
| 2 |
+
steppe @ git+https://github.com/tensorforger/ComfyUI-Steppe@df0ab4ba218e5b7509a6c30ecf0db55a0b17ab83
|