File size: 2,382 Bytes
d8cfaa8
a544a50
d8cfaa8
 
a544a50
d8cfaa8
a544a50
d77e99f
1973147
 
d77e99f
 
aff5726
 
 
 
 
 
d8cfaa8
aff5726
de5a0fd
d8cfaa8
 
d77e99f
a544a50
 
 
 
d77e99f
 
 
 
d8cfaa8
 
 
 
 
 
1973147
de5a0fd
 
 
 
 
 
 
 
 
 
 
08c3363
a544a50
 
 
 
 
 
de5a0fd
aff5726
a544a50
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
"""Alternative entry point for local development.

NOTE: HuggingFace Spaces Docker deployment uses `python -m stroke_deepisles_demo.ui.app`
(see Dockerfile CMD). This file is for local development convenience only.

For HF Spaces deployment, see: src/stroke_deepisles_demo/ui/app.py
"""

from pathlib import Path

import gradio as gr

# CRITICAL: Allow direct file serving for local assets (niivue.js)
# Must be called BEFORE creating any Blocks
_ASSETS_DIR = Path(__file__).parent / "src" / "stroke_deepisles_demo" / "ui" / "assets"
gr.set_static_paths(paths=[str(_ASSETS_DIR)])

from stroke_deepisles_demo.core.config import get_settings  # noqa: E402
from stroke_deepisles_demo.core.logging import get_logger, setup_logging  # noqa: E402
from stroke_deepisles_demo.ui.app import get_demo  # noqa: E402
from stroke_deepisles_demo.ui.viewer import get_niivue_head_html  # noqa: E402

logger = get_logger(__name__)

# Initialize logging
settings = get_settings()
setup_logging(settings.log_level, format_style=settings.log_format)

# Create the demo instance at module level for Gradio
demo = get_demo()

if __name__ == "__main__":
    # Log startup info for debugging
    logger.info("=" * 60)
    logger.info("STARTUP: stroke-deepisles-demo (root app.py)")
    logger.info("Assets directory: %s", _ASSETS_DIR.resolve())
    logger.info("Assets exists: %s", _ASSETS_DIR.exists())
    logger.info("=" * 60)

    # CRITICAL FIX (Issue #24): Load NiiVue via head= parameter
    #
    # The head= parameter injects a <script type="module"> into <head> that loads
    # NiiVue BEFORE Gradio's Svelte app hydrates. This is critical because:
    #
    # 1. Dynamic import() inside js_on_load blocks Svelte hydration on HF Spaces
    # 2. head= scripts run BEFORE Gradio mounts, so failures don't block the app
    # 3. js_on_load then just USES window.Niivue (no imports)
    #
    # Evidence: A/B test in docs/specs/24-bug-hf-spaces-loading-forever.md showed
    # disabling js_on_load makes the app load. The fix is head= for loading.

    demo.launch(
        server_name=settings.gradio_server_name,
        server_port=settings.gradio_server_port,
        share=settings.gradio_share,
        theme=gr.themes.Soft(),
        css="footer {visibility: hidden}",
        head=get_niivue_head_html(),  # Load NiiVue before Gradio hydrates
        allowed_paths=[str(_ASSETS_DIR)],
    )