# SPDX-License-Identifier: MIT

#!UseOBSRepositories
#!ExclusiveArch: aarch64 x86_64

# Unified BCI & Orchid Tags
#!BuildTag: containers/open-webui:%%open_webui_version%%%BUILD_FLAVOR%-%RELEASE%
#!BuildTag: containers/open-webui:%%open_webui_version%%%BUILD_FLAVOR%
#!BuildTag: containers/open-webui:0%BUILD_FLAVOR%

#!ForceMultiVersion
#!BuildName: containers-open-webui-0%BUILD_FLAVOR%
#!BuildVersion: %%open_webui_version%%
#!BuildRelease: 0

FROM registry.suse.com/bci/bci-micro:15.6 AS micro
FROM registry.suse.com/bci/bci-base:15.6 AS base

FROM base as builder
COPY --from=micro / /chroot/
ENV CHKSTAT_ALLOW_INSECURE_MODE_IF_NO_PROC=1
ENV ZYPP_CONF=/scratch-zypp.conf
RUN printf '[main]\nrpm.install.excludedocs = yes\n' >/scratch-zypp.conf

WORKDIR /chroot/app/backend

RUN mkdir -p data
RUN touch data/readme.txt

RUN mkdir -p /chroot/root/.cache/chroma
RUN echo -n 00000000-0000-0000-0000-000000000000 > /chroot/root/.cache/chroma/telemetry_user_id

RUN zypper --installroot /chroot -n -v --gpg-auto-import-keys in python311-open-webui
RUN zypper clean --all

## NLTK datasets location. Since the /app/backend/data is backed by persistent storage, setting
## the NLTK_DATA environment variable to /app/backend/data.
## In the official documentation, Customer will be advised to download/install the datasets to this location
RUN mkdir -p ./data/nltk_data

COPY start_open_webui.sh ./start_open_webui.sh
RUN chmod +x ./start_open_webui.sh

# Remove *.pyc files
RUN find /chroot/usr -name '*.pyc' -delete

FROM micro
COPY --from=builder /chroot/ /
# Define labels according to https://en.suse.org/Building_derived_containers
# labelprefix=com.suse.application.open-webui
LABEL org.opencontainers.image.authors=""
LABEL org.opencontainers.image.title="Open WebUI"
LABEL org.opencontainers.image.description="Open WebUI is an extensible, feature-rich, and user-friendly self-hosted WebUI designed to operate entirely offline. It supports various LLM runners, including Ollama and OpenAI-compatible APIs."
LABEL org.opencontainers.image.version="%%open_webui_version%%"
LABEL org.opencontainers.image.url="https://apps.rancher.io/applications/open-webui"
LABEL org.opencontainers.image.created="%BUILDTIME%"
LABEL org.opencontainers.image.vendor="SUSE LLC"
LABEL org.opencontainers.image.source="%SOURCEURL%"
LABEL org.opencontainers.image.ref.name="%%open_webui_version%%%BUILD_FLAVOR%-%RELEASE%"
LABEL org.opencontainers.image.base.digest="%BASE_IMAGE_DIGEST%"
LABEL org.opencontainers.image.base.name="%BASE_IMAGE_TAG%"
LABEL org.opensuse.reference="dp.apps.rancher.io/containers/open-webui:%%open_webui_version%%-%RELEASE%"
LABEL org.openbuildservice.disturl="%DISTURL%"
LABEL com.suse.supportlevel="l3"
LABEL com.suse.eula="sle-eula"
LABEL com.suse.lifecycle-url="https://www.suse.com/lifecycle"
LABEL com.suse.image-type="application"
LABEL com.suse.release-stage="released"
# endlabelprefix

ENV FRONTEND_BUILD_DIR="/usr/lib/python3.11/site-packages/open_webui/frontend"
ENV ENV=prod \
    PORT=8080 \
    USE_OLLAMA_DOCKER="false"\
    OPENAI_API_KEY="" \
    WEBUI_SECRET_KEY="" \
    SCARF_NO_ANALYTICS=true \
    DO_NOT_TRACK=true \
    ANONYMIZED_TELEMETRY=false \
    OLLAMA_BASE_URL="/ollama" \
    OPENAI_API_BASE_URL=""

## whisper TTS model settings ##
ENV WHISPER_MODEL="base" \
    WHISPER_MODEL_DIR="/app/backend/data/cache/whisper/models"

## RAG Embedding model settings ##
ENV RAG_EMBEDDING_MODEL="" \
    RAG_RERANKING_MODEL="" \
    SENTENCE_TRANSFORMERS_HOME="/app/backend/data/cache/embedding/models"

## Hugging Face download cache ##
ENV HF_HOME="/app/backend/data/cache/embedding/models"

ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8
ENV DATA_DIR /app/backend/data

ENV NLTK_DATA="/app/backend/data/nltk_data"

EXPOSE 8080
ENTRYPOINT ["/app/backend/start_open_webui.sh"]
