Create build_dataset.py
Browse files- scripts/build_dataset.py +99 -0
scripts/build_dataset.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Build Hugging Face dataset artifacts from JSONL.
|
| 4 |
+
|
| 5 |
+
- Loads data/mux_assets.jsonl
|
| 6 |
+
- Runs a light validation (required fields + raw payload)
|
| 7 |
+
- Builds a datasets.Dataset
|
| 8 |
+
- Exports to Parquet at data/mux_assets.parquet
|
| 9 |
+
|
| 10 |
+
Usage:
|
| 11 |
+
pip install -U datasets pyarrow
|
| 12 |
+
python scripts/build_dataset.py
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import json
|
| 18 |
+
import os
|
| 19 |
+
from typing import Any, Dict, List
|
| 20 |
+
|
| 21 |
+
from datasets import Dataset
|
| 22 |
+
|
| 23 |
+
JSONL_PATH = os.path.join("data", "mux_assets.jsonl")
|
| 24 |
+
PARQUET_PATH = os.path.join("data", "mux_assets.parquet")
|
| 25 |
+
|
| 26 |
+
REQUIRED_FIELDS = [
|
| 27 |
+
"asset_id",
|
| 28 |
+
"upload_id",
|
| 29 |
+
"title",
|
| 30 |
+
"external_id",
|
| 31 |
+
"creator_id",
|
| 32 |
+
"status",
|
| 33 |
+
"video_quality",
|
| 34 |
+
"resolution_tier",
|
| 35 |
+
"duration_seconds",
|
| 36 |
+
"aspect_ratio",
|
| 37 |
+
"max_width",
|
| 38 |
+
"max_height",
|
| 39 |
+
"max_frame_rate",
|
| 40 |
+
"encoding_tier",
|
| 41 |
+
"normalize_audio",
|
| 42 |
+
"mp4_support",
|
| 43 |
+
"playback_policy",
|
| 44 |
+
"playback_id_public",
|
| 45 |
+
"video_track_id",
|
| 46 |
+
"audio_track_id",
|
| 47 |
+
"audio_primary",
|
| 48 |
+
"audio_channels",
|
| 49 |
+
"audio_language_code",
|
| 50 |
+
"created_at_unix",
|
| 51 |
+
"raw_mux_json",
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def load_jsonl(path: str) -> List[Dict[str, Any]]:
|
| 56 |
+
rows: List[Dict[str, Any]] = []
|
| 57 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 58 |
+
for i, line in enumerate(f, start=1):
|
| 59 |
+
line = line.strip()
|
| 60 |
+
if not line:
|
| 61 |
+
continue
|
| 62 |
+
try:
|
| 63 |
+
rows.append(json.loads(line))
|
| 64 |
+
except json.JSONDecodeError as e:
|
| 65 |
+
raise SystemExit(f"[ERROR] Invalid JSON on line {i}: {e}") from e
|
| 66 |
+
return rows
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def light_validate(rows: List[Dict[str, Any]]) -> None:
|
| 70 |
+
if not rows:
|
| 71 |
+
raise SystemExit("[ERROR] No rows found in JSONL.")
|
| 72 |
+
for idx, row in enumerate(rows):
|
| 73 |
+
missing = [k for k in REQUIRED_FIELDS if k not in row]
|
| 74 |
+
if missing:
|
| 75 |
+
raise SystemExit(f"[ERROR] Row {idx} missing required fields: {missing}")
|
| 76 |
+
if not isinstance(row["raw_mux_json"], dict):
|
| 77 |
+
raise SystemExit(f"[ERROR] Row {idx} raw_mux_json must be an object/dict.")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def main() -> None:
|
| 81 |
+
if not os.path.exists(JSONL_PATH):
|
| 82 |
+
raise SystemExit(f"[ERROR] Missing file: {JSONL_PATH}")
|
| 83 |
+
|
| 84 |
+
rows = load_jsonl(JSONL_PATH)
|
| 85 |
+
light_validate(rows)
|
| 86 |
+
|
| 87 |
+
ds = Dataset.from_list(rows)
|
| 88 |
+
|
| 89 |
+
print("\n=== Dataset Preview ===")
|
| 90 |
+
print(ds)
|
| 91 |
+
print("\n=== Features ===")
|
| 92 |
+
print(ds.features)
|
| 93 |
+
|
| 94 |
+
ds.to_parquet(PARQUET_PATH)
|
| 95 |
+
print(f"\n[OK] Wrote Parquet: {PARQUET_PATH}")
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
main()
|