Spaces:
Running
Running
File size: 9,354 Bytes
24f95f0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | """
persistent_store.py — HF Spaces-aware persistent storage
THE PROBLEM:
HF Spaces has an ephemeral filesystem. Every time the Space sleeps and wakes,
or is restarted, ALL files in data/ are deleted. This means:
- All case memory is lost
- All cached responses are lost
- All learned skills are lost
- All simulation history is lost
THE SOLUTION:
Use HF Datasets as a key-value store. It's free, persistent across restarts,
and works with your existing HF_TOKEN secret.
Fallback: if HF_TOKEN is not set (local dev), uses local JSON files as before.
USAGE:
from app.services.persistent_store import store
# Save
store.set("cases:abc123", {"query": "...", "answer": "..."})
# Load
case = store.get("cases:abc123")
# List all keys with prefix
keys = store.list_prefix("cases:")
"""
from __future__ import annotations
import json
import logging
import os
import pathlib
import threading
import time
from typing import Any, Optional
logger = logging.getLogger(__name__)
HF_TOKEN = os.getenv("HF_TOKEN", "")
HF_REPO_ID = os.getenv("HF_STORE_REPO", "") # e.g. "DevodG/janus-memory"
IS_HF_SPACE = os.getenv("SPACE_ID", "") != "" # HF injects SPACE_ID automatically
# Local fallback path
try:
from app.config import DATA_DIR
except ImportError:
DATA_DIR = pathlib.Path(__file__).parent.parent / "data"
class _LocalStore:
"""Local JSON file store — for development."""
def __init__(self):
self._base = pathlib.Path(DATA_DIR)
self._lock = threading.RLock()
def _path(self, key: str) -> pathlib.Path:
# key like "cases:abc123" → data/cases/abc123.json
parts = key.split(":", 1)
if len(parts) == 2:
folder, name = parts
else:
folder, name = "misc", parts[0]
p = self._base / folder
p.mkdir(parents=True, exist_ok=True)
return p / f"{name}.json"
def get(self, key: str) -> Optional[Any]:
with self._lock:
p = self._path(key)
if not p.exists():
return None
try:
return json.loads(p.read_text())
except Exception:
return None
def set(self, key: str, value: Any) -> bool:
with self._lock:
try:
p = self._path(key)
p.write_text(json.dumps(value, indent=2, default=str))
return True
except Exception as e:
logger.warning("LocalStore.set(%s) failed: %s", key, e)
return False
def delete(self, key: str) -> bool:
with self._lock:
p = self._path(key)
if p.exists():
p.unlink()
return True
return False
def list_prefix(self, prefix: str) -> list[str]:
with self._lock:
parts = prefix.rstrip(":").split(":", 1)
folder = parts[0]
folder_path = self._base / folder
if not folder_path.exists():
return []
return [
f"{folder}:{f.stem}"
for f in folder_path.glob("*.json")
]
class _HFDatasetStore:
"""
Persistent store backed by a private HF Dataset repo.
Each key is stored as a file in the dataset repo:
cases/abc123.json
skills/pattern_xyz.json
memory/index.json
etc.
Writes are batched and committed every 60s to avoid rate limits.
Reads always check local cache first.
"""
def __init__(self):
from huggingface_hub import HfApi
self._api = HfApi(token=HF_TOKEN)
self._repo = HF_REPO_ID
self._cache: dict[str, Any] = {}
self._dirty: dict[str, Any] = {} # pending writes
self._lock = threading.RLock()
self._last_commit = 0.0
self._commit_interval = 60 # seconds
# Ensure repo exists
try:
self._api.repo_info(repo_id=self._repo, repo_type="dataset")
except Exception:
logger.info("Creating HF dataset repo: %s", self._repo)
try:
self._api.create_repo(repo_id=self._repo, repo_type="dataset", private=True)
except Exception as e:
logger.error("Could not create HF dataset repo: %s", e)
# Background commit thread
t = threading.Thread(target=self._commit_loop, daemon=True)
t.start()
def get(self, key: str) -> Optional[Any]:
with self._lock:
if key in self._dirty:
return self._dirty[key]
if key in self._cache:
return self._cache[key]
# Fetch from HF
try:
from huggingface_hub import hf_hub_download
path = hf_hub_download(
repo_id=self._repo,
filename=self._key_to_filename(key),
repo_type="dataset",
token=HF_TOKEN,
)
data = json.loads(pathlib.Path(path).read_text())
with self._lock:
self._cache[key] = data
return data
except Exception:
return None
def set(self, key: str, value: Any) -> bool:
with self._lock:
self._dirty[key] = value
self._cache[key] = value
return True
def delete(self, key: str) -> bool:
with self._lock:
self._dirty.pop(key, None)
self._cache.pop(key, None)
try:
self._api.delete_file(
path_in_repo=self._key_to_filename(key),
repo_id=self._repo,
repo_type="dataset",
)
return True
except Exception:
return False
def list_prefix(self, prefix: str) -> list[str]:
try:
files = self._api.list_repo_files(repo_id=self._repo, repo_type="dataset")
folder = prefix.rstrip(":").replace(":", "/")
return [
self._filename_to_key(f)
for f in files
if f.startswith(folder + "/")
]
except Exception:
return []
def flush(self):
"""Force-commit all pending writes now."""
self._commit_dirty()
def _commit_loop(self):
while True:
time.sleep(self._commit_interval)
self._commit_dirty()
def _commit_dirty(self):
with self._lock:
if not self._dirty:
return
batch = dict(self._dirty)
self._dirty.clear()
import tempfile
ops = []
tmp_files = []
try:
for key, value in batch.items():
f = tempfile.NamedTemporaryFile(
mode="w", suffix=".json", delete=False
)
json.dump(value, f, indent=2, default=str)
f.close()
tmp_files.append(f.name)
ops.append({
"path_in_repo": self._key_to_filename(key),
"path_or_fileobj": f.name,
})
self._api.upload_folder(
repo_id=self._repo,
repo_type="dataset",
folder_path=None, # type: ignore
path_in_repo="",
# use individual file uploads instead
)
# Simpler: upload each file
for op in ops:
self._api.upload_file(
repo_id=self._repo,
repo_type="dataset",
path_in_repo=op["path_in_repo"],
path_or_fileobj=op["path_or_fileobj"],
token=HF_TOKEN,
)
logger.info("PersistentStore: committed %d keys to HF", len(batch))
except Exception as e:
logger.error("PersistentStore: commit failed: %s", e)
# Re-queue failed writes
with self._lock:
for key, value in batch.items():
if key not in self._dirty:
self._dirty[key] = value
finally:
for f in tmp_files:
try:
os.unlink(f)
except Exception:
pass
@staticmethod
def _key_to_filename(key: str) -> str:
"""cases:abc123 → cases/abc123.json"""
return key.replace(":", "/") + ".json"
@staticmethod
def _filename_to_key(filename: str) -> str:
"""cases/abc123.json → cases:abc123"""
return filename.replace("/", ":").removesuffix(".json")
def _build_store():
"""Pick the right backend based on environment."""
if IS_HF_SPACE and HF_TOKEN and HF_REPO_ID:
logger.info("PersistentStore: using HF Datasets backend (repo: %s)", HF_REPO_ID)
try:
return _HFDatasetStore()
except ImportError:
logger.warning("huggingface_hub not installed — falling back to local store")
if IS_HF_SPACE and not HF_REPO_ID:
logger.warning(
"PersistentStore: running on HF Space but HF_STORE_REPO is not set! "
"All data will be lost on restart. Set HF_STORE_REPO=YourUsername/janus-memory "
"in Space Secrets."
)
return _LocalStore()
# Module-level singleton
store = _build_store()
|