Upload app.py
Browse files109 unmodified lines1101111121131141155 unmodified lines121122123124125126109 unmodified linesreturn f"Motion enhancer loaded: {len(bases)} adapters, {folded} matrices"def _load_pipeline():from diffusers import ComponentsManager5 unmodified linespipe.audio_vae.set_attention_backend("native")pipe.transformer.set_attention_backend(os.environ.get("H3_ATTENTION", "_native_cudnn"))print(_apply_motion_lora(pipe.transformer), flush=True)# Module-scope placement lets ZeroGPU pack the weights once instead of per request.pipe.transformer.to("cuda")return pipe109 unmodified lines1101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951965 unmodified lines202203204205206207208109 unmodified linesreturn f"Motion enhancer loaded: {len(bases)} adapters, {folded} matrices"# HMBreasts V2 is an additional anatomy adapter. It is merged independently# after the existing 8-step Motion Enhancer; the 8-step schedule is unchanged.HMBREASTS_LORA_REPO = os.environ.get("H3_HMBREASTS_LORA_REPO","Hearmeman/minimax-h3-loras",)HMBREASTS_LORA_FILE = os.environ.get("H3_HMBREASTS_LORA_FILE","HMBreastsV2.safetensors",)HMBREASTS_LORA_SCALE = float(os.environ.get("H3_HMBREASTS_LORA_SCALE", "1.0"))def _apply_hmbreasts_lora(transformer) -> str:from huggingface_hub import hf_hub_downloadfrom safetensors.torch import load_filepath = hf_hub_download(HMBREASTS_LORA_REPO, HMBREASTS_LORA_FILE)raw_lora = load_file(path, device="cpu")# Normalize common Diffusers/ComfyUI LoRA key prefixes and suffixes.lora = {}for key, tensor in raw_lora.items():k = keyfor prefix in ("base_model.model.", "diffusion_model.", "transformer.", "pipe.dit."):if k.startswith(prefix):k = k[len(prefix):]breakk = k.replace(".self_attn.", ".attn.")k = k.replace(".lora_down.weight", ".lora_A.weight")k = k.replace(".lora_up.weight", ".lora_B.weight")k = k.replace(".lora_A.default.weight", ".lora_A.weight")k = k.replace(".lora_B.default.weight", ".lora_B.weight")lora[k] = tensorsuffix_a = ".lora_A.weight"suffix_b = ".lora_B.weight"bases = sorted({key[:-len(suffix_a)] for key in lora if key.endswith(suffix_a)})if not bases:raise ValueError("HMBreasts V2: no lora_A/lora_B pairs found; "f"sample keys: {list(lora)[:5]}")params = dict(transformer.named_parameters())num_heads = transformer.config.num_attention_headshead_dim = transformer.config.attention_head_dimfolded = 0missed = []with torch.no_grad():for name in bases:a = lora[f"{name}{suffix_a}"]b = lora.get(f"{name}{suffix_b}")if b is None:raise ValueError(f"HMBreasts V2: missing lora_B for {name}")for key, b_part in _lora_targets(name, b, num_heads, head_dim):param = params.get(key)if param is None:missed.append(key)continuedelta = float(HMBREASTS_LORA_SCALE) * (b_part.float() @ a.float())if delta.shape != param.shape:raise ValueError(f"HMBreasts V2 shape mismatch for {key}: "f"{tuple(delta.shape)} vs {tuple(param.shape)}")param.data = (param.data.float() + delta.to(param.device)).to(param.dtype)folded += 1if missed:raise ValueError(f"HMBreasts V2: {len(missed)} targets did not match the H3 transformer, "f"e.g. {missed[:5]}")return (f"HMBreasts V2 loaded: {len(bases)} adapters, {folded} matrices, "f"scale {HMBREASTS_LORA_SCALE:g}")def _load_pipeline():from diffusers import ComponentsManager5 unmodified linespipe.audio_vae.set_attention_backend("native")pipe.transformer.set_attention_backend(os.environ.get("H3_ATTENTION", "_native_cudnn"))print(_apply_motion_lora(pipe.transformer), flush=True)print(_apply_hmbreasts_lora(pipe.transformer), flush=True)# Module-scope placement lets ZeroGPU pack the weights once instead of per request.pipe.transformer.to("cuda")return pipe
app.py+82