File size: 2,176 Bytes
2a074d9
 
 
 
 
 
c5ec981
 
598bc26
 
849bc8d
 
 
 
 
 
8191118
 
c5ec981
fe38b5b
2a074d9
 
 
c96b0f8
 
 
 
 
 
 
 
 
a15a72c
 
 
67d8e2d
 
 
 
 
 
 
 
 
 
 
 
3cf942c
 
c5ec981
 
 
 
 
3cf942c
2a074d9
 
3cf942c
2a074d9
 
 
 
 
 
 
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
"""Entry point for Hugging Face Spaces deployment.

This module serves as the main entry point when deploying Mosaic to
Hugging Face Spaces. It initializes the models and launches the Gradio interface.
"""

import os
import subprocess
import sys

# Import spaces first to avoid CUDA initialization issues
try:
    import spaces
except ImportError:
    pass

# Add src directory to path for Hugging Face Spaces
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))

from mosaic.gradio_app import download_and_process_models
from mosaic.ui import launch_gradio

if __name__ == "__main__":
    # Fix OMP_NUM_THREADS environment variable if invalid
    omp_threads = os.environ.get("OMP_NUM_THREADS", "")
    if not omp_threads or omp_threads == "0" or (omp_threads.lstrip('-').isdigit() and int(omp_threads) <= 0):
        # Set to a reasonable default based on CPU count
        import multiprocessing
        num_cpus = multiprocessing.cpu_count()
        # Use half of available CPUs, minimum 1, maximum 8 for stability
        os.environ["OMP_NUM_THREADS"] = str(max(1, min(8, num_cpus // 2)))
    
    # Enable HF transfer for faster downloads on Spaces
    os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
    
    # Install flash-attn for H100 acceleration (if on Spaces)
    if os.environ.get("SPACE_ID"):
        try:
            subprocess.run(
                "pip install flash-attn --no-build-isolation",
                shell=True,
                check=False,
                capture_output=True,
            )
        except Exception:
            pass  # Flash-attn is optional, continue without it
    
    # Install Paladin from GitHub
    GIT_TOKEN = os.environ.get("GH_TOKEN")
    if GIT_TOKEN:
        subprocess.run(
            f"pip install git+https://{GIT_TOKEN}@github.com/pathology-data-mining/paladin.git@dev",
            shell=True,
        )

    # Download models and initialize cancer subtype mappings
    download_and_process_models()

    # Launch the Gradio interface
    # Use default settings suitable for Hugging Face Spaces
    launch_gradio(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
    )