goumsss Claude Sonnet 4.6 commited on
Commit
1f6e488
·
1 Parent(s): 885e6f8

Restore startup pre-download for FLUX.2-klein-4B on HF Spaces

Browse files

Model is downloaded to /data/hf_cache at startup (once, then cached).
Subsequent restarts skip the download — cold start is fast.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. image_generator.py +40 -1
image_generator.py CHANGED
@@ -31,8 +31,47 @@ if IS_HF_SPACE and os.path.isdir("/data"):
31
  os.makedirs(_cache_dir, exist_ok=True)
32
  os.environ.setdefault("HF_HOME", _cache_dir)
33
  print(f"Persistent cache active → {_cache_dir}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  else:
35
- print("HF Space detected model will load on first call and stay cached in memory." if IS_HF_SPACE else "Local mode.")
36
 
37
  # ---------------------------------------------------------------------------
38
  # Emoji → descriptive text maps (fed into FLUX prompt)
 
31
  os.makedirs(_cache_dir, exist_ok=True)
32
  os.environ.setdefault("HF_HOME", _cache_dir)
33
  print(f"Persistent cache active → {_cache_dir}")
34
+
35
+ # Pre-download model at startup so the first @spaces.GPU call is fast.
36
+ # /data survives sleep/restart — download only happens once.
37
+ import threading, asyncio
38
+ from huggingface_hub import snapshot_download
39
+
40
+ _hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
41
+ _model_id = "black-forest-labs/FLUX.2-klein-4B"
42
+ _model_cache = os.path.join(_cache_dir, "models--black-forest-labs--FLUX.2-klein-4B")
43
+
44
+ if os.path.isdir(_model_cache):
45
+ print(f"✅ FLUX.2-klein-4B already cached — skipping download")
46
+ else:
47
+ print(f"Downloading FLUX.2-klein-4B to persistent cache…")
48
+ try:
49
+ _result = {}
50
+ def _download():
51
+ loop = asyncio.new_event_loop()
52
+ asyncio.set_event_loop(loop)
53
+ try:
54
+ snapshot_download(
55
+ _model_id,
56
+ cache_dir=_cache_dir,
57
+ token=_hf_token,
58
+ ignore_patterns=["*.msgpack", "*.h5", "flax_model*"],
59
+ )
60
+ _result["ok"] = True
61
+ except Exception as e:
62
+ _result["error"] = e
63
+ finally:
64
+ try: loop.close()
65
+ except: pass
66
+ _t = threading.Thread(target=_download, daemon=True)
67
+ _t.start(); _t.join()
68
+ if "error" in _result:
69
+ raise _result["error"]
70
+ print("✅ FLUX.2-klein-4B cached successfully")
71
+ except Exception as e:
72
+ print(f"⚠️ Pre-cache warning (will retry at generation time): {e}")
73
  else:
74
+ print("Local mode." if not IS_HF_SPACE else "HF Space no /data mount, model loads on first call.")
75
 
76
  # ---------------------------------------------------------------------------
77
  # Emoji → descriptive text maps (fed into FLUX prompt)