File size: 2,319 Bytes
c73181c
c9637ca
 
c73181c
 
 
 
 
 
c9637ca
c73181c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9637ca
931edec
c9637ca
 
 
931edec
 
c9637ca
 
 
c73181c
931edec
 
 
 
 
 
 
c9637ca
931edec
c9637ca
c73181c
931edec
 
 
c73181c
931edec
c9637ca
c73181c
 
c9637ca
 
931edec
 
c9637ca
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
# ---------- Stage 1: Build Next.js / React frontend ----------
FROM node:18 AS frontend-builder
WORKDIR /frontend

# Install deps
COPY frontend/package*.json ./
RUN npm install

# Copy source
COPY frontend/ .

# Build (Next.js, CRA, or Vite)
RUN npm run build

# Try Next.js static export if script or config supports it (ignore failure)
RUN npm run export || true

# Normalize build output into /frontend_build
# Priority: out (next export) > dist (vite) > build (CRA) > .next (Next.js server build + public)
RUN set -eux; \
    mkdir -p /frontend_build; \
    if [ -d out ]; then \
        echo "Using Next.js static export (out)"; cp -R out/* /frontend_build/; \
    elif [ -d dist ]; then \
        echo "Using Vite dist"; cp -R dist/* /frontend_build/; \
    elif [ -d build ]; then \
        echo "Using CRA build"; cp -R build/* /frontend_build/; \
    elif [ -d .next ]; then \
        echo "Using Next.js server build (.next) (NOTE: For full static hosting, enable next export)"; \
        cp -R .next /frontend_build/.next; \
        [ -d public ] && cp -R public /frontend_build/public || true; \
    else \
        echo "No recognized frontend build output (out/dist/build/.next) found." >&2; exit 1; \
    fi

# ---------- Stage 2: Backend (Flask + Gunicorn) ----------
FROM python:3.10-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential git wget curl ffmpeg libglib2.0-0 libsm6 libxext6 libxrender1 \
  && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Writable cache locations (avoid /.cache PermissionError)
ENV HF_HOME=/tmp/.cache/huggingface \
    TORCH_HOME=/tmp/.cache/torch \
    TIMM_CACHE_DIR=/tmp/.cache/timm \
    XDG_CACHE_HOME=/tmp/.cache \
    HF_HUB_CACHE=/tmp/.cache/huggingface \
    TRANSFORMERS_CACHE=/tmp/.cache/huggingface \
    NO_ALBUMENTATIONS_UPDATE=1

RUN mkdir -p /tmp/.cache/huggingface /tmp/.cache/torch /tmp/.cache/timm

# Backend deps
COPY backend/requirements.txt ./backend/requirements.txt
RUN pip install --no-cache-dir -r backend/requirements.txt

# Copy backend
COPY backend/ ./backend

# Copy normalized frontend build into static assets
COPY --from=frontend-builder /frontend_build ./backend/static

WORKDIR /app/backend
EXPOSE 7860

CMD ["gunicorn", "app:app", "--bind", "0.0.0.0:7860", "--workers", "2", "--timeout", "180"]