JSONL conversion
#6
by
jeanma - opened
I adapted the conversion script from the one @laurievb wrote for FLORES+. I also added tests adapted from @cointegrated 's, which are all passing. Beyond the conversion, this PR fixes some typos and releases a new version.
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "datasets",
# "fire",
# ]
# ///
from pathlib import Path
import datasets
import fire
def verify_parquet_matches_jsonl(
parquet_file: Path,
jsonl_file: Path,
) -> None:
print(f"verifying {parquet_file} against {jsonl_file}")
parquet_ds = datasets.load_dataset(
"parquet", data_files={"train": str(parquet_file)}
)["train"]
jsonl_ds = datasets.load_dataset("json", data_files={"train": str(jsonl_file)})[
"train"
]
if len(parquet_ds) != len(jsonl_ds):
raise RuntimeError(
f"{jsonl_file}: row count mismatch "
f"(parquet={len(parquet_ds)}, jsonl={len(jsonl_ds)})"
)
if parquet_ds.column_names != jsonl_ds.column_names:
raise RuntimeError(
f"{jsonl_file}: column mismatch "
f"(parquet={parquet_ds.column_names}, jsonl={jsonl_ds.column_names})"
)
for row_idx, (parquet_row, jsonl_row) in enumerate(zip(parquet_ds, jsonl_ds)):
if parquet_row != jsonl_row:
raise RuntimeError(f"{jsonl_file}: mismatch at row {row_idx}")
def convert_parquet_to_jsonl(override: bool = False) -> None:
for parquet_file in list(Path("seed").glob("*.parquet")):
jsonl_file = parquet_file.with_suffix(".jsonl")
if jsonl_file.exists() and not override:
print(f"found {jsonl_file}, skipping conversion of {parquet_file}")
else:
print(f"loading {parquet_file}")
ds = datasets.load_dataset(
"parquet", data_files={"train": str(parquet_file)}
)
ds["train"].to_json(jsonl_file, force_ascii=False)
verify_parquet_matches_jsonl(parquet_file, jsonl_file)
if __name__ == "__main__":
fire.Fire(convert_parquet_to_jsonl)
jeanma changed pull request status to
open
LGTM; only the EXPECTED_COPIES list in the test that has been copy-pasted from FLORES+ needs to be updated based on actual Seed stats.
It has, yes – it's not visible here because HF truncates the diff view to a limited number of files, annoyingly. The duplicate counts are somewhat higher compared to FLORES+, because this dataset contains a number of bibliographic references that can legitimately be left as-is.
jeanma changed pull request status to
merged