Spaces:
Sleeping
Sleeping
| from pathlib import Path | |
| import json | |
| from src.chunking.models import Chunk | |
| from src.chunking.validators import validate_chunks | |
| def write_chunks(chunks: list[Chunk], output_path: str | Path) -> None: | |
| validate_chunks(chunks) | |
| path = Path(output_path) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| if path.suffix.lower() == ".json": | |
| path.write_text( | |
| json.dumps([chunk.to_dict() for chunk in chunks], ensure_ascii=False, indent=2), | |
| encoding="utf-8", | |
| ) | |
| return | |
| if path.suffix.lower() != ".jsonl": | |
| raise ValueError("Output path must end with .jsonl or .json") | |
| with path.open("w", encoding="utf-8") as file: | |
| for chunk in chunks: | |
| file.write(json.dumps(chunk.to_dict(), ensure_ascii=False) + "\n") | |