Gorluxor commited on
Commit
395ab11
·
1 Parent(s): d735fd9

hopefully final path fix

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -17,16 +17,27 @@ import base64
17
  from io import BytesIO
18
  import tempfile
19
  import uuid
 
20
 
21
- # import pathlib
22
- # HF_BASE = "/data/.huggingface" if os.getenv("SYSTEM") == "spaces" else "./.hf_cache" # /data/hf_cache
23
- # HF_BASE = str(pathlib.Path(HF_BASE).absolute())
24
- # os.environ.setdefault("HF_HOME", HF_BASE)
25
- # os.environ.setdefault("HF_HUB_CACHE", os.path.join(HF_BASE, "hub"))
26
- # os.environ.setdefault("HF_DATASETS_CACHE", os.path.join(HF_BASE, "datasets"))
27
- # os.environ.setdefault("TRANSFORMERS_CACHE", os.path.join(HF_BASE, "transformers"))
28
- # for k in ("HF_HUB_CACHE", "HF_DATASETS_CACHE", "TRANSFORMERS_CACHE"):
29
- # os.makedirs(os.environ[k], exist_ok=True)
 
 
 
 
 
 
 
 
 
 
30
 
31
  MAX_SEED = np.iinfo(np.int32).max
32
  CACHE_EXAMPLES = os.environ.get("CACHE_EXAMPLES") == "1"
@@ -34,7 +45,7 @@ AVAILABLE_TOKENS = list(PART_TOKENS.keys())
34
 
35
  # Download examples directly from the huggingface PartEdit-Bench
36
  # Login using e.g. `huggingface-cli login` or `hf login` if needed.
37
- bench = load_dataset("Aleksandar/PartEdit-Bench", revision="v1.1", split="synth", cache_dir=os.environ["HF_BASE"])
38
 
39
  use_examples = None # all with None
40
  logo = "assets/partedit.png"
 
17
  from io import BytesIO
18
  import tempfile
19
  import uuid
20
+ import pathlib
21
 
22
+ # ---- Robust HF cache setup for Spaces (persistent or not) ----
23
+
24
+ def _pick_hf_cache_base() -> str:
25
+ data = "/data"
26
+ if os.path.isdir(data) and os.access(data, os.W_OK):
27
+ return "/data/.huggingface" # persistent disk on Spaces
28
+ return "/tmp/hf_cache" # ephemeral but writable everywhere
29
+
30
+ HF_BASE = os.environ.get("HF_HOME") or _pick_hf_cache_base()
31
+ HF_BASE = str(pathlib.Path(HF_BASE).absolute())
32
+
33
+ os.environ.setdefault("HF_HOME", HF_BASE) # shared root (recommended)
34
+ os.environ.setdefault("HF_HUB_CACHE", f"{HF_BASE}/hub") # hub cache (models/datasets/spaces)
35
+ os.environ.setdefault("HF_DATASETS_CACHE", f"{HF_BASE}/datasets") # datasets Arrow cache
36
+ os.environ.setdefault("TRANSFORMERS_CACHE", f"{HF_BASE}/hub") # transformers cache
37
+
38
+ for k in ("HF_HUB_CACHE", "HF_DATASETS_CACHE", "TRANSFORMERS_CACHE"):
39
+ os.makedirs(os.environ[k], exist_ok=True)
40
+ # ---------------------------------------------------------------
41
 
42
  MAX_SEED = np.iinfo(np.int32).max
43
  CACHE_EXAMPLES = os.environ.get("CACHE_EXAMPLES") == "1"
 
45
 
46
  # Download examples directly from the huggingface PartEdit-Bench
47
  # Login using e.g. `huggingface-cli login` or `hf login` if needed.
48
+ bench = load_dataset("Aleksandar/PartEdit-Bench", revision="v1.1", split="synth", cache_dir=os.environ["HF_DATASETS_CACHE"])
49
 
50
  use_examples = None # all with None
51
  logo = "assets/partedit.png"