File size: 1,259 Bytes
73ddeba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52bf7b0
73ddeba
 
 
 
 
 
52bf7b0
 
 
 
73ddeba
 
 
 
 
 
 
 
 
 
 
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
# Use an official lightweight Python base image
FROM python:3.9-slim

# Set system environment variables
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    HF_HOME=/code/model_cache \
    PORT=7860

# Set working directory
WORKDIR /code

# Copy dependencies first to leverage Docker layer caching
COPY ./requirements.txt /code/requirements.txt

# Install python dependencies
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

# Create cache folder and make it accessible
RUN mkdir -p /code/model_cache

# Pre-download the embedding model at build time to prevent cold-start latency
RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')"

# Set up a non-root user (Hugging Face Spaces requirement)
RUN useradd -m -u 1000 user

# Change ownership of the model cache directory to the non-root user
RUN chown -R user:user /code/model_cache

USER user

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

# Copy application files, changing ownership to the non-root user
COPY --chown=user:user . /code

# Run the FastAPI server on port 7860 (Hugging Face Spaces default port)
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]