File size: 1,158 Bytes
e1ba2b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e11947a
e1ba2b5
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
FROM python:3.10-slim

# HF Spaces 要求使用 7860 端口,非 root 用户
RUN useradd -m -u 1000 user
WORKDIR /app

# 安装系统依赖
RUN apt-get update && apt-get install -y \
    libgomp1 \
    && rm -rf /var/lib/apt/lists/*

# 先装 CPU 版 PyTorch(体积比 CUDA 版小 ~2GB)
RUN pip install --no-cache-dir \
    torch==2.3.1+cpu \
    --index-url https://download.pytorch.org/whl/cpu

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

# ✅ 构建阶段预下载模型,彻底避免运行时冷启动下载
# 模型文件烘焙进 Docker 镜像层
ENV HF_HOME=/app/hf_cache
RUN python -c "\
from huggingface_hub import snapshot_download; \
snapshot_download('BAAI/bge-m3', ignore_patterns=['*.msgpack','*.h5','flax_model*','tf_model*','rust_model*']); \
snapshot_download('BAAI/bge-reranker-v2-m3', ignore_patterns=['*.msgpack','*.h5','flax_model*','tf_model*','rust_model*']); \
print('Models downloaded successfully')"

RUN chown -R user:user /app/hf_cache
COPY --chown=user:user . .

USER user
EXPOSE 7860

CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]