File size: 3,003 Bytes
a9ad71d
28ba0c9
a9ad71d
28ba0c9
 
 
a9ad71d
 
28ba0c9
eef1d74
28ba0c9
eef1d74
a9ad71d
 
28ba0c9
eef1d74
 
 
a9ad71d
28ba0c9
 
 
eef1d74
28ba0c9
 
 
 
eef1d74
 
a9ad71d
28ba0c9
 
 
 
 
 
a9ad71d
28ba0c9
e534831
28ba0c9
e534831
 
 
 
 
a9ad71d
28ba0c9
 
 
 
a9ad71d
28ba0c9
 
 
a9ad71d
28ba0c9
a9ad71d
28ba0c9
 
 
 
 
d510a4f
e534831
 
 
 
28ba0c9
 
d510a4f
28ba0c9
 
 
 
d510a4f
28ba0c9
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
# ============================================================================
# Necthrall - Optimized Dockerfile for Hugging Face Spaces (Docker SDK)
# ============================================================================
# - Base: python:3.11-slim
# - Pre-downloads sentence-transformers/all-MiniLM-L6-v2
# - Runs as non-root user (UID 1000) on port 7860
# ============================================================================

FROM python:3.11-slim

# Environment variables for Python and ML optimization
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    OMP_NUM_THREADS=1 \
    MKL_NUM_THREADS=1 \
    TOKENIZERS_PARALLELISM=false

# ============================================================================
# STEP 1: Install system dependencies
# ============================================================================
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        gcc \
        g++ \
        ca-certificates && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# ============================================================================
# STEP 2: Install Python dependencies
# ============================================================================
COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt && \
    rm /tmp/requirements.txt

# ============================================================================
# STEP 3: Pre-download and convert embedding model to ONNX (cached in image layer)
# ============================================================================
# Copy the setup script first
COPY scripts/setup_onnx.py /tmp/setup_onnx.py

# Run the ONNX setup script to download and quantize the model
RUN python /tmp/setup_onnx.py && rm /tmp/setup_onnx.py

# ============================================================================
# STEP 4: Create non-root user (HF Spaces requirement: UID 1000)
# ============================================================================
RUN useradd -m -u 1000 user

# Set home and working directory
ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH

WORKDIR $HOME/app

# ============================================================================
# STEP 5: Copy application code
# ============================================================================
# Copy with ownership set to user
COPY --chown=user:user . $HOME/app

# Copy the pre-built ONNX model cache to the app directory
RUN cp -r /onnx_model_cache $HOME/app/onnx_model_cache && \
    chown -R user:user $HOME/app/onnx_model_cache

# Switch to non-root user
USER user

# ============================================================================
# STEP 6: Expose port and run application
# ============================================================================
EXPOSE 7860

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]