Skip to content
~/projects/gallerai

Galleria: Event Photo Sharing Powered by Face Recognition

complete
PythonFastAPIPostgreSQLpgvectorRedisCeleryRetinaFaceArcFaceSSE

Overview

GallerAI is for event organizers who want to hand attendees a link and let them find themselves in the photos. Organizers create an event, bulk-upload up to around 1000 photos and videos, and share the link. Attendees open it (entering a passcode if the event is private), browse the gallery under All / Photos / Videos tabs, and take a selfie to find their own shots. They can download single files or zips. Attendees never need an account. Only organizers log in.

The face search works like this. A selfie goes through RetinaFace to find the largest face, then ArcFace produces a 512-dimension embedding. That embedding runs a pgvector cosine search against every face in the event, with a 0.6 similarity threshold and a cap of 1000 results. Matches and the embedding are stored in Redis under a scan token that lasts two hours. The embedding never touches the users table, because attendees don't have rows there.

Screenshots

Challenges

Ingesting 1000 files without melting the server

A single request carrying 1000 files is slow to fail and hard to retry. Processing them one at a time through the face pipeline would also take far too long.

The frontend splits uploads into chunks of about 25 and POSTs 4 to 6 of them in parallel. On the backend, each file streams to temp storage and gets hashed while it is written, so the file is read only once. Each chunk then runs one dedupe query on (event_id, file_hash) and one bulk insert. If the insert hits a conflict, it falls back to row-by-row inserts, drops the race duplicates, and cleans up their temp files. Celery tasks are dispatched per chunk as soon as it lands, not after the whole batch finishes. Face detection is CPU-bound, so Celery concurrency is set to the number of CPU cores, and the models load once per worker through worker_process_init. Nested process pools were the other option. They would have fought Celery's own parallelism for the same cores, so I left them out.

Promoting files without losing them

Writing directly to the final storage path means a crash partway through leaves a file that looks finished but isn't, and detection results can point at it before it's fully written.

Every upload stages to a temp directory first. Detection runs against the temp copy. Promotion then copies the file to final storage through save_from_path(), and one code path deletes the temp copy. Temp and final live in separate directories on purpose. A NULL storage_backend means the file hasn't been promoted yet, so an interrupted upload is easy to find and clean up. Local storage counts as a real permanent backend, not scratch space, so the same call shape will work when Cloudflare R2 is added later.

Showing progress across concurrent uploaders

Several uploaders can be pushing files into one event at the same time. Tracking progress per upload ID means a dropped connection loses its place, and the progress bar only reflects one client's share of the work.

Progress is keyed by event_id, not by upload. Workers publish updates to a Redis event:{id}:progress channel, and the frontend reads them from one SSE stream at GET /media/{id}/upload-progress. That stream aggregates all concurrent uploaders, and a reconnect picks up the event's current state without any upload-specific bookkeeping.

Outcomes

  • Uploads of around 1000 files run as chunked, parallel POSTs with deduplication by SHA256 per event. Each response reports accepted, skipped, and errors.
  • Face search uses a single pgvector query with a MAX over each media item's faces, returning matches above the threshold with a match_score.
  • Attendee search state lives in Redis for two hours and never reaches the database.

What I learned

The anonymous attendee model came from deleting code. Early drafts had UserEventGallery rows, a claim-into-account flow, and a User.face_embedding column. All of that went away in migration 3a1b2c4d5e6f. Attendees are now anonymous, identified only by a passcode cookie and a scan token. Only organizers have User rows. I assumed attendees needed some persistent identity to make the face search useful. They didn't. The search result only has to last as long as the browsing session, so a short-lived token in Redis was enough.

The temp-then-promote split was a default I adopted after the first version wrote straight to final storage. A half-written file was indistinguishable from a complete one, and I had no clean way to find the broken ones. Staging everything first costs one extra copy. In exchange, a failed upload leaves a visible state rather than a silent corrupt file.