# Use NVIDIA's CUDA base image with Python
FROM nvidia/cuda:11.8.0-runtime-ubuntu20.04

# Set environment variables
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
ENV PATH="/home/user/.local/bin:$PATH"

# Install Python, pip, and other dependencies
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip \
    python3-dev \
    libgl1-mesa-glx \
    libglib2.0-0 \  # Required for GLib
    && apt-get clean && rm -rf /var/lib/apt/lists/*

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

# Set the user to the non-root user
USER user

# Set the working directory to /app
WORKDIR /app

# Copy the requirements.txt file and install dependencies
COPY --chown=user requirements.txt /app/requirements.txt
RUN pip3 install --no-cache-dir --upgrade -r /app/requirements.txt

# Copy the application code into the container
COPY --chown=user . /app

# Create the folders for uploads and results with appropriate permissions
RUN mkdir -p /app/static/uploads /app/static/results && \
    chmod -R 755 /app/static/uploads /app/static/results

# Expose the port the app will run on
EXPOSE 7860

# Run the Flask app using uvicorn
CMD ["python3", "-m", "uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]