Repository navigation
Expand file tree
/
Copy pathpull_remote_data.py
More file actions
146 lines (119 loc) · 4.38 KB
/
Copy pathpull_remote_data.py
File metadata and controls
146 lines (119 loc) · 4.38 KB
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
#!/usr/bin/env python3
"""
pull_remote_data.py
Pulls experiences from the remote D1 database (via wrangler) into local saige.db.
Runs wrangler d1 execute --remote to fetch JSON, then inserts into local SQLite.
"""
import subprocess
import json
import sqlite3
import sys
def extract_json(text):
"""Extract the JSON array from wrangler's mixed output (status lines + JSON)."""
# Find the first '[' which starts the JSON array
start = text.find('[')
if start == -1:
return None
# Find the matching closing ']' by tracking bracket depth
depth = 0
for i in range(start, len(text)):
if text[i] == '[':
depth += 1
elif text[i] == ']':
depth -= 1
if depth == 0:
return json.loads(text[start:i+1])
return None
def fetch_remote_experiences(batch_size=50):
"""Fetch all experiences from remote D1 via wrangler in batches."""
all_experiences = []
offset = 0
while True:
query = (
f"SELECT id, scenario_id, ai_response, predicted_harm, actual_harm, "
f"harm_breakdown, learned_lesson, buddhist_scores, buddhist_alignment, "
f"model_version, timestamp FROM experiences LIMIT {batch_size} OFFSET {offset}"
)
result = subprocess.run(
[
"wrangler", "d1", "execute", "buddhist-ai-training",
"--remote", "--command", query
],
capture_output=True, text=True,
cwd="/home/m1styk/Projects/S-A-I-G-E/worker"
)
if result.returncode != 0:
print(f"❌ wrangler error: {result.stderr}", file=sys.stderr)
break
# Extract JSON from mixed wrangler output
data = extract_json(result.stdout)
if not data:
print(f"❌ Could not parse JSON from wrangler output", file=sys.stderr)
break
rows = data[0]["results"] if data else []
if not rows:
break
all_experiences.extend(rows)
print(f" Fetched {len(all_experiences)} experiences so far...")
offset += batch_size
return all_experiences
def insert_into_local(experiences, db_path="saige.db"):
"""Insert fetched experiences into local SQLite database."""
conn = sqlite3.connect(db_path)
conn.executemany(
"""
INSERT OR REPLACE INTO experiences
(id, scenario_id, ai_response, predicted_harm, actual_harm,
harm_breakdown, learned_lesson, buddhist_scores, buddhist_alignment,
model_version, timestamp)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
[
(
row["id"],
row["scenario_id"],
row["ai_response"],
row["predicted_harm"],
row["actual_harm"],
row["harm_breakdown"],
row["learned_lesson"],
row["buddhist_scores"],
row["buddhist_alignment"],
row["model_version"],
row["timestamp"],
)
for row in experiences
]
)
conn.commit()
conn.close()
return len(experiences)
def main():
print("📥 Pulling remote D1 experiences into local saige.db...")
print()
experiences = fetch_remote_experiences()
if not experiences:
print("❌ No experiences found on remote database.")
sys.exit(1)
print(f"\n✅ Fetched {len(experiences)} experiences from remote D1")
db_path = "/home/m1styk/Projects/S-A-I-G-E/saige.db"
count = insert_into_local(experiences, db_path)
print(f"✅ Inserted {count} experiences into local database: {db_path}")
# Quick stats
conn = sqlite3.connect(db_path)
row = conn.execute("SELECT COUNT(*), AVG(actual_harm), MIN(actual_harm), MAX(actual_harm) FROM experiences").fetchone()
print(f"\n📊 Local DB Summary:")
print(f" Total experiences: {row[0]}")
print(f" Avg harm: {row[1]:.3f}")
print(f" Min harm: {row[2]:.3f}")
print(f" Max harm: {row[3]:.3f}")
# Alignment distribution
rows = conn.execute(
"SELECT buddhist_alignment, COUNT(*) FROM experiences GROUP BY buddhist_alignment ORDER BY COUNT(*) DESC"
).fetchall()
print(f" Alignment distribution:")
for alignment, cnt in rows:
print(f" {alignment}: {cnt}")
conn.close()
if __name__ == "__main__":
main()