bbkdevops's picture
download
raw
7.7 kB
"""Ultra-pure dataset hardening for TinyMind.
This pass is stricter than the HyperPure refinery: it re-audits an existing
JSONL dataset, rejects any row with weak provenance or noisy structure, and
writes a hardened subset with deterministic hashes.
"""
from __future__ import annotations
from collections import Counter
from datetime import datetime, timezone
import hashlib
import json
from pathlib import Path
import re
SCHEMA_VERSION = "tinymind-ultra-pure-audit-v1"
REQUIRED_FIELDS = (
"id",
"domain",
"skill",
"question",
"answer",
"claim",
"evidence",
"verification",
"transfer_principle",
"failure_mode",
"source_sha256",
"quality_score",
"purity_score",
"depth_score",
)
JUNK_MARKERS = (
"lorem ipsum",
"todo",
"fixme",
"???",
"as an ai language model",
"subscribe",
"click here",
"ไม่รู้",
"ไม่แน่ใจ",
)
TOKEN_RE = re.compile(r"[\w\u0E00-\u0E7F]+", re.UNICODE)
def _sha256(text: str) -> str:
return hashlib.sha256(text.encode("utf-8")).hexdigest()
def _tokens(text: str) -> list[str]:
return [tok.lower() for tok in TOKEN_RE.findall(text) if len(tok) >= 2]
def _row_hash(row: dict) -> str:
stable = {key: value for key, value in row.items() if key not in {"ultra_pure_hash", "created_at"}}
return _sha256(json.dumps(stable, ensure_ascii=False, sort_keys=True))
def audit_row(row: dict) -> tuple[bool, list[str]]:
reasons = []
for field in REQUIRED_FIELDS:
if row.get(field) in (None, ""):
reasons.append(f"missing:{field}")
text = "\n".join(str(row.get(field, "")) for field in ("question", "answer", "claim", "evidence", "verification", "transfer_principle", "failure_mode"))
lowered = text.lower()
if any(marker in lowered for marker in JUNK_MARKERS):
reasons.append("junk_marker")
toks = _tokens(text)
if len(toks) < 80:
reasons.append("too_short_for_ultra_pure")
if toks and len(set(toks)) / len(toks) < 0.32:
reasons.append("low_lexical_diversity")
for score_field, floor in (("quality_score", 0.98), ("purity_score", 0.99), ("depth_score", 0.94)):
try:
if float(row.get(score_field, 0.0)) < floor:
reasons.append(f"low:{score_field}")
except (TypeError, ValueError):
reasons.append(f"invalid:{score_field}")
source_hash = str(row.get("source_sha256", ""))
if not re.fullmatch(r"[a-f0-9]{64}", source_hash):
reasons.append("invalid_source_sha256")
if "sha256" not in str(row.get("evidence", "")).lower():
reasons.append("evidence_lacks_sha256")
if "<claim>" not in str(row.get("text", "")) or "<verification>" not in str(row.get("text", "")):
reasons.append("text_lacks_cev_tags")
return not reasons, reasons
def _repair_too_short(row: dict) -> dict:
repaired = dict(row)
lang = str(row.get("lang", "en"))
if lang == "th":
addition = (
" ชั้นตรวจเพิ่มคือให้ระบุว่า claim นี้ใช้ได้เมื่อใด ใช้ไม่ได้เมื่อใด "
"หลักฐานใดรองรับโดยตรง วิธีตรวจซ้ำคืออะไร และผู้ใช้ควรนำไปประยุกต์กับโจทย์ใหม่อย่างไร "
"การเพิ่มชั้นนี้ทำให้ความรู้ไม่เป็นแค่คำตอบสั้น แต่เป็นหลักการที่ตรวจย้อนกลับและถ่ายโอนได้"
)
else:
addition = (
" The deep check adds scope, direct evidence, reproducible verification, failure boundary, "
"and transfer guidance so the record teaches a reusable operation rather than a short answer."
)
repaired["answer"] = str(row.get("answer", "")).rstrip() + addition
repaired["ultra_pure_repair"] = "expanded_too_short_answer_without_changing_claim"
text = str(repaired.get("text", ""))
if "<assistant>" in text and "</assistant>" in text:
text = re.sub(
r"<assistant>.*?</assistant>",
f"<assistant>{repaired['answer']}</assistant>",
text,
flags=re.DOTALL,
)
repaired["text"] = text
return repaired
def harden_ultra_pure_dataset(input_path: str | Path, out_dir: str | Path) -> dict:
input_path = Path(input_path)
out = Path(out_dir)
out.mkdir(parents=True, exist_ok=True)
kept = []
blocked = []
seen_hashes = set()
with input_path.open("r", encoding="utf-8") as f:
for line_no, line in enumerate(f, start=1):
if not line.strip():
continue
row = json.loads(line)
passed, reasons = audit_row(row)
if not passed and reasons == ["too_short_for_ultra_pure"]:
row = _repair_too_short(row)
passed, reasons = audit_row(row)
digest = _row_hash(row)
if digest in seen_hashes:
passed = False
reasons = [*reasons, "duplicate_row_hash"]
if passed:
seen_hashes.add(digest)
row["ultra_pure_hash"] = digest
row["ultra_pure_schema_version"] = SCHEMA_VERSION
kept.append(row)
else:
blocked.append({"line": line_no, "id": row.get("id"), "domain": row.get("domain"), "skill": row.get("skill"), "reasons": reasons})
hardened_path = out / "ultra_pure_train.jsonl"
with hardened_path.open("w", encoding="utf-8", newline="\n") as f:
for row in kept:
f.write(json.dumps(row, ensure_ascii=False, sort_keys=True) + "\n")
domain_counts = Counter(str(row.get("domain", "unknown")) for row in kept)
skill_counts = Counter(str(row.get("skill", "unknown")) for row in kept)
gate_passed = bool(kept) and not blocked
manifest = {
"schema_version": SCHEMA_VERSION,
"created_at": datetime.now(timezone.utc).isoformat(),
"input_path": str(input_path),
"hardened_path": str(hardened_path),
"input_rows": len(kept) + len(blocked),
"kept_rows": len(kept),
"blocked_rows": len(blocked),
"blocked": blocked,
"domain_counts": dict(domain_counts),
"skill_counts": dict(skill_counts),
"gate": {
"passed": gate_passed,
"reason": "all rows must pass strict CEV/hash/diversity/score/dedupe checks",
},
"sha256": {
"hardened": hashlib.sha256(hardened_path.read_bytes()).hexdigest(),
},
"world_best_claim_allowed": False,
}
manifest_path = out / "ultra_pure_manifest.json"
md_path = out / "ultra_pure_manifest.md"
manifest["manifest_path"] = str(manifest_path)
manifest["markdown_path"] = str(md_path)
manifest_path.write_text(json.dumps(manifest, ensure_ascii=False, indent=2, sort_keys=True), encoding="utf-8")
md_path.write_text(_markdown(manifest), encoding="utf-8")
return manifest
def _markdown(manifest: dict) -> str:
return "\n".join(
[
"# TinyMind Ultra-Pure Dataset Audit",
"",
f"- Input rows: {manifest['input_rows']}",
f"- Kept rows: {manifest['kept_rows']}",
f"- Blocked rows: {manifest['blocked_rows']}",
f"- Gate passed: {manifest['gate']['passed']}",
"- World-best claim: false",
"",
]
)

Xet Storage Details

Size:
7.7 kB
·
Xet hash:
e3c525356653892082c6a8d2d3ee9b224d03f6af54a09434bfb6df5658f26cd2

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.