This commit is contained in:
2026-05-15 00:06:14 +08:00
parent 83c2c26e89
commit b4c7697e8a
6 changed files with 174 additions and 45 deletions
@@ -7,6 +7,32 @@ import numpy as np
from PIL import Image
def _align_spatial_dim_for_flux_fill(n: int, multiple: int = 16) -> int:
"""FLUX Fill 要求 H/W 为 (vae_scale_factor*2) 的倍数,常见为 16。"""
n = int(max(n, multiple))
return n - (n % multiple)
def _flux_clip_prompt_and_t5_full(pipe: Any, text_full: str) -> tuple[str, str]:
"""
CLIP 仅约 77 token:用与管线一致的 tokenizer 截断得到 prompt(池化向量)。
完整文本走 prompt_2 → T5max_sequence_length 可达 512),长中文不再整段喂给 CLIP。
"""
text_full = (text_full or "").strip()
if not text_full:
d = "Chinese ink painting background, natural texture, seamless fill, no figures"
return d, d
tok = getattr(pipe, "tokenizer", None)
if tok is None:
return text_full[:120], text_full
ml = int(getattr(tok, "model_max_length", 77) or 77)
enc = tok([text_full], padding="max_length", max_length=ml, truncation=True, return_tensors="pt")
clip_s = tok.batch_decode(enc.input_ids, skip_special_tokens=True)[0].strip()
if not clip_s:
clip_s = text_full[:80]
return clip_s, text_full
def make_flux_fill_predictor(
model_id: str,
device: str | None = None,
@@ -69,8 +95,8 @@ def make_flux_fill_predictor(
scale = max_side / float(max(orig_w, orig_h))
run_w = int(round(orig_w * scale))
run_h = int(round(orig_h * scale))
run_w = max(8, run_w - (run_w % 8))
run_h = max(8, run_h - (run_h % 8))
run_w = _align_spatial_dim_for_flux_fill(run_w)
run_h = _align_spatial_dim_for_flux_fill(run_h)
if (run_w, run_h) != (orig_w, orig_h):
image_run = image.resize((run_w, run_h), resample=Image.BICUBIC)
@@ -79,9 +105,10 @@ def make_flux_fill_predictor(
image_run = image
mask_run = mask
p = (prompt or "").strip()
if not p:
p = "Chinese ink painting background, natural texture, seamless fill, no figures"
p_full = (prompt or "").strip()
if not p_full:
p_full = "Chinese ink painting background, natural texture, seamless fill, no figures"
p_clip, p_t5 = _flux_clip_prompt_and_t5_full(pipe, p_full)
st = float(strength)
st = min(1.0, max(0.05, st))
@@ -93,7 +120,8 @@ def make_flux_fill_predictor(
pass
out = pipe(
prompt=p,
prompt=p_clip,
prompt_2=p_t5,
image=image_run,
mask_image=mask_run,
height=run_h,