File size: 3,740 Bytes
85f2567
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""Copy the canonical project into a Hugging Face Space bundle.

The Space app imports ``src``. This script copies ``src/``, ``config/``,
and the bundled synthetic sample next to ``app.py`` so the Hub repo root
can import them. It does not train a model and it does not upload the bundle.

Run from a checkout, before the Hub upload::

    python space/sync_bundle.py --out /tmp/cloudie-space
"""

from __future__ import annotations

import argparse
import shutil
from pathlib import Path


DEFAULT_HF_MODEL_REPO = "Anlea/cloudie-weights"


class SpaceBundle:
    """Build the directory that is pushed to the Space repository."""

    SPACE_FILES = ("app.py", "requirements.txt", "README.md", "sync_bundle.py")
    PROJECT_TREES = ("src", "config")
    SAMPLE = ("cloudie", "samples", "synthetic_ir.npy")
    SKIP_NAMES = {
        "__pycache__",
        ".pytest_cache",
        ".mypy_cache",
        ".ruff_cache",
        "artifacts",
        "data",
        "reports",
        ".git",
    }

    @staticmethod
    def repo_root() -> Path:
        """GitHub checkout that contains ``src/inference.py`` beside ``space/``."""
        here = Path(__file__).resolve().parent
        candidate = here.parent
        if (candidate / "src" / "inference.py").is_file() and (here / "app.py").is_file():
            return candidate
        if (here / "src" / "inference.py").is_file():
            return here
        raise FileNotFoundError(
            "src/inference.py was not found. Run this script from the Cloudie checkout."
        )

    @staticmethod
    def ignore(directory: str, names: list[str]) -> set[str]:
        """Skip caches and local data. Keep ``cloudie/samples``."""
        del directory
        skipped = set()
        for name in names:
            if name in SpaceBundle.SKIP_NAMES or name.endswith((".pyc", ".pt", ".pth")):
                skipped.add(name)
        return skipped

    @staticmethod
    def build(destination: Path, repo_root: Path | None = None) -> Path:
        """Write a fresh bundle. Refuses to replace the checkout or its source trees."""
        root = (repo_root or SpaceBundle.repo_root()).resolve()
        destination = destination.resolve()
        space_dir = root / "space"
        if destination == root or destination in root.parents or destination == space_dir:
            raise ValueError("Write the bundle to a new directory, for example space/bundle.")
        for name in SpaceBundle.PROJECT_TREES:
            tree = root / name
            if destination == tree or tree in destination.parents or destination in tree.parents:
                raise ValueError("The bundle destination must sit outside src, config, and cloudie.")
        if destination.exists():
            shutil.rmtree(destination)
        destination.mkdir(parents=True)
        for name in SpaceBundle.SPACE_FILES:
            shutil.copy2(space_dir / name, destination / name)
        for name in SpaceBundle.PROJECT_TREES:
            shutil.copytree(root / name, destination / name, ignore=SpaceBundle.ignore)
        sample = root.joinpath(*SpaceBundle.SAMPLE)
        dest_sample = destination.joinpath(*SpaceBundle.SAMPLE)
        dest_sample.parent.mkdir(parents=True, exist_ok=True)
        shutil.copy2(sample, dest_sample)
        return destination


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--out",
        type=Path,
        default=Path("space/bundle"),
        help="Directory to write. Replaced when it already exists.",
    )
    args = parser.parse_args(argv)
    built = SpaceBundle.build(args.out)
    print(built)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())