# 构建阶段
FROM swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nvidia/cuda:12.4.1-cudnn-runtime-ubuntu22.04 AS builder


ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai

# 配置apt源为清华源
RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
    sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list

RUN apt-get update && apt-get install -y \
    software-properties-common \
    curl \
    build-essential \
    && add-apt-repository ppa:deadsnakes/ppa \
    && apt-get update \
    && apt-get install -y python3.11 python3.11-dev python3.11-distutils \
    && curl -sS https://bootstrap.pypa.io/get-pip.py -o get-pip.py \
    && python3.11 get-pip.py -i https://pypi.tuna.tsinghua.edu.cn/simple \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 设置Python 3.11为默认版本
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1 && \
    update-alternatives --set python3 /usr/bin/python3.11

WORKDIR /api_chat

# 配置pip源为清华源
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 安装依赖
COPY requirements.txt .
RUN python3 -m pip install --no-cache-dir -r requirements.txt

# 修复LangSegment库
RUN sed -i 's/from .LangSegment import LangSegment,getTexts,classify,getCounts,printList,setLangfilters,getLangfilters,setfilters,getfilters/from .LangSegment import LangSegment,getTexts,classify,getCounts,printList,setfilters,getfilters/' /usr/local/lib/python3.11/dist-packages/LangSegment/__init__.py

# 最终阶段
FROM swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nvidia/cuda:12.4.1-cudnn-runtime-ubuntu22.04 

ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai
# 添加路径映射环境变量
ENV OBSCURA_BASE_PATH=/obscura
ENV OBSCURA_AUDIO_PATH=/obscura/task/audio_files


# 配置apt源为清华源
RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
    sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list

RUN apt-get update && apt-get install -y \
    ffmpeg \
    libgl1-mesa-glx \
    python3.11 \
    python3.11-distutils \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 设置Python 3.11为默认版本
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1 && \
    update-alternatives --set python3 /usr/bin/python3.11

WORKDIR /api_chat

# 从构建阶段复制Python包
COPY --from=builder /usr/local/lib/python3.11/dist-packages /usr/local/lib/python3.11/dist-packages

# 创建必要的目录（这些目录将被挂载）
RUN mkdir -p ${OBSCURA_AUDIO_PATH} /sample /TTS

# 复制应用代码，包括TTS目录
COPY . .

# 启动服务并保持容器运行
CMD ["sh", "-c", "bash start.sh && tail -f /dev/null"]