File size: 2,769 Bytes
95967ab
1ee2bb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef8bb5f
 
 
 
 
1ee2bb3
 
 
 
 
 
 
 
 
 
 
66f5aac
 
 
 
 
 
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
# QuDA Studio — two-stage image.
#   stage 1 (node): vite build of the React frontend
#   stage 2 (python): backend deps + qlib + the built frontend/dist
#
# Targets HuggingFace Spaces (sdk: docker, app_port: 7860).

# ---------- Stage 1: build the frontend ----------
FROM node:20-bookworm-slim AS frontend

WORKDIR /build

# Install deps first for better layer caching.
COPY frontend/package.json frontend/package-lock.json* frontend/pnpm-lock.yaml* ./
RUN if [ -f pnpm-lock.yaml ]; then \
        corepack enable && corepack prepare pnpm@latest --activate && pnpm install --frozen-lockfile; \
    elif [ -f package-lock.json ]; then \
        npm ci; \
    else \
        npm install; \
    fi

COPY frontend/ ./

# Static base path so it works at the site root on HF.
ENV VITE_API_BASE=/api
RUN if [ -f pnpm-lock.yaml ]; then pnpm run build; else npm run build; fi


# ---------- Stage 2: runtime ----------
FROM python:3.11-slim-bookworm AS runtime

# HF Spaces user setup — the user inside the container must own /home/user.
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    APP_HOME=/home/user/app

RUN apt-get update \
    && apt-get install -y --no-install-recommends build-essential curl \
    && rm -rf /var/lib/apt/lists/* \
    && useradd --create-home --uid 1000 user

WORKDIR ${APP_HOME}

# Install torch first from the CPU-only wheel index (~200 MB vs 2 GB for CUDA).
RUN pip install --index-url https://download.pytorch.org/whl/cpu torch==2.5.1

COPY --chown=user:user backend/requirements.txt ./backend/requirements.txt
RUN pip install -r backend/requirements.txt

# App code
COPY --chown=user:user backend/ ./backend/

# Prebuilt frontend bundle from stage 1
COPY --chown=user:user --from=frontend /build/dist ./frontend/dist

# Example plugins (small .zip files) — served read-only at
# /api/plugins/examples so users can grab one to test the upload flow
# without writing a plugin from scratch.
COPY --chown=user:user example_plugins/ ./example_plugins/

# Clean caches the user can't write at runtime (HF runs as non-root).
RUN chown -R user:user ${APP_HOME}

USER user

# HF Spaces expects 7860
EXPOSE 7860
ENV PORT=7860

# FastAPI app lives in backend/app/main.py
WORKDIR ${APP_HOME}/backend
# --proxy-headers + --forwarded-allow-ips='*' so FastAPI trusts HF Space's
# edge nginx when it sets X-Forwarded-Proto: https. Without this, FastAPI's
# automatic trailing-slash redirects and any request.url_for() calls come
# back as http:// and get mixed-content-blocked by the browser on an https
# page. HF's reverse proxy IP isn't stable, so we accept any.
CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT} --proxy-headers --forwarded-allow-ips=*"]