FROM ubuntu:22.04 AS build
WORKDIR /app

RUN apt-get update && \
    apt-get install -y build-essential wget cmake git \
    && rm -rf /var/lib/apt/lists/*

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

ARG WHISPER_MODEL=base.en
RUN bash ./models/download-ggml-model.sh ${WHISPER_MODEL}
RUN cmake -B build && cmake --build build --config Release -j$(nproc)

FROM ubuntu:22.04
WORKDIR /app

RUN apt-get update && \
    apt-get install -y curl ffmpeg \
    && rm -rf /var/lib/apt/lists/*

COPY --from=build /app/build/bin/whisper-server /usr/local/bin/whisper-server
COPY --from=build /app/build/src/libwhisper.so* /usr/local/lib/
COPY --from=build /app/build/ggml/src/libggml*.so* /usr/local/lib/
COPY --from=build /app/models /app/models
RUN ldconfig

ARG WHISPER_MODEL=base.en
ENV WHISPER_MODEL_PATH=/app/models/ggml-${WHISPER_MODEL}.bin

EXPOSE 8080

ENTRYPOINT ["whisper-server"]
CMD ["--model", "/app/models/ggml-base.en.bin", \
     "--host", "0.0.0.0", \
     "--port", "8080", \
     "--convert", \
     "--language", "en", \
     "--inference-path", "/v1/audio/transcriptions"]
