Download csv_to_jsonl.py from Tasfiya025/robotics-basic-dataset: direct link, hf CLI and curl.
- Browser
- Download file 966 Bytes
-
https://huggingface.co/datasets/Tasfiya025/robotics-basic-dataset/resolve/main/csv_to_jsonl.py
- Command line
-
hf download hf://datasets/Tasfiya025/robotics-basic-dataset/csv_to_jsonl.py
-
curl -L -o csv_to_jsonl.py https://huggingface.co/datasets/Tasfiya025/robotics-basic-dataset/resolve/main/csv_to_jsonl.py
966 Bytes
| """ | |
| csv_to_jsonl.py | |
| Converts a CSV file with columns `prompt` and `response` into a JSONL file | |
| where each line is a JSON object: {"input": "<prompt>", "output": "<response>"} | |
| Run: | |
| python csv_to_jsonl.py | |
| This will create `dataset.jsonl` in the same folder. | |
| """ | |
| import csv | |
| import json | |
| INPUT_CSV = "dataset.csv" | |
| OUTPUT_JSONL = "dataset.jsonl" | |
| def csv_to_jsonl(in_csv: str = INPUT_CSV, out_jsonl: str = OUTPUT_JSONL): | |
| with open(in_csv, "r", encoding="utf-8") as f_in, open(out_jsonl, "w", encoding="utf-8") as f_out: | |
| reader = csv.DictReader(f_in) | |
| # Ensure header contains prompt and response | |
| if "prompt" not in reader.fieldnames or "response" not in reader.fieldnames: | |
| raise ValueError("CSV must contain 'prompt' and 'response' columns") | |
| for row in reader: | |
| obj = {"input": row["prompt"].strip(), "output": row["response"].strip()} | |
| f_out.write(json.dumps(obj, ensure_ascii=False) + "\n") | |
| print(f"Wrote {out_jsonl}") | |
| if __name__ == "__main__": | |
| csv_to_jsonl() |