Repository navigation
Expand file tree
/
Copy pathrun.py
More file actions
730 lines (594 loc) · 28.4 KB
/
Copy pathrun.py
File metadata and controls
730 lines (594 loc) · 28.4 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
"""
CLI entrypoint for the Life Interest Map pipeline.
Usage:
python run.py all # Full pipeline
python run.py embed # Stream from iCloud + generate embeddings
python run.py embed --resume # Resume interrupted embedding
python run.py cluster # Run UMAP + HDBSCAN on saved embeddings
python run.py visualize # Generate HTML from clustered data
python run.py stats # Print summary
"""
import argparse
import sys
from pathlib import Path
# Ensure project root is on the path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import config
def cmd_embed(args):
"""Stream photos from iCloud, generate CLIP embeddings."""
from src.icloud_stream import authenticate, stream_photos, download_video_to_memory
from src.video_keyframes import extract_keyframes, check_ffmpeg
from src.embedder import CLIPEmbedder, save_embeddings
# Initialize CLIP model
embedder = CLIPEmbedder()
# Authenticate with iCloud
api = authenticate()
include_videos = args.include_videos
if include_videos and not check_ffmpeg():
print("WARNING: ffmpeg not found. Install it for video keyframe extraction.")
print(" choco install ffmpeg OR download from https://ffmpeg.org/download.html")
print("Continuing without video support.")
include_videos = False
# Load existing embeddings from prior sessions so we don't lose them
import numpy as np
from src.embedder import load_embeddings
all_embeddings = []
all_metadata = []
all_thumbnails = []
if config.EMBEDDINGS_FILE.exists():
try:
existing_emb, existing_meta, existing_thumbs = load_embeddings()
all_embeddings.append(existing_emb)
all_metadata.extend(existing_meta)
all_thumbnails.extend(existing_thumbs)
print(f"Loaded {len(existing_meta)} existing embeddings from prior sessions.")
except Exception as e:
print(f"Warning: could not load existing embeddings: {e}")
batch_images_bytes = []
batch_meta = []
batch_count = 0
def flush_batch():
nonlocal batch_count
if not batch_images_bytes:
return
items = zip(batch_images_bytes, batch_meta)
embs, meta, thumbs = embedder.process_stream(items, total=len(batch_images_bytes))
if len(meta) > 0:
all_embeddings.append(embs)
all_metadata.extend(meta)
all_thumbnails.extend(thumbs)
batch_images_bytes.clear()
batch_meta.clear()
batch_count += 1
# Save incrementally every 500 items
if len(all_metadata) % 500 < config.CLIP_BATCH_SIZE:
_save_current()
def _save_current():
if not all_embeddings:
return
combined = np.concatenate(all_embeddings, axis=0)
save_embeddings(combined, all_metadata, all_thumbnails, append=False)
max_session_retries = 5
for session_attempt in range(max_session_retries):
try:
for photo, image_bytes, meta in stream_photos(api, resume=True, include_videos=include_videos):
if meta["media_type"] == "video":
video_bytes = download_video_to_memory(photo)
if video_bytes:
frames = extract_keyframes(video_bytes)
for i, frame_bytes in enumerate(frames):
frame_meta = {
**meta,
"media_type": "video_keyframe",
"filename": f"{meta['filename']}_frame{i}",
}
batch_images_bytes.append(frame_bytes)
batch_meta.append(frame_meta)
else:
if image_bytes:
batch_images_bytes.append(image_bytes)
batch_meta.append(meta)
if len(batch_images_bytes) >= config.CLIP_BATCH_SIZE:
flush_batch()
# If we get here, the full stream completed successfully
break
except Exception as e:
# Save what we have so far
flush_batch()
_save_current()
if session_attempt < max_session_retries - 1:
wait = 10 * (session_attempt + 1)
print(f"\niCloud connection lost: {type(e).__name__}: {e}")
print(f"Saved progress. Re-authenticating in {wait}s... (attempt {session_attempt + 2}/{max_session_retries})")
import time
time.sleep(wait)
api = authenticate()
else:
print(f"\nFailed after {max_session_retries} session retries. Run again to resume.")
# Final flush
flush_batch()
# Final save
_save_current()
print(f"\nEmbedding complete. Total items: {len(all_metadata)}")
def cmd_cluster(args):
"""Run UMAP + HDBSCAN clustering on saved embeddings."""
from src.embedder import load_embeddings
from src.cluster import run_clustering
embeddings, metadata, thumbnails = load_embeddings()
coords_2d, labels, cluster_names = run_clustering(embeddings, metadata)
print(f"\nClustering complete. {len(set(labels)) - (1 if -1 in labels else 0)} clusters found.")
def cmd_visualize(args):
"""Generate interactive HTML visualization."""
from src.embedder import load_embeddings
from src.cluster import load_clusters
from src.visualize import build_visualization
embeddings, metadata, thumbnails = load_embeddings()
coords_2d, labels, cluster_names = load_clusters()
output = build_visualization(
coords_2d=coords_2d,
labels=labels,
cluster_names=cluster_names,
metadata=metadata,
thumbnails=thumbnails,
)
print(f"\nOpen in your browser: {output}")
def cmd_relabel(args):
"""Re-label clusters using Gemini Vision or local llama-server."""
from src.embedder import load_embeddings
from src.cluster import load_clusters
from src.labeler import (
relabel_all_clusters, relabel_all_clusters_local, save_relabeled_clusters
)
embeddings, metadata, thumbnails = load_embeddings()
coords_2d, labels, old_names = load_clusters()
if args.local:
server_url = args.server_url or config.LOCAL_LLM_URL
n_images = args.n_images or config.LOCAL_LLM_IMAGES_PER_CLUSTER
cluster_names = relabel_all_clusters_local(
embeddings, labels, thumbnails,
server_url=server_url, n_images=n_images,
)
else:
model_name = args.model if args.model else None
cluster_names = relabel_all_clusters(
embeddings, labels, thumbnails, model_name=model_name
)
if cluster_names:
save_relabeled_clusters(cluster_names)
print(f"\nRelabeling complete. {len(cluster_names)} clusters labeled.")
print("Run 'python run.py visualize' to regenerate the map.")
def cmd_stats(args):
"""Print summary statistics."""
import numpy as np
if not config.EMBEDDINGS_FILE.exists():
print("No embeddings found. Run 'python run.py embed' first.")
return
from src.embedder import load_embeddings
embeddings, metadata, thumbnails = load_embeddings()
print(f"\n{'='*50}")
print(f" LIFE INTEREST MAP — STATS")
print(f"{'='*50}")
print(f" Total items: {len(metadata)}")
print(f" Embedding shape: {embeddings.shape}")
print(f" Embedding dtype: {embeddings.dtype}")
# Count media types
types = {}
for m in metadata:
t = m.get("media_type", "unknown")
types[t] = types.get(t, 0) + 1
print(f"\n Media types:")
for t, count in sorted(types.items(), key=lambda x: -x[1]):
print(f" {t:20s} {count:>6d}")
# Date range
dates = [m.get("date_taken") for m in metadata if m.get("date_taken")]
if dates:
dates_sorted = sorted(dates)
print(f"\n Date range: {dates_sorted[0][:10]} → {dates_sorted[-1][:10]}")
# Cluster info
if config.CLUSTER_FILE.exists():
from src.cluster import load_clusters
coords_2d, labels, cluster_names = load_clusters()
n_clusters = len(set(labels)) - (1 if -1 in labels else 0)
n_noise = int((labels == -1).sum())
print(f"\n Clusters: {n_clusters}")
print(f" Unclustered: {n_noise} ({n_noise/len(labels)*100:.1f}%)")
print(f"\n Cluster breakdown:")
for cid in sorted(set(labels)):
count = int((labels == cid).sum())
name = cluster_names.get(cid, f"Cluster {cid}")
print(f" {name:40s} {count:>5d} items")
# File sizes
print(f"\n Data files:")
for p in [config.EMBEDDINGS_FILE, config.CLUSTER_FILE, config.PROGRESS_FILE]:
if p.exists():
size_mb = p.stat().st_size / 1024 / 1024
print(f" {p.name:25s} {size_mb:>8.2f} MB")
print(f"{'='*50}\n")
# ═══════════════════════════════════════════════════════════════════════
# COMBINED NARRATIVE
# ═══════════════════════════════════════════════════════════════════════
def cmd_narrate_combined(args):
"""Generate a combined narrative cross-referencing photos and spending."""
from src.combined_narrative import generate_combined_narrative, save_combined_narrative
text = generate_combined_narrative(
server_url=args.server_url if hasattr(args, 'server_url') else None,
use_local=args.local,
model_name=args.model if hasattr(args, 'model') else None,
)
if text:
save_combined_narrative(text)
else:
print("Failed to generate combined narrative.")
# ═══════════════════════════════════════════════════════════════════════
# SPENDING MAP COMMANDS
# ═══════════════════════════════════════════════════════════════════════
def cmd_spending_ingest(args):
"""Parse bank transactions from CSV, PDF, or Plaid."""
from src.spending.parser import parse_csv, save_transactions
all_txns = []
# Auto-detect file types from all provided paths
from glob import glob as globfn
all_files = []
for source in [args.csv, args.pdf, args.xlsx]:
if source:
for pattern in source:
all_files.extend(globfn(pattern))
for f in all_files:
f_lower = f.lower()
try:
if f_lower.endswith(".csv"):
txns = parse_csv(f, currency=args.currency)
elif f_lower.endswith(".pdf"):
from src.spending.pdf_parser import parse_pdf
txns = parse_pdf(f, currency=args.currency)
elif f_lower.endswith((".xlsx", ".xls")):
from src.spending.xlsx_parser import parse_xlsx
txns = parse_xlsx(f, currency=args.currency)
else:
print(f"Unsupported file type: {f}")
continue
all_txns.extend(txns)
except Exception as e:
print(f"Error parsing {f}: {e}")
if args.plaid:
from src.spending.plaid_client import fetch_transactions
txns = fetch_transactions()
all_txns.extend(txns)
if args.plaid_link:
from src.spending.plaid_client import link_bank
link_bank()
return
if all_txns:
save_transactions(all_txns, append=not args.replace)
else:
print("No transactions parsed. Provide --csv, --pdf, or --plaid.")
def cmd_spending_embed(args):
"""Embed all parsed transactions."""
from src.spending.parser import load_transactions
from src.spending.embedder import embed_transactions, save_spending_embeddings
transactions = load_transactions()
embeddings, metadata = embed_transactions(transactions)
save_spending_embeddings(embeddings, metadata)
def cmd_spending_cluster(args):
"""Cluster spending embeddings."""
import hdbscan
from src.cluster import reduce_dimensions
from src.spending.embedder import load_spending_embeddings
from src.spending.labeler import label_spending_clusters, save_spending_clusters
embeddings, metadata = load_spending_embeddings()
# UMAP to 50D for clustering
emb_50d = reduce_dimensions(embeddings, n_components=config.UMAP_N_COMPONENTS_CLUSTER)
# HDBSCAN with spending-tuned params
print(f"HDBSCAN clustering {len(emb_50d)} transactions...")
clusterer = hdbscan.HDBSCAN(
min_cluster_size=config.SPENDING_HDBSCAN_MIN_CLUSTER_SIZE,
min_samples=config.SPENDING_HDBSCAN_MIN_SAMPLES,
metric="euclidean",
cluster_selection_method="eom",
)
labels = clusterer.fit_predict(emb_50d)
n_clusters = len(set(labels)) - (1 if -1 in labels else 0)
n_noise = (labels == -1).sum()
print(f"Found {n_clusters} clusters, {n_noise} noise ({n_noise/len(labels)*100:.1f}%)")
if n_noise / len(labels) > 0.4:
print("WARNING: >40% noise. Consider lowering SPENDING_HDBSCAN_MIN_CLUSTER_SIZE in config.")
# UMAP to 2D for visualization
coords_2d = reduce_dimensions(embeddings, n_components=config.UMAP_N_COMPONENTS_VIS)
# Label clusters
cluster_names = label_spending_clusters(metadata, labels, embeddings)
save_spending_clusters(coords_2d, labels, cluster_names)
print(f"\nSpending clustering complete. {n_clusters} categories found.")
def cmd_spending_relabel(args):
"""Re-label spending clusters using local LLM."""
import numpy as np
import requests as http_requests
from tqdm import tqdm
from src.spending.embedder import load_spending_embeddings
from src.spending.labeler import load_spending_clusters, save_spending_clusters
embeddings, metadata = load_spending_embeddings()
coords_2d, labels, old_names = load_spending_clusters()
server_url = args.server_url or config.LOCAL_LLM_URL
# Check server
try:
r = http_requests.get(f"{server_url}/health", timeout=5)
assert r.status_code == 200
print(f"Using local LLM at {server_url}")
except Exception:
print(f"ERROR: LLM server not reachable at {server_url}")
print("Start it with:")
print(" .\\llama-cpp\\llama-server.exe -m models\\Qwen2.5-VL-7B-Instruct-Q4_K_M.gguf --mmproj models\\Qwen2.5-VL-7B-Instruct-mmproj.gguf --port 8080 -ngl 99")
return
unique_labels = sorted(set(labels))
cluster_names = {}
prompt_template = (
"Below are transaction descriptions from one cluster in a personal bank statement. "
"What specific spending category do they represent? "
"Reply with ONLY a 2-5 word category label. Be specific - "
"'Sportybet Deposits' not 'Gambling', 'OPay Savings' not 'Transfers', "
"'MTN Airtime' not 'Bills'. No quotes, no explanation.\n\n"
"Transactions:\n{transactions}"
)
for label in tqdm(unique_labels, desc="LLM labeling"):
if label == -1:
cluster_names[-1] = "unclustered"
continue
mask = labels == label
indices = np.where(mask)[0]
descs = [metadata[i]["description"] for i in indices]
amounts = [metadata[i].get("amount", 0) for i in indices]
count = len(descs)
# Pick up to 20 representative descriptions
sample = descs[:20]
sample_with_amounts = [
f" {d} ({amounts[i]:,.0f} NGN)" for i, d in enumerate(sample)
]
txn_text = "\n".join(sample_with_amounts)
prompt = prompt_template.format(transactions=txn_text)
payload = {
"model": "local",
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 30,
"temperature": 0.1,
}
try:
r = http_requests.post(
f"{server_url}/v1/chat/completions",
json=payload, timeout=60,
)
r.raise_for_status()
text = r.json()["choices"][0]["message"]["content"]
# Clean up
import re
text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL)
text = text.strip().strip('"').strip("'").split("\n")[0].strip()
if len(text) < 2 or len(text) > 60:
text = old_names.get(label, f"Cluster {label}")
cluster_names[label] = text
except Exception as e:
tqdm.write(f" LLM error for cluster {label}: {e}")
cluster_names[label] = old_names.get(label, f"Cluster {label}")
tqdm.write(f" Cluster {label} ({count} txns): {cluster_names[label]}")
save_spending_clusters(coords_2d, labels, cluster_names)
print(f"\nRelabeling complete. Run 'python run.py spending-visualize' to regenerate.")
def cmd_spending_recluster(args):
"""Recluster spending data using entity-first approach."""
from src.spending.embedder import load_spending_embeddings
from src.spending.recluster import recluster
embeddings, metadata = load_spending_embeddings()
recluster(
embeddings, metadata,
use_llm=not args.no_llm,
server_url=args.server_url,
)
print("\nRun 'python run.py spending-visualize' to see the results.")
def cmd_spending_aliases(args):
"""Manage merchant aliases."""
from src.spending.entities import load_aliases, save_aliases, ALIASES_FILE
from src.spending.embedder import load_spending_embeddings
from src.spending.entities import extract_all_entities, generate_seed_aliases
if args.generate:
# Use LLM to generate initial aliases
embeddings, metadata = load_spending_embeddings()
suggested = generate_seed_aliases(metadata)
existing = load_aliases()
# Only add new ones, don't overwrite user corrections
added = 0
for key, val in suggested.items():
if key not in existing:
existing[key] = val
added += 1
save_aliases(existing)
print(f"Added {added} new alias suggestions. Edit {ALIASES_FILE} to correct them.")
elif args.show:
aliases = load_aliases()
if not aliases:
print("No aliases yet. Run 'python run.py spending-aliases --generate' to create initial guesses.")
else:
for key, val in sorted(aliases.items()):
print(f" {key:40s} -> {val['label']:30s} [{val['macro']}]")
print(f"\n{len(aliases)} aliases. Edit: {ALIASES_FILE}")
else:
print(f"Alias file: {ALIASES_FILE}")
print(" --show List all aliases")
print(" --generate Use LLM to suggest aliases for unknown entities")
def cmd_spending_narrate(args):
"""Generate a written narrative analysis of spending patterns."""
from src.spending.embedder import load_spending_embeddings
from src.spending.labeler import load_spending_clusters
from src.spending.narrate import (
generate_narrative_local, generate_narrative_gemini, save_narrative,
)
embeddings, metadata = load_spending_embeddings()
coords_2d, labels, cluster_names = load_spending_clusters()
if args.local:
text = generate_narrative_local(
metadata, labels, cluster_names,
server_url=args.server_url,
)
else:
text = generate_narrative_gemini(
metadata, labels, cluster_names,
model_name=args.model,
)
if text:
save_narrative(text)
else:
print("Failed to generate narrative.")
def cmd_spending_visualize(args):
"""Generate spending map HTML."""
from src.spending.embedder import load_spending_embeddings
from src.spending.labeler import load_spending_clusters
from src.spending.visualize import build_spending_visualization
embeddings, metadata = load_spending_embeddings()
coords_2d, labels, cluster_names = load_spending_clusters()
output = build_spending_visualization(
coords_2d=coords_2d,
labels=labels,
cluster_names=cluster_names,
metadata=metadata,
)
print(f"\nOpen in your browser: {output}")
def cmd_spending_all(args):
"""Run full spending pipeline: ingest -> embed -> cluster -> visualize."""
print("=" * 50)
print(" SPENDING: Ingest")
print("=" * 50)
cmd_spending_ingest(args)
print("\n" + "=" * 50)
print(" SPENDING: Embed")
print("=" * 50)
cmd_spending_embed(args)
print("\n" + "=" * 50)
print(" SPENDING: Cluster")
print("=" * 50)
cmd_spending_cluster(args)
print("\n" + "=" * 50)
print(" SPENDING: Visualize")
print("=" * 50)
cmd_spending_visualize(args)
print("\nDone! Open output/spending_map.html in your browser.")
# ═══════════════════════════════════════════════════════════════════════
def cmd_all(args):
"""Run the full pipeline: embed -> cluster -> visualize."""
print("=" * 50)
print(" PHASE 1: Embedding")
print("=" * 50)
cmd_embed(args)
print("\n" + "=" * 50)
print(" PHASE 2: Clustering")
print("=" * 50)
cmd_cluster(args)
print("\n" + "=" * 50)
print(" PHASE 3: Visualization")
print("=" * 50)
cmd_visualize(args)
print("\nDone! Open output/interest_map.html in your browser.")
def main():
parser = argparse.ArgumentParser(
description="Life Interest Map — discover your interests from your photo library",
)
subparsers = parser.add_subparsers(dest="command", help="Pipeline stage to run")
# embed
p_embed = subparsers.add_parser("embed", help="Stream from iCloud + generate CLIP embeddings")
p_embed.add_argument("--resume", action="store_true", default=True,
help="Resume from last checkpoint (default: True)")
p_embed.add_argument("--no-resume", action="store_false", dest="resume",
help="Start fresh, re-process everything")
p_embed.add_argument("--include-videos", action="store_true", default=False,
help="Include video keyframe extraction (requires ffmpeg)")
# cluster
p_cluster = subparsers.add_parser("cluster", help="Run UMAP + HDBSCAN on saved embeddings")
# visualize
p_vis = subparsers.add_parser("visualize", help="Generate interactive HTML from clustered data")
# relabel
p_relabel = subparsers.add_parser("relabel", help="Re-label clusters using Gemini or local VLM")
p_relabel.add_argument("--model", type=str, default=None,
help="Gemini model override (e.g. gemini-3-flash-preview)")
p_relabel.add_argument("--local", action="store_true", default=False,
help="Use local llama-server instead of Gemini")
p_relabel.add_argument("--server-url", type=str, default=None,
help="llama-server URL (default: http://localhost:8080)")
p_relabel.add_argument("--n-images", type=int, default=None,
help="Images per cluster for local VLM (default: 10)")
# stats
p_stats = subparsers.add_parser("stats", help="Print summary statistics")
# all
p_all = subparsers.add_parser("all", help="Run full pipeline: embed -> cluster -> visualize")
p_all.add_argument("--resume", action="store_true", default=True)
p_all.add_argument("--no-resume", action="store_false", dest="resume")
p_all.add_argument("--include-videos", action="store_true", default=False)
# ── Spending Map commands ─────────────────────────────────────────
p_singest = subparsers.add_parser("spending-ingest", help="Parse bank transactions")
p_singest.add_argument("--csv", nargs="+", default=None, help="CSV file(s) or glob pattern")
p_singest.add_argument("--pdf", nargs="+", default=None, help="PDF file(s) or glob pattern")
p_singest.add_argument("--xlsx", nargs="+", default=None, help="Excel file(s) or glob pattern")
p_singest.add_argument("--plaid", action="store_true", default=False,
help="Fetch from linked Plaid accounts")
p_singest.add_argument("--plaid-link", action="store_true", default=False,
help="Link a new bank via Plaid")
p_singest.add_argument("--currency", type=str, default=None,
help="Override currency code (e.g. NGN, USD, GBP)")
p_singest.add_argument("--replace", action="store_true", default=False,
help="Replace existing transactions instead of appending")
p_sembed = subparsers.add_parser("spending-embed", help="Embed parsed transactions")
p_scluster = subparsers.add_parser("spending-cluster", help="Cluster spending embeddings")
p_srelabel = subparsers.add_parser("spending-relabel", help="Re-label spending clusters with local LLM")
p_srelabel.add_argument("--server-url", type=str, default=None,
help="LLM server URL (default: http://localhost:8080)")
p_srecluster = subparsers.add_parser("spending-recluster",
help="Recluster by entity (not payment rail)")
p_srecluster.add_argument("--no-llm", action="store_true", default=False,
help="Skip LLM labeling, use entity names only")
p_srecluster.add_argument("--server-url", type=str, default=None,
help="LLM server URL (default: http://localhost:8080)")
p_saliases = subparsers.add_parser("spending-aliases", help="Manage merchant aliases")
p_saliases.add_argument("--show", action="store_true", help="List all aliases")
p_saliases.add_argument("--generate", action="store_true",
help="Use LLM to generate initial alias suggestions")
p_snarrate = subparsers.add_parser("spending-narrate", help="Generate written spending narrative")
p_snarrate.add_argument("--local", action="store_true", default=False,
help="Use local llama-server")
p_snarrate.add_argument("--server-url", type=str, default=None)
p_snarrate.add_argument("--model", type=str, default=None,
help="Gemini model (default: config)")
p_combined = subparsers.add_parser("narrate-combined",
help="Cross-reference photos + spending narrative")
p_combined.add_argument("--local", action="store_true", default=False,
help="Use local llama-server")
p_combined.add_argument("--server-url", type=str, default=None)
p_combined.add_argument("--model", type=str, default=None,
help="Gemini model (default: config)")
p_svis = subparsers.add_parser("spending-visualize", help="Generate spending map HTML")
p_sall = subparsers.add_parser("spending-all", help="Full spending pipeline")
p_sall.add_argument("--csv", nargs="+", default=None, help="CSV file(s)")
p_sall.add_argument("--pdf", nargs="+", default=None, help="PDF file(s)")
p_sall.add_argument("--xlsx", nargs="+", default=None, help="Excel file(s)")
p_sall.add_argument("--plaid", action="store_true", default=False)
p_sall.add_argument("--plaid-link", action="store_true", default=False)
p_sall.add_argument("--currency", type=str, default=None)
p_sall.add_argument("--replace", action="store_true", default=False)
args = parser.parse_args()
if not args.command:
parser.print_help()
sys.exit(1)
commands = {
"embed": cmd_embed,
"cluster": cmd_cluster,
"relabel": cmd_relabel,
"visualize": cmd_visualize,
"stats": cmd_stats,
"all": cmd_all,
"spending-ingest": cmd_spending_ingest,
"spending-embed": cmd_spending_embed,
"spending-cluster": cmd_spending_cluster,
"spending-relabel": cmd_spending_relabel,
"spending-recluster": cmd_spending_recluster,
"spending-aliases": cmd_spending_aliases,
"spending-narrate": cmd_spending_narrate,
"narrate-combined": cmd_narrate_combined,
"spending-visualize": cmd_spending_visualize,
"spending-all": cmd_spending_all,
}
commands[args.command](args)
if __name__ == "__main__":
main()