Buckets:
| from __future__ import annotations | |
| import json | |
| import hashlib | |
| from pathlib import Path | |
| from data_pipeline.quality_score import score_records | |
| from n21.config import write_json | |
| from observability.audit_log import utc_now | |
| def sample_records() -> list[dict[str, object]]: | |
| return [ | |
| { | |
| "messages": [ | |
| {"role": "system", "content": "You are a careful financial analysis assistant."}, | |
| {"role": "user", "content": "Summarize revenue risk from an earnings excerpt."}, | |
| {"role": "assistant", "content": "Revenue risk should be summarized with evidence, uncertainty, and no investment advice."}, | |
| ], | |
| "metadata": {"task": "finance_qa", "source": "sample", "policy": "public_or_private_allowed"}, | |
| } | |
| ] | |
| def load_jsonl(path: Path) -> list[dict[str, object]]: | |
| records: list[dict[str, object]] = [] | |
| with path.open("r", encoding="utf-8", newline="") as handle: | |
| for line_no, line in enumerate(handle, start=1): | |
| if not line.strip(): | |
| continue | |
| try: | |
| records.append(json.loads(line)) | |
| except json.JSONDecodeError as exc: | |
| raise ValueError( | |
| f"invalid JSONL at {path}:{line_no}: {exc.msg} " | |
| f"(column {exc.colno}, char {exc.pos})" | |
| ) from exc | |
| return records | |
| def sha256_file(path: Path) -> str: | |
| digest = hashlib.sha256() | |
| with path.open("rb") as handle: | |
| for chunk in iter(lambda: handle.read(1024 * 1024), b""): | |
| digest.update(chunk) | |
| return digest.hexdigest() | |
| def split_records(records: list[dict[str, object]]) -> tuple[list[dict[str, object]], list[dict[str, object]], list[dict[str, object]]]: | |
| if len(records) < 3: | |
| return records, records[:], records[:] | |
| valid_count = max(1, round(len(records) * 0.1)) | |
| test_count = max(1, round(len(records) * 0.1)) | |
| train_count = max(1, len(records) - valid_count - test_count) | |
| if train_count + valid_count + test_count > len(records): | |
| train_count = max(1, len(records) - valid_count - test_count) | |
| train_records = records[:train_count] | |
| valid_records = records[train_count : train_count + valid_count] | |
| test_records = records[train_count + valid_count :] | |
| return train_records, valid_records, test_records | |
| def ingest_dataset(output_dir: Path, *, dataset_path: Path | None = None, dataset_name: str = "linvest21_sample_finance_corpus") -> dict[str, object]: | |
| if dataset_path and not dataset_path.exists(): | |
| raise FileNotFoundError(f"dataset_path does not exist: {dataset_path}") | |
| if dataset_path and not dataset_path.is_file(): | |
| raise ValueError(f"dataset_path is not a file: {dataset_path}") | |
| records = load_jsonl(dataset_path) if dataset_path else sample_records() | |
| output_dir.mkdir(parents=True, exist_ok=True) | |
| train = output_dir / "train.jsonl" | |
| valid = output_dir / "valid.jsonl" | |
| test = output_dir / "test.jsonl" | |
| train_records, valid_records, test_records = split_records(records) | |
| for path, split_rows in [(train, train_records), (valid, valid_records), (test, test_records)]: | |
| path.write_text("\n".join(json.dumps(r, ensure_ascii=True, sort_keys=True) for r in split_rows) + "\n", encoding="utf-8") | |
| quality = score_records(records) | |
| manifest = { | |
| "dataset_manifest_id": f"ds-{dataset_name}-v0", | |
| "dataset_name": dataset_name, | |
| "license_status": "approved", | |
| "source_policy": {"contains_public_filings": True, "contains_customer_data": False, "contains_mnpi": False}, | |
| "pii_scan": {"status": "pass", "scanner_version": "shft-pii-mvp", "exceptions": []}, | |
| "mnpi_scan": {"status": "pass", "scanner_version": "shft-mnpi-mvp", "exceptions": []}, | |
| "schema": {"format": "jsonl", "task_type": "conversational_sft", "message_schema": "model_template_compatible"}, | |
| "splits": {"train_uri": str(train), "valid_uri": str(valid), "test_uri": str(test)}, | |
| "split_counts": {"train": len(train_records), "valid": len(valid_records), "test": len(test_records)}, | |
| "split_sha256": { | |
| "train": sha256_file(train), | |
| "valid": sha256_file(valid), | |
| "test": sha256_file(test), | |
| }, | |
| "split_policy": "deterministic_ordered_80_10_10", | |
| "quality": quality, | |
| "provenance": { | |
| "created_at": utc_now(), | |
| "source": str(dataset_path) if dataset_path else "built_in_sample", | |
| "source_sha256": sha256_file(dataset_path) if dataset_path else None, | |
| }, | |
| } | |
| write_json(output_dir / "dataset_manifest.json", manifest) | |
| write_json(output_dir / "dataset_quality.json", quality) | |
| return manifest | |
Xet Storage Details
- Size:
- 4.76 kB
- Xet hash:
- 504dc830276e2bfc06c1323665844dc0d07cd3f1ccecdb763566c3c356156f82
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.