File size: 14,724 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""Public API for HyperView."""

import json
import os
import socket
import threading
import time
import webbrowser
from dataclasses import dataclass
from urllib.error import URLError
from urllib.request import Request, urlopen
from uuid import uuid4

import uvicorn

from hyperview.core.dataset import Dataset
from hyperview.server.app import create_app, set_dataset

__all__ = ["Dataset", "launch", "Session"]


@dataclass(frozen=True)
class _HealthResponse:
    name: str | None
    session_id: str | None
    dataset: str | None
    pid: int | None


def _can_connect(host: str, port: int, timeout_s: float) -> bool:
    try:
        with socket.create_connection((host, port), timeout=timeout_s):
            return True
    except OSError:
        return False


def _try_read_health(url: str, timeout_s: float) -> _HealthResponse | None:
    try:
        return _read_health(url, timeout_s=timeout_s)
    except (URLError, TimeoutError, OSError, ValueError, json.JSONDecodeError):
        return None


def _read_health(url: str, timeout_s: float) -> _HealthResponse:
    request = Request(url, headers={"Accept": "application/json"})
    with urlopen(request, timeout=timeout_s) as response:
        data = json.loads(response.read().decode("utf-8"))

    return _HealthResponse(
        name=data.get("name"),
        session_id=data.get("session_id"),
        dataset=data.get("dataset"),
        pid=data.get("pid") if isinstance(data.get("pid"), int) else None,
    )


class Session:
    """A session for the HyperView visualizer."""

    def __init__(self, dataset: Dataset, host: str, port: int):
        self.dataset = dataset
        self.host = host
        self.port = port
        # Prefer a browser-connectable host for user-facing URLs.
        # When binding to 0.0.0.0, users should connect via 127.0.0.1 locally.
        self.url = f"http://{self._connect_host}:{port}"
        self._server_thread: threading.Thread | None = None
        self._server: uvicorn.Server | None = None
        self._startup_error: BaseException | None = None
        self.session_id = uuid4().hex

    @property
    def _connect_host(self) -> str:
        return "127.0.0.1" if self.host == "0.0.0.0" else self.host

    @property
    def _health_url(self) -> str:
        return f"http://{self._connect_host}:{self.port}/__hyperview__/health"

    def _run_server(self):
        try:
            set_dataset(self.dataset)
            app = create_app(self.dataset, session_id=self.session_id)
            config = uvicorn.Config(app, host=self.host, port=self.port, log_level="warning")
            self._server = uvicorn.Server(config)
            self._server.run()
        except BaseException as exc:
            self._startup_error = exc

    def start(self, background: bool = True):
        """Start the visualizer server."""
        if not background:
            self._run_server()
            return

        # Fail fast if something is already listening on this port.
        if _can_connect(self._connect_host, self.port, timeout_s=0.2):
            health = _try_read_health(self._health_url, timeout_s=0.2)
            if health is not None and health.name == "hyperview":
                raise RuntimeError(
                    "HyperView failed to start because the port is already serving "
                    f"HyperView (port={self.port}, session_id={health.session_id}). "
                    "Choose a different port or stop the existing server."
                )

            raise RuntimeError(
                "HyperView failed to start because the port is already in use "
                f"by a non-HyperView service (port={self.port}). Choose a different "
                "port or stop the process listening on that port."
            )

        self._startup_error = None
        self._server_thread = threading.Thread(target=self._run_server, daemon=True)
        self._server_thread.start()

        deadline = time.time() + 5.0
        last_health_error: Exception | None = None

        while time.time() < deadline:
            if self._startup_error is not None:
                raise RuntimeError(
                    f"HyperView server failed to start (port={self.port}): "
                    f"{type(self._startup_error).__name__}: {self._startup_error}"
                )

            if self._server_thread is not None and not self._server_thread.is_alive():
                raise RuntimeError(
                    "HyperView server thread exited during startup. "
                    f"The port may be in use (port={self.port})."
                )

            try:
                health = _read_health(self._health_url, timeout_s=0.2)
            except (URLError, TimeoutError, OSError, ValueError, json.JSONDecodeError) as exc:
                last_health_error = exc
                time.sleep(0.05)
                continue

            if health.name == "hyperview" and health.session_id == self.session_id:
                return

            if health.name == "hyperview":
                raise RuntimeError(
                    "HyperView failed to start because the port is already serving "
                    f"a different HyperView session (port={self.port}, "
                    f"session_id={health.session_id})."
                )

            raise RuntimeError(
                "HyperView failed to start because the port is already serving "
                f"a non-HyperView app (port={self.port})."
            )

        raise TimeoutError(
            "HyperView server did not become ready in time "
            f"(port={self.port}). Last error: {last_health_error}"
        )

    def stop(self):
        """Stop the visualizer server."""
        if self._server:
            self._server.should_exit = True

    def show(self, height: int = 800):
        """Display the visualizer in a notebook.

        In Google Colab, notebook kernels cannot be accessed via localhost.
        Colab exposes kernel ports through a proxy URL (see
        `google.colab.kernel.proxyPort`). This renders a link to the proxied URL
        that opens in a new tab.

        In other notebook environments, it renders a clickable link to the local
        URL and a best-effort JavaScript auto-open.
        """
        if _is_colab():
            try:
                from google.colab.output import eval_js  # type: ignore[import-not-found]
                from IPython.display import HTML, display

                proxy_url = eval_js(f"google.colab.kernel.proxyPort({self.port})")
                app_url = str(proxy_url).rstrip("/") + "/"

                display(
                    HTML(
                        "<p>HyperView is running in Colab. "
                        f"<a href=\"{app_url}\" target=\"_blank\" rel=\"noopener noreferrer\">"
                        "Open HyperView in a new tab</a>.</p>"
                    )
                )
                display(HTML(f"<p style=\"font-size:12px;color:#666;\">{app_url}</p>"))
                return
            except Exception:
                # Fall through to the generic notebook behavior.
                pass

        # Default: open in a new browser tab (works well for Jupyter).
        try:
            from IPython.display import HTML, Javascript, display

            display(
                HTML(
                    "<p>HyperView is running. "
                    f"<a href=\"{self.url}\" target=\"_blank\" rel=\"noopener\">Open in a new tab</a>."
                    "</p>"
                )
            )

            # Best-effort auto-open. Some browsers may block popups.
            display(Javascript(f'window.open("{self.url}", "_blank");'))
        except ImportError:
            print(f"IPython not installed. Please visit {self.url} in your browser.")

    def open_browser(self):
        """Open the visualizer in a browser window."""
        webbrowser.open(self.url)


def launch(
    dataset: Dataset,
    port: int = 6262,
    host: str = "127.0.0.1",
    open_browser: bool = True,
    notebook: bool | None = None,
    height: int = 800,
    reuse_server: bool = False,
) -> Session:
    """Launch the HyperView visualization server.

    Note:
        HyperView's UI requires 2D layouts (Euclidean + Poincare). If they are
        missing but high-dimensional embeddings exist, this function will compute
        them automatically.

    Args:
        dataset: The dataset to visualize.
        port: Port to run the server on.
        host: Host to bind to.
        open_browser: Whether to open a browser window.
        notebook: Whether to display in a notebook. If None, auto-detects.
        height: Height of the iframe in the notebook.
        reuse_server: If True, and the requested port is already serving HyperView,
            attach to the existing server instead of starting a new one. For safety,
            this will only attach when the existing server reports the same dataset
            name (via `/__hyperview__/health`).

    Returns:
        A Session object.

    Example:
        >>> import hyperview as hv
        >>> dataset = hv.Dataset("my_dataset")
        >>> dataset.add_images_dir("/path/to/images", label_from_folder=True)
        >>> dataset.compute_embeddings()
        >>> dataset.compute_visualization()
        >>> hv.launch(dataset)
    """
    if notebook is None:
        # Colab is always a notebook environment, even if _is_notebook() fails to detect it
        notebook = _is_notebook() or _is_colab()

    if _is_colab() and host == "127.0.0.1":
        # Colab port forwarding/proxying is most reliable when the server binds
        # to all interfaces.
        host = "0.0.0.0"

    # Preflight: avoid doing expensive work if the port is already in use.
    # If it's already serving HyperView and reuse_server=True, we can safely attach.
    connect_host = "127.0.0.1" if host == "0.0.0.0" else host
    health_url = f"http://{connect_host}:{port}/__hyperview__/health"

    if _can_connect(connect_host, port, timeout_s=0.2):
        health = _try_read_health(health_url, timeout_s=0.2)
        if health is not None and health.name == "hyperview":
            if not reuse_server:
                raise RuntimeError(
                    "HyperView failed to start because the port is already serving "
                    f"HyperView (port={port}, dataset={health.dataset}, "
                    f"session_id={health.session_id}, pid={health.pid}). "
                    "Choose a different port, stop the existing server, or pass "
                    "reuse_server=True to attach."
                )

            if health.dataset is not None and health.dataset != dataset.name:
                raise RuntimeError(
                    "HyperView refused to attach to the existing server because it is "
                    f"serving a different dataset (port={port}, dataset={health.dataset}). "
                    f"Requested dataset={dataset.name}. Stop the existing server or "
                    "choose a different port."
                )

            session = Session(dataset, host, port)
            if health.session_id is not None:
                session.session_id = health.session_id

            if notebook:
                if _is_colab():
                    print(
                        f"\nHyperView is already running (Colab, port={session.port}). "
                        "Use the link below to open it."
                    )
                else:
                    print(
                        f"\nHyperView is already running at {session.url} (port={session.port}). "
                        "Opening a new tab..."
                    )
                session.show(height=height)
            else:
                print(f"\nHyperView is already running at {session.url} (port={session.port}).")
                if open_browser:
                    session.open_browser()

            return session

        raise RuntimeError(
            "HyperView failed to start because the port is already in use "
            f"by a non-HyperView service (port={port}). Choose a different "
            "port or stop the process listening on that port."
        )

    # The frontend requires 2D coords from /api/embeddings.
    # Ensure at least one layout exists; do not auto-generate optional geometries.
    layouts = dataset.list_layouts()
    spaces = dataset.list_spaces()

    if not spaces:
        raise ValueError(
            "HyperView launch requires 2D projections for the UI. "
            "No projections or embedding spaces were found. "
            "Call `dataset.compute_embeddings()` and `dataset.compute_visualization()` "
            "before `hv.launch()`."
        )

    if not layouts:
        default_space_key = spaces[0].space_key
        print("No layouts found. Computing euclidean visualization...")
        dataset.compute_visualization(space_key=default_space_key, geometry="euclidean")

    session = Session(dataset, host, port)

    if notebook:
        session.start(background=True)
        if _is_colab():
            print(
                f"\nHyperView is running (Colab, port={session.port}). "
                "Use the link below to open it."
            )
        else:
            print(f"\nHyperView is running at {session.url}. Opening a new tab...")
        session.show(height=height)
    else:
        session.start(background=True)
        print("   Press Ctrl+C to stop.\n")
        print(f"\nHyperView is running at {session.url}")

        if open_browser:
            session.open_browser()

        try:
            while True:
                # Keep the main thread alive so the daemon server thread can run.
                time.sleep(0.25)
                if session._server_thread is not None and not session._server_thread.is_alive():
                    raise RuntimeError("HyperView server stopped unexpectedly.")
        except KeyboardInterrupt:
            pass
        finally:
            session.stop()
            if session._server_thread is not None:
                session._server_thread.join(timeout=2.0)

    return session


def _is_notebook() -> bool:
    """Check if running in a notebook environment."""
    try:
        from IPython import get_ipython
    except ImportError:
        return False

    shell = get_ipython()
    return shell is not None and shell.__class__.__name__ == "ZMQInteractiveShell"


def _is_colab() -> bool:
    """Check if running inside a Google Colab notebook runtime."""
    if os.environ.get("COLAB_RELEASE_TAG"):
        return True
    try:
        import google.colab  # type: ignore[import-not-found]

        return True
    except ImportError:
        return False