File size: 4,041 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
# =============================================================================
# HyperView - Hugging Face Spaces Dockerfile
# =============================================================================
# Multi-stage build for deploying HyperView to HuggingFace Spaces.
#
# Features:
# - CLIP embeddings (Euclidean) via embed-anything
# - HyCoCLIP embeddings (Hyperbolic) via hyper-models ONNX
# - Pre-computed demo dataset (300 CIFAR-10 samples)
# - Torch-free runtime for minimal image size
#
# Deploy: https://huggingface.co/spaces/Hyper3Labs/HyperView
# =============================================================================

# -----------------------------------------------------------------------------
# Stage 1: Build Frontend (Next.js static export)
# -----------------------------------------------------------------------------
FROM node:20-slim AS frontend-builder

WORKDIR /app/frontend

# Install dependencies first (better caching)
COPY frontend/package.json frontend/package-lock.json ./
RUN npm ci --prefer-offline

# Build hyper-scatter (installed from source tarball, dist-lib not prebuilt)
RUN cd node_modules/hyper-scatter \
    && npm install \
    && npm run build:lib

# Copy frontend source and build
COPY frontend/ ./
RUN npm run build

# Verify output exists
RUN ls -la out/ && echo "Frontend build complete"

# -----------------------------------------------------------------------------
# Stage 2: Python Runtime
# -----------------------------------------------------------------------------
FROM python:3.11-slim AS runtime

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    curl \
    git \
    libssl-dev \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*

# HuggingFace Spaces requirement: create user with UID 1000
RUN useradd -m -u 1000 user

# Switch to user
USER user

# Set environment variables
ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH \
    HF_HOME=/home/user/.cache/huggingface \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1

WORKDIR $HOME/app

# Upgrade pip
RUN pip install --upgrade pip

# Copy Python package files
COPY --chown=user pyproject.toml README.md LICENSE ./
COPY --chown=user src/ ./src/
COPY --chown=user scripts/ ./scripts/

# Install Python package (without ML extras - we use ONNX)
RUN pip install -e .

# Copy built frontend to static directory
COPY --from=frontend-builder --chown=user /app/frontend/out ./src/hyperview/server/static/

# Verify frontend is in place
RUN ls -la src/hyperview/server/static/ && echo "Frontend copied successfully"

# -----------------------------------------------------------------------------
# Stage 3: Pre-compute Demo Dataset
# -----------------------------------------------------------------------------
# Create output directories
RUN mkdir -p $HOME/app/demo_data/datasets $HOME/app/demo_data/media

# Set environment for precomputation
ENV HYPERVIEW_DATASETS_DIR=/home/user/app/demo_data/datasets \
    HYPERVIEW_MEDIA_DIR=/home/user/app/demo_data/media \
    DEMO_SAMPLES=300

# Pre-download HuggingFace models and compute embeddings
# This runs during build to ensure fast startup
RUN python scripts/precompute_hf_demo.py

# Verify dataset was created
RUN ls -la demo_data/ && echo "Demo dataset pre-computed successfully"

# -----------------------------------------------------------------------------
# Final Configuration
# -----------------------------------------------------------------------------
# Copy entrypoint
COPY --chown=user app_hf.py ./

# Set runtime environment
ENV HOST=0.0.0.0 \
    PORT=7860 \
    DEMO_DATASET=cifar10_hf_demo \
    HYPERVIEW_DATASETS_DIR=/home/user/app/demo_data/datasets \
    HYPERVIEW_MEDIA_DIR=/home/user/app/demo_data/media

# Expose port (HuggingFace Spaces default)
EXPOSE 7860

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD curl -f http://localhost:7860/__hyperview__/health || exit 1

# Start server
CMD ["python", "app_hf.py"]