Files
chopratejas--headroom/examples/tabular_compression_demo.py
wehub-resource-sync 0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:03:20 +08:00

161 lines
5.6 KiB
Python

#!/usr/bin/env python3
"""Demo / test harness for tabular + spreadsheet compression.
Generates representative sample data and runs it through Headroom's tabular
compressor so you can see where it helps (verbose / redundant tables, and
query-driven selection) and where it correctly does nothing (compact, all-unique
data with no signal to compress against).
Usage:
python examples/tabular_compression_demo.py # run all scenarios
python examples/tabular_compression_demo.py --write DIR # also save sample files
The .xlsx scenario requires the spreadsheet extra:
pip install headroom-ai[spreadsheet]
"""
from __future__ import annotations
import argparse
import importlib.util
from pathlib import Path
import headroom
from headroom.transforms.content_router import ContentRouter
_HAS_OPENPYXL = importlib.util.find_spec("openpyxl") is not None
# ─── Sample data generators ─────────────────────────────────────────────────
def compact_unique_csv(rows: int = 60) -> str:
"""Minimal CSV, every row unique — nothing safely removable (~0 savings)."""
lines = ["id,name,age,city"]
lines += [f"{i},user_{i},{20 + i % 50},city_{i}" for i in range(rows)]
return "\n".join(lines)
def redundant_csv(rows: int = 120) -> str:
"""Highly repetitive rows — SmartCrusher can dedupe (big savings)."""
lines = ["region,product,status"]
lines += ["EMEA,widget-A,shipped" for _ in range(rows)]
return "\n".join(lines)
def verbose_markdown(rows: int = 40) -> str:
"""A padded markdown table — verbose source, lossless compaction wins."""
header = "| name | age | city | status | dept |\n| --- | --- | --- | --- | --- |"
body = "\n".join(
f"| user_{i} | {20 + i} | city_{i % 5} | active | engineering |" for i in range(rows)
)
return f"{header}\n{body}"
# ─── Runners ────────────────────────────────────────────────────────────────
def _run_router(label: str, content: str) -> None:
"""Compress raw tabular text through the ContentRouter."""
result = ContentRouter().compress(content)
before = len(content)
after = len(result.compressed)
pct = 100 * (before - after) / before if before else 0.0
print(
f"{label:24s} strat={result.strategy_used.value:9s} "
f"chars {before:6d} -> {after:6d} ({pct:5.1f}% saved)"
)
def _run_messages(label: str, content: str) -> None:
"""Compress via the full pipeline (real tokenizer accounting)."""
res = headroom.compress(
[{"role": "user", "content": content}],
compress_user_messages=True,
)
pct = 100 * res.tokens_saved / res.tokens_before if res.tokens_before else 0.0
print(
f"{label:24s} tokens {res.tokens_before:6d} -> "
f"{res.tokens_after:6d} ({pct:5.1f}% saved)"
)
def _run_xlsx(label: str, path: Path) -> None:
res = headroom.compress_spreadsheet(str(path))
pct = 100 * res.tokens_saved / res.tokens_before if res.tokens_before else 0.0
print(
f"{label:24s} tokens {res.tokens_before:6d} -> "
f"{res.tokens_after:6d} ({pct:5.1f}% saved)"
)
def _build_xlsx(path: Path) -> None:
import openpyxl
wb = openpyxl.Workbook()
unique = wb.active
unique.title = "Unique"
unique.append(["id", "name", "dept"])
for i in range(60):
unique.append([i, f"user_{i}", ["eng", "sales", "ops"][i % 3]])
redundant = wb.create_sheet("Redundant")
redundant.append(["region", "product", "status"])
for _ in range(120):
redundant.append(["EMEA", "widget-A", "shipped"])
wb.save(path)
# ─── Main ───────────────────────────────────────────────────────────────────
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--write",
metavar="DIR",
help="Also write the generated sample files (.csv/.md/.xlsx) to DIR",
)
args = parser.parse_args()
samples = {
"compact_unique.csv": compact_unique_csv(),
"redundant.csv": redundant_csv(),
"verbose_table.md": verbose_markdown(),
}
print("=== Raw tabular text (ContentRouter, char-level) ===")
_run_router("compact unique CSV", samples["compact_unique.csv"])
_run_router("redundant CSV", samples["redundant.csv"])
_run_router("verbose markdown", samples["verbose_table.md"])
print("\n=== Full pipeline (real tokenizer) ===")
_run_messages("redundant CSV", samples["redundant.csv"])
print("\n=== Binary spreadsheet (.xlsx) ===")
if not _HAS_OPENPYXL:
print(" skipped — install: pip install headroom-ai[spreadsheet]")
else:
out_dir = Path(args.write) if args.write else Path("/tmp")
out_dir.mkdir(parents=True, exist_ok=True)
xlsx_path = out_dir / "demo.xlsx"
_build_xlsx(xlsx_path)
_run_xlsx("2-sheet workbook", xlsx_path)
if args.write:
out = Path(args.write)
out.mkdir(parents=True, exist_ok=True)
for name, content in samples.items():
(out / name).write_text(content)
print(f"\nSample files written to {out.resolve()}")
print(
"\nTakeaway: redundant/verbose tables compress; compact all-unique data "
"correctly passes through (lossless-only — nothing safely removable)."
)
if __name__ == "__main__":
main()