darachhat
feat: build production-ready Khmer Document Corpus v0.2.0 with Typer CLI, PyMuPDF, Polars, and DI architecture
c4e128a Download cli/commands/export.py from Darachhat/khmer-document-corpus: direct link, hf CLI and curl.
- Browser
- Download file 1.69 kB
-
https://huggingface.co/datasets/Darachhat/khmer-document-corpus/resolve/main/cli/commands/export.py
- Command line
-
hf download hf://datasets/Darachhat/khmer-document-corpus/cli/commands/export.py
-
curl -L -o export.py https://huggingface.co/datasets/Darachhat/khmer-document-corpus/resolve/main/cli/commands/export.py
1.69 kB
| """kdc export command — re-export metadata into Parquet/JSONL datasets.""" | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| import typer | |
| from rich.console import Console | |
| from app.models.document import DocumentMeta | |
| from app.utils.container import Container | |
| app = typer.Typer(help="Export metadata to Parquet or JSONL datasets.") | |
| console = Console() | |
| def run( | |
| ctx: typer.Context, | |
| meta_dir: Path = typer.Option(Path("metadata"), "--meta-dir", help="Metadata directory."), | |
| format: str = typer.Option("both", "--format", help="Export format: parquet | jsonl | both"), | |
| ) -> None: | |
| """Re-export metadata files into Parquet or JSONL dataset formats.""" | |
| container: Container = ctx.obj | |
| meta_dir = meta_dir.resolve() | |
| files = list(meta_dir.glob("*.json")) | |
| if not files: | |
| console.print(f"[yellow]No metadata files found in {meta_dir}[/yellow]") | |
| raise typer.Exit() | |
| documents: list[DocumentMeta] = [] | |
| for f in files: | |
| try: | |
| data = json.loads(f.read_text(encoding="utf-8")) | |
| documents.append(DocumentMeta.model_validate(data)) | |
| except Exception as exc: | |
| console.print(f"[yellow]Skipping invalid JSON {f.name}: {exc}[/yellow]") | |
| fmt = format.lower() | |
| if fmt in ("parquet", "both") and container.parquet_exporter: | |
| path = container.parquet_exporter.export(documents) | |
| console.print(f"[green]✓ Parquet dataset exported to {path}[/green]") | |
| if fmt in ("jsonl", "both") and container.jsonl_exporter: | |
| path = container.jsonl_exporter.export(documents) | |
| console.print(f"[green]✓ JSONL dataset exported to {path}[/green]") | |