ThesisProject / Dockerfile
JeyBii's picture
Upload folder using huggingface_hub
2b9b5b5 verified
Raw
History Blame Contribute Delete
1.65 kB
# Use the official Python base image
FROM python:3.10-slim
# Set the working directory for root installations
WORKDIR /setup
# Copy requirements first to leverage Docker cache
COPY ./requirements.txt /setup/requirements.txt
# Install python packages (single RUN to reduce layers)
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r /setup/requirements.txt
# Pre-download the MiniLM model during BUILD so it's baked into the image.
# This saves ~2-3 minutes on every cold start.
ENV HF_HOME=/setup/hf_cache
RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')"
# Hugging Face Spaces require running as a non-root user for security.
# Set up a new user named "user" with user ID 1000
RUN useradd -m -u 1000 user
# Move the cached model to the user's home so it's accessible after user switch
RUN mkdir -p /home/user/.cache && \
cp -r /setup/hf_cache /home/user/.cache/huggingface && \
chown -R user:user /home/user/.cache
# Switch to the "user" user
USER user
# Set home to the user's home directory
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH \
HF_HOME=/home/user/.cache/huggingface
# Set the working directory to the user's home directory
WORKDIR $HOME/app
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
COPY --chown=user . $HOME/app
# Make the startup script executable
RUN chmod +x start.sh
# HuggingFace Spaces requires port 7860
ENV PORT=7860
EXPOSE 7860
# Startup: auto-train models if missing, then start Uvicorn
CMD ["bash", "start.sh"]