FROM jupyter/datascience-notebook:hub-3.1.1

# Use root to install packages and modify permissions
USER root

# Copy requirements and environment files to the container
COPY requirements.txt environment.yml /tmp/

# Update conda environment, install pip packages, and adjust permissions
RUN conda env update -q -f /tmp/environment.yml && \
    /opt/conda/bin/pip install --no-cache-dir -r /tmp/requirements.txt && \
    conda clean -y --all && \
    jupyter lab build && \
    python -m nltk.downloader stopwords && \
    python -m nltk.downloader wordnet && \
    python -m spacy download en_core_web_sm

# Fix permissions for the pip cache directory to avoid permission warnings
RUN chown -R jovyan:users /home/jovyan/.cache

# Ensure numpy and scikit-learn are compatible
RUN /opt/conda/bin/pip install --no-cache-dir numpy==1.22.0 scikit-learn==1.2.2

# Copy and install custom packages
COPY dash_proxy /tmp/dash_proxy/
RUN pip install --no-cache-dir /tmp/dash_proxy/

COPY llm_utils /llm_utils/
RUN pip install --no-cache-dir /llm_utils/

# Set environment variable for config path
ENV CONFIG_PATH=/home/jovyan/config.txt

# Copy application files and adjust permissions for the jovyan user
COPY app /dash/app/
RUN chown -R jovyan:users /dash/app/

# Switch back to jovyan user
USER jovyan