| FROM python:3.10-slim | |
| # 安装系统依赖(CLIP 需要 git) | |
| RUN apt-get update && apt-get install -y \ | |
| git \ | |
| build-essential \ | |
| wget \ | |
| && rm -rf /var/lib/apt/lists/* | |
| WORKDIR /app | |
| # 复制并安装 Python 依赖 | |
| COPY requirements.txt . | |
| RUN pip install --no-cache-dir -r requirements.txt | |
| # 预下载 CLIP 模型(减少首次启动时间) | |
| RUN python -c "import clip; clip.load('ViT-B/32', device='cpu')" | |
| # 复制应用代码和数据 | |
| COPY api_server.py config.py llm.py rag.py ./ | |
| COPY amazon_multimodal_clean.csv . | |
| COPY frontend/ ./frontend/ | |
| # 创建必要的目录 | |
| RUN mkdir -p images chromadb_store | |
| # 在 Docker 构建时预先构建索引(避免每次启动都重建) | |
| RUN python rag.py --build --csv amazon_multimodal_clean.csv | |
| # 暴露 Hugging Face Spaces 端口 | |
| EXPOSE 7860 | |
| # 设置环境变量 | |
| ENV PYTHONUNBUFFERED=1 | |
| ENV API_HOST=0.0.0.0 | |
| ENV API_PORT=7860 | |
| # 健康检查(启动等待 120 秒给索引构建时间) | |
| HEALTHCHECK --interval=30s --timeout=10s --start-period=120s --retries=3 \ | |
| CMD wget --no-verbose --tries=1 --spider http://localhost:7860/health || exit 1 | |
| # 启动 FastAPI | |
| CMD ["python", "-u", "api_server.py"] | |