FROM nvidia/cuda:11.7.1-cudnn8-devel-ubuntu22.04

RUN apt update \
  && apt-key adv --fetch-keys \
  && apt install --no-install-recommends -y build-essential wget git curl unzip python3 python3-pip python3-dev libgl1 libglib2.0-0 \
  && apt clean && rm -rf /var/lib/apt/lists/* /var/tmp/* /tmp/* /var/log/*

RUN useradd -ms /bin/bash sd
USER sd

RUN pip install --no-cache-dir torch==1.12.1+cu113 torchvision==0.13.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113

RUN mkdir /home/sd/stable-diffusion-webui
WORKDIR /home/sd/stable-diffusion-webui
RUN mkdir repositories && \

  git clone https://github.com/CompVis/stable-diffusion.git repositories/stable-diffusion && \
  cd repositories/stable-diffusion && git checkout 69ae4b35e0a0f6ee1af8bb9a5d0016ccb27e36dc && \
  python3 setup.py install --user && \
  cd ../.. && \

  git clone https://github.com/CompVis/taming-transformers.git repositories/taming-transformers && \
  cd repositories/taming-transformers && git checkout 24268930bf1dce879235a7fddd0b2355b84d7ea6 && \
  cd ../.. && \

  git clone https://github.com/sczhou/CodeFormer.git repositories/CodeFormer && \
  cd repositories/CodeFormer && git checkout c5b4593074ba6214284d6acd5f1719b6c5d739af && \
  cd ../.. && \

  git clone https://github.com/salesforce/BLIP.git repositories/BLIP && \
  cd repositories/BLIP && git checkout 48211a1594f1321b00f14c9f7a5b4813144b2fb9 && \
  cd ../.. && \

  git clone https://github.com/crowsonkb/k-diffusion.git repositories/k-diffusion && \
  cd repositories/k-diffusion && git checkout f4e99857772fc3a126ba886aadf795a332774878 && \
  cd ../.. && \

  git clone https://github.com/Hafiidz/latent-diffusion repositories/latent-diffusion && \
  pip install -r repositories/latent-diffusion/requirements.txt --prefer-binary && \

  # update numpy to latest version
  pip install --no-cache-dir -U numpy  --prefer-binary && \

  pip install --no-cache-dir \

    # install requirements of Stable Diffusion
    transformers==4.19.2 diffusers invisible-watermark triton \

    # install k-diffusion
    git+https://github.com/crowsonkb/k-diffusion.git \

    # (optional) install GFPGAN (face restoration)
    git+https://github.com/TencentARC/GFPGAN.git@8d2447a2d918f8eba5a4a01463fd48e45126a379 \

    # install CLIP
    git+https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1 \

    # install xformers
    # git+https://github.com/facebookresearch/xformers.git@v0.0.13#egg=xformers \

    # install deepdanbooru
    git+https://github.com/KichangKim/DeepDanbooru.git@d91a2963bf87c6a770d74894667e9ffa9f6de7ff#egg=deepdanbooru[tensorflow] tensorflow==2.10.0 tensorflow-io==0.27.0 \

  # end install requirements
  --prefer-binary && \

  # (optional) install requirements for CodeFormer (face restoration)
  pip install -r repositories/CodeFormer/requirements.txt --prefer-binary

COPY --chown=sd ./requirements.txt ./requirements.txt
RUN pip install --no-cache-dir -r requirements.txt  --prefer-binary
COPY --chown=sd . ./

# Plugins
RUN git clone --depth=1 https://github.com/AUTOMATIC1111/stable-diffusion-webui-aesthetic-gradients extensions/aesthetic-gradients && \
  git clone --depth=1 https://github.com/yfszzx/stable-diffusion-webui-images-browser extensions/images-browser && \
  git clone --depth=1 https://github.com/AUTOMATIC1111/stable-diffusion-webui-wildcards extensions/wildcards && \
  git clone --depth=1 https://github.com/adieyal/sd-dynamic-prompts extensions/dynamic-prompts && \
  git clone --depth=1 https://github.com/yfszzx/stable-diffusion-webui-inspiration extensions/inspiration && \
  git clone --depth=1 https://github.com/deforum-art/deforum-for-automatic1111-webui extensions/deforum && \
  git clone --depth=1 https://github.com/camenduru/stable-diffusion-webui-artists-to-study extensions/artists-to-study && \
  git clone --depth=1 https://github.com/tsngo/stable-diffusion-webui-aesthetic-image-scorer extensions/aesthetic-image-scorer && \
  git clone --depth=1 https://github.com/toshiaki1729/stable-diffusion-webui-dataset-tag-editor extensions/dataset-tag-editor

ENTRYPOINT ["python3", "webui.py"]
CMD ["--listen", "--port", "8080"]
