Spaces:
Sleeping
Sleeping
Upload scripts/backup_memory.py with huggingface_hub
Browse files- scripts/backup_memory.py +36 -0
scripts/backup_memory.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
try:
|
| 6 |
+
from huggingface_hub import HfApi
|
| 7 |
+
HF_AVAILABLE = True
|
| 8 |
+
except ImportError:
|
| 9 |
+
HF_AVAILABLE = False
|
| 10 |
+
print("huggingface_hub not found. Backup disabled.")
|
| 11 |
+
|
| 12 |
+
CONFIG_DIR = Path(".openclaw")
|
| 13 |
+
MEMORY_FILE = CONFIG_DIR / "cain_memory.json"
|
| 14 |
+
REPO_ID = os.environ.get("SPACE_ID", "tao-shen/HuggingClaw-Cain")
|
| 15 |
+
|
| 16 |
+
def save_memory(memory_data: dict):
|
| 17 |
+
"""Save local memory state and commit to dataset."""
|
| 18 |
+
print(f"[Cain] Saving memory to local dataset...")
|
| 19 |
+
|
| 20 |
+
CONFIG_DIR.mkdir(exist_ok=True)
|
| 21 |
+
with open(MEMORY_FILE, "w") as f:
|
| 22 |
+
json.dump(memory_data, f, indent=2)
|
| 23 |
+
|
| 24 |
+
if HF_AVAILABLE:
|
| 25 |
+
try:
|
| 26 |
+
api = HfApi()
|
| 27 |
+
api.upload_file(
|
| 28 |
+
path_or_fileobj=MEMORY_FILE,
|
| 29 |
+
path_in_repo=MEMORY_FILE.as_posix(),
|
| 30 |
+
repo_id=REPO_ID,
|
| 31 |
+
repo_type="dataset",
|
| 32 |
+
commit_message="Update Cain memory"
|
| 33 |
+
)
|
| 34 |
+
print(f"[Cain] Memory successfully synced to Dataset.")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"[Cain] Warning: Failed to sync to dataset: {e}")
|