|
|
"""Entry point for Hugging Face Spaces ZeroGPU deployment. |
|
|
|
|
|
This module serves as the main entry point when deploying Mosaic to |
|
|
Hugging Face Spaces using ZeroGPU (Gradio SDK). It installs dependencies |
|
|
at runtime and launches the Gradio interface. |
|
|
|
|
|
NOTE: This is NOT used for Docker SDK deployments. For Docker deployments, |
|
|
the Dockerfile handles all dependency installation and uses the 'mosaic' |
|
|
CLI command as the entrypoint. |
|
|
|
|
|
Runtime dependency installation (via pip) is required for ZeroGPU deployment |
|
|
because: |
|
|
1. Paladin is a private repository requiring GH_TOKEN authentication |
|
|
2. requirements.txt is processed before the app starts and doesn't have |
|
|
access to secrets |
|
|
3. ZeroGPU Spaces use Gradio SDK which expects runtime installation |
|
|
""" |
|
|
|
|
|
import os |
|
|
import subprocess |
|
|
import sys |
|
|
|
|
|
|
|
|
try: |
|
|
import spaces |
|
|
except ImportError: |
|
|
pass |
|
|
|
|
|
|
|
|
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__": |
|
|
|
|
|
omp_threads = os.environ.get("OMP_NUM_THREADS", "") |
|
|
try: |
|
|
|
|
|
if not omp_threads or int(omp_threads) <= 0: |
|
|
raise ValueError("Invalid OMP_NUM_THREADS") |
|
|
except (ValueError, AttributeError): |
|
|
|
|
|
import multiprocessing |
|
|
|
|
|
num_cpus = multiprocessing.cpu_count() |
|
|
|
|
|
os.environ["OMP_NUM_THREADS"] = str(max(1, min(8, num_cpus // 2))) |
|
|
|
|
|
|
|
|
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1" |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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_and_process_models() |
|
|
|
|
|
|
|
|
|
|
|
launch_gradio( |
|
|
server_name="0.0.0.0", |
|
|
server_port=7860, |
|
|
share=False, |
|
|
) |
|
|
|