#!/usr/bin/env python3 from __future__ import annotations import argparse import os import sys import zipfile from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) from romani_asr.manifest import read_manifest_csv DEFAULT_MANIFESTS = [ Path("artifacts/manifests/clean/train_clean.csv"), Path("artifacts/manifests/clean/validation_clean.csv"), Path("artifacts/manifests/test.csv"), ] CODE_PATHS = [ Path("requirements.txt"), Path("README.md"), Path("scripts/train_mms_adapter.py"), Path("scripts/evaluate_mms_asr.py"), Path("scripts/summarize_frozen_asr_eval.py"), Path("scripts/run_mms_gpu_training.sh"), Path("docs/mms-gpu-training.md"), Path("reports/frozen-asr-evaluation-2026-08-10.md"), ] def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser( description="Create a portable ZIP for MMS GPU training." ) parser.add_argument( "--output", type=Path, default=Path("artifacts/gpu/mms-romani-gpu-training-bundle.zip"), ) parser.add_argument( "--manifest", action="append", type=Path, dest="manifests", help="Manifest to include. Defaults to clean train/validation and test.", ) parser.add_argument( "--dry-run", action="store_true", help="Validate files and print the bundle plan without writing the ZIP.", ) return parser.parse_args() def project_files() -> list[Path]: files = list(CODE_PATHS) files.extend(sorted(Path("src").glob("romani_asr/*.py"))) return files def referenced_audio_files(manifests: list[Path]) -> list[Path]: audio_paths: set[Path] = set() for manifest in manifests: for row in read_manifest_csv(manifest): audio_paths.add(Path(row["audio_path"])) return sorted(audio_paths) def validate_files(paths: list[Path]) -> None: missing = [path for path in paths if not path.exists()] if missing: formatted = "\n".join(f"- {path}" for path in missing[:20]) suffix = "" if len(missing) <= 20 else f"\n... and {len(missing) - 20} more" raise FileNotFoundError(f"Missing files for bundle:\n{formatted}{suffix}") def bundle_paths(manifests: list[Path]) -> list[Path]: paths: set[Path] = set(manifests) paths.update(project_files()) paths.update(referenced_audio_files(manifests)) return sorted(paths) def write_zip(output: Path, paths: list[Path]) -> None: output.parent.mkdir(parents=True, exist_ok=True) temporary = output.with_suffix(output.suffix + ".tmp") if temporary.exists(): temporary.unlink() with zipfile.ZipFile( temporary, "w", compression=zipfile.ZIP_DEFLATED, compresslevel=6, ) as archive: for path in paths: archive.write(path, path.as_posix()) os.replace(temporary, output) def main() -> None: args = parse_args() manifests = args.manifests or DEFAULT_MANIFESTS paths = bundle_paths(manifests) validate_files(paths) total_bytes = sum(path.stat().st_size for path in paths) print(f"Files: {len(paths)}") print(f"Uncompressed: {total_bytes / 1024 / 1024:.1f} MB") if args.dry_run: print("Dry run only; no ZIP written.") return write_zip(args.output, paths) print(f"Wrote: {args.output}") print(f"ZIP size: {args.output.stat().st_size / 1024 / 1024:.1f} MB") if __name__ == "__main__": main()