FROM python:3.12-slim

WORKDIR /app

# 系统依赖（MinerU 需要 libGL）
RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    libgl1-mesa-glx \
    libglib2.0-0 \
    libsm6 \
    libxrender1 \
    libxext6 \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt \
    --index-url https://pypi.tuna.tsinghua.edu.cn/simple \
    --trusted-host pypi.tuna.tsinghua.edu.cn

# 预下载 MinerU 模型（构建时执行，加速启动）
RUN python -c "
import os
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
try:
    from magic_pdf.model.doc_analyze_by_custom_model import ModelSingleton
    print('MinerU 模型下载完成')
except Exception as e:
    print(f'模型下载跳过（将在运行时下载）: {e}')
" || true

COPY main.py .

HEALTHCHECK --interval=30s --timeout=10s --start-period=120s --retries=3 \
    CMD curl -f http://localhost:8011/health || exit 1

EXPOSE 8011

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