File size: 2,549 Bytes
8f4d405
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea87e33
 
 
 
 
 
 
67c580c
ea87e33
67c580c
 
 
ea87e33
 
67c580c
8f4d405
 
 
 
 
 
 
 
 
 
ea87e33
 
 
 
8f4d405
 
 
 
 
 
79ea999
 
 
1d056ab
 
79ea999
 
67c580c
 
 
8f4d405
 
 
 
 
79ea999
 
8f4d405
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
# Dockerfile for Hugging Face Spaces
# Based on HF Spaces Docker SDK documentation: https://huggingface.co/docs/hub/spaces-sdks-docker

FROM python:3.10-slim

# Set working directory
WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    gcc \
    g++ \
    cmake \
    libopenblas-dev \
    libomp-dev \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Create app user with UID 1000 (fixes getpwuid(): uid not found: 1000 error)
# Note: Hugging Face Spaces may run as root, but creating user prevents errors
# Use || true to allow graceful failure if user already exists
RUN (useradd -u 1000 -m -s /bin/bash appuser 2>/dev/null) || \
    (groupadd -g 1000 appuser 2>/dev/null && useradd -u 1000 -g appuser -m -s /bin/bash appuser 2>/dev/null) || \
    echo "User creation skipped (may already exist)"

# Create cache directories with proper permissions
# Hugging Face Spaces runs as root, but we ensure appuser can access
RUN mkdir -p /tmp/huggingface_cache && \
    chmod 777 /tmp/huggingface_cache && \
    mkdir -p /tmp/logs && \
    chmod 777 /tmp/logs && \
    (chown -R appuser:appuser /tmp/huggingface_cache /tmp/logs 2>/dev/null || true)

# Copy requirements file first (for better caching)
COPY requirements.txt .

# Install Python dependencies
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Set ownership of application files (if running as root, this ensures appuser can access)
# Use || true to allow graceful failure if chown not needed
RUN chown -R appuser:appuser /app 2>/dev/null || true

# Expose port 7860 (HF Spaces standard)
EXPOSE 7860

# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV PORT=7860
# Set OMP_NUM_THREADS to valid integer (not empty string)
ENV OMP_NUM_THREADS=4
ENV MKL_NUM_THREADS=4
ENV DB_PATH=/tmp/sessions.db
ENV FAISS_INDEX_PATH=/tmp/embeddings.faiss
ENV LOG_DIR=/tmp/logs
ENV RATE_LIMIT_ENABLED=true
# Cache directories - will be used by transformers and huggingface_hub
ENV HF_HOME=/tmp/huggingface_cache
ENV TRANSFORMERS_CACHE=/tmp/huggingface_cache

# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=120s --retries=3 \
    CMD curl -f http://localhost:7860/api/health || exit 1

# Run with Gunicorn production WSGI server (replaces Flask dev server)
CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "4", "--threads", "2", "--timeout", "120", "--access-logfile", "-", "--error-logfile", "-", "--log-level", "info", "flask_api_standalone:app"]