mmiakashs commited on
Commit
514b890
·
verified ·
1 Parent(s): f318801

Rewrite loader to read from local HF checkout instead of S3

Browse files
Files changed (1) hide show
  1. load_dataset.py +135 -0
load_dataset.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ FCC Invoices Verified Augmented - Dataset Loader
3
+
4
+ Usage:
5
+ from huggingface_hub import snapshot_download
6
+ from load_dataset import FCCInvoicesDataset
7
+
8
+ local_dir = snapshot_download(repo_id="amazon/ConfBench", repo_type="dataset")
9
+ ds = FCCInvoicesDataset(local_dir=local_dir)
10
+
11
+ # Iterate all (document, pipeline) pairs
12
+ for sample in ds:
13
+ print(sample["doc_id"], sample["pipeline_name"])
14
+ gt = sample["ground_truth"]["inference_result"]
15
+ print(gt["Agency"], gt["GrossTotal"])
16
+
17
+ # Get the local path to a single noisy PDF
18
+ path = ds.pdf_path(doc_id="033f718b16cb597c065930410752c294", pipeline_name="custom13")
19
+
20
+ Requirements:
21
+ pip install huggingface_hub
22
+ """
23
+
24
+ import json
25
+ from pathlib import Path
26
+ from typing import Iterator
27
+
28
+
29
+ PIPELINES = [
30
+ "default", "archetype3", "archetype4", "archetype7", "archetype9",
31
+ "archetype10", "archetype11", "custom12", "custom13", "custom14",
32
+ "custom15", "custom16", "custom17", "custom18", "custom19",
33
+ "custom20", "custom21", "custom22",
34
+ ]
35
+
36
+
37
+ class FCCInvoicesDataset:
38
+ """Lazy iterator over all (document, pipeline) pairs in a local ConfBench checkout."""
39
+
40
+ def __init__(self, local_dir: str):
41
+ self.local_dir = Path(local_dir)
42
+ self._doc_ids: list[str] | None = None
43
+
44
+ # ------------------------------------------------------------------
45
+ # Public API
46
+ # ------------------------------------------------------------------
47
+
48
+ def doc_ids(self) -> list[str]:
49
+ """Return all document IDs (md5 hashes)."""
50
+ if self._doc_ids is None:
51
+ self._doc_ids = sorted(
52
+ p.name for p in self.local_dir.iterdir()
53
+ if p.is_dir() and (p / "metadata.json").exists()
54
+ )
55
+ return self._doc_ids
56
+
57
+ def load_ground_truth(self, doc_id: str) -> dict:
58
+ """Read and parse gt.json for a document."""
59
+ return json.loads((self.local_dir / doc_id / "gt.json").read_text())
60
+
61
+ def load_metadata(self, doc_id: str) -> dict:
62
+ """Read and parse metadata.json (pipeline manifest) for a document."""
63
+ return json.loads((self.local_dir / doc_id / "metadata.json").read_text())
64
+
65
+ def pdf_path(self, doc_id: str, pipeline_name: str) -> str:
66
+ """Return the local path to a noisy PDF."""
67
+ return str(self.local_dir / doc_id / pipeline_name / f"{pipeline_name}_noisy.pdf")
68
+
69
+ def original_pdf_path(self, doc_id: str) -> str:
70
+ """Return the local path to the original clean PDF."""
71
+ return str(self.local_dir / doc_id / "original.pdf")
72
+
73
+ def get_sample(self, doc_id: str, pipeline_name: str) -> dict:
74
+ """Return a single sample dict."""
75
+ gt = self.load_ground_truth(doc_id)
76
+ return {
77
+ "doc_id": doc_id,
78
+ "pipeline_name": pipeline_name,
79
+ "pipeline_type": "archetype" if not pipeline_name.startswith("custom") else "custom",
80
+ "original_pdf": self.original_pdf_path(doc_id),
81
+ "noisy_pdf": self.pdf_path(doc_id, pipeline_name),
82
+ "ground_truth": gt,
83
+ }
84
+
85
+ def __iter__(self) -> Iterator[dict]:
86
+ """Yield one dict per (document, pipeline) pair."""
87
+ for doc_id in self.doc_ids():
88
+ meta = self.load_metadata(doc_id)
89
+ gt = self.load_ground_truth(doc_id)
90
+ for pipeline in meta.get("pipelines", []):
91
+ name = pipeline["pipeline_name"]
92
+ yield {
93
+ "doc_id": doc_id,
94
+ "pipeline_name": name,
95
+ "pipeline_type": "archetype" if not name.startswith("custom") else "custom",
96
+ "original_pdf": self.original_pdf_path(doc_id),
97
+ "noisy_pdf": self.pdf_path(doc_id, name),
98
+ "ground_truth": gt,
99
+ }
100
+
101
+ def __len__(self) -> int:
102
+ return len(self.doc_ids()) * len(PIPELINES)
103
+
104
+
105
+ # ------------------------------------------------------------------
106
+ # CLI convenience
107
+ # ------------------------------------------------------------------
108
+ if __name__ == "__main__":
109
+ import argparse
110
+
111
+ parser = argparse.ArgumentParser(description="FCC Invoices dataset helper")
112
+ parser.add_argument("--local-dir", default=".", help="Path to local ConfBench checkout")
113
+ sub = parser.add_subparsers(dest="cmd")
114
+
115
+ p_list = sub.add_parser("list", help="List all document IDs")
116
+
117
+ p_gt = sub.add_parser("gt", help="Print ground truth for a document")
118
+ p_gt.add_argument("doc_id")
119
+
120
+ p_path = sub.add_parser("path", help="Print the local path to a noisy PDF")
121
+ p_path.add_argument("doc_id")
122
+ p_path.add_argument("pipeline_name")
123
+
124
+ args = parser.parse_args()
125
+ ds = FCCInvoicesDataset(local_dir=args.local_dir)
126
+
127
+ if args.cmd == "list":
128
+ for d in ds.doc_ids():
129
+ print(d)
130
+ elif args.cmd == "gt":
131
+ print(json.dumps(ds.load_ground_truth(args.doc_id), indent=2))
132
+ elif args.cmd == "path":
133
+ print(ds.pdf_path(args.doc_id, args.pipeline_name))
134
+ else:
135
+ parser.print_help()