File size: 4,505 Bytes
23680f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""Command-line interface for HyperView."""

import argparse
import sys

from hyperview import Dataset, launch


def main():
    """Main CLI entry point."""
    parser = argparse.ArgumentParser(
        prog="hyperview",
        description="HyperView - Dataset visualization with hyperbolic embeddings",
    )
    subparsers = parser.add_subparsers(dest="command", help="Available commands")

    # Demo command
    demo_parser = subparsers.add_parser("demo", help="Run a demo with sample data")
    demo_parser.add_argument(
        "--samples",
        type=int,
        default=500,
        help="Number of samples to load (default: 500)",
    )
    demo_parser.add_argument(
        "--port",
        type=int,
        default=6262,
        help="Port to run the server on (default: 6262)",
    )
    demo_parser.add_argument(
        "--host",
        type=str,
        default="127.0.0.1",
        help="Host to bind the server to (default: 127.0.0.1)",
    )
    demo_parser.add_argument(
        "--no-browser",
        action="store_true",
        help="Do not open a browser window automatically",
    )
    demo_parser.add_argument(
        "--reuse-server",
        action="store_true",
        help=(
            "If the port is already serving HyperView, attach instead of failing. "
            "For safety, this only attaches when the existing server reports the same dataset name."
        ),
    )

    # Serve command
    serve_parser = subparsers.add_parser("serve", help="Serve a saved dataset")
    serve_parser.add_argument("dataset", help="Path to saved dataset JSON file")
    serve_parser.add_argument(
        "--port",
        type=int,
        default=6262,
        help="Port to run the server on (default: 6262)",
    )
    serve_parser.add_argument(
        "--host",
        type=str,
        default="127.0.0.1",
        help="Host to bind the server to (default: 127.0.0.1)",
    )
    serve_parser.add_argument(
        "--no-browser",
        action="store_true",
        help="Do not open a browser window automatically",
    )
    serve_parser.add_argument(
        "--reuse-server",
        action="store_true",
        help=(
            "If the port is already serving HyperView, attach instead of failing. "
            "For safety, this only attaches when the existing server reports the same dataset name."
        ),
    )

    args = parser.parse_args()

    if args.command == "demo":
        run_demo(
            args.samples,
            args.port,
            host=args.host,
            open_browser=not args.no_browser,
            reuse_server=args.reuse_server,
        )
    elif args.command == "serve":
        serve_dataset(
            args.dataset,
            args.port,
            host=args.host,
            open_browser=not args.no_browser,
            reuse_server=args.reuse_server,
        )
    else:
        parser.print_help()
        sys.exit(1)


def run_demo(
    num_samples: int = 500,
    port: int = 6262,
    *,
    host: str = "127.0.0.1",
    open_browser: bool = True,
    reuse_server: bool = False,
):
    """Run a demo with CIFAR-10 data."""
    print("Loading CIFAR-10 dataset...")
    dataset = Dataset("cifar10_demo")

    added, skipped = dataset.add_from_huggingface(
        "uoft-cs/cifar10",
        split="train",
        image_key="img",
        label_key="label",
        max_samples=num_samples,
    )
    if skipped > 0:
        print(f"Loaded {added} samples ({skipped} already present)")
    else:
        print(f"Loaded {added} samples")

    print("Computing embeddings...")
    dataset.compute_embeddings(show_progress=True)
    print("Embeddings computed")

    print("Computing visualizations...")
    # Compute both euclidean and poincare layouts
    dataset.compute_visualization(geometry="euclidean")
    dataset.compute_visualization(geometry="poincare")
    print("Visualizations ready")

    launch(dataset, port=port, host=host, open_browser=open_browser, reuse_server=reuse_server)


def serve_dataset(
    filepath: str,
    port: int = 6262,
    *,
    host: str = "127.0.0.1",
    open_browser: bool = True,
    reuse_server: bool = False,
):
    """Serve a saved dataset."""
    from hyperview import Dataset, launch

    print(f"Loading dataset from {filepath}...")
    dataset = Dataset.load(filepath)
    print(f"Loaded {len(dataset)} samples")

    launch(dataset, port=port, host=host, open_browser=open_browser, reuse_server=reuse_server)


if __name__ == "__main__":
    main()