Spaces:
Sleeping
Sleeping
### ------ | |
### The builder image, used to build the virtual environment | |
### ------ | |
# Start with a minimal Python 3.11 base image based on Debian Buster | |
FROM python:3.11-slim-buster | |
### ------ | |
### Set up user | |
### ------ | |
RUN useradd -m -u 1000 user | |
### ------ | |
### Set up Python environment variables | |
### ------ | |
# Ensures Python output is sent directly to the terminal (no buffering) | |
ENV PYTHONUNBUFFERED=1 \ | |
# Prevents Python from writing .pyc files (compiled bytecode) | |
PYTHONDONTWRITEBYTECODE=1 \ | |
### pip configuration to optimize the installation process | |
# Do not cache pip packages | |
PIP_NO_CACHE_DIR=off \ | |
# Disable pip version checking | |
PIP_DISABLE_PIP_VERSION_CHECK=on \ | |
# Set a longer timeout for pip commands (100 seconds) | |
PIP_DEFAULT_TIMEOUT=100 \ | |
### poetry configuration to install and manage dependencies | |
# Set the version of Poetry to use | |
POETRY_VERSION=1.4.2 \ | |
# Define the location where Poetry will be installed | |
POETRY_HOME="/opt/poetry" \ | |
#POETRY_HOME="/home/user/.local" \ | |
# Create the virtual environment inside the project folder | |
POETRY_VIRTUALENVS_IN_PROJECT=true \ | |
# Ensure Poetry creates virtual environments for the project | |
POETRY_VIRTUALENVS_CREATE=1 \ | |
# Prevent Poetry from asking for interactive input during install | |
POETRY_NO_INTERACTION=1 \ | |
### Define paths for the virtual environment and Python setup | |
# Define where Python setup files will reside | |
# Define where the virtual environment will be created | |
PYSETUP_PATH="/opt/pysetup" \ | |
#PYSETUP_PATH="/opt/pysetup" \ | |
VENV_PATH="/opt/pysetup/.venv" \ | |
# PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH" \ | |
### Hugging Face | |
# Set Hugging Face to offline mode to avoid network access | |
HF_HUB_OFFLINE=0 \ | |
NLTK_DATA="/home/user/nltk/" \ | |
# Store files | |
DATA_DIR="/app/data/" \ | |
# Set the custom Poetry configuration directory | |
POETRY_CONFIG_DIR="$POETRY_HOME/.config/pypoetry" \ | |
PASSLIB_BUILTIN_BCRYPT="enabled" | |
# Add Poetry and virtual environment to the system PATH (Note add after declared ENV) | |
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH" | |
### ------ | |
### Package | |
### ------ | |
# Update package lists and install required build dependencies | |
RUN apt-get update \ | |
&& apt-get install --no-install-recommends -y \ | |
# Install curl to download files from the internet | |
curl \ | |
# Install essential build tools (for building Poetry and other packages) | |
build-essential \ | |
wget \ | |
git \ | |
openssh-client \ | |
&& curl -sSL https://install.python-poetry.org | python3 - \ | |
# Remove build tools after installation to reduce image size | |
&& apt-get purge -y --auto-remove build-essential \ | |
# Clean up apt cache to minimize image size | |
&& apt-get clean \ | |
# Remove leftover lists to further reduce image size | |
&& rm -rf /var/lib/apt/lists/* | |
### ------ | |
### Create DIRs | |
### ------ | |
WORKDIR /app | |
WORKDIR /repo | |
### ------ | |
### Git Clone secret | |
### ------ | |
RUN --mount=type=secret,id=TAG,mode=0444,required=true \ | |
--mount=type=secret,id=URL,mode=0444,required=true \ | |
git clone --branch $(cat /run/secrets/TAG) $(cat /run/secrets/URL) /repo | |
### ------ | |
### Copy file | |
### ------ | |
RUN cp -r /repo/api_writer/* /app/ | |
RUN cp -r /repo/gary /app/gary | |
RUN rm -rf /repo | |
### ------ | |
### Modif path of Lib | |
### ------ | |
WORKDIR /app | |
RUN sed -i 's/\.\.\/gary/gary/g' pyproject.toml | |
### ------ | |
### Install project dependencies using Poetry | |
### ------ | |
# Move to gary and install need optional lib | |
WORKDIR /app/gary | |
# Install only the dependencies needed (Mango and pymupdf) | |
RUN poetry install --no-interaction --with mangodb --with pymupdf \ | |
# Clean up the Poetry cache to reduce image size | |
&& rm -rf $POETRY_CACHE_DIR | |
WORKDIR /app | |
# Optimize Poetry's performance with more parallel workers | |
RUN poetry config installer.max-workers 10 \ | |
# Install only the dependencies needed | |
&& poetry install --no-interaction --no-dev \ | |
# Clean up the Poetry cache to reduce image size | |
&& rm -rf $POETRY_CACHE_DIR | |
# Create the custom Poetry configuration directory and set permissions | |
RUN mkdir -p $POETRY_CONFIG_DIR && \ | |
# Create the NTLK folder (llama) | |
mkdir -p $NLTK_DATA && \ | |
# Create the folder store files | |
mkdir -p $DATA_DIR && \ | |
chown -R user:user $POETRY_CONFIG_DIR && \ | |
chown -R user:user $NLTK_DATA && \ | |
chown -R user:user $DATA_DIR && \ | |
chmod -R 777 $DATA_DIR | |
### ------ | |
### Command to run | |
### ------ | |
# Expose the port FastAPI will run on | |
EXPOSE 7860 | |
# This command starts the API using Poetry's virtual environment | |
CMD ["poetry", "run", "api"] | |