File size: 1,514 Bytes
1cd56b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Use Python 3.10 Slim image for a smaller footprint
FROM python:3.10-slim

# Set working directory
WORKDIR /app

# Install system dependencies (needed for some Python packages like lxml)
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libxml2-dev \
    libxslt-dev \
    git-lfs \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements first to leverage Docker cache
COPY requirements-hf.txt requirements.txt

# Install Python dependencies
# Use --no-cache-dir to keep image size down
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code
# We copy the 'backend' and 'brain' folders to the root of the container
COPY backend/ backend/
COPY brain/ brain/
COPY models/ models/

# Create a non-root user for security (Required by Hugging Face Spaces)
RUN useradd -m -u 1000 user && chown -R user:user /app
USER user
ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH

# Set PYTHONPATH so backend/app.py can import 'brain'
ENV PYTHONPATH=/app

# Expose the port that Hugging Face Spaces uses (7860)
EXPOSE 7860

# Run the application using Gunicorn (Production Server)
# We need to install gunicorn first if it's not in requirements.txt
RUN pip install gunicorn

# Command to run the app
# -w 1: 1 worker (limited resources on free tier)
# -b 0.0.0.0:7860: Bind to all interfaces on port 7860
# --timeout 120: Increase timeout for ML model loading
CMD ["gunicorn", "-w", "1", "-b", "0.0.0.0:7860", "--timeout", "300", "backend.app:app"]