Promotes three previously-manual maintenance tasks to Celery Beat schedules
so the user doesn't have to remember to run them:
- ml.backfill daily
- apply_auto_accept_predictions daily
- recompute_all_centroids weekly
Cadences are env-overridable (ML_BACKFILL_EVERY_SECONDS,
AUTO_ACCEPT_EVERY_SECONDS, CENTROIDS_EVERY_SECONDS).
Each task self-gates so a scheduled run is a no-op when there's nothing
to do:
- ml.backfill: already self-gating — its first paginated query returns
zero rows when no image is missing predictions/embeddings, the loop
breaks, and the task returns. No code change.
- apply_auto_accept_predictions: adds a fast-path NOT EXISTS query that
returns immediately when no WD14 prediction at/above the threshold
exists for an unattached, non-rejected (image, tag) pair. The full
walk only fires when fresh predictions have landed since the last run.
Returns {'skipped_no_candidates': True} on the no-op path.
- recompute_all_centroids: tightens the aggregate query to LEFT JOIN
tag_reference_embedding and skip tags whose stored reference_count
already matches current image_tags membership count. Without this gate
the daily-scheduled sweep would re-enqueue a recompute for every
eligible tag every run, contending with tag_and_embed on the ml queue.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Adds per-call instrumentation to _run_video_inference so we can see
where the cost goes on slow videos: frame extraction (ffmpeg seeks),
WD14, or SigLIP. Also captures file size to correlate slowness with
file characteristics.
Output shape:
tag_and_embed: video 7384 timings extract=12.3s wd14=24.1s
siglip=82.6s frames=10/10 file_size=412.5MB
Investigating a soft-time-limit storm where the deployed code has
soft_time_limit=900 but workers are firing limits at ~120s intervals.
This patch is pure observability — no behavior change.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
The deployed worker hit soft_time_limit=120s on a video that took longer
than that to process — 10-frame SigLIP on CPU is ~10-15s/frame plus WD14,
so 120s (and even the prior 240s in source) was tight for any non-trivial
video.
- soft_time_limit: 240 -> 900 (15min), time_limit: 360 -> 1200 (20min).
Gives videos comfortable headroom on CPU.
- Catch SoftTimeLimitExceeded explicitly so the task ends with a clean
'soft_time_limit' status instead of bubbling up + retrying through
self.retry(). Retrying a CPU-bound timeout is pointless — same input,
same wall.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Adds per-image integrity tracking so corrupt files are detected, excluded
from random/showcase/ML/suggestion paths, and recoverable by dropping a
fresh copy in /import — closing the gap that surfaced as the WD14
'6 bytes not processed' OSError.
Schema (migration l26042501)
- image_record.integrity_status: unknown | ok | truncated | unreadable | missing
- image_record.integrity_checked_at: timestamptz
- partial index on status <> 'ok' for cheap report/filter queries
Verifier
- app/services/integrity.py: verify_path() dispatches by extension
- PIL two-stage (verify + load with LOAD_TRUNCATED_IMAGES disabled)
- ffprobe for video, zipfile.testzip for archives
- Truncation-vs-unreadable distinction via PIL message hints
Pipeline
- verify_media_integrity Celery task: per-image, idempotent
- verify_unverified_images sweep: only_unknown by default, skips
paths in active import tasks
- Hooked into the end of import_media_file (new + archive paths) and
the supersede branch
- supersede_image() resets status to 'unknown' so the post-supersede
verify writes a fresh truth
- Supersede-on-replace: a fresh /import/<artist>/<filename> matching
a flagged-corrupt record routes through _supersede_existing,
preserving tags/series/embeddings
Exclusions
- /, /api/random-images, tag_and_embed, ml.backfill enqueue, and
get_suggestions all filter integrity_status IN ('ok', 'unknown') so
flagged rows don't poison the gallery, ML, or suggestion math.
'unknown' is treated as healthy so post-migration data stays visible
until the sweep runs.
UI / report
- Settings -> Maintenance: 'Verify unknown' + 'Force re-verify all'
- GET /api/integrity/failed (paginated list of flagged rows)
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Videos now route through a 10-frame sampling branch (configurable via
VIDEO_ML_FRAMES env) instead of the previous unsupported_format early
return. WD14 predictions are aggregated by max-confidence per (name,
category) across frames so sparse signals aren't diluted; SigLIP
embeddings are mean-pooled for a representative shot. Also generates a
fallback thumbnail when the record is missing one, and removes the
video_filter from backfill so videos get enqueued.
Celery soft/hard limits bumped to 240s/360s to accommodate 10x inference.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
WD14 and SigLIP are image-only. The current backfill enqueues every
ImageRecord row, and tag_and_embed retries 3 times (~3 min) on each
video before giving up, blocking the serialized ml queue. Worse,
videos never get a prediction row, so every subsequent backfill
re-enqueues them.
- tag_and_embed: early return 'unsupported_format' for video
extensions, no retry.
- backfill: filter the outerjoin query to exclude video extensions
via VIDEO_EXTS from app.utils.image_importer.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Consolidated merge of feat/tag-suggestions branch. Original 64-commit history
was lost to git-object corruption in a Nextcloud-synced checkout; this single
commit captures the equivalent diff.
Includes:
- pgvector-backed tag suggestion infra (WD14 + SigLIP centroids, ml-worker
container, Celery tasks, suggestion service, accept/reject endpoints + modal
UI with green/red chip buttons)
- Character/fandom integrity: title-case normalization on every write path,
fandom-id backfill, maintenance task + settings button, migrations g26041901
+ h26041901 to canonicalize legacy rows with case-only duplicate merging
- Tag-underscores + modal polish: WD14 name canonicalization at emit + accept
+ add/bulk-add paths, migration i26041901 for legacy-row rename-or-merge
across character/fandom/NULL kinds, suggestion-accept refresh parity via
awaited loadTags, persistent chip tint