File size: 1,523 Bytes
11016fe
814e44a
11016fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Use the official Python 3.9 image
FROM python:3.9-slim

# Create a non-root user
RUN useradd -m appuser

# Set the working directory inside the container
WORKDIR /app

# Set environment variables for cache
ENV TRANSFORMERS_CACHE=/app/cache/huggingface/transformers
ENV HF_HOME=/app/cache/huggingface
ENV SENTENCE_TRANSFORMERS_HOME=/app/cache/sentence_transformers

# Ensure the cache directory can be written to
RUN mkdir -p /app/cache/huggingface/transformers && \
    mkdir -p /app/cache/sentence_transformers && \
    chown -R appuser:appuser /app/cache

# Copy the requirements file into the container
COPY requirements.txt ./requirements.txt

# Install system dependencies and Python packages
RUN apt-get update && \
    apt-get -y install gcc libpq-dev && \
    pip install --no-cache-dir -r requirements.txt

# Copy the entire project into the container
COPY . /app

# Change ownership of the /app directory to the non-root user
RUN chown -R appuser:appuser /app

# Switch to the non-root user
USER appuser

# Create a script to load the models
RUN echo "from sentence_transformers import SentenceTransformer; \
    SentenceTransformer('sentence-transformers/msmarco-distilbert-base-v3'); \
    SentenceTransformer('sentence-transformers/all-mpnet-base-v2'); \
    SentenceTransformer('sentence-transformers/paraphrase-MiniLM-L6-v2');" > load_models.py

# Run the model loading script
RUN python load_models.py

# Start the application
CMD ["uvicorn", "semantic_search:app", "--host", "0.0.0.0", "--port", "7860"]