Spaces:
Running on Zero
Running on Zero
File size: 21,902 Bytes
41ff959 1be1197 41ff959 1be1197 41ff959 1be1197 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 1be1197 41ff959 1be1197 41ff959 1be1197 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 1be1197 da3ef7c 41ff959 a6825eb 41ff959 da3ef7c 41ff959 1be1197 41ff959 e08fcbd 41ff959 a6825eb e08fcbd 41ff959 e08fcbd 41ff959 a6825eb 386744b bbef0dc 386744b da3ef7c 386744b da3ef7c 386744b a6825eb da3ef7c 1be1197 da3ef7c e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 e08fcbd 41ff959 | 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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 | import ast
import os
from pathlib import Path
import pytest
import torch
from PIL import Image
from src.demo import hf_runtime, hf_ui
from src.demo.hf_runtime import (
LOCAL_RGB_CHECKPOINT,
SPLAT_TRANSFORM_PACKAGE,
BrowserViewerArtifacts,
GaussianArtifact,
ViewerTemplate,
build_standalone_viewer,
build_splat_transform_command,
build_viewer_template_command,
export_filtered_gaussian_ply,
install_shared_viewer_assets,
load_gaussian_artifact,
prepare_viewer_template,
resolve_rgb_checkpoint,
save_gaussian_artifact,
)
from src.demo.hf_ui import (
build_viewer_iframe,
build_viewer_preloader,
cleanup_request_directories,
)
from src.demo.infer_single_image import patch_supersplat_html_viewer_bridge
from src.utils.gaussians import Gaussians3D
def _gaussians() -> Gaussians3D:
return Gaussians3D(
mean_vectors=torch.tensor([[[1.0, 2.0, 3.0]]]),
singular_values=torch.tensor([[[0.1, 0.2, 0.3]]]),
quaternions=torch.tensor([[[1.0, 0.0, 0.0, 0.0]]]),
colors=torch.tensor([[[0.4, 0.5, 0.6]]]),
opacities=torch.tensor([[0.9]]),
covariances=torch.eye(3).reshape(1, 1, 3, 3),
)
def test_checkpoint_override_has_highest_priority(tmp_path, monkeypatch) -> None:
override = tmp_path / "override.ckpt"
override.touch()
monkeypatch.setenv("INFINISPLAT_CHECKPOINT", str(override))
assert resolve_rgb_checkpoint() == override
def test_repository_checkpoint_is_reused_before_hub(tmp_path, monkeypatch) -> None:
checkpoint = tmp_path / "infinisplat_rgb.ckpt"
checkpoint.touch()
monkeypatch.delenv("INFINISPLAT_CHECKPOINT", raising=False)
monkeypatch.setattr(hf_runtime, "LOCAL_RGB_CHECKPOINT", checkpoint)
monkeypatch.setattr(
hf_runtime,
"hf_hub_download",
lambda **_: pytest.fail("Hub download should not be called"),
)
assert resolve_rgb_checkpoint() == checkpoint
def test_checkpoint_falls_back_to_hub(tmp_path, monkeypatch) -> None:
downloaded = tmp_path / "downloaded.ckpt"
downloaded.touch()
monkeypatch.delenv("INFINISPLAT_CHECKPOINT", raising=False)
monkeypatch.setattr(hf_runtime, "LOCAL_RGB_CHECKPOINT", tmp_path / "missing.ckpt")
monkeypatch.setattr(hf_runtime, "hf_hub_download", lambda **_: str(downloaded))
assert resolve_rgb_checkpoint() == downloaded
def test_local_checkpoint_path_matches_repository_layout() -> None:
assert LOCAL_RGB_CHECKPOINT == Path("checkpoints/infinisplat_rgb.ckpt").resolve()
def test_request_cleanup_removes_only_expired_uuid_directories(tmp_path) -> None:
old_request = tmp_path / ("a" * 32)
recent_request = tmp_path / ("b" * 32)
shared_assets = tmp_path / "_viewer_assets"
old_request.mkdir()
recent_request.mkdir()
shared_assets.mkdir()
(old_request / "viewer.sog").write_bytes(b"old")
(recent_request / "viewer.sog").write_bytes(b"recent")
(shared_assets / "index.js").write_text("shared")
os.utime(old_request, (100.0, 100.0))
os.utime(recent_request, (900.0, 900.0))
removed = cleanup_request_directories(
output_root=tmp_path,
max_age_seconds=500,
now=1000.0,
)
assert removed == [old_request]
assert not old_request.exists()
assert recent_request.is_dir()
assert shared_assets.is_dir()
def test_gaussian_artifact_round_trip(tmp_path) -> None:
path = tmp_path / "artifact.pt"
expected = GaussianArtifact(
gaussians=_gaussians(),
focal_length_px=1200.0,
image_shape=(1152, 1536),
)
save_gaussian_artifact(expected, path)
actual = load_gaussian_artifact(path)
assert actual.focal_length_px == expected.focal_length_px
assert actual.image_shape == expected.image_shape
for expected_tensor, actual_tensor in zip(expected.gaussians, actual.gaussians):
assert torch.equal(expected_tensor, actual_tensor)
def test_filtered_ply_export_removes_spatial_outlier(tmp_path, monkeypatch) -> None:
points = torch.cat(
[
torch.randn(100, 3) * 0.01,
torch.tensor([[10.0, 10.0, 10.0]]),
],
dim=0,
).unsqueeze(0)
count = points.shape[1]
gaussians = Gaussians3D(
mean_vectors=points,
singular_values=torch.ones(1, count, 3),
quaternions=torch.ones(1, count, 4),
colors=torch.ones(1, count, 3),
opacities=torch.ones(1, count),
)
artifact_path = tmp_path / "gaussians.pt"
save_gaussian_artifact(
GaussianArtifact(
gaussians=gaussians,
focal_length_px=1200.0,
image_shape=(1152, 1536),
),
artifact_path,
)
captured = {}
def fake_save_ply(*, gaussians, path, **_) -> None:
captured["gaussians"] = gaussians
path.write_bytes(b"ply")
monkeypatch.setattr(hf_runtime, "save_ply", fake_save_ply)
result = export_filtered_gaussian_ply(artifact_path, tmp_path / "output")
assert result.read_bytes() == b"ply"
assert captured["gaussians"].mean_vectors.shape[1] == count - 1
assert float(captured["gaussians"].mean_vectors.abs().max()) < 1.0
def test_build_splat_transform_command_is_fixed_and_non_interactive() -> None:
command = build_splat_transform_command(
scene_ply=Path("scene.ply"),
output_sog=Path("viewer.sog"),
)
assert command[:4] == [
"npx",
"--yes",
"--prefer-offline",
SPLAT_TRANSFORM_PACKAGE,
]
assert SPLAT_TRANSFORM_PACKAGE == "@playcanvas/splat-transform@3.1.6"
assert command[4:] == [
"--quiet",
"--overwrite",
"scene.ply",
"--filter-harmonics",
"0",
"viewer.sog",
]
def test_build_viewer_template_command_is_unbundled() -> None:
command = build_viewer_template_command(
scene_ply=Path("template.ply"),
output_html=Path("template.html"),
viewer_settings=Path("viewer.json"),
)
assert command[:4] == [
"npx",
"--yes",
"--prefer-offline",
SPLAT_TRANSFORM_PACKAGE,
]
assert command[4:] == [
"--quiet",
"--overwrite",
"--unbundled",
"--viewer-settings",
"viewer.json",
"template.ply",
"--filter-harmonics",
"0",
"template.html",
]
def test_build_viewer_iframe_uses_gradio_file_route(tmp_path) -> None:
viewer_path = tmp_path / "viewer output.html"
iframe = build_viewer_iframe(viewer_path)
assert '<iframe class="splat-frame"' in iframe
assert 'data-viewer-state="loading"' in iframe
assert "Initializing 3D viewer" in iframe
assert "/gradio_api/file=" in iframe
assert "viewer%20output.html" in iframe
def test_build_viewer_preloader_warms_template_in_hidden_iframe(tmp_path) -> None:
preload = build_viewer_preloader(tmp_path / "template.html")
assert "Ready" in preload
assert 'class="viewer-preloader"' in preload
assert "/gradio_api/file=" in preload
assert "data-infinisplat-viewer" not in preload
def test_supersplat_viewer_bridge_reports_first_frame(tmp_path) -> None:
viewer_path = tmp_path / "viewer.html"
viewer_path.write_text(
"<html><head></head><body>window.firstFrame?.();</body></html>"
)
patch_supersplat_html_viewer_bridge(viewer_path)
patched_once = viewer_path.read_text()
patch_supersplat_html_viewer_bridge(viewer_path)
assert "data-infinisplat-viewer-bridge" in patched_once
assert 'window.firstFrame = () => setViewerState("ready")' in patched_once
assert "host.dataset.viewerState = state" in patched_once
assert viewer_path.read_text() == patched_once
def test_supersplat_viewer_bridge_accepts_external_script(tmp_path) -> None:
viewer_path = tmp_path / "viewer.html"
script_path = tmp_path / "index.js"
viewer_path.write_text("<html><head></head><body></body></html>")
script_path.write_text("window.firstFrame?.();")
patch_supersplat_html_viewer_bridge(viewer_path, script_path)
assert "data-infinisplat-viewer-bridge" in viewer_path.read_text()
def test_supersplat_viewer_bridge_rejects_changed_bundle(tmp_path) -> None:
viewer_path = tmp_path / "viewer.html"
viewer_path.write_text("<html><head></head><body></body></html>")
with pytest.raises(RuntimeError, match="viewer bundle changed"):
patch_supersplat_html_viewer_bridge(viewer_path)
def test_hf_space_examples_exist() -> None:
discovered = {
path.relative_to(hf_ui.REPO_ROOT).as_posix()
for path in hf_ui.RGB_EXAMPLE_DIR.iterdir()
if path.is_file()
and path.suffix.lower() in hf_ui.SUPPORTED_EXAMPLE_SUFFIXES
}
assert set(hf_ui.RGB_EXAMPLES) == discovered
assert len(hf_ui.RGB_EXAMPLES) == len(discovered)
assert hf_ui.RGB_EXAMPLES[0] == "examples/data/rgb_demo/painting_room.jpg"
assert hf_ui.RGB_EXAMPLES[1] == "examples/data/rgb_demo/summer_room.jpg"
assert hf_ui.RGB_EXAMPLES[3] == "examples/data/rgb_demo/meerkat.jpg"
assert hf_ui.RGB_EXAMPLES[8] == "examples/data/rgb_demo/eth3d_courtyard.png"
assert hf_ui.RGB_EXAMPLES[14] == "examples/data/rgb_demo/pexels-masi.jpg"
for path in hf_ui.RGB_EXAMPLES:
assert Path(path).is_file()
def test_hf_space_uses_node_22_for_splat_transform() -> None:
requirements = Path("requirements.txt").read_text().splitlines()
packages = Path("packages.txt").read_text().splitlines()
assert "nodejs-wheel==22.20.0" in requirements
assert "nodejs" not in packages
assert "npm" not in packages
def test_select_example_returns_gallery_path() -> None:
event = type("SelectEvent", (), {"index": 2})()
assert hf_ui.select_example(event) == hf_ui.RGB_EXAMPLES[2]
def test_example_thumbnails_are_smaller_than_gallery_sources(tmp_path) -> None:
source = tmp_path / "source.jpg"
Image.new("RGB", (1200, 900), "white").save(source)
thumbnails = hf_ui.prepare_example_thumbnails(
example_paths=[str(source)],
output_dir=tmp_path / "thumbnails",
)
with Image.open(thumbnails[0]) as thumbnail:
assert thumbnail.size == (427, 320)
assert thumbnail.format == "WEBP"
def test_viewer_statuses_are_explicit() -> None:
assert hf_ui.GPU_DURATION_SECONDS == 6
assert "Reconstructing scene" in hf_ui.show_reconstructing_viewer()
exporting_status = hf_ui.show_exporting_viewer()
assert "Preparing scene" in exporting_status
assert "float" not in exporting_status.lower()
assert "PLY ready" in hf_ui.show_ply_ready_viewer()
assert "Reconstruction stopped" in hf_ui.show_failed_viewer()
assert hf_ui.show_failed_html_download()["label"] == "HTML export failed"
def test_hf_space_header_content() -> None:
assert hf_ui.FULL_TITLE == (
"Implicit Gaussian Decoding for Large-Baseline Monocular View Synthesis"
)
assert hf_ui.GITHUB_URL == "https://github.com/PLUS-WAVE/InfiniSplat-oss"
assert hf_ui.PROJECT_PAGE_URL == "https://pluswave.top/InfiniSplat-page/"
assert hf_ui.INPUT_IMAGE_HINT == (
"Better for indoor scenes due to HyperSim-only training"
)
assert ".panel-heading {\n display: flex;\n align-items: center;" in (
hf_ui.APP_CSS
)
assert "box-sizing: border-box;" in hf_ui.APP_CSS
assert "padding: 17px 12px 15px;" in hf_ui.APP_CSS
assert ".panel-heading .input-hint" in hf_ui.APP_CSS
assert "white-space: normal;" in hf_ui.APP_CSS
assert "font-size: clamp(0.58rem, 0.72vw, 0.66rem);" in hf_ui.APP_CSS
def test_hf_space_gallery_has_no_captions_or_thumbnail_frames() -> None:
assert all(isinstance(path, str) for path in hf_ui.RGB_EXAMPLES)
assert "#example-gallery .thumbnail-item" in hf_ui.APP_CSS
assert "#example-gallery .caption-label" in hf_ui.APP_CSS
assert "grid-template-columns: repeat(28, minmax(0, 1fr))" in hf_ui.APP_CSS
assert "#example-gallery .gallery-item:nth-child(10)" in hf_ui.APP_CSS
assert "#example-gallery .gallery-item:nth-child(8)" in hf_ui.APP_CSS
assert "grid-column: span 7" not in hf_ui.APP_CSS
assert "grid-column: span 2" in hf_ui.APP_CSS
assert "#example-gallery .gallery-item:last-child:nth-child(odd)" in (
hf_ui.APP_CSS
)
def _write_unbundled_viewer(
output_dir: Path,
viewer_name: str = "viewer.html",
) -> Path:
output_dir.mkdir(parents=True, exist_ok=True)
viewer_html = output_dir / viewer_name
scene_sog = viewer_html.with_suffix(".sog")
viewer_html.write_text(
'<html><head><link rel="stylesheet" href="./index.css"></head>'
'<body><script type="module">'
"const settingsUrl = './settings.json';"
'const settings = {settings: fetch(settingsUrl).then(response => response.json())};'
f'const scene = fetch("{scene_sog.name}");'
"import { main } from './index.js';"
"</script></body></html>"
)
scene_sog.write_bytes(b"scene")
(output_dir / "index.css").write_text("canvas { display: block; }")
(output_dir / "index.js").write_text("const main = true;")
(output_dir / "settings.json").write_text('{"camera":{"fov":60}}')
return viewer_html
def test_build_standalone_viewer_embeds_unbundled_assets(tmp_path) -> None:
request_dir = tmp_path / "request"
viewer_html = _write_unbundled_viewer(request_dir)
shared_dir = install_shared_viewer_assets(viewer_html)
standalone_html = request_dir / "scene.html"
build_standalone_viewer(viewer_html, shared_dir, standalone_html)
source = standalone_html.read_text()
assert "canvas { display: block; }" in source
assert "const main = true;" in source
assert 'settings: {"camera":{"fov":60}}' in source
assert "data:application/octet-stream;base64,c2NlbmU=" in source
def test_prepare_viewer_template_builds_assets_once(tmp_path, monkeypatch) -> None:
captured = {}
def fake_save_ply(*, path, **_) -> None:
path.write_bytes(b"ply")
def fake_run(command, **_) -> None:
captured["command"] = command
output_html = Path(command[-1])
_write_unbundled_viewer(output_html.parent, output_html.name)
monkeypatch.setattr(hf_runtime, "save_ply", fake_save_ply)
monkeypatch.setattr(hf_runtime.subprocess, "run", fake_run)
monkeypatch.setattr(hf_runtime, "patch_supersplat_html_auto_rotate", lambda _: None)
monkeypatch.setattr(
hf_runtime,
"patch_supersplat_html_viewer_bridge",
lambda *_, **__: None,
)
template = prepare_viewer_template(
output_root=tmp_path,
command_prefix=["splat-transform"],
)
assert "--unbundled" in captured["command"]
assert template.viewer_html.name == "template.html"
assert template.viewer_assets_dir.parent == tmp_path / "_viewer_assets"
assert "../_viewer_assets/" in template.viewer_html.read_text()
def test_install_shared_viewer_assets_uses_content_hash(tmp_path) -> None:
request_dir = tmp_path / "request"
request_dir.mkdir()
viewer_html = _write_unbundled_viewer(request_dir)
shared_dir = install_shared_viewer_assets(viewer_html)
source = viewer_html.read_text()
assert shared_dir.parent == tmp_path / "_viewer_assets"
assert len(shared_dir.name) == 16
assert (shared_dir / "index.js").read_text() == "const main = true;"
assert f"../_viewer_assets/{shared_dir.name}/index.js" in source
def test_export_gaussian_artifact_builds_filtered_downloads(
tmp_path, monkeypatch
) -> None:
artifact_path = tmp_path / "gaussians.pt"
expected = _gaussians()
save_gaussian_artifact(
GaussianArtifact(
gaussians=expected,
focal_length_px=1200.0,
image_shape=(1152, 1536),
),
artifact_path,
)
captured = {}
template_html = _write_unbundled_viewer(
tmp_path / "_viewer_template",
"template.html",
)
viewer_template = ViewerTemplate(
viewer_html=template_html,
viewer_assets_dir=install_shared_viewer_assets(template_html),
)
def fake_save_ply(*, gaussians, path, **_) -> None:
captured["gaussians"] = gaussians
path.write_bytes(b"ply")
def fake_run(command, **_) -> None:
captured["command"] = command
Path(command[-1]).write_bytes(b"scene")
monkeypatch.setattr(hf_runtime, "save_ply", fake_save_ply)
monkeypatch.setattr(hf_runtime.subprocess, "run", fake_run)
exported = hf_runtime.export_gaussian_artifact(
artifact_path=artifact_path,
output_dir=tmp_path / "output",
viewer_template=viewer_template,
command_prefix=["splat-transform"],
)
actual = captured["gaussians"]
assert actual.mean_vectors.shape == expected.mean_vectors.shape
for expected_tensor, actual_tensor in zip(expected, actual):
assert torch.equal(expected_tensor, actual_tensor)
assert "--unbundled" not in captured["command"]
assert captured["command"][-1] == str(exported.scene_sog)
assert exported.scene_sog.read_bytes() == b"scene"
assert exported.viewer_html.is_file()
assert 'fetch("viewer.sog")' in exported.viewer_html.read_text()
assert not (exported.viewer_html.parent / "index.js").exists()
assert exported.standalone_html.is_file()
assert "data:application/octet-stream;base64,c2NlbmU=" in exported.standalone_html.read_text()
def test_export_ply_result_exposes_ply_before_viewer(tmp_path, monkeypatch) -> None:
internal_artifact = tmp_path / "gaussians.pt"
scene_ply = tmp_path / "scene.ply"
for path in (internal_artifact, scene_ply):
path.write_bytes(b"test")
monkeypatch.setattr(
hf_ui,
"export_filtered_gaussian_ply",
lambda **_: scene_ply,
)
result = hf_ui.export_ply_result(str(internal_artifact))
assert not internal_artifact.exists()
assert scene_ply.exists()
assert "PLY ready" in result[0]
assert result[1]["label"] == "Download PLY - Ready"
assert result[1]["value"] == str(scene_ply)
assert result[1]["interactive"] is True
assert result[1]["elem_classes"] == ["artifact-button", "artifact-ready"]
assert result[2] == str(scene_ply)
def test_export_viewer_result_starts_iframe_before_html(tmp_path, monkeypatch) -> None:
scene_ply = tmp_path / "scene.ply"
scene_sog = tmp_path / "viewer.sog"
viewer_html = tmp_path / "viewer.html"
scene_sog.write_bytes(b"sog")
viewer_html.write_text("<html></html>")
template = ViewerTemplate(
viewer_html=tmp_path / "template.html",
viewer_assets_dir=tmp_path / "assets",
)
monkeypatch.setattr(hf_ui, "_viewer_template", template)
monkeypatch.setattr(
hf_ui,
"export_browser_viewer",
lambda **_: BrowserViewerArtifacts(
scene_sog=scene_sog,
viewer_html=viewer_html,
),
)
result = hf_ui.export_viewer_result(str(scene_ply))
assert "Initializing 3D viewer" in result[0]
assert result[1] == str(viewer_html)
def test_export_html_result_returns_native_download(tmp_path, monkeypatch) -> None:
viewer_html = tmp_path / "viewer.html"
standalone_html = tmp_path / "scene.html"
standalone_html.write_text("<html></html>")
template = ViewerTemplate(
viewer_html=tmp_path / "template.html",
viewer_assets_dir=tmp_path / "assets",
)
monkeypatch.setattr(hf_ui, "_viewer_template", template)
monkeypatch.setattr(
hf_ui,
"export_standalone_viewer",
lambda **_: standalone_html,
)
result = hf_ui.export_html_result(str(viewer_html))
assert result["label"] == "Download HTML viewer - Ready"
assert result["value"] == str(standalone_html)
assert result["interactive"] is True
assert result["elem_classes"] == ["artifact-button", "artifact-ready"]
def test_begin_reconstruction_resets_download_states() -> None:
result = hf_ui.begin_reconstruction()
assert "Reconstructing scene" in result[0]
assert result[1]["label"] == "Preparing PLY..."
assert result[1]["value"] is None
assert result[1]["interactive"] is False
assert result[2] is None
assert result[3]["label"] == "Preparing HTML viewer..."
assert result[3]["value"] is None
assert result[3]["interactive"] is False
assert result[4] is None
def test_demo_uses_native_staged_downloads(tmp_path, monkeypatch) -> None:
template = ViewerTemplate(
viewer_html=tmp_path / "template.html",
viewer_assets_dir=tmp_path / "assets",
)
monkeypatch.setattr(hf_ui, "prepare_viewer_template", lambda _: template)
monkeypatch.setattr(hf_ui, "configure_runtime", lambda *_: None)
demo = hf_ui.create_demo(object())
config = demo.get_config_file()
download_labels = {
component["props"].get("label"): component
for component in config["components"]
if component["type"] == "downloadbutton"
}
function_names = [
block.fn.__name__
for block in demo.fns.values()
if block.fn is not None
]
assert "Download PLY" in download_labels
assert "Download HTML viewer" in download_labels
assert function_names.index("export_ply_result") < function_names.index("export_viewer_result")
assert function_names.index("export_viewer_result") < function_names.index("export_html_result")
def test_app_imports_spaces_before_model_modules() -> None:
tree = ast.parse(Path("app.py").read_text())
imported_modules = []
for node in tree.body:
if isinstance(node, ast.Import):
imported_modules.extend(alias.name for alias in node.names)
elif isinstance(node, ast.ImportFrom) and node.module:
imported_modules.append(node.module)
assert imported_modules.index("spaces") < imported_modules.index("src.demo.hf_runtime")
|