wkea's picture
Improve error messages for client calls
eacd242 verified
Raw History Blame Contribute Delete
11.7 kB
from __future__ import annotations
"""
Hugging Face Spaces 入口(Gradio)。
目标:
- 复用本项目现有“加载 checkpoint -> DDPM/DDIM 采样 -> 输出图片”的逻辑;
- 增加“伪私有”鉴权:所有推理请求必须提供正确的 API_KEY,否则拒绝;
- 支持从 Hugging Face Hub 下载 checkpoint(便于 Spaces 部署,不必把大文件提交到 Space 仓库)。
重要说明(伪私有的边界):
- Space 本身仍然是公开的(你选择 public 的情况下),只是把推理能力用 API_KEY 做访问控制;
- 拿到 API_KEY 的人仍可调用(这是预期)。
"""
import hmac
import os
from typing import Optional, Tuple, List
import torch
import gradio as gr
from huggingface_hub import hf_hub_download
from PIL import Image
# 复用现有逻辑(不重复造轮子)
from webui import _load_cached, _tensor_to_pil # type: ignore
from blockdiffusion.train.guidance import CFGuidanceModel
from blockdiffusion.text.simple_tokenizer import tokenize
from blockdiffusion.utils.seed import seed_everything, get_device
def _get_env(name: str, default: str = "") -> str:
v = os.getenv(name, default)
return (v or "").strip()
def _require_space_api_key() -> str:
"""
Space 的“伪私有”Key:建议在 Hugging Face Space Settings -> Secrets 里配置。
"""
# 注意:Hugging Face 网页端对 Secret 名称有时会限制为字母数字且字母开头,
# 导致带下划线的 `SPACE_API_KEY` 无法创建;因此这里兼容 `SPACEAPIKEY`。
key = _get_env("SPACE_API_KEY", "") or _get_env("SPACEAPIKEY", "")
if not key:
# 失败更安全:避免误部署成“无鉴权公开推理”
raise RuntimeError("缺少环境变量 SPACE_API_KEY/SPACEAPIKEY(请在 Space Secrets 中配置)")
return key
_RESOLVED_CKPT_PATH: Optional[str] = None
def _resolve_ckpt_path() -> str:
"""
获取 checkpoint 路径:
- 优先使用本地路径 BD_CKPT_PATH
- 否则从 Hugging Face Hub 下载:BD_CKPT_REPO + BD_CKPT_FILENAME(可选 BD_CKPT_REVISION)
说明:
- 若模型仓库是私有/受限,需要在 Space Secrets 设置 HF_TOKEN(或 HUGGINGFACE_HUB_TOKEN)
"""
global _RESOLVED_CKPT_PATH
if _RESOLVED_CKPT_PATH and os.path.isfile(_RESOLVED_CKPT_PATH):
return _RESOLVED_CKPT_PATH
local_path = _get_env("BD_CKPT_PATH", "")
if local_path:
if not os.path.isfile(local_path):
raise RuntimeError(f"BD_CKPT_PATH 指向的文件不存在:{local_path}")
_RESOLVED_CKPT_PATH = local_path
return _RESOLVED_CKPT_PATH
repo_id = _get_env("BD_CKPT_REPO", "")
# 约定优先使用生态常见的权重名,减少歧义
filename = _get_env("BD_CKPT_FILENAME", "pytorch_model.bin")
revision = _get_env("BD_CKPT_REVISION", "") or None
if not repo_id:
raise RuntimeError("未配置 checkpoint:请设置 BD_CKPT_PATH 或 BD_CKPT_REPO/BD_CKPT_FILENAME")
token = _get_env("HF_TOKEN", "") or _get_env("HUGGINGFACE_HUB_TOKEN", "") or None
try:
path = hf_hub_download(repo_id=repo_id, filename=filename, revision=revision, token=token)
except Exception as e:
raise RuntimeError(f"从Hub下载checkpoint失败:repo={repo_id} file={filename} rev={revision} err={e!r}") from e
if not os.path.isfile(path):
raise RuntimeError(f"下载完成但文件不存在:{path}")
_RESOLVED_CKPT_PATH = path
return path
def _check_api_key(user_key: str) -> None:
"""
伪私有鉴权:对外只暴露“成功/失败”,避免泄漏真实 Key。
"""
# 若 Space 端未配置 Key,给出明确错误,避免客户端只能看到“上游异常但无verbose”
try:
expected = _require_space_api_key()
except Exception:
raise gr.Error("服务端未配置SPACEAPIKEY/SPACE_API_KEY:请到Space Settings->Secrets 新建后重启")
user_key = (user_key or "").strip()
if not user_key or not hmac.compare_digest(user_key, expected):
raise gr.Error("未授权:API_KEY错误")
@torch.no_grad()
def _infer(
api_key: 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,
preview_upscale: int,
) -> Tuple[str, Image.Image, List[Image.Image]]:
# 1) 鉴权
_check_api_key(api_key)
# 2) 参数整理
n_images = int(max(1, n_images))
batch = int(max(1, batch))
preview_upscale = int(max(1, preview_upscale))
seed_everything(int(seed), deterministic=False)
# 3) 定位 ckpt + device
ckpt_path = _resolve_ckpt_path()
# Spaces 上尽量自动选择(有GPU则用GPU)
device = get_device(None)
device_text = str(device)
# 4) 复用 webui 的缓存加载(包含:读取meta、构建模型、EMA覆盖、构建diffusion)
loaded = _load_cached(ckpt_path=ckpt_path, device_text=device_text, use_ema=bool(use_ema))
# 5) 条件采样(prompt + CFG)
sampler_model: torch.nn.Module = loaded.model
prompt = (prompt or "").strip()
if prompt:
if loaded.model_type != "text_cond":
raise gr.Error("该checkpoint为无条件模型,不支持prompt/CFG")
if loaded.vocab is None or loaded.text_cfg is None:
raise gr.Error("条件模型缺少tokenizer/text_cfg")
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()
# 6) 采样(分批)
all_imgs: List[torch.Tensor] = []
remaining = n_images
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
imgs = torch.cat(all_imgs, dim=0) # (N,C,H,W) on CPU
# 7) 输出:网格图 + gallery(按像素纹理预览做NEAREST放大)
# 为了不重复引入 torchvision/make_grid,这里直接拼接成单行网格(N很小时足够)。
# 如果你需要更复杂网格,可直接复用 webui.py 的 make_grid 逻辑。
gallery: List[Image.Image] = []
for i in range(n_images):
im = _tensor_to_pil(imgs[i])
if preview_upscale > 1:
w, h = im.size
im = im.resize((w * preview_upscale, h * preview_upscale), resample=Image.NEAREST)
gallery.append(im)
# 简单拼图:横向拼接(避免引入额外依赖/逻辑)
total_w = sum(im.size[0] for im in gallery)
max_h = max(im.size[1] for im in gallery)
grid = Image.new("RGBA", (total_w, max_h))
x0 = 0
for im in gallery:
grid.paste(im, (x0, 0))
x0 += im.size[0]
# 提示词token诊断:是否大量<unk>(仅用于展示)
unk_info = ""
if prompt and loaded.vocab is not None:
toks = tokenize(prompt)
unk = [t for t in toks if int(loaded.vocab.token_to_id.get(t, loaded.vocab.unk_id)) == int(loaded.vocab.unk_id)]
if unk:
unk_info = f" unk_tokens={len(unk)}"
info = (
f"ckpt={os.path.basename(ckpt_path)} device={device_text} "
f"type={loaded.model_type} size={loaded.image_size} ch={loaded.channels} "
f"sampler={'DDIM' if use_ddim else 'DDPM'} n={n_images} batch={batch} seed={int(seed)} "
f"prompt={'(none)' if not prompt else prompt[:80]}{unk_info}"
)
return info, grid, gallery
def build_demo() -> gr.Blocks:
"""
Gradio UI(同时提供 /api/predict 的在线调用能力)。
"""
with gr.Blocks(title="BlockDiffusion (Pseudo-Private)") as demo:
gr.Markdown("### BlockDiffusion Spaces(伪私有:需API_KEY)")
with gr.Row():
api_key = gr.Textbox(label="API_KEY(必填)", type="password")
with gr.Row():
prompt = gr.Textbox(label="Prompt(条件模型可用;留空=无条件)", value="", lines=2)
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=8, value=1, step=1, label="生成张数N(Spaces建议<=8)")
batch = gr.Slider(minimum=1, maximum=8, value=1, step=1, label="采样batch(Spaces建议<=8)")
seed = gr.Number(value=123, precision=0, label="seed")
preview_upscale = gr.Slider(minimum=1, maximum=64, value=16, step=1, label="预览放大倍数(NEAREST)")
run = gr.Button("生成")
info = gr.Textbox(label="信息", lines=2)
grid = gr.Image(label="网格图", type="pil")
gallery = gr.Gallery(label="单张结果")
def _run(*args):
# 把非 gr.Error 的异常也转换成可见错误信息(不暴露堆栈)
try:
return _infer(*args)
except gr.Error:
raise
except Exception as e:
raise gr.Error(f"推理失败:{e}")
run.click(
_run,
inputs=[
api_key,
prompt,
cfg_scale,
use_ema,
use_ddim,
ddim_steps,
ddim_eta,
n_images,
batch,
seed,
preview_upscale,
],
outputs=[info, grid, gallery],
)
return demo
demo = build_demo()
def main() -> None:
"""
Hugging Face Spaces 会直接执行 `python app.py`;
如果不显式 launch,进程会立刻退出(Exit code 0)并被判定为 Runtime error。
"""
port = int(os.getenv("PORT", "7860"))
# 监听 0.0.0.0 才能被 Space 反向代理访问到
demo.launch(server_name="0.0.0.0", server_port=port, share=False, show_error=False)
if __name__ == "__main__":
main()