aitrainingdatabroker commited on
Commit
d8f22b7
·
verified ·
1 Parent(s): b19ec8c

Add source/gen_derived.py

Browse files
Files changed (1) hide show
  1. source/gen_derived.py +114 -0
source/gen_derived.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Datasets 1 to 4 and the training files of models 1 to 3, built from the text of our own published pages (2026-10-30).
2
+
3
+ python hf-work/gen_derived.py
4
+ Reads hf-work/site_content.json (extracted from aitrainingdatabroker.com's page sources by extract_src.ts) and writes
5
+ datasets/<name>/data/train.jsonl and models/<name>/data/{train,test}.jsonl. Every row is our own text; nothing is from a customer.
6
+ """
7
+
8
+ import json
9
+ import random
10
+ from pathlib import Path
11
+
12
+ ROOT = Path(__file__).resolve().parent.parent
13
+ SITE = "https://aitrainingdatabroker.com"
14
+ src = json.load(open(Path(__file__).parent / "site_content.json", encoding="utf-8"))
15
+ pages = src["pages"]
16
+ industries = [p for p in pages if p["kind"] == "industry"]
17
+ records_pages = [p for p in pages if p["kind"] == "data"]
18
+ rng = random.Random(20261030)
19
+
20
+
21
+ def write(kind, name, split, rows):
22
+ out = ROOT / kind / name / "data"
23
+ out.mkdir(parents=True, exist_ok=True)
24
+ with open(out / f"{split}.jsonl", "w", encoding="utf-8", newline="\n") as f:
25
+ for r in rows:
26
+ f.write(json.dumps(r, ensure_ascii=False) + "\n")
27
+ print(f"{kind}/{name}/{split}: {len(rows)} rows")
28
+
29
+
30
+ def url(p):
31
+ return SITE + p["path"]
32
+
33
+
34
+ # ---- dataset 1: what a company in each industry can consider licensing and what stays out ----
35
+ rows = []
36
+ for p in industries:
37
+ for kind, items in (("can_be_considered", p["records"]), ("stays_out", p["keepsOut"])):
38
+ for it in items:
39
+ rows.append({"industry": p["name"], "industry_slug": p["slug"], "kind": kind, "label": it["label"],
40
+ "text": it["text"], "source_url": url(p)})
41
+ for i, r in enumerate(rows, 1):
42
+ r["id"] = f"ind-{i:03d}"
43
+ write("datasets", "company-data-licensability-by-industry", "train", [{"id": r.pop("id"), **r} for r in rows])
44
+ ind_rows = rows
45
+
46
+ # ---- dataset 2: the same for 33 record types ----
47
+ rows = []
48
+ for p in records_pages:
49
+ for kind, items in (("what_it_holds", p["records"]), ("limits", p["keepsOut"])):
50
+ for it in items:
51
+ rows.append({"record_type": p["name"], "record_type_slug": p["slug"], "kind": kind, "label": it["label"],
52
+ "text": it["text"], "source_url": url(p)})
53
+ write("datasets", "company-record-types-and-limits", "train", [{"id": f"rec-{i:03d}", **r} for i, r in enumerate(rows, 1)])
54
+ rec_rows = rows
55
+
56
+ # ---- dataset 3: questions and answers, by industry, record type and guide ----
57
+ rows = []
58
+ for p in pages:
59
+ for q in p["faq"]:
60
+ rows.append({"topic_kind": "industry" if p["kind"] == "industry" else "record_type", "topic": p["name"],
61
+ "question": q["q"], "answer": q["a"], "source_url": url(p)})
62
+ for g in src["guides"]:
63
+ for q in g["faq"]:
64
+ rows.append({"topic_kind": f"{g['area']}_guide", "topic": g["h1"], "question": q["q"], "answer": q["a"],
65
+ "source_url": SITE + g["url"]})
66
+ write("datasets", "company-data-licensing-faq-by-topic", "train", [{"id": f"faq-{i:03d}", **r} for i, r in enumerate(rows, 1)])
67
+ faq_rows = rows
68
+
69
+ # ---- dataset 4: glossary ----
70
+ rows = []
71
+ for t in src["glossary"]:
72
+ more = t.get("more") or []
73
+ rows.append({"term": t["term"], "definition": t["def"], "see_also": more[0] if len(more) > 0 else "",
74
+ "see_also_url": SITE + more[1] if len(more) > 1 else ""})
75
+ write("datasets", "data-licensing-glossary", "train", [{"id": f"gls-{i:03d}", **r} for i, r in enumerate(rows, 1)])
76
+
77
+ # ---- model 1: which industry page does a passage belong to (train on records, ledes and sections; test on its FAQ) ----
78
+ train, test = [], []
79
+ for p in industries:
80
+ for it in p["records"] + p["keepsOut"]:
81
+ train.append({"text": f"{it['label']}. {it['text']}", "label": p["slug"]})
82
+ train.append({"text": p["lede"], "label": p["slug"]})
83
+ for s in p["sections"]:
84
+ for para in s["paragraphs"]:
85
+ train.append({"text": para, "label": p["slug"]})
86
+ for q in p["faq"]:
87
+ test.append({"text": f"{q['q']} {q['a']}", "label": p["slug"]})
88
+ rng.shuffle(train)
89
+ write("models", "industry-page-classifier", "train", train)
90
+ write("models", "industry-page-classifier", "test", test)
91
+
92
+ # ---- model 2: can be considered, or stays out (train on industry pages, test on record-type pages: a different subject) ----
93
+ train = [{"text": f"{it['label']}. {it['text']}", "label": "can_be_considered" if k == "r" else "stays_out"}
94
+ for p in industries for k, items in (("r", p["records"]), ("o", p["keepsOut"])) for it in items]
95
+ test = [{"text": f"{it['label']}. {it['text']}", "label": "can_be_considered" if k == "r" else "stays_out"}
96
+ for p in records_pages for k, items in (("r", p["records"]), ("o", p["keepsOut"])) for it in items]
97
+ rng.shuffle(train)
98
+ write("models", "licensable-or-stays-out-classifier", "train", train)
99
+ write("models", "licensable-or-stays-out-classifier", "test", test)
100
+
101
+ # ---- model 3: which record-type page does a passage belong to (train on records, ledes, sections; test on FAQ) ----
102
+ train, test = [], []
103
+ for p in records_pages:
104
+ for it in p["records"] + p["keepsOut"]:
105
+ train.append({"text": f"{it['label']}. {it['text']}", "label": p["slug"]})
106
+ train.append({"text": p["lede"], "label": p["slug"]})
107
+ for s in p["sections"]:
108
+ for para in s["paragraphs"]:
109
+ train.append({"text": para, "label": p["slug"]})
110
+ for q in p["faq"]:
111
+ test.append({"text": f"{q['q']} {q['a']}", "label": p["slug"]})
112
+ rng.shuffle(train)
113
+ write("models", "record-type-page-classifier", "train", train)
114
+ write("models", "record-type-page-classifier", "test", test)