File size: 2,755 Bytes
e8a6607
 
 
fe2dc13
 
e8a6607
 
 
 
 
 
 
 
 
fe2dc13
e8a6607
 
 
 
 
 
 
 
 
 
 
 
 
 
fe2dc13
e8a6607
 
 
 
 
 
 
fe2dc13
 
 
 
e8a6607
 
 
 
 
fe2dc13
e8a6607
fe2dc13
 
e8a6607
 
 
 
 
 
 
fe2dc13
e8a6607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe2dc13
 
 
e8a6607
 
 
 
fe2dc13
 
 
 
 
 
 
e8a6607
 
 
 
 
 
 
 
 
 
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
# ==========================================
# Production CPU Runtime Environment
# ==========================================
FROM python:3.12-slim


# Install system dependencies, Redis server, OpenBLAS for CPU matrix math, and Node.js
RUN apt-get update && apt-get install -y \
    curl \
    gnupg \
    build-essential \
    cmake \
    pkg-config \
    git \
    # libopenblas-dev \
    redis-server \
    && curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
    && apt-get install -y nodejs \
    && rm -rf /var/lib/apt/lists/*


# Install PM2 globally to manage background processes
RUN npm install -g pm2

# Setup non-root user for Hugging Face compliance (UID 1000)
RUN useradd -m -u 1000 user
WORKDIR /home/user/app

# Force llama-cpp-python to compile cleanly for CPU optimization
# ENV CMAKE_ARGS="-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS"

# Create cache directory for Hugging Face model downloads and set ownership
ENV HF_HOME=/home/user/app/.cache/huggingface
RUN mkdir -p $HF_HOME

# --- Setup Python Worker ---
COPY requirements.txt .

ENV CMAKE_BUILD_PARALLEL_LEVEL=1

RUN pip install -vvv --no-cache-dir -r requirements.txt
COPY python-ai-service/ ./python-ai-service

# --- Setup Node Backend ---
COPY node-backend/package*.json ./node-backend/
RUN cd node-backend && npm ci --only=production
RUN cd node-backend && npm install typescript
COPY node-backend/ ./node-backend
RUN cd node-backend && npx --package typescript tsc
RUN cd node-backend && npm prune --production

# --- Setup Pre-built Frontend Static Files ---
# Copies directly from your react-frontend/dist folder
COPY react-frontend/dist ./node-backend/public

# Copy startup script
COPY start.sh .
RUN chmod +x /home/user/app/start.sh

# Set environment variables
ENV PORT=7860
ENV NODE_ENV=production
ENV APP_ENV=production
ENV REDIS_URL=redis://127.0.0.1:6379

# Set write/read permissions for the Hugging Face non-root user
RUN chown -R user:user /home/user/app
USER user

# Create a PM2 ecosystem file to launch Redis, Node Backend, and Python Worker
RUN echo 'module.exports = { \
  apps: [ \
    { \
      name: "node-backend", \
      script: "npm", \
      args: ["run", "start"], \
      cwd: "node-backend", \
      env: { PORT: "7860", REDIS_URL: "redis://127.0.0.1:6379" } \
    }, \
    { \
      name: "python-worker", \
      script: "python", \
      args: "main.py", \
      cwd: "python-ai-service", \
      env: { \
        REDIS_URL: "redis://127.0.0.1:6379", \
        PYTHONPATH: "/home/user/app/python-ai-service" \
      } \
    } \
  ] \
};' > ecosystem.config.js

# Expose Hugging Face Space default web port
EXPOSE 7860

# Start Redis, Node, and Python simultaneously
# CMD ["pm2-runtime", "ecosystem.config.js"]
CMD ["./start.sh"]