FROM python:3.10-bookworm

ARG DEBIAN_FRONTEND=noninteractive

# Create log directories with proper permissions
RUN mkdir -p /app/logs && \
    touch /app/logs/whisperlive.log && \
    touch /app/logs/connections.log && \
    chmod 666 /app/logs/whisperlive.log && \
    chmod 666 /app/logs/connections.log

# install lib required for pyaudio
RUN apt update && apt install -y portaudio19-dev && apt-get clean && rm -rf /var/lib/apt/lists/*

# update pip to support for whl.metadata -> less downloading
RUN pip install --no-cache-dir -U "pip>=24"

# create a working directory
WORKDIR /app

# install the requirements for running the whisper-live server
COPY requirements/server.txt /app/
RUN pip install -r server.txt && rm server.txt

COPY whisper_live /app/whisper_live
COPY run_server.py /app

# Port options
EXPOSE ${PORT_WHISPERLIVE}
EXPOSE ${PORT_WHISPERLIVE_SSL}
ARG PORT_WHISPERLIVE
ENV PORT_WHISPERLIVE=${PORT_WHISPERLIVE}
ARG PORT_WHISPERLIVE_SSL
ENV PORT_WHISPERLIVE_SSL=${PORT_WHISPERLIVE_SSL}

# SSL options
ARG WHISPERLIVE_SSL
ENV WHISPERLIVE_SSL=${WHISPERLIVE_SSL}

# Model options
ARG WHISPL_USE_CUSTOM_MODEL
ENV WHISPL_USE_CUSTOM_MODEL=${WHISPL_USE_CUSTOM_MODEL}
ARG FASTERWHISPER_MODEL
ENV FASTERWHISPER_MODEL=${FASTERWHISPER_MODEL}

CMD ["sh", "-c", "\
    if [ \"$WHISPERLIVE_SSL\" = \"true\" ]; then \
        python3 -u run_server.py --port $PORT_WHISPERLIVE_SSL --backend faster_whisper --faster_whisper_custom_model_path /app/models/$FASTERWHISPER_MODEL --ssl_cert_path /app/ssl; \
    else \
        python3 -u run_server.py --port $PORT_WHISPERLIVE --backend faster_whisper --faster_whisper_custom_model_path /app/models/$FASTERWHISPER_MODEL --no_single_model; \
    fi"]
