blockdiffusion-api / webui.py
wkea's picture
Fix SyntaxError in webui.py for Spaces import
97bb244 verified
Raw History Blame Contribute Delete
18.6 kB
from __future__ import annotations
"""
BlockDiffusion WebUI(用于快速目测 checkpoint 的采样效果)
说明:
- 本文件只做“加载 ckpt -> 采样 -> 展示/保存”,复用项目现有扩散/CFG/EMA 逻辑。
- 依赖:gradio(见 requirements.txt)。
- 像素纹理预览:默认用 NEAREST 放大,方便看 16×16 的细节,不改变真实输出内容。
"""
import glob
import math
import os
import re
import time
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple
import torch
from PIL import Image
from torchvision.utils import make_grid
from blockdiffusion.diffusion.gaussian_diffusion import GaussianDiffusion
from blockdiffusion.diffusion.schedules import get_beta_schedule
from blockdiffusion.models.text_unet16 import TextCondConfig, TextCondUNet16
from blockdiffusion.models.unet16 import UNet16, UNet16Config
from blockdiffusion.text.simple_tokenizer import SimpleVocab, tokenize
from blockdiffusion.train.guidance import CFGuidanceModel
from blockdiffusion.utils.checkpoint import load_checkpoint
from blockdiffusion.utils.ema import EMA
from blockdiffusion.utils.seed import get_device, seed_everything
# ==============
# Gradio import
# ==============
try:
import gradio as gr
except Exception as e: # pragma: no cover
# 这里不要给“命令行安装指令”,只给明确缺依赖提示。
raise RuntimeError("缺少依赖:gradio。请先安装后再运行 WebUI。") from e
@dataclass(frozen=True)
class _Loaded:
"""
缓存后的模型与配置(避免每次点击都重新加载 ckpt)。
注意:
- device 变化时会重新加载
- use_ema 变化时也会重新加载(因为 EMA 会 copy_to 覆盖权重)
"""
key: Tuple[str, str, bool] # (ckpt_path, device_str, use_ema)
model_type: str
image_size: int
channels: int
timesteps: int
beta_schedule: str
unet_cfg: UNet16Config
text_cfg: Optional[TextCondConfig]
vocab: Optional[SimpleVocab]
model: torch.nn.Module
diffusion: GaussianDiffusion
_CACHE: Optional[_Loaded] = None
def _ensure_localhost_no_proxy() -> None:
"""
规避 Windows/公司网络环境下的代理设置导致 localhost 访问被转发到代理,从而触发 Gradio 启动自检 502。
- gradio.launch() 内部会请求 `http://127.0.0.1:<port>/gradio_api/startup-events` 做自检
- 若 HTTP(S)_PROXY 存在且 NO_PROXY 未包含 localhost/127.0.0.1,可能出现 502
"""
# 兼容大小写环境变量
existing = os.environ.get("NO_PROXY") or os.environ.get("no_proxy") or ""
parts = [p.strip() for p in existing.split(",") if p.strip()]
want = ["127.0.0.1", "localhost"]
for w in want:
if w not in parts:
parts.append(w)
val = ",".join(parts)
os.environ["NO_PROXY"] = val
os.environ["no_proxy"] = val
def _parse_step_from_filename(path: str) -> int:
"""
从 step_XXXX.pt 提取步数;提取失败则返回 -1,确保排序时落后。
"""
base = os.path.basename(path)
m = re.search(r"step_(\d+)\.pt$", base)
if not m:
return -1
try:
return int(m.group(1))
except Exception:
return -1
def list_checkpoints(outputs_dir: str = "outputs") -> List[str]:
"""
扫描 outputs/**/checkpoints/step_*.pt,按 step 倒序排列(新的在前)。
"""
pattern = os.path.join(outputs_dir, "**", "checkpoints", "step_*.pt")
paths = glob.glob(pattern, recursive=True)
paths.sort(key=lambda p: (_parse_step_from_filename(p), p), reverse=True)
return paths
def _device_from_ui(device_text: str) -> torch.device:
"""
UI 设备选择:
- "自动" => get_device(None)
- 其他 => get_device(str)
"""
device_text = (device_text or "").strip()
if device_text in ("", "自动"):
return get_device(None)
return get_device(device_text)
def _tensor_to_pil(x_chw_01: torch.Tensor) -> Image.Image:
"""
(C,H,W) 且值域 [0,1] -> PIL(RGB/RGBA)
"""
if x_chw_01.ndim != 3:
raise ValueError("x 必须是 (C,H,W)")
c = int(x_chw_01.shape[0])
if c not in (3, 4):
raise ValueError("仅支持 3(RGB) / 4(RGBA) 通道")
x = x_chw_01.clamp(0.0, 1.0)
arr = (x.permute(1, 2, 0) * 255.0).to(torch.uint8).cpu().numpy()
mode = "RGBA" if c == 4 else "RGB"
return Image.fromarray(arr, mode=mode)
def _upscale_nearest(im: Image.Image, scale: int) -> Image.Image:
"""
像素纹理预览:NEAREST 放大,不改像素风。
"""
scale = int(scale)
if scale <= 1:
return im
w, h = im.size
return im.resize((w * scale, h * scale), resample=Image.NEAREST)
@torch.no_grad()
def _load_cached(ckpt_path: str, device_text: str, use_ema: bool) -> _Loaded:
"""
加载/复用模型缓存。
"""
global _CACHE
ckpt_path = os.path.abspath(ckpt_path)
key = (ckpt_path, (device_text or "").strip() or "自动", bool(use_ema))
if _CACHE is not None and _CACHE.key == key:
return _CACHE
device = _device_from_ui(device_text)
ckpt = load_checkpoint(ckpt_path, map_location="cpu")
meta = ckpt.get("meta", {}) or {}
model_type = str(meta.get("model_type", "uncond"))
unet_cfg_dict = meta.get("unet_cfg", None)
if unet_cfg_dict is None:
# 兼容:旧 ckpt 未保存配置
unet_cfg = UNet16Config()
timesteps = 1000
beta_schedule = "cosine"
image_size = 16
channels = 3
model_type = "uncond"
else:
unet_cfg = UNet16Config(**unet_cfg_dict)
diff_meta = meta.get("diffusion", {}) or {}
timesteps = int(diff_meta.get("timesteps", 1000))
beta_schedule = str(diff_meta.get("beta_schedule", "cosine"))
data_meta = meta.get("data", {}) or {}
image_size = int(data_meta.get("image_size", 16))
channels = int(data_meta.get("channels", unet_cfg.in_channels))
vocab = None
text_cfg = None
if model_type == "text_cond":
tok_dict = meta.get("tokenizer", None)
text_cfg_dict = meta.get("text_cfg", None)
if tok_dict is None or text_cfg_dict is None:
raise ValueError("该 checkpoint 标记为 text_cond,但缺少 tokenizer/text_cfg")
vocab = SimpleVocab.from_dict(tok_dict)
text_cfg = TextCondConfig(**text_cfg_dict)
model = TextCondUNet16(unet_cfg=unet_cfg, vocab=vocab, text_cfg=text_cfg).to(device)
else:
model = UNet16(unet_cfg).to(device)
model.load_state_dict(ckpt["model"], strict=True)
model.eval()
if use_ema and ("ema" in ckpt):
# 使用 EMA 权重采样(推荐),会覆盖 model 当前参数
ema = EMA(model, decay=0.9999)
ema.load_state_dict(ckpt["ema"])
ema.copy_to(model)
model.eval()
betas = get_beta_schedule(beta_schedule, timesteps).to(device)
diffusion = GaussianDiffusion(betas)
_CACHE = _Loaded(
key=key,
model_type=model_type,
image_size=image_size,
channels=channels,
timesteps=timesteps,
beta_schedule=beta_schedule,
unet_cfg=unet_cfg,
text_cfg=text_cfg,
vocab=vocab,
model=model,
diffusion=diffusion,
)
return _CACHE
@torch.no_grad()
def _sample(
ckpt_path: str,
prompt: str,
cfg_scale: float,
use_ema: bool,
use_ddim: bool,
ddim_steps: int,
ddim_eta: float,
n_images: int,
batch: int,
seed: int,
device_text: str,
save_dir: str,
preview_upscale: int,
gallery_n: int,
) -> Tuple[str, Optional[str], Optional[List[Image.Image]], Optional[Image.Image]]:
"""
返回:
- info 文本(用于 UI 展示)
- 保存路径(或 None)
- gallery 图片列表(或 None)
- 网格图 PIL(或 None)
"""
ckpt_path = (ckpt_path or "").strip()
if not ckpt_path:
return "错误:请先选择/填写 checkpoint 路径。", None, None, None
if not os.path.isfile(ckpt_path):
return f"错误:checkpoint 不存在:{ckpt_path}", None, None, None
save_dir = (save_dir or "").strip() or "webui_samples"
os.makedirs(save_dir, exist_ok=True)
# 采样随机性(只影响本次点击)
seed_everything(int(seed), deterministic=False)
loaded = _load_cached(ckpt_path=ckpt_path, device_text=device_text, use_ema=use_ema)
device = _device_from_ui(device_text)
# 条件采样:仅 text_cond 支持 prompt + CFG
sampler_model: torch.nn.Module = loaded.model
prompt_tokens: List[str] = []
prompt_unk: List[str] = []
if (prompt or "").strip():
if loaded.model_type != "text_cond":
return "错误:当前 checkpoint 是无条件模型,不支持 prompt/CFG。", None, None, None
if loaded.vocab is None or loaded.text_cfg is None:
return "错误:条件模型缺少 tokenizer/text_cfg。", None, None, None
# 用于诊断:提示词是否大量变成 <unk>(这会导致“看起来不遵从提示词”)
prompt_tokens = tokenize(prompt)
for t in prompt_tokens:
tid = loaded.vocab.token_to_id.get(t, loaded.vocab.unk_id)
if int(tid) == int(loaded.vocab.unk_id):
prompt_unk.append(t)
tok = torch.tensor(
[loaded.vocab.encode(prompt, max_len=loaded.text_cfg.max_text_len)],
dtype=torch.long,
device=device,
)
m = tok != loaded.vocab.pad_id
cond_kwargs = {"tokens": tok, "mask": m}
un_tok = torch.full_like(tok, fill_value=loaded.vocab.pad_id)
un_m = torch.zeros_like(m)
uncond_kwargs = {"tokens": un_tok, "mask": un_m}
sampler_model = CFGuidanceModel(
base_model=loaded.model,
cond_kwargs=cond_kwargs,
uncond_kwargs=uncond_kwargs,
scale=float(cfg_scale),
).to(device)
sampler_model.eval()
else:
# 不提供 prompt 时:对条件模型等价无条件采样
pass
n_images = int(max(1, n_images))
batch = int(max(1, batch))
gallery_n = int(max(0, min(gallery_n, n_images)))
# 分批采样
all_imgs: List[torch.Tensor] = []
remaining = n_images
t0 = time.time()
while remaining > 0:
b = min(batch, remaining)
shape = (b, loaded.channels, loaded.image_size, loaded.image_size)
if use_ddim:
x = loaded.diffusion.ddim_sample_loop(
sampler_model,
shape=shape,
device=device,
steps=int(ddim_steps),
eta=float(ddim_eta),
model_kwargs=None,
progress=False,
)
else:
x = loaded.diffusion.p_sample_loop(
sampler_model,
shape=shape,
device=device,
model_kwargs=None,
progress=False,
)
x = (x.clamp(-1, 1) + 1) * 0.5 # [0,1]
all_imgs.append(x.detach().cpu())
remaining -= b
dt = time.time() - t0
imgs = torch.cat(all_imgs, dim=0) # (N,C,H,W) on CPU
# 网格图(更适合一次看很多张)
nrow = int(max(1, math.isqrt(n_images)))
grid = make_grid(imgs, nrow=nrow, padding=1).clamp(0.0, 1.0) # (C,H,W)
grid_im_raw = _tensor_to_pil(grid) # 原始分辨率,用于保存
grid_im = _upscale_nearest(grid_im_raw, int(preview_upscale)) # 仅预览放大
# 单张 gallery(可选)
gallery: Optional[List[Image.Image]] = None
if gallery_n > 0:
gallery = []
for i in range(gallery_n):
im = _tensor_to_pil(imgs[i])
im = _upscale_nearest(im, int(preview_upscale))
gallery.append(im)
# 保存(默认保存网格图,文件名包含 step 与时间戳,方便对比)
step = _parse_step_from_filename(ckpt_path)
ts = time.strftime("%Y%m%d-%H%M%S")
out_name = f"grid_step_{step if step >= 0 else 'unk'}_{ts}.png"
out_path = os.path.join(save_dir, out_name)
grid_im_raw.save(out_path)
info = (
f"ckpt: {os.path.abspath(ckpt_path)}\n"
f"model_type: {loaded.model_type}\n"
f"size: {loaded.image_size}x{loaded.image_size} channels: {loaded.channels}\n"
f"timesteps: {loaded.timesteps} beta_schedule: {loaded.beta_schedule}\n"
f"device: {device}\n"
f"use_ema: {bool(use_ema)} sampler: {'DDIM' if use_ddim else 'DDPM'}\n"
f"n_images: {n_images} batch: {batch} seed: {int(seed)}\n"
f"prompt: {(prompt or '').strip() or '(none)'} cfg_scale: {float(cfg_scale):.2f}\n"
+ (
f"prompt_tokens: {' '.join(prompt_tokens[:64])}\n"
f"prompt_unk({len(prompt_unk)}): {' '.join(prompt_unk[:64])}\n"
if prompt_tokens
else ""
)
+ f"preview_upscale(NEAREST): {int(preview_upscale)}x\n"
+ f"save: {os.path.abspath(out_path)}\n"
+ f"time: {dt:.2f}s\n"
)
return info, os.path.abspath(out_path), gallery, grid_im
def _build_ui() -> gr.Blocks:
ckpts = list_checkpoints("outputs")
default_ckpt = ckpts[0] if ckpts else ""
with gr.Blocks(title="BlockDiffusion WebUI") as demo:
gr.Markdown("### BlockDiffusion WebUI(采样预览)")
with gr.Row():
ckpt_quick = gr.Dropdown(
choices=ckpts,
value=default_ckpt or None,
label="快速选择 checkpoint(从 outputs 扫描)",
)
refresh = gr.Button("刷新列表")
ckpt_path = gr.Textbox(value=default_ckpt, label="Checkpoint 路径(可手填)")
with gr.Row():
prompt = gr.Textbox(value="", label="Prompt(条件模型可用;留空=无条件)")
cfg_scale = gr.Slider(minimum=0.0, maximum=12.0, value=3.0, step=0.1, label="CFG scale")
with gr.Row():
use_ema = gr.Checkbox(value=True, label="使用 EMA 权重(推荐)")
use_ddim = gr.Checkbox(value=True, label="使用 DDIM(更快)")
ddim_steps = gr.Slider(minimum=10, maximum=200, value=50, step=1, label="DDIM steps")
ddim_eta = gr.Slider(minimum=0.0, maximum=1.0, value=0.0, step=0.01, label="DDIM eta")
with gr.Row():
n_images = gr.Slider(minimum=1, maximum=256, value=64, step=1, label="生成张数 N")
batch = gr.Slider(minimum=1, maximum=256, value=64, step=1, label="采样 batch(显存不够就调小)")
seed = gr.Number(value=123, precision=0, label="随机种子 seed")
with gr.Row():
device_text = gr.Dropdown(
choices=["自动", "cuda", "cuda:0", "cpu"],
value="自动",
label="设备",
)
save_dir = gr.Textbox(value="webui_samples", label="保存目录")
preview_upscale = gr.Slider(minimum=1, maximum=64, value=16, step=1, label="预览放大倍数(NEAREST)")
gallery_n = gr.Slider(minimum=0, maximum=64, value=16, step=1, label="单张预览数量(0=关闭)")
run = gr.Button("生成")
info = gr.Textbox(label="信息/日志", lines=10)
grid_out = gr.Image(label="网格图(已放大预览)", type="pil")
gallery_out = gr.Gallery(label="单张预览(已放大)", columns=8, rows=2)
saved_path = gr.Textbox(label="已保存路径", lines=1)
def _on_refresh():
new_ckpts = list_checkpoints("outputs")
return gr.update(choices=new_ckpts, value=(new_ckpts[0] if new_ckpts else None))
def _on_pick(p: Optional[str]) -> str:
return p or ""
def _on_run(
ckpt_path_v: str,
prompt_v: str,
cfg_scale_v: float,
use_ema_v: bool,
use_ddim_v: bool,
ddim_steps_v: int,
ddim_eta_v: float,
n_images_v: int,
batch_v: int,
seed_v: int,
device_text_v: str,
save_dir_v: str,
preview_upscale_v: int,
gallery_n_v: int,
):
try:
txt, out_path, gal, grid = _sample(
ckpt_path=ckpt_path_v,
prompt=prompt_v,
cfg_scale=float(cfg_scale_v),
use_ema=bool(use_ema_v),
use_ddim=bool(use_ddim_v),
ddim_steps=int(ddim_steps_v),
ddim_eta=float(ddim_eta_v),
n_images=int(n_images_v),
batch=int(batch_v),
seed=int(seed_v),
device_text=device_text_v,
save_dir=save_dir_v,
preview_upscale=int(preview_upscale_v),
gallery_n=int(gallery_n_v),
)
return txt, grid, (gal or []), (out_path or "")
except Exception as e:
# UI 侧直接给出错误信息,避免整个服务崩溃
return f"错误:{e}", None, [], ""
refresh.click(_on_refresh, inputs=[], outputs=[ckpt_quick])
ckpt_quick.change(_on_pick, inputs=[ckpt_quick], outputs=[ckpt_path])
run.click(
_on_run,
inputs=[
ckpt_path,
prompt,
cfg_scale,
use_ema,
use_ddim,
ddim_steps,
ddim_eta,
n_images,
batch,
seed,
device_text,
save_dir,
preview_upscale,
gallery_n,
],
outputs=[info, grid_out, gallery_out, saved_path],
)
return demo
def main() -> None:
demo = _build_ui()
# 默认监听 127.0.0.1:仅本机访问更安全
_ensure_localhost_no_proxy()
demo.launch(server_name="127.0.0.1", share=False)
if __name__ == "__main__":
main()