Spaces:
Sleeping
Sleeping
KevinIsInCoding Claude Fable 5 commited on
chore(deploy): add _ensure_data() so main is Space-deployable (one-branch prep) (#20)
Browse filesAdds the HF-dataset download shim (chroma index + graph pickle) that previously only
lived on hf-clean. It is a no-op locally (data present) and fetches runtime data on the
Space (empty storage). With this, main serves both dev and the Space — the only reason a
separate hf-clean branch existed.
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -44,6 +44,50 @@ def _load_collection():
|
|
| 44 |
_logger.warning("ChromaDB collection not found — running in demo mode (no data)")
|
| 45 |
return None
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
_collection = _load_collection()
|
| 48 |
_graph = _load_graph()
|
| 49 |
_trials = _load_trials()
|
|
|
|
| 44 |
_logger.warning("ChromaDB collection not found — running in demo mode (no data)")
|
| 45 |
return None
|
| 46 |
|
| 47 |
+
|
| 48 |
+
_HF_DATASET = "KevinIsCoding/candle-fire-data"
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def _ensure_data() -> None:
|
| 52 |
+
"""Download chroma index + graph from the HF dataset repo if not already present.
|
| 53 |
+
|
| 54 |
+
No-op locally (data is on disk); on the HF Space (empty storage) it fetches the
|
| 55 |
+
runtime data. Having it here lets a single branch serve both dev and the Space.
|
| 56 |
+
"""
|
| 57 |
+
need_chroma = not (CHROMA_DIR / "chroma.sqlite3").exists()
|
| 58 |
+
need_graph = not GRAPH_PICKLE_PATH.exists()
|
| 59 |
+
if not need_chroma and not need_graph:
|
| 60 |
+
return
|
| 61 |
+
try:
|
| 62 |
+
from huggingface_hub import hf_hub_download, snapshot_download
|
| 63 |
+
if need_chroma:
|
| 64 |
+
_logger.info("Downloading chroma index from HF dataset...")
|
| 65 |
+
CHROMA_DIR.mkdir(parents=True, exist_ok=True)
|
| 66 |
+
snapshot_download(
|
| 67 |
+
repo_id=_HF_DATASET, repo_type="dataset",
|
| 68 |
+
local_dir=str(CHROMA_DIR), allow_patterns=["chroma/**"],
|
| 69 |
+
)
|
| 70 |
+
nested = CHROMA_DIR / "chroma"
|
| 71 |
+
if nested.exists() and not (CHROMA_DIR / "chroma.sqlite3").exists():
|
| 72 |
+
import shutil
|
| 73 |
+
for item in nested.iterdir():
|
| 74 |
+
shutil.move(str(item), str(CHROMA_DIR / item.name))
|
| 75 |
+
nested.rmdir()
|
| 76 |
+
_logger.info("Chroma download complete")
|
| 77 |
+
if need_graph:
|
| 78 |
+
_logger.info("Downloading graph from HF dataset...")
|
| 79 |
+
GRAPH_PICKLE_PATH.parent.mkdir(parents=True, exist_ok=True)
|
| 80 |
+
hf_hub_download(
|
| 81 |
+
repo_id=_HF_DATASET, repo_type="dataset",
|
| 82 |
+
filename="graph/als_graph.pkl", local_dir=str(GRAPH_PICKLE_PATH.parent.parent),
|
| 83 |
+
)
|
| 84 |
+
_logger.info("Graph download complete")
|
| 85 |
+
except Exception as e:
|
| 86 |
+
_logger.warning(f"Failed to download data from HF dataset: {e}")
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
_ensure_data()
|
| 90 |
+
|
| 91 |
_collection = _load_collection()
|
| 92 |
_graph = _load_graph()
|
| 93 |
_trials = _load_trials()
|