FROM python:3.13-slim AS dependencies

WORKDIR /app

# 安装系统依赖(CLIP需要)
RUN apt-get update && apt-get install -y \
    git \
    && rm -rf /var/lib/apt/lists/*

# 设置环境变量
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

# 先安装基础依赖
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip

# 安装 CLIP
RUN pip install --no-cache-dir git+https://github.com/openai/CLIP.git

# 安装其他依赖
RUN pip install --no-cache-dir -r requirements.txt

# 第二阶段:运行时环境
FROM python:3.13-slim AS runtime

WORKDIR /app

# 设置环境变量
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

# 从第一阶段复制已安装的包
COPY --from=dependencies /usr/local/lib/python3.13/site-packages /usr/local/lib/python3.13/site-packages
COPY --from=dependencies /usr/local/bin /usr/local/bin

# 复制应用代码(放在最后,避免代码变更影响依赖缓存)
COPY . .

EXPOSE 5000

CMD ["python", "run.py"]