File size: 3,234 Bytes
38c016b 486eff6 44d22f1 38c016b 44d22f1 38c016b 44d22f1 38c016b 1cecbce 38c016b 1cecbce 38c016b 44d22f1 38c016b 9cd7541 44d22f1 b0d8e46 4b3475b b0d8e46 44d22f1 38c016b 486eff6 38c016b 44d22f1 38c016b b0d8e46 38c016b 44d22f1 38c016b 44d22f1 38c016b b0d8e46 9cd7541 38c016b 486eff6 44d22f1 38c016b 486eff6 |
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 |
# Multi-stage build to optimize performance and security
# Stage 1: Builder - Install dependencies and build as root
FROM python:3.11-slim AS builder
# Set working directory
WORKDIR /app
# Install system dependencies for sentence-transformers and FAISS
RUN apt-get update && apt-get install -y \
gcc \
g++ \
wget \
curl \
git \
&& rm -rf /var/lib/apt/lists/*
# Install Node.js for frontend build
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
apt-get install -y nodejs
# Copy frontend package files and install dependencies first (for better caching)
COPY crossword-app/frontend/package*.json ./frontend/
RUN cd frontend && npm ci
# Copy Python backend requirements and install dependencies
COPY crossword-app/backend-py/requirements.txt ./backend-py/
#COPY crossword-app/backend-py/requirements-dev.txt ./backend-py/
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 && \
pip install --no-cache-dir -r backend-py/requirements.txt
# Copy all source code
COPY crossword-app/frontend/ ./frontend/
COPY crossword-app/backend-py/ ./backend-py/
COPY crossword-app/words/ ./backend-py/words/
# Copy cache directory with pre-built models and NLTK data
COPY cache-dir/ ./cache-dir/
RUN chmod -R 755 ./cache-dir/ || true
# Build the React frontend
RUN cd frontend && npm run build
# Copy built frontend files to Python backend public directory
RUN mkdir -p backend-py/public && cp -r frontend/dist/* backend-py/public/
# Stage 2: Runtime - Copy only necessary files as non-root user
FROM python:3.11-slim AS runtime
# Copy Python packages from builder stage
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin
# Install minimal runtime dependencies
RUN apt-get update && apt-get install -y \
curl \
&& rm -rf /var/lib/apt/lists/*
# Create non-root user
RUN useradd -m -u 1000 appuser
# Set working directory
WORKDIR /app/backend-py
# Copy built application files with correct ownership
COPY --from=builder --chown=appuser:appuser /app/backend-py ./
# Copy cache directory to backend-py/cache for model and NLTK data
COPY --from=builder --chown=appuser:appuser /app/cache-dir /app/backend-py/cache
# Switch to non-root user
USER appuser
# Expose port 7860 (Hugging Face Spaces standard)
EXPOSE 7860
# Set environment variables for production
ENV NODE_ENV=production
ENV PORT=7860
ENV PYTHONPATH=/app/backend-py
ENV PYTHONUNBUFFERED=1
ENV PIP_NO_CACHE_DIR=1
# Set cache directory for thematic service and NLTK
ENV CACHE_DIR=/app/backend-py/cache
ENV NLTK_DATA=/app/backend-py/cache/nltk_data
# Set vocabulary source and path for Norvig vocabulary
ENV VOCAB_SOURCE=norvig
ENV NORVIG_VOCAB_PATH=/app/backend-py/words/norvig/count_1w100k.txt
# Health check
# HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
# CMD curl -f http://localhost:7860/health || exit 1
# Start the Python backend server with uvicorn for better production performance
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]
|