Spaces:
Running
Running
# 使用Python官方镜像 | |
FROM python:3.10-slim | |
# 设置环境变量 | |
ENV HF_HOME=/model-cache \ | |
PYTHONUNBUFFERED=1 \ | |
PORT=7860 # Hugging Face强制使用7860端口 | |
# 安装系统依赖 | |
RUN apt-get update && \ | |
apt-get install -y --no-install-recommends gcc python3-dev && \ | |
rm -rf /var/lib/apt/lists/* | |
# 创建非root用户 | |
RUN useradd -m appuser && \ | |
mkdir -p /model-cache && \ | |
chown -R appuser:appuser /model-cache | |
# 切换用户 | |
USER appuser | |
WORKDIR /app | |
# 复制依赖文件 | |
COPY requirements.txt . | |
# 安装依赖并预下载模型 | |
RUN pip install --no-cache-dir --upgrade pip && \ | |
pip install --no-cache-dir -r requirements.txt && \ | |
python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('flax-sentence-embeddings/st-codesearch-distilroberta-base')" | |
# 复制应用代码 | |
COPY app.py . | |
# 暴露Hugging Face强制端口 | |
EXPOSE 7860 | |
# 启动命令(必须使用7860端口) | |
CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--timeout", "600", "app:app"] |