add script to convert training snippets into a jsonl file
This commit is contained in:
1 parent
af8a510d15
commit
a7db571513
6 files changed
+97
-4
No files matched your search
Executable
+43
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Read all JSON files in a directory, minify each, and append to an output JSONL file."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Minify each JSON file in a directory and append to a JSONL file."
|
||||
)
|
||||
parser.add_argument("directory", help="Directory containing .json files")
|
||||
parser.add_argument("output", help="Output JSONL file to append to")
|
||||
args = parser.parse_args()
|
||||
|
||||
dir_path = Path(args.directory)
|
||||
if not dir_path.is_dir():
|
||||
print(f"Error: '{args.directory}' is not a directory", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
json_files = sorted(dir_path.glob("*.json"))
|
||||
if not json_files:
|
||||
print(f"No .json files found in '{args.directory}'", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
written = 0
|
||||
with open(args.output, "a", encoding="utf-8") as out:
|
||||
for f in json_files:
|
||||
try:
|
||||
data = json.loads(f.read_text(encoding="utf-8"))
|
||||
out.write(json.dumps(data, separators=(",", ":"), ensure_ascii=False) + "\n")
|
||||
written += 1
|
||||
except (json.JSONDecodeError, OSError) as e:
|
||||
print(f"Warning: skipping {f}: {e}", file=sys.stderr)
|
||||
|
||||
print(f"Wrote {written} record(s) to {args.output}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user