# whisper.cpp inference server for meetrec.
#
# Built from the same pinned whisper.cpp release (v1.9.3) as the Android
# app's JNI layer, compiled statically WITH the Vulkan GPU backend for
# AMD GPUs (Radeon AI PRO R9700 / RDNA, also NVIDIA/Intel). Without a GPU
# present it transparently falls back to CPU.
#
# Note: ggml builds with -march=native by default, so build the image ON
# the machine that will run it (docker compose build on the server).

FROM debian:trixie-slim AS build

RUN apt-get update && apt-get install -y --no-install-recommends \
        git cmake build-essential ca-certificates curl \
        libvulkan-dev glslang-tools \
    && rm -rf /var/lib/apt/lists/*

ARG WHISPER_TAG=v1.9.3
RUN git clone --depth 1 --branch ${WHISPER_TAG} \
        https://github.com/ggml-org/whisper.cpp /src

# expose tinydiarize speaker_turn_next in verbose_json (see patch header)
COPY speaker-turn.patch /src/
RUN git -C /src apply speaker-turn.patch

RUN cmake -S /src -B /src/build -DCMAKE_BUILD_TYPE=Release \
        -DBUILD_SHARED_LIBS=OFF \
        -DWHISPER_BUILD_EXAMPLES=ON \
        -DWHISPER_BUILD_TESTS=OFF \
        -DWHISPER_BUILD_SERVER=ON \
        -DWHISPER_VULKAN=ON \
    && cmake --build /src/build --target whisper-server -j"$(nproc)"

FROM debian:trixie-slim

# libvulkan1 + mesa-vulkan-drivers: the AMD RADV Vulkan driver used by
# the R9700. trixie's Mesa (25.x) is required — bookworm's 22.x has no
# RDNA 3.5 (gfx1151) support and whisper silently falls back to CPU.
RUN apt-get update && apt-get install -y --no-install-recommends \
        libgomp1 ca-certificates curl libvulkan1 mesa-vulkan-drivers \
    && rm -rf /var/lib/apt/lists/*

COPY --from=build /src/build/bin/whisper-server /usr/local/bin/whisper-server
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

EXPOSE 8085
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]