Delete app.py
Browse files
app.py
DELETED
|
@@ -1,213 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
===========================================
|
| 3 |
-
FLUX.2 Klein-4B CPU Demo(仅技术验证用)
|
| 4 |
-
===========================================
|
| 5 |
-
⚠️ 重要说明:
|
| 6 |
-
1. 性能极差:256x256 1步约30-60秒,512x512 1步约2-5分钟,1024x1024大概率OOM
|
| 7 |
-
2. 内存要求:至少需要16G CPU内存(跑512需32G+),HF Space CPU Basic(16G)建议用256分辨率
|
| 8 |
-
3. 前置条件:已同意FLUX.2-dev的Gated License,Space配置了HF_TOKEN环境变量
|
| 9 |
-
4. 依赖已在你现有requirements.txt基础上兼容,无需额外修改
|
| 10 |
-
"""
|
| 11 |
-
|
| 12 |
-
import torch
|
| 13 |
-
import gradio as gr
|
| 14 |
-
from PIL import Image
|
| 15 |
-
from safetensors.torch import load_file
|
| 16 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 17 |
-
from flux2_adapter import Flux2AdapterModel, Flux2VAETokenizer
|
| 18 |
-
|
| 19 |
-
# ==================== 全局配置 ====================
|
| 20 |
-
RES = 256 # 👈 优先用256,512需要≥32G内存,1024不建议
|
| 21 |
-
NUM_STEPS = 1 # 1步最快,增加步数会指数级变慢
|
| 22 |
-
MODEL_PATH = "model.safetensors" # 和ae.safetensors同目录
|
| 23 |
-
AE_PATH = "ae.safetensors"
|
| 24 |
-
|
| 25 |
-
# ==================== 全局状态缓存 ====================
|
| 26 |
-
_STATE = {} # 避免重复加载模型
|
| 27 |
-
|
| 28 |
-
# ==================== CPU版Qwen3 Embedder(核心:禁用FP8) ====================
|
| 29 |
-
def load_qwen3_embedder_cpu():
|
| 30 |
-
"""
|
| 31 |
-
手动加载Qwen3-4B BF16版本,完全规避FP8依赖
|
| 32 |
-
CPU模式下只能用BF16/FP32,FP8不存在
|
| 33 |
-
"""
|
| 34 |
-
if "qwen3" in _STATE:
|
| 35 |
-
return _STATE["qwen3"]
|
| 36 |
-
|
| 37 |
-
print("Loading Qwen3-4B (CPU/BF16)...")
|
| 38 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 39 |
-
"Qwen/Qwen3-4B",
|
| 40 |
-
padding_side="left",
|
| 41 |
-
trust_remote_code=True
|
| 42 |
-
)
|
| 43 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 44 |
-
"Qwen/Qwen3-4B",
|
| 45 |
-
torch_dtype=torch.bfloat16, # CPU支持BF16,比FP32快30%
|
| 46 |
-
device_map={"": "cpu"}, # 强制CPU
|
| 47 |
-
low_cpu_mem_usage=True, # 降低加载时内存峰值
|
| 48 |
-
trust_remote_code=True
|
| 49 |
-
).eval()
|
| 50 |
-
|
| 51 |
-
_STATE["qwen3"] = (model, tokenizer)
|
| 52 |
-
print("Qwen3-4B loaded (CPU)")
|
| 53 |
-
return model, tokenizer
|
| 54 |
-
|
| 55 |
-
# ==================== 模型加载(全CPU) ====================
|
| 56 |
-
def ensure_loaded():
|
| 57 |
-
if _STATE.get("model"):
|
| 58 |
-
return
|
| 59 |
-
|
| 60 |
-
print("Loading FLUX.2 Klein-4B (CPU)...")
|
| 61 |
-
|
| 62 |
-
# 1. 加载主模型权重(CPU,lazy load 自动生效)
|
| 63 |
-
sd = load_file(MODEL_PATH, device="cpu")
|
| 64 |
-
|
| 65 |
-
model = Flux2AdapterModel(
|
| 66 |
-
state_dict=sd,
|
| 67 |
-
image_resolution=RES,
|
| 68 |
-
param_dtype=torch.bfloat16
|
| 69 |
-
).to("cpu").eval()
|
| 70 |
-
|
| 71 |
-
# 2. 加载 VAE
|
| 72 |
-
print("Loading FLUX.2 AE (CPU)...")
|
| 73 |
-
vae = Flux2VAETokenizer(
|
| 74 |
-
AE_PATH,
|
| 75 |
-
device="cpu",
|
| 76 |
-
torch_dtype=torch.bfloat16
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
# 3. 加载 Qwen3 Embedder
|
| 80 |
-
emb_model, emb_tokenizer = load_qwen3_embedder_cpu()
|
| 81 |
-
|
| 82 |
-
_STATE.update({
|
| 83 |
-
"model": model,
|
| 84 |
-
"vae": vae,
|
| 85 |
-
"emb_model": emb_model,
|
| 86 |
-
"emb_tokenizer": emb_tokenizer
|
| 87 |
-
})
|
| 88 |
-
|
| 89 |
-
print(f"All models loaded (CPU), ready for {RES}x{RES} inference")
|
| 90 |
-
# ==================== Prompt编码(适配FLUX.2的7680维要求) ====================
|
| 91 |
-
def encode_prompt(prompt: str) -> torch.Tensor:
|
| 92 |
-
"""
|
| 93 |
-
FLUX.2 Klein要求context维度为7680,来自3×Qwen3-4B的hidden state
|
| 94 |
-
CPU模式下简化为重复3次最后一层的hidden state
|
| 95 |
-
"""
|
| 96 |
-
model, tokenizer = _STATE["emb_model"], _STATE["emb_tokenizer"]
|
| 97 |
-
|
| 98 |
-
inputs = tokenizer(
|
| 99 |
-
prompt,
|
| 100 |
-
return_tensors="pt",
|
| 101 |
-
padding=True,
|
| 102 |
-
truncation=True,
|
| 103 |
-
max_length=256 # Klein的文本长度限制
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
with torch.no_grad(), torch.inference_mode():
|
| 107 |
-
outputs = model(**inputs, output_hidden_states=True)
|
| 108 |
-
# 用倒数第二层的hidden state(和训练时对齐性更好)
|
| 109 |
-
hidden = outputs.hidden_states[-2][:, -1, :] # shape: (1, 2560)
|
| 110 |
-
|
| 111 |
-
# 重复3次得到7680维,符合FLUX.2输入要求
|
| 112 |
-
ctx = hidden.repeat(1, 3) # shape: (1, 7680)
|
| 113 |
-
return ctx.to(torch.bfloat16)
|
| 114 |
-
|
| 115 |
-
# ==================== Gradio推理逻辑 ====================
|
| 116 |
-
def generate(prompt: str, seed: int, randomize: bool):
|
| 117 |
-
prompt = (prompt or "").strip()
|
| 118 |
-
if not prompt:
|
| 119 |
-
raise gr.Error("请输入提示词")
|
| 120 |
-
|
| 121 |
-
ensure_loaded()
|
| 122 |
-
|
| 123 |
-
# 种子处理(纯CPU Generator)
|
| 124 |
-
if randomize:
|
| 125 |
-
seed = int(torch.randint(0, 2**31 - 1, (1,)).item())
|
| 126 |
-
seed = int(seed)
|
| 127 |
-
generator = torch.Generator().manual_seed(seed)
|
| 128 |
-
|
| 129 |
-
# 生成随机噪声(CPU)
|
| 130 |
-
noise = torch.randn(
|
| 131 |
-
1,
|
| 132 |
-
128,
|
| 133 |
-
RES // 16,
|
| 134 |
-
RES // 16,
|
| 135 |
-
generator=generator,
|
| 136 |
-
dtype=torch.bfloat16
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
# 编码提示词
|
| 140 |
-
ctx = encode_prompt(prompt)
|
| 141 |
-
|
| 142 |
-
# 推理(全程CPU,无GPU代码)
|
| 143 |
-
start = torch.datetime.now()
|
| 144 |
-
with torch.no_grad(), torch.inference_mode():
|
| 145 |
-
latents = _STATE["model"].sample_images_with_grad(
|
| 146 |
-
noise, ctx, {"num_steps": NUM_STEPS}
|
| 147 |
-
)
|
| 148 |
-
pixels = _STATE["vae"].detokenize(latents) # 输出[0,1]范围
|
| 149 |
-
elapsed = (torch.datetime.now() - start).total_seconds()
|
| 150 |
-
|
| 151 |
-
# 转换为PIL Image
|
| 152 |
-
pixels = (
|
| 153 |
-
pixels[0]
|
| 154 |
-
.float()
|
| 155 |
-
.clamp(0, 1)
|
| 156 |
-
.mul(255)
|
| 157 |
-
.round()
|
| 158 |
-
.byte()
|
| 159 |
-
.permute(1, 2, 0)
|
| 160 |
-
.cpu()
|
| 161 |
-
.numpy()
|
| 162 |
-
)
|
| 163 |
-
img = Image.fromarray(pixels)
|
| 164 |
-
|
| 165 |
-
info = f"CPU · {RES}x{RES} · {NUM_STEPS}步 · {elapsed:.1f}s · Seed: {seed}"
|
| 166 |
-
return img, seed, info
|
| 167 |
-
|
| 168 |
-
# ==================== Gradio界面 ====================
|
| 169 |
-
with gr.Blocks(title="FLUX.2 Klein CPU Demo") as demo:
|
| 170 |
-
gr.Markdown(
|
| 171 |
-
f"""
|
| 172 |
-
# ⚠️ FLUX.2 Klein-4B CPU Demo(仅技术验证)
|
| 173 |
-
- 当前分辨率:{RES}x{RES},步数:{NUM_STEPS}
|
| 174 |
-
- CPU性能有限:{RES}x{RES} 1步约需30-180秒,请勿频繁提交
|
| 175 |
-
- 如遇OOM:请将RES改为256,或升级Space硬件为GPU
|
| 176 |
-
"""
|
| 177 |
-
)
|
| 178 |
-
|
| 179 |
-
with gr.Row():
|
| 180 |
-
with gr.Column():
|
| 181 |
-
prompt = gr.Textbox(
|
| 182 |
-
label="提示词",
|
| 183 |
-
placeholder="例如:a cute cat sitting on a sofa",
|
| 184 |
-
lines=2
|
| 185 |
-
)
|
| 186 |
-
with gr.Row():
|
| 187 |
-
seed = gr.Number(label="种子", value=42, precision=0)
|
| 188 |
-
randomize = gr.Checkbox(label="随机种子", value=False)
|
| 189 |
-
btn = gr.Button("生成", variant="primary")
|
| 190 |
-
with gr.Column():
|
| 191 |
-
output_img = gr.Image(label="生成结果", type="pil")
|
| 192 |
-
output_info = gr.Textbox(label="运行信息", interactive=False)
|
| 193 |
-
|
| 194 |
-
btn.click(
|
| 195 |
-
fn=generate,
|
| 196 |
-
inputs=[prompt, seed, randomize],
|
| 197 |
-
outputs=[output_img, seed, output_info]
|
| 198 |
-
)
|
| 199 |
-
|
| 200 |
-
# 预设提示词
|
| 201 |
-
gr.Examples(
|
| 202 |
-
examples=[
|
| 203 |
-
["a cute cat sleeping on a windowsill"],
|
| 204 |
-
["a futuristic city at night, neon lights"],
|
| 205 |
-
["a watercolor painting of a mountain landscape"]
|
| 206 |
-
],
|
| 207 |
-
inputs=prompt
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
-
# ==================== 启动 ====================
|
| 211 |
-
if __name__ == "__main__":
|
| 212 |
-
# HF Space不需要share=True,本地运行可加share=True
|
| 213 |
-
demo.launch(show_error=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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