File size: 1,500 Bytes
099df87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# === STAGE 1: The Builder (Downloads the model) ===
FROM python:3.10-slim as builder
RUN pip install --no-cache-dir sentence-transformers
ENV MODEL_NAME=sentence-transformers/all-MiniLM-L6-v2
ENV SAVE_PATH=/opt/models/all-MiniLM-L6-v2
RUN python -c "import os; from sentence_transformers import SentenceTransformer; SentenceTransformer(os.getenv('MODEL_NAME')).save(os.getenv('SAVE_PATH'))"

# === STAGE 2: The Final Application Image ===
FROM python:3.11-slim

# Set environment variables for writable cache directories (matching your reference)
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    NLTK_DATA=/tmp/nltk_data \
    TRANSFORMERS_CACHE=/tmp/transformers_cache \
    HF_HOME=/tmp/huggingface \
    PYTHONPATH=/app

# Set work directory
WORKDIR /app

# Install system dependencies (if needed)
RUN apt-get update && apt-get install -y \
    build-essential \
    && rm -rf /var/lib/apt/lists/*

# Create writable cache directories
RUN mkdir -p /tmp/nltk_data /tmp/transformers_cache /tmp/huggingface

# Install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy your application code (exactly like your reference)
COPY main.py .
COPY api/ ./api/

# Copy the pre-downloaded model from the builder stage
COPY --from=builder /opt/models/all-MiniLM-L6-v2 ./models/all-MiniLM-L6-v2

# Expose the port
EXPOSE 7860

# Run the application (exactly like your reference)
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]