""" GhostGPT Memory - Space based (Salman's order 2026-11-09: NO Google Cloud) How it works: - Primary storage = the Space's own files: chats.json + MEMORY.md (saved on every chat) - Backup storage = a PRIVATE Hugging Face Dataset in the same HF account (env: HF_TOKEN + HF_CHAT_DATASET, e.g. fitzpatrick9/misty-store-1349) save() uploads both files to the dataset, restore() downloads them on startup. This is what makes chat history survive Space restarts, like ChatGPT. - Google Cloud code was removed on Salman's order. """ import os import json BASE_DIR = os.path.dirname(os.path.abspath(__file__)) MEMORY_FILE = os.path.join(BASE_DIR, "MEMORY.md") CHATS_FILE = os.path.join(BASE_DIR, "chats.json") HF_TOKEN = os.environ.get("HF_TOKEN", "").strip() HF_DATASET = os.environ.get("HF_CHAT_DATASET", "").strip() def _ensure_local_files(): if not os.path.exists(CHATS_FILE): with open(CHATS_FILE, "w", encoding="utf-8") as f: json.dump({}, f) if not os.path.exists(MEMORY_FILE): with open(MEMORY_FILE, "w", encoding="utf-8") as f: f.write("# GhostGPT Memory\n\n") def _hf_api(): if not HF_TOKEN or not HF_DATASET: return None try: from huggingface_hub import HfApi return HfApi(token=HF_TOKEN) except Exception as e: print(f"[memory] HF api error: {e}") return None def save(): """Upload chats.json + MEMORY.md to the private HF Dataset backup.""" api = _hf_api() if api is None: print("[memory] HF backup not configured (HF_TOKEN / HF_CHAT_DATASET missing) - local save only") return False try: for local_path, name in [(CHATS_FILE, "chats.json"), (MEMORY_FILE, "MEMORY.md")]: if os.path.exists(local_path): api.upload_file(path_or_fileobj=local_path, path_in_repo=name, repo_id=HF_DATASET, repo_type="dataset", commit_message="GhostGPT memory backup") print(f"[memory] backed up {name} -> {HF_DATASET}") return True except Exception as e: print(f"[memory] HF backup error: {e}") return False def restore(): """On startup, download chats.json + MEMORY.md from the HF Dataset backup.""" _ensure_local_files() api = _hf_api() if api is None: print("[memory] HF backup not configured - starting with local files") return False try: from huggingface_hub import hf_hub_download for name, local_path in [("chats.json", CHATS_FILE), ("MEMORY.md", MEMORY_FILE)]: try: downloaded = hf_hub_download(repo_id=HF_DATASET, filename=name, repo_type="dataset", token=HF_TOKEN, local_dir=BASE_DIR, force_download=True) print(f"[memory] restored {name} from {HF_DATASET}") except Exception as e: print(f"[memory] no backup for {name} yet ({str(e)[:100]}) - first run") _ensure_local_files() return True except Exception as e: print(f"[memory] restore error: {e}") return False if __name__ == "__main__": import sys cmd = sys.argv[1] if len(sys.argv) > 1 else "save" if cmd == "restore": restore() else: save()