From 29bddc9f553ff0a08ef343954ce226815efc34cc Mon Sep 17 00:00:00 2001 From: "anthropic-code-agent[bot]" <242468646+Claude@users.noreply.github.com> Date: Fri, 27 Mar 2026 15:59:45 +0000 Subject: [PATCH] Add self-healing cache validation for DeepFace weight files - Add GitHub Actions cache for ~/.deepface/weights with dynamic key based on requirements.txt hash - Implement H5 file integrity validation using h5py to detect corrupted weight files - Remove both suspiciously small (<100KB) and corrupted H5 files before tests - Gracefully fall back to size-based cleanup if h5py is unavailable - Cache key invalidates when dependencies change to prevent stale cache reuse Fixes issue where corrupted/truncated model weight files in CI cache cause ValueError during model.load_weights() and cascade into API test failures. Agent-Logs-Url: https://github.com/JayNightmare/Deepface-Mirror/sessions/a5d961cd-5f82-40c9-8b22-1c3bac0cfc54 Co-authored-by: JayNightmare <34739807+JayNightmare@users.noreply.github.com> --- .github/workflows/tests.yml | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 757988f..7c2b253 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -74,6 +74,14 @@ jobs: uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} + - name: Cache DeepFace weights + uses: actions/cache@v4 + with: + path: ~/.deepface/weights + key: deepface-weights-${{ runner.os }}-py${{ matrix.python-version }}-${{ hashFiles('requirements.txt') }} + restore-keys: | + deepface-weights-${{ runner.os }}-py${{ matrix.python-version }}- + deepface-weights-${{ runner.os }}- - name: Install dependencies run: | python -m pip install --upgrade pip @@ -81,6 +89,41 @@ jobs: # sending files in form data throwing error in flask 3 while running tests pip install Werkzeug==2.0.2 flask==2.0.2 pip install . + - name: Remove corrupted DeepFace weights (if any) + run: | + python - <<'PY' + import os, glob + + weights_dir = os.path.expanduser("~/.deepface/weights") + if not os.path.isdir(weights_dir): + raise SystemExit(0) + + # Validate HDF5 integrity; delete invalid files so they will be re-downloaded. + try: + import h5py + except Exception: + # If h5py isn't present for some reason, fall back to size-based cleanup only. + h5py = None + + for p in glob.glob(os.path.join(weights_dir, "*.h5")): + try: + # Keep your existing small-file guard + if os.path.getsize(p) < 1024 * 100: + print("Removing tiny weight file:", p) + os.remove(p) + continue + + if h5py is not None: + try: + with h5py.File(p, "r"): + pass + except Exception as e: + print("Removing corrupted H5 weight file:", p, "reason:", repr(e)) + os.remove(p) + + except OSError: + pass + PY - name: Test with pytest run: | cd tests/unit