From 80871103734752e55af81b721f20a113d4101268 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Wed, 18 Feb 2026 09:20:15 +0000 Subject: [PATCH 1/3] Use CPU-only torch for Docker builds to reduce image size Configure uv to install CPU-only torch from PyTorch's dedicated index via [tool.uv.sources] in pyproject.toml. Update Dockerfile to use uv sync (which respects uv.sources) instead of uv pip install, and upgrade uv from 0.1.44 to 0.5.0 for [tool.uv.sources] support. This reduces the Docker image by ~3GB (CUDA libraries are unnecessary for the CPU-only Flask server). Co-Authored-By: Claude Haiku 4.5 --- pyproject.toml | 4 ++++ src/mock_vws/_flask_server/Dockerfile | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 83b732734..29a85c1d5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -138,6 +138,10 @@ fallback_version = "0.0.0" # Code to match this is in ``conf.py``. version_scheme = "post-release" +[tool.uv] +sources.torch = { index = "pytorch-cpu" } +index = [ { name = "pytorch-cpu", url = "https://download.pytorch.org/whl/cpu", explicit = true } ] + [tool.ruff] line-length = 79 lint.select = [ diff --git a/src/mock_vws/_flask_server/Dockerfile b/src/mock_vws/_flask_server/Dockerfile index a08e67063..94fe4c6d1 100644 --- a/src/mock_vws/_flask_server/Dockerfile +++ b/src/mock_vws/_flask_server/Dockerfile @@ -12,12 +12,12 @@ COPY --chown=myuser:myuser . /app # See https://pythonspeed.com/articles/activate-virtualenv-dockerfile/ # For why we use this method of activating the virtual environment. ENV VIRTUAL_ENV=/app/docker_venvs/.venv -RUN python3 -m venv $VIRTUAL_ENV +ENV UV_PROJECT_ENVIRONMENT=/app/docker_venvs/.venv ENV PATH="$VIRTUAL_ENV/bin:$PATH" WORKDIR /app -RUN pip install --no-cache-dir uv==0.1.44 && \ - uv pip install --no-cache-dir --upgrade --editable . +RUN pip install --no-cache-dir uv==0.5.0 && \ + uv sync --no-cache EXPOSE 5000 ENTRYPOINT ["python"] HEALTHCHECK --interval=1s --timeout=10s --start-period=5s --retries=3 CMD ["python", "/app/src/mock_vws/_flask_server/healthcheck.py"] From 1410e79736b4cc938dd9844684fd406d4f1d1af5 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Wed, 18 Feb 2026 09:28:10 +0000 Subject: [PATCH 2/3] Fix torchvision CPU index and Docker venv creation Add torchvision to [tool.uv.sources] so it also comes from the CPU index (piq depends on torchvision, which crashes on import with CPU-only torch when installed from PyPI). Restore python3 -m venv in the Dockerfile so pip install uv puts the uv binary into the venv's bin directory (which is on PATH), rather than ~/.local/bin (which is not). Co-Authored-By: Claude Sonnet 4.6 --- pyproject.toml | 1 + src/mock_vws/_flask_server/Dockerfile | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 3f261e459..78f9f3c6d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -140,6 +140,7 @@ version_scheme = "post-release" [tool.uv] sources.torch = { index = "pytorch-cpu" } +sources.torchvision = { index = "pytorch-cpu" } index = [ { name = "pytorch-cpu", url = "https://download.pytorch.org/whl/cpu", explicit = true } ] [tool.ruff] diff --git a/src/mock_vws/_flask_server/Dockerfile b/src/mock_vws/_flask_server/Dockerfile index cf24f86a3..9565f9d90 100644 --- a/src/mock_vws/_flask_server/Dockerfile +++ b/src/mock_vws/_flask_server/Dockerfile @@ -16,7 +16,8 @@ ENV UV_PROJECT_ENVIRONMENT=/app/docker_venvs/.venv ENV PATH="$VIRTUAL_ENV/bin:$PATH" WORKDIR /app -RUN pip install --no-cache-dir uv==0.10.4 && \ +RUN python3 -m venv $VIRTUAL_ENV && \ + pip install --no-cache-dir uv==0.10.4 && \ uv sync --no-cache EXPOSE 5000 ENTRYPOINT ["python"] From 9f484ea5aa98af328793dcd908d00a97ff389854 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Wed, 18 Feb 2026 09:40:43 +0000 Subject: [PATCH 3/3] Add torchvision as direct dependency for CPU index source to apply tool.uv.sources only applies to direct dependencies; torchvision was only a transitive dependency via piq, so sources.torchvision was silently ignored and PyPI's torchvision was installed instead. PyPI's torchvision registers torchvision::nms operators that don't exist in CPU-only torch, causing RuntimeError on import. Making torchvision a direct dependency causes uv to route it through the CPU index. Co-Authored-By: Claude Sonnet 4.6 --- pyproject.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 78f9f3c6d..f4e49c096 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,6 +44,7 @@ dependencies = [ "responses>=0.25.3", "torch>=2.5.1", "torchmetrics>=1.5.1", + "torchvision>=0.20.1", "tzdata; sys_platform=='win32'", "vws-auth-tools>=2024.7.12", "werkzeug>=3.1.2", @@ -317,6 +318,9 @@ per_rule_ignores.DEP002 = [ # tzdata is needed on Windows for zoneinfo to work. # See https://docs.python.org/3/library/zoneinfo.html#data-sources. "tzdata", + # torchvision is used transitively via piq, but must be a direct dependency + # so that tool.uv.sources can route it to the CPU-only PyTorch index. + "torchvision", ] [tool.pyproject-fmt]