Spaces:
Sleeping
Sleeping
Download test_encoding.py from ksk76/structural-encoder-uplan: direct link, hf CLI and curl.
- Browser
- Download file 3.97 kB
-
https://huggingface.co/spaces/ksk76/structural-encoder-uplan/resolve/main/test_encoding.py
- Command line
-
hf download hf://spaces/ksk76/structural-encoder-uplan/test_encoding.py
-
curl -L -o test_encoding.py https://huggingface.co/spaces/ksk76/structural-encoder-uplan/resolve/main/test_encoding.py
3.97 kB
| """ | |
| 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() | |