File size: 3,965 Bytes
5774aff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Uplan β€” Standalone Encoding Test

Usage:
    python test_encoding.py <path_to_pdf>

Runs the full encoding pipeline (extract pages β†’ build graph β†’ run rules)
on a single PDF and prints the SemanticGraph JSON + compression stats.

This validates the core innovation before wiring LangGraph.
"""

from __future__ import annotations

import json
import sys
import time


def main():
    if len(sys.argv) < 2:
        print("Usage: python test_encoding.py <path_to_pdf>")
        print("  e.g. python test_encoding.py ./test_files/bank_statement.pdf")
        sys.exit(1)

    pdf_path = sys.argv[1]

    print(f"\n{'═' * 60}")
    print(f"  UPLAN STRUCTURAL ENCODING TEST")
    print(f"  Input: {pdf_path}")
    print(f"{'═' * 60}\n")

    # Read PDF bytes
    with open(pdf_path, "rb") as f:
        pdf_bytes = f.read()
    print(f"  PDF size: {len(pdf_bytes):,} bytes\n")

    # ── Stage 1: Per-page extraction ─────────────────────────────────
    print("─── STAGE 1: Per-page entity extraction (Gemini Flash) ───")
    from encoding.extractor import extract_pages

    t0 = time.time()
    pages = extract_pages(pdf_bytes)
    extraction_time = time.time() - t0

    print(f"\n  Extracted {len(pages)} pages in {extraction_time:.1f}s")
    for p in pages:
        print(f"    Page {p.page_number}: {p.page_type.value}"
              f" | txns={len(p.transactions)}"
              f" | flags={p.anomaly_flags}")

    # ── Stage 2: Graph building ──────────────────────────────────────
    print(f"\n─── STAGE 2: Graph merge ───")
    from encoding.graph_builder import build_graph

    t1 = time.time()
    graph = build_graph(pages)
    build_time = time.time() - t1

    print(f"\n  Graph built in {build_time:.1f}s")

    # ── Stage 3: Rule engine ─────────────────────────────────────────
    print(f"\n─── STAGE 3: Deterministic rule engine ───")
    from rules.engine import run_rules

    findings = run_rules(graph)
    print(f"  {len(findings)} rule finding(s):")
    for f in findings:
        icon = "πŸ”΄" if f.severity == "critical" else "🟑" if f.severity == "warning" else "πŸ”΅"
        print(f"    {icon} [{f.rule_id}] {f.message}")

    # ── Compression stats (your strongest demo moment) ───────────────
    print(f"\n{'═' * 60}")
    print(f"  COMPRESSION STATS")
    print(f"{'═' * 60}")
    print(f"  Source pages:          {graph.source_page_count}")
    print(f"  Est. raw tokens:       {graph.estimated_raw_tokens:,}")
    print(f"  Graph tokens:          {graph.token_count:,}")
    if graph.estimated_raw_tokens > 0:
        reduction = (1 - graph.token_count / graph.estimated_raw_tokens) * 100
        ratio = graph.estimated_raw_tokens / max(graph.token_count, 1)
        print(f"  Token reduction:       {reduction:.1f}%")
        print(f"  Compression ratio:     {ratio:.0f}:1")
    print(f"  Extraction time:       {extraction_time:.1f}s")
    print(f"  Build time:            {build_time:.1f}s")
    print(f"  Total pipeline:        {extraction_time + build_time:.1f}s")

    # ── Output graph JSON ────────────────────────────────────────────
    graph_json = graph.model_dump(mode="json")
    print(f"\n{'═' * 60}")
    print(f"  SEMANTIC GRAPH JSON")
    print(f"{'═' * 60}")
    print(json.dumps(graph_json, indent=2, default=str))

    # Also save to file
    output_path = pdf_path.rsplit(".", 1)[0] + "_graph.json"
    with open(output_path, "w") as f:
        json.dump(graph_json, f, indent=2, default=str)
    print(f"\n  βœ“ Graph saved to: {output_path}")


if __name__ == "__main__":
    main()