FROM python:3.11-slim

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
    libgl1-mesa-glx \
    libjemalloc-dev \
    libmagic1 \
    libglib2.0-0 \
    poppler-utils \
    tesseract-ocr

RUN apt-get update && apt-get install -y gnupg wget 
RUN wget -qO - https://repositories.intel.com/gpu/intel-graphics.key | \
    gpg --yes --dearmor --output /usr/share/keyrings/intel-graphics.gpg
RUN echo "deb [arch=amd64,i386 signed-by=/usr/share/keyrings/intel-graphics.gpg] https://repositories.intel.com/gpu/ubuntu jammy client" | \
    tee /etc/apt/sources.list.d/intel-gpu-jammy.list
RUN apt-get update
RUN apt-get install -y \
    intel-opencl-icd intel-level-zero-gpu level-zero intel-level-zero-gpu-raytracing \
    intel-media-va-driver-non-free libmfx1 libmfxgen1 libvpl2 \
    libegl-mesa0 libegl1-mesa libegl1-mesa-dev libgbm1 libgl1-mesa-dev libgl1-mesa-dri \
    libglapi-mesa libgles2-mesa-dev libglx-mesa0 libigdgmm12 libxatracker2 mesa-va-drivers \
    mesa-vdpau-drivers mesa-vulkan-drivers va-driver-all vainfo hwinfo clinfo 

RUN useradd -m -s /bin/bash user && \
    mkdir -p /home/user && \
    chown -R user /home/user/ 

RUN mkdir /templates && \
    chown -R user /templates
COPY ./edgecraftrag/prompt_template/default_prompt.txt /templates/
RUN chown -R user /templates/default_prompt.txt

COPY ./edgecraftrag /home/user/edgecraftrag

RUN mkdir -p /home/user/gradio_cache 
ENV GRADIO_TEMP_DIR=/home/user/gradio_cache

WORKDIR /home/user/edgecraftrag
RUN pip install --no-cache-dir -r requirements.txt

WORKDIR /home/user/

USER user

ENTRYPOINT ["python", "-m", "edgecraftrag.server"]
