cuio commited on
Commit
96a0849
·
verified ·
1 Parent(s): bdc8c57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -20
app.py CHANGED
@@ -213,48 +213,79 @@ def encode_prompt(prompt: str) -> torch.Tensor:
213
  # =============================================================================
214
  # MODEL LOADING WITH MAXIMUM MEMORY OPTIMIZATION
215
  # =============================================================================
 
 
 
216
  def ensure_loaded():
217
- """Load all models with aggressive memory optimizations"""
218
  if _STATE.get("model"):
219
  return
220
-
221
- print("[Memory] Starting FLUX.2 Klein-4B loading (16GB optimized)...")
222
-
223
- # 1. Load main model with memory mapping (lazy loading)
224
- print("[Memory] Loading model.safetensors (lazy load)...")
225
- sd = load_file(MODEL_PATH, device="cpu") # mmap automatically enabled
226
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  model = Flux2AdapterModel(
228
  state_dict=sd,
229
  image_resolution=RES,
230
- param_dtype=torch.float32 # float32 more stable on CPU
231
  ).to("cpu").eval()
232
-
233
- # Freeze model parameters
234
  for param in model.parameters():
235
  param.requires_grad = False
236
-
237
- # 2. Load VAE with same optimizations
238
  print("[Memory] Loading VAE...")
239
  vae = Flux2VAETokenizer(
240
  AE_PATH,
241
  device="cpu",
242
  torch_dtype=torch.float32
243
  )
244
-
245
- # 3. Load text encoder (will be Qwen3-0.6B)
246
- print("[Memory] Loading text encoder...")
247
  emb_model, emb_tokenizer = load_qwen3_embedder_cpu()
248
-
249
  _STATE.update({
250
  "model": model,
251
  "vae": vae,
252
  "emb_model": emb_model,
253
  "emb_tokenizer": emb_tokenizer
254
  })
255
-
256
- print(f"[Memory] All models loaded! Ready for {RES}x{RES} inference")
257
- print(f"[Memory] Estimated usage: ~12-14GB / 16GB")
258
 
259
  # =============================================================================
260
  # GRADIO INFERENCE FUNCTION
 
213
  # =============================================================================
214
  # MODEL LOADING WITH MAXIMUM MEMORY OPTIMIZATION
215
  # =============================================================================
216
+ from huggingface_hub import hf_hub_download, login
217
+ import os
218
+
219
  def ensure_loaded():
 
220
  if _STATE.get("model"):
221
  return
222
+
223
+ # ---------- 1. 读取并验证 HF_TOKEN ----------
224
+ hf_token = os.environ.get("HF_TOKEN")
225
+ if not hf_token:
226
+ raise RuntimeError(
227
+ "HF_TOKEN not found. "
228
+ "Go to Space Settings → Secrets → add HF_TOKEN"
229
+ )
230
+ login(token=hf_token)
231
+ print("[Setup] Authenticated to HuggingFace")
232
+
233
+ # ---------- 2. 下载模型文件(若不存在) ----------
234
+ print("[Setup] Ensuring model.safetensors exists...")
235
+ global MODEL_PATH, AE_PATH
236
+ MODEL_PATH = hf_hub_download(
237
+ repo_id="black-forest-labs/FLUX.2-dev",
238
+ filename="model.safetensors",
239
+ cache_dir="./models", # 本地缓存,避免权限问题
240
+ local_files_only=False,
241
+ token=hf_token
242
+ )
243
+
244
+ print("[Setup] Ensuring ae.safetensors exists...")
245
+ AE_PATH = hf_hub_download(
246
+ repo_id="black-forest-labs/FLUX.2-dev",
247
+ filename="ae.safetensors",
248
+ cache_dir="./models",
249
+ local_files_only=False,
250
+ token=hf_token
251
+ )
252
+
253
+ print(f"[Setup] Model: {MODEL_PATH}")
254
+ print(f"[Setup] AE: {AE_PATH}")
255
+
256
+ # ---------- 3. 加载 FLUX.2 主模型 ----------
257
+ print("[Memory] Loading FLUX.2 Klein-4B (16GB optimized)...")
258
+ sd = load_file(MODEL_PATH, device="cpu")
259
+
260
  model = Flux2AdapterModel(
261
  state_dict=sd,
262
  image_resolution=RES,
263
+ param_dtype=torch.float32
264
  ).to("cpu").eval()
265
+
 
266
  for param in model.parameters():
267
  param.requires_grad = False
268
+
269
+ # ---------- 4. 加载 VAE ----------
270
  print("[Memory] Loading VAE...")
271
  vae = Flux2VAETokenizer(
272
  AE_PATH,
273
  device="cpu",
274
  torch_dtype=torch.float32
275
  )
276
+
277
+ # ---------- 5. 加载 Qwen3 Embedder ----------
278
+ print("[Memory] Loading Qwen3-0.6B...")
279
  emb_model, emb_tokenizer = load_qwen3_embedder_cpu()
280
+
281
  _STATE.update({
282
  "model": model,
283
  "vae": vae,
284
  "emb_model": emb_model,
285
  "emb_tokenizer": emb_tokenizer
286
  })
287
+
288
+ print(f"[Memory] All models loaded! Ready for {RES}x{RES}")
 
289
 
290
  # =============================================================================
291
  # GRADIO INFERENCE FUNCTION