-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_data.py
More file actions
238 lines (194 loc) · 7.55 KB
/
load_data.py
File metadata and controls
238 lines (194 loc) · 7.55 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
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
#!/usr/bin/env python3
"""
Unified data loading script for all KG projects.
Checks kg_export against each project's processed_ids.json,
reports what's new, then runs ingestion for any project with new data.
"""
import json
import os
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).parent.resolve()
EXPORT_DATA = ROOT / "kg_export" / "data"
EXPORT_FILES = ROOT / "kg_export" / "files"
PROJECTS = {
"KG-1": {
"dir": ROOT / "kg-1",
"processed_file": ROOT / "kg-1" / "processed_ids.json",
"id_type": "uuid",
"python": ROOT / "kg-1" / "transcript-kg" / ".venv" / "bin" / "python",
},
"KG-2": {
"dir": ROOT / "kg-2",
"processed_file": ROOT / "kg-2" / "processed_ids.json",
"id_type": "uuid",
"python": ROOT / "kg-2" / "venv" / "bin" / "python",
},
"KG-3": {
"dir": ROOT / "kg-3",
"processed_file": ROOT / "kg-3" / "processed_ids.json",
"id_type": "file_stem",
"files_dir": ROOT / "kg-3" / "upload-files",
"prepare_script": ROOT / "kg-3" / "prepare_uploads.py",
"python": ROOT / "kg-3" / "app" / "backend" / "venv" / "bin" / "python",
},
"KG-4": {
"dir": ROOT / "kg-4",
"processed_file": ROOT / "kg-4" / "processed_ids.json",
"id_type": "file_stem",
"files_dir": ROOT / "kg-4" / "content_lists",
"prepare_script": ROOT / "kg-4" / "prepare_content.py",
"python": ROOT / "kg-4" / "venv" / "bin" / "python",
},
}
def load_processed(path):
if path.exists():
with open(path) as f:
return set(json.load(f))
return set()
def get_all_meeting_uuids():
"""Get all meeting UUIDs from the export."""
with open(EXPORT_DATA / "zoom_meetings.json") as f:
meetings = json.load(f)
with open(EXPORT_DATA / "zoom_past_meetings.json") as f:
past = json.load(f)
return set(m["uuid"] for m in meetings) | set(pm["uuid"] for pm in past)
def get_file_stems(directory, ext):
"""Get all file stems from a directory."""
if not directory.exists():
return set()
return set(f.stem for f in directory.glob(f"*{ext}"))
def run_script(script_path, args=None, cwd=None, python=None):
"""Run a Python script and stream output."""
cmd = [str(python) if python else sys.executable, str(script_path)]
if args:
cmd.extend(args)
result = subprocess.run(cmd, cwd=cwd or str(script_path.parent))
return result.returncode
def check_export():
"""Verify kg_export exists and report basic stats."""
if not EXPORT_DATA.exists():
print("ERROR: kg_export/data/ not found")
sys.exit(1)
with open(EXPORT_DATA / "zoom_meetings.json") as f:
meetings = json.load(f)
print(f" Export: {len(meetings)} meetings in kg_export/data/")
file_count = sum(1 for _ in EXPORT_FILES.iterdir()) if EXPORT_FILES.exists() else 0
print(f" Export: {file_count:,} files in kg_export/files/")
return len(meetings)
def prepare_project_files(name, config):
"""Run prepare script for projects that need it, so file counts are current."""
if "prepare_script" not in config:
return True
print(f" Preparing {name} files...")
rc = run_script(config["prepare_script"], python=config.get("python"))
if rc != 0:
print(f" ERROR: {name} prepare script failed (exit {rc})")
return False
return True
def check_project(name, config):
"""Check a single project and return (total, processed, new) counts."""
processed = load_processed(config["processed_file"])
if config["id_type"] == "uuid":
total_ids = get_all_meeting_uuids()
else:
# File-stem based — need to check prepared files
files_dir = config["files_dir"]
ext = ".txt" if name == "KG-3" else ".json"
total_ids = get_file_stems(files_dir, ext)
new_ids = total_ids - processed
done = len(total_ids & processed)
return len(total_ids), done, len(new_ids)
def run_project(name, config, new_count):
"""Run ingestion for a single project."""
print(f"\n{'='*50}")
print(f" Running {name} ({new_count} new)")
print(f"{'='*50}\n")
# Run the main ingestion script
python = config.get("python")
if name == "KG-1":
script = config["dir"] / "transcript-kg" / "ingest_zts_data.py"
rc = run_script(script, python=python)
elif name == "KG-2":
script = config["dir"] / "ingest_transcripts.py"
rc = run_script(script, python=python)
elif name == "KG-3":
script = config["dir"] / "extract_direct.py"
rc = run_script(script, python=python)
elif name == "KG-4":
script = config["dir"] / "ingest_raganything.py"
rc = run_script(script, python=python)
if rc != 0:
print(f" WARNING: {name} exited with code {rc}")
return False
return True
def main():
import argparse
parser = argparse.ArgumentParser(description="Unified data loader for all KG projects")
parser.add_argument("--check", action="store_true", help="Check only, don't run ingestion")
parser.add_argument("--only", nargs="+", choices=["kg-1", "kg-2", "kg-3", "kg-4"],
help="Only run specific projects (e.g., --only kg-1 kg-3)")
args = parser.parse_args()
print("=" * 50)
print(" KG Data Loader")
print("=" * 50)
# Check export
print("\nChecking export data...")
check_export()
# Determine which projects to check
projects_to_check = PROJECTS
if args.only:
filter_names = set(o.upper() for o in args.only)
projects_to_check = {k: v for k, v in PROJECTS.items() if k.upper() in filter_names}
# Prepare files for KG-3/KG-4 so counts reflect current export
for name, config in projects_to_check.items():
if "prepare_script" in config:
if not prepare_project_files(name, config):
print(f" WARNING: Could not prepare {name}, counts may be stale")
# Check each project
print("\nChecking projects...\n")
print(f" {'Project':<8} {'Total':>8} {'Done':>8} {'New':>8}")
print(f" {'-'*8} {'-'*8} {'-'*8} {'-'*8}")
results = {}
for name, config in projects_to_check.items():
total, done, new = check_project(name, config)
results[name] = {"total": total, "done": done, "new": new, "config": config}
marker = " <--" if new > 0 else ""
print(f" {name:<8} {total:>8,} {done:>8,} {new:>8,}{marker}")
projects_with_new = {name: v for name, v in results.items() if v["new"] > 0}
if not projects_with_new:
print("\n All projects are up to date. Nothing to do.")
return
if args.check:
print(f"\n {len(projects_with_new)} project(s) have new data. Run without --check to ingest.")
return
# Confirm
print(f"\n Will run ingestion for: {', '.join(projects_with_new.keys())}")
try:
answer = input(" Proceed? [y/N] ").strip().lower()
except (EOFError, KeyboardInterrupt):
print("\n Aborted.")
return
if answer != "y":
print(" Aborted.")
return
# Run ingestion
succeeded = []
failed = []
for name, info in projects_with_new.items():
ok = run_project(name, info["config"], info["new"])
if ok:
succeeded.append(name)
else:
failed.append(name)
# Final report
print(f"\n{'='*50}")
print(f" Done!")
if succeeded:
print(f" Succeeded: {', '.join(succeeded)}")
if failed:
print(f" Failed: {', '.join(failed)}")
print(f"{'='*50}")
if __name__ == "__main__":
main()