sujimart commited on
Commit
405798f
·
verified ·
1 Parent(s): 2735c00

Remove old file: load_dataset.py

Browse files
Files changed (1) hide show
  1. load_dataset.py +0 -140
load_dataset.py DELETED
@@ -1,140 +0,0 @@
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
- # Documents live under assets/; accept a checkout root or the assets dir itself.
43
- if (self.local_dir / "assets").is_dir():
44
- self.assets_dir = self.local_dir / "assets"
45
- else:
46
- self.assets_dir = self.local_dir
47
- self._doc_ids: list[str] | None = None
48
-
49
- # ------------------------------------------------------------------
50
- # Public API
51
- # ------------------------------------------------------------------
52
-
53
- def doc_ids(self) -> list[str]:
54
- """Return all document IDs (md5 hashes)."""
55
- if self._doc_ids is None:
56
- self._doc_ids = sorted(
57
- p.name for p in self.assets_dir.iterdir()
58
- if p.is_dir() and (p / "metadata.json").exists()
59
- )
60
- return self._doc_ids
61
-
62
- def load_ground_truth(self, doc_id: str) -> dict:
63
- """Read and parse gt.json for a document."""
64
- return json.loads((self.assets_dir / doc_id / "gt.json").read_text())
65
-
66
- def load_metadata(self, doc_id: str) -> dict:
67
- """Read and parse metadata.json (pipeline manifest) for a document."""
68
- return json.loads((self.assets_dir / doc_id / "metadata.json").read_text())
69
-
70
- def pdf_path(self, doc_id: str, pipeline_name: str) -> str:
71
- """Return the local path to a noisy PDF."""
72
- return str(self.assets_dir / doc_id / pipeline_name / f"{pipeline_name}_noisy.pdf")
73
-
74
- def original_pdf_path(self, doc_id: str) -> str:
75
- """Return the local path to the original clean PDF."""
76
- return str(self.assets_dir / doc_id / "original.pdf")
77
-
78
- def get_sample(self, doc_id: str, pipeline_name: str) -> dict:
79
- """Return a single sample dict."""
80
- gt = self.load_ground_truth(doc_id)
81
- return {
82
- "doc_id": doc_id,
83
- "pipeline_name": pipeline_name,
84
- "pipeline_type": "archetype" if not pipeline_name.startswith("custom") else "custom",
85
- "original_pdf": self.original_pdf_path(doc_id),
86
- "noisy_pdf": self.pdf_path(doc_id, pipeline_name),
87
- "ground_truth": gt,
88
- }
89
-
90
- def __iter__(self) -> Iterator[dict]:
91
- """Yield one dict per (document, pipeline) pair."""
92
- for doc_id in self.doc_ids():
93
- meta = self.load_metadata(doc_id)
94
- gt = self.load_ground_truth(doc_id)
95
- for pipeline in meta.get("pipelines", []):
96
- name = pipeline["pipeline_name"]
97
- yield {
98
- "doc_id": doc_id,
99
- "pipeline_name": name,
100
- "pipeline_type": "archetype" if not name.startswith("custom") else "custom",
101
- "original_pdf": self.original_pdf_path(doc_id),
102
- "noisy_pdf": self.pdf_path(doc_id, name),
103
- "ground_truth": gt,
104
- }
105
-
106
- def __len__(self) -> int:
107
- return len(self.doc_ids()) * len(PIPELINES)
108
-
109
-
110
- # ------------------------------------------------------------------
111
- # CLI convenience
112
- # ------------------------------------------------------------------
113
- if __name__ == "__main__":
114
- import argparse
115
-
116
- parser = argparse.ArgumentParser(description="FCC Invoices dataset helper")
117
- parser.add_argument("--local-dir", default=".", help="Path to local ConfBench checkout")
118
- sub = parser.add_subparsers(dest="cmd")
119
-
120
- p_list = sub.add_parser("list", help="List all document IDs")
121
-
122
- p_gt = sub.add_parser("gt", help="Print ground truth for a document")
123
- p_gt.add_argument("doc_id")
124
-
125
- p_path = sub.add_parser("path", help="Print the local path to a noisy PDF")
126
- p_path.add_argument("doc_id")
127
- p_path.add_argument("pipeline_name")
128
-
129
- args = parser.parse_args()
130
- ds = FCCInvoicesDataset(local_dir=args.local_dir)
131
-
132
- if args.cmd == "list":
133
- for d in ds.doc_ids():
134
- print(d)
135
- elif args.cmd == "gt":
136
- print(json.dumps(ds.load_ground_truth(args.doc_id), indent=2))
137
- elif args.cmd == "path":
138
- print(ds.pdf_path(args.doc_id, args.pipeline_name))
139
- else:
140
- parser.print_help()