Prefetch moved to a separate python:3.10-slim stage that only sees weights.py/fetch-weights.py, so editing app.py or vendor/ no longer invalidates the 360 MB weights layer. Weight catalog and download helpers extracted to photo-ai/weights.py; fetch-weights.py no longer imports app (torch). Cache mount switched to sharing=shared: a canceled build could leave the exclusive lock held and hang the next build forever; downloads now use a pid-unique tmp file, so concurrent builds cannot corrupt it. Docs recommend 'docker compose up -d --build app' for app updates instead of rebuilding photo-ai/face-id.
71 lines
2.8 KiB
Docker
71 lines
2.8 KiB
Docker
# syntax=docker/dockerfile:1
|
|
|
|
FROM python:3.10-slim AS weights
|
|
|
|
ARG PHOTO_AI_PREFETCH=codeformer
|
|
|
|
WORKDIR /seed
|
|
COPY weights.py fetch-weights.py ./
|
|
|
|
RUN --mount=type=cache,target=/var/cache/photo-ai-weights,sharing=shared \
|
|
mkdir -p /opt/photo-ai-seed && \
|
|
if [ -n "$PHOTO_AI_PREFETCH" ] && [ "$PHOTO_AI_PREFETCH" != "none" ]; then \
|
|
PHOTO_AI_PREFETCH="$PHOTO_AI_PREFETCH" python fetch-weights.py /opt/photo-ai-seed /var/cache/photo-ai-weights || echo "предзагрузка весов не удалась, сервис скачает их при первом запросе"; \
|
|
else \
|
|
echo "предзагрузка весов отключена (PHOTO_AI_PREFETCH=$PHOTO_AI_PREFETCH)"; \
|
|
fi
|
|
|
|
|
|
FROM python:3.10-slim
|
|
|
|
ARG TORCH_VARIANT=cpu
|
|
ARG TORCH_INDEX=https://download.pytorch.org/whl/${TORCH_VARIANT}
|
|
|
|
WORKDIR /app
|
|
|
|
ENV PIP_BUILD_ISOLATION=0 \
|
|
PYTHONDONTWRITEBYTECODE=1 \
|
|
PYTHONUNBUFFERED=1 \
|
|
HF_HOME=/root/.cache/huggingface \
|
|
TORCH_HOME=/root/.cache/torch \
|
|
TRANSFORMERS_CACHE=/root/.cache/huggingface \
|
|
XDG_CACHE_HOME=/root/.cache
|
|
|
|
RUN apt-get update && apt-get install -y --no-install-recommends libgl1 libglib2.0-0 && rm -rf /var/lib/apt/lists/*
|
|
|
|
RUN --mount=type=cache,target=/root/.cache/pip,sharing=locked \
|
|
pip install --upgrade pip && \
|
|
pip install "typing-extensions==4.12.2" "numpy==2.2.6"
|
|
|
|
RUN --mount=type=cache,target=/root/.cache/pip,sharing=locked \
|
|
pip install --timeout 120 --retries 15 --resume-retries 50 \
|
|
torch torchvision --index-url ${TORCH_INDEX}
|
|
|
|
RUN --mount=type=cache,target=/root/.cache/pip,sharing=locked \
|
|
pip install --upgrade setuptools wheel && \
|
|
pip install --no-build-isolation --no-deps basicsr==1.4.2 realesrgan==0.3.0 gfpgan==1.3.8 facexlib==0.3.0 && \
|
|
pip install "numpy==2.2.6" "opencv-python-headless==5.0.0.93" addict future lmdb Pillow pyyaml \
|
|
requests scikit-image scipy tqdm filterpy numba fastapi "uvicorn[standard]" python-multipart
|
|
|
|
RUN BASICSR_DEG=$(python -c "import basicsr; import os; print(os.path.join(os.path.dirname(basicsr.__file__), 'data', 'degradations.py'))" 2>/dev/null) || \
|
|
BASICSR_DEG=$(find /usr/local/lib/python3.10 -path "*/basicsr/data/degradations.py" 2>/dev/null | head -1) && \
|
|
if [ -n "$BASICSR_DEG" ]; then \
|
|
sed -i 's/from torchvision.transforms.functional_tensor/from torchvision.transforms.functional/g' "$BASICSR_DEG" && \
|
|
echo "basicsr patch applied to $BASICSR_DEG"; \
|
|
else \
|
|
echo "basicsr degradations.py not found, skipping patch"; \
|
|
fi
|
|
|
|
COPY weights.py app.py fetch-weights.py ./
|
|
COPY vendor/ ./vendor/
|
|
|
|
ENV PHOTO_AI_MODELS_DIR=/models
|
|
ENV MODEL_PATH=/models/RealESRGAN_x2plus.pth
|
|
ENV PHOTO_AI_SEED_DIR=/opt/photo-ai-seed
|
|
|
|
COPY --from=weights /opt/photo-ai-seed /opt/photo-ai-seed
|
|
|
|
VOLUME /models
|
|
|
|
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]
|