MaxSainz2000 commited on
Commit
944e031
·
verified ·
1 Parent(s): 868997d

Switch to Supabase Storage

Browse files
Files changed (2) hide show
  1. Dockerfile +17 -5
  2. sync_memory.py +88 -0
Dockerfile CHANGED
@@ -1,8 +1,20 @@
1
  FROM moltbot/moltbot:latest
2
 
3
- # Expose the port Hugging Face Spaces expects
4
- EXPOSE 7860
5
 
6
- # Symlink /root/.molt to /data for persistent storage compatibility
7
- # (Persistent storage on HF Spaces is mounted at /data)
8
- RUN mkdir -p /data && rm -rf /root/.molt && ln -s /data /root/.molt
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  FROM moltbot/moltbot:latest
2
 
3
+ # Switch to root to install python and access /root/.molt
4
+ USER root
5
 
6
+ # Install Python3 and Pip (Debian Bookworm base)
7
+ RUN apt-get update && \
8
+ apt-get install -y python3 python3-pip && \
9
+ rm -rf /var/lib/apt/lists/*
10
+
11
+ # Install Supabase client
12
+ # Using --break-system-packages because we are in a container and want global install
13
+ RUN pip3 install supabase --break-system-packages
14
+
15
+ # Copy the sync script
16
+ COPY sync_memory.py /app/sync_memory.py
17
+
18
+ # Launch sync script in background, then start Moltbot
19
+ # We use a shell to handle the '&' operator
20
+ CMD ["/bin/bash", "-c", "python3 /app/sync_memory.py & node dist/index.js"]
sync_memory.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import zipfile
4
+ import shutil
5
+ import threading
6
+ from supabase import create_client, Client
7
+
8
+ # Configuration
9
+ SUPABASE_URL = os.environ.get("SUPABASE_URL")
10
+ SUPABASE_KEY = os.environ.get("SUPABASE_KEY")
11
+ BUCKET_NAME = "moltbot-memory"
12
+ MEMORY_DIR = "/root/.molt"
13
+ ZIP_FILENAME = "memory.zip"
14
+ ZIP_PATH = os.path.join("/app", ZIP_FILENAME) # Working in /app
15
+ SYNC_INTERVAL = 300 # 5 minutes
16
+
17
+ def get_supabase_client():
18
+ if not SUPABASE_URL or not SUPABASE_KEY:
19
+ print("Error: SUPABASE_URL or SUPABASE_KEY not set.")
20
+ return None
21
+ try:
22
+ return create_client(SUPABASE_URL, SUPABASE_KEY)
23
+ except Exception as e:
24
+ print(f"Error connecting to Supabase: {e}")
25
+ return None
26
+
27
+ def download_and_extract_memory():
28
+ client = get_supabase_client()
29
+ if not client: return
30
+
31
+ print(f"Attempting to download {ZIP_FILENAME} from bucket {BUCKET_NAME}...")
32
+ try:
33
+ # Check if bucket exists? We assume yes.
34
+ # Download file
35
+ with open(ZIP_PATH, "wb") as f:
36
+ res = client.storage.from_(BUCKET_NAME).download(ZIP_FILENAME)
37
+ f.write(res)
38
+
39
+ print("Download successful. Extracting...")
40
+ # Create destination dir if not exists
41
+ os.makedirs(MEMORY_DIR, exist_ok=True)
42
+
43
+ with zipfile.ZipFile(ZIP_PATH, 'r') as zip_ref:
44
+ zip_ref.extractall(MEMORY_DIR)
45
+ print(f"Extracted memory to {MEMORY_DIR}")
46
+
47
+ except Exception as e:
48
+ print(f"Download/Extraction failed (Normal on first run if no memory exists): {e}")
49
+
50
+ def zip_and_upload_memory():
51
+ client = get_supabase_client()
52
+ if not client: return
53
+
54
+ if not os.path.exists(MEMORY_DIR):
55
+ print(f"Memory directory {MEMORY_DIR} does not exist. Skipping upload.")
56
+ return
57
+
58
+ print(f"Zipping {MEMORY_DIR}...")
59
+ try:
60
+ # Create a zip file
61
+ # shutil.make_archive creates file with .zip extension, so we pass base name
62
+ base_name = os.path.join("/app", "memory_upload")
63
+ shutil.make_archive(base_name, 'zip', MEMORY_DIR)
64
+ upload_zip_path = base_name + ".zip"
65
+
66
+ print(f"Uploading {upload_zip_path} to {BUCKET_NAME}/{ZIP_FILENAME}...")
67
+ with open(upload_zip_path, "rb") as f:
68
+ # Upsert=true to overwrite
69
+ client.storage.from_(BUCKET_NAME).upload(ZIP_FILENAME, f, file_options={"upsert": "true"})
70
+ print("Upload successful.")
71
+
72
+ except Exception as e:
73
+ print(f"Upload failed: {e}")
74
+
75
+ def sync_loop():
76
+ while True:
77
+ time.sleep(SYNC_INTERVAL)
78
+ zip_and_upload_memory()
79
+
80
+ if __name__ == "__main__":
81
+ # 1. On Startup: Download
82
+ download_and_extract_memory()
83
+
84
+ # 2. Start Loop
85
+ # We run the loop in the main thread?
86
+ # The CMD will run this script in background (&).
87
+ # So this script should block (run loop forever).
88
+ sync_loop()