-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify_external_report.py
More file actions
241 lines (211 loc) · 8.33 KB
/
Copy pathclassify_external_report.py
File metadata and controls
241 lines (211 loc) · 8.33 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python3
"""
Fetch a report URL (HTML or PDF) or read plain text, chunk it, run
PeriodicTableClassifier on each chunk, and write JSON + Markdown summaries.
Meta Transparency often serves **PDF** for semiannual reports; `curl` + `pdftotext`
is used when the response is PDF (avoids macOS Python SSL issues with urllib).
Usage:
curl -sL -o report.pdf URL && pdftotext -layout report.txt report.txt
python scripts/classify_external_report.py --input report.txt --out-prefix out/base --no-write-source
python scripts/classify_external_report.py \\
--url https://transparency.meta.com/sr/first-half-2026-Adversarial-threat-report/ \\
--out-prefix reports/meta-integrity-h1-2026/adversarial-h1-2026-live
"""
from __future__ import annotations
import argparse
import json
import re
import subprocess
import sys
from collections import Counter
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
from src.classifier import PeriodicTableClassifier # noqa: E402
def _display_source_path(p: Path) -> str:
try:
return str(p.resolve().relative_to(ROOT))
except ValueError:
return str(p)
def strip_html_to_text(html: str) -> str:
html = re.sub(r"(?is)<script[^>]*>.*?</script>", "", html)
html = re.sub(r"(?is)<style[^>]*>.*?</style>", "", html)
html = re.sub(r"(?is)<noscript[^>]*>.*?</noscript>", "", html)
text = re.sub(r"<br\s*/?>", "\n", html, flags=re.I)
text = re.sub(r"</p\s*>", "\n\n", text, flags=re.I)
text = re.sub(r"</div\s*>", "\n", text, flags=re.I)
text = re.sub(r"<[^>]+>", " ", text)
for a, b in (
(" ", " "),
("&", "&"),
("<", "<"),
(">", ">"),
(""", '"'),
):
text = text.replace(a, b)
text = re.sub(r"[ \t]+", " ", text)
text = re.sub(r"\n[ \t]+", "\n", text)
text = re.sub(r"\n{3,}", "\n\n", text)
return text.strip()
def fetch_url_bytes(url: str, timeout: int = 60) -> bytes:
ua = (
"AIFailurePeriodicTable-ReportClassifier/1.0 "
"(+https://github.com/lml-layer-system/ai-failure-periodic-table)"
)
r = subprocess.run(
["curl", "-sL", "--max-time", str(timeout), "-A", ua, url],
capture_output=True,
check=False,
)
if r.returncode != 0:
raise RuntimeError(f"curl failed (exit {r.returncode}): {r.stderr.decode(errors='replace')[:500]}")
return r.stdout
def pdf_to_text(pdf_path: Path, pdftotext_bin: str, out_txt: Path) -> None:
subprocess.run(
[pdftotext_bin, "-layout", str(pdf_path), str(out_txt)],
check=True,
capture_output=True,
)
def paragraph_chunks(text: str, max_chars: int = 1400) -> list[str]:
text = text.replace("\f", "\n")
paras = [p.strip() for p in re.split(r"\n\s*\n+", text) if p.strip()]
chunks: list[str] = []
buf: list[str] = []
n = 0
for p in paras:
if n + len(p) + 2 > max_chars and buf:
chunks.append("\n\n".join(buf))
buf = [p]
n = len(p)
else:
buf.append(p)
n += len(p) + 2
if buf:
chunks.append("\n\n".join(buf))
return chunks
def main() -> None:
ap = argparse.ArgumentParser(description="Classify external HTML/PDF/text report into periodic table")
g = ap.add_mutually_exclusive_group(required=True)
g.add_argument("--url", help="Fetch URL via curl (HTML or PDF)")
g.add_argument("--input", type=Path, help="Existing plain-text file")
ap.add_argument(
"--out-prefix",
type=Path,
required=True,
help="Write {out-prefix}-chunks.json, -summary.md; optional source/pdf",
)
ap.add_argument("--max-chars", type=int, default=1400)
ap.add_argument("--timeout", type=int, default=60)
ap.add_argument(
"--no-write-source",
action="store_true",
help="With --input: do not copy to {prefix}-source.txt; JSON references --input path",
)
ap.add_argument(
"--pdftotext",
default="pdftotext",
help="Path to pdftotext (poppler) when URL returns PDF",
)
args = ap.parse_args()
prefix = args.out_prefix
prefix.parent.mkdir(parents=True, exist_ok=True)
source_path: Path
if args.url:
raw = fetch_url_bytes(args.url, timeout=args.timeout)
if raw[:4] == b"%PDF":
pdf_path = Path(str(prefix) + "-official.pdf")
pdf_path.write_bytes(raw)
source_path = Path(str(prefix) + "-source.txt")
pdf_to_text(pdf_path, args.pdftotext, source_path)
header = (
f"PDF URL: {args.url}\n"
f"PDF saved: {pdf_path.name}\n"
f"Extracted with: {args.pdftotext} -layout\n\n"
)
source_path.write_text(header + source_path.read_text(encoding="utf-8", errors="replace"))
else:
text = strip_html_to_text(raw.decode("utf-8", errors="replace"))
source_path = Path(str(prefix) + "-source.txt")
source_path.write_text(f"Fetched from: {args.url}\n\n{text}", encoding="utf-8")
text = source_path.read_text(encoding="utf-8", errors="replace")
if text.startswith("PDF URL:"):
text = text.split("\n\n", 2)[-1] # strip header for chunking
elif text.startswith("Fetched from:"):
text = text.split("\n\n", 1)[-1]
else:
text = args.input.read_text(encoding="utf-8", errors="replace")
if args.no_write_source:
source_path = args.input.resolve()
else:
source_path = Path(str(prefix) + "-source.txt")
note = f"Loaded from file: {args.input}\n\n"
source_path.write_text(note + text, encoding="utf-8")
chunks = paragraph_chunks(text, max_chars=args.max_chars)
clf = PeriodicTableClassifier()
per_chunk: list[dict] = []
id_counts: Counter[str] = Counter()
name_for_id: dict[str, str] = {}
for i, chunk in enumerate(chunks):
r = clf.classify(chunk)
row: dict = {
"chunk_index": i,
"char_len": len(chunk),
"preview": chunk[:220].replace("\n", " ") + ("…" if len(chunk) > 220 else ""),
"in_table": r.in_table,
"dimensions_activated": list(r.dimensions_activated),
}
if r.matches:
row["top_matches"] = [m.as_dict() for m in r.matches[:5]]
top = r.matches[0]
id_counts[top.failure_id] += 1
name_for_id[top.failure_id] = top.name
else:
row["top_matches"] = []
row["closest"] = [m.as_dict() for m in r.closest[:3]]
per_chunk.append(row)
src_disp = _display_source_path(source_path)
summary_lines = [
"# Classifier pass: external report (live PDF/text)",
"",
f"**Source file:** `{src_disp}`",
f"**Chunks:** {len(chunks)} at ~{args.max_chars} chars (paragraph-bounded).",
"**Tool:** `PeriodicTableClassifier` (keyword) in this repo.",
"",
"## Top-1 class histogram",
"",
"| Hits | ID | Name |",
"|------|-----|------|",
]
for fid, c in id_counts.most_common(40):
summary_lines.append(f"| {c} | `{fid}` | {name_for_id.get(fid, '')} |")
if not id_counts:
summary_lines.append("| — | — | no chunk reached match threshold |")
summary_lines.extend(["", "## Chunk → top match", ""])
for row in per_chunk:
if row.get("top_matches"):
tid = row["top_matches"][0]["id"]
tname = row["top_matches"][0]["name"]
summary_lines.append(
f"- **{row['chunk_index']}** → `{tid}` — {tname} — _{row['preview'][:120]}…_"
)
summary_path = Path(str(prefix) + "-summary.md")
summary_path.write_text("\n".join(summary_lines), encoding="utf-8")
json_path = Path(str(prefix) + "-chunks.json")
json_path.write_text(
json.dumps(
{
"source_file": src_disp,
"chunk_count": len(chunks),
"max_chars": args.max_chars,
"top1_histogram": dict(id_counts.most_common(50)),
"chunks": per_chunk,
},
indent=2,
),
encoding="utf-8",
)
print(f"Wrote:\n {json_path}\n {summary_path}")
if args.url or not args.no_write_source:
print(f" {source_path}")
if __name__ == "__main__":
main()