Python Backend Scope: FaceFusion Logic + Async Queue on Modal
Budget: £20 – £250 GBP
My Stack: My service runs on Modal.com using FastAPI. It already handles uploads, GPU inference (FaceFusion 3.3.2), and file storage.
The Task: I need you to implement two missing backend features: an Asynchronous Queue and a Face Analysis endpoint. Please rely on Modal’s native task primitives (modal.Dict, modal.Function.spawn) rather than external queues like Celery/Redis.
1. Async Queue (Fixing Timeouts)
Refactor: Update the /swap endpoint to be non-blocking. It should return a JSON payload with a job_id immediately.
Background Worker: Push the heavy FaceFusion work to a background Modal function (.spawn()).
State: Persist the job status in a modal.Dict.
Polling: Create a /status/{job_id} endpoint that returns: queued, processing, completed (with result URL), or failed.
2. Face Analysis (New Feature)
Endpoint: Create /analyze-video that accepts a video file.
Logic: Use Google MediaPipe (for commercial license safety) to scan the video.
Response: Return a JSON list containing:
face_id: A unique ID (index or hash) for each distinct person found.
thumbnail: A base64-encoded jpg of that face (cropped from the first clear frame).
3. Mapped Swap Logic
Update: Modify the swap worker to accept an optional mapping dictionary: {"0": "source_image_A.jpg", "1": "source_image_B.jpg"}.
Logic: If a map is provided, swap specific source images onto specific target Face IDs. If no map is provided, fall back to the current "swap all" or "swap one" behavior.
Code Guidelines
Python Version: Use Python 3.10 (matches current stable backend).
Testing: The solution must run on Modal (modal deploy or modal serve). I do not need a local Docker setup; testing via a standard Python venv or Modal dev environment is fine.
Style: Follow the existing facefusion_backend.py pattern (FastAPI decorators).
Deliverables:
Updated facefusion_backend.py.
A helper module (e.g., face_analysis.py) for the MediaPipe logic.
A simple test_api.py script demonstrating the new Async workflow (Submit -> Poll -> Result).
The Task: I need you to implement two missing backend features: an Asynchronous Queue and a Face Analysis endpoint. Please rely on Modal’s native task primitives (modal.Dict, modal.Function.spawn) rather than external queues like Celery/Redis.
1. Async Queue (Fixing Timeouts)
Refactor: Update the /swap endpoint to be non-blocking. It should return a JSON payload with a job_id immediately.
Background Worker: Push the heavy FaceFusion work to a background Modal function (.spawn()).
State: Persist the job status in a modal.Dict.
Polling: Create a /status/{job_id} endpoint that returns: queued, processing, completed (with result URL), or failed.
2. Face Analysis (New Feature)
Endpoint: Create /analyze-video that accepts a video file.
Logic: Use Google MediaPipe (for commercial license safety) to scan the video.
Response: Return a JSON list containing:
face_id: A unique ID (index or hash) for each distinct person found.
thumbnail: A base64-encoded jpg of that face (cropped from the first clear frame).
3. Mapped Swap Logic
Update: Modify the swap worker to accept an optional mapping dictionary: {"0": "source_image_A.jpg", "1": "source_image_B.jpg"}.
Logic: If a map is provided, swap specific source images onto specific target Face IDs. If no map is provided, fall back to the current "swap all" or "swap one" behavior.
Code Guidelines
Python Version: Use Python 3.10 (matches current stable backend).
Testing: The solution must run on Modal (modal deploy or modal serve). I do not need a local Docker setup; testing via a standard Python venv or Modal dev environment is fine.
Style: Follow the existing facefusion_backend.py pattern (FastAPI decorators).
Deliverables:
Updated facefusion_backend.py.
A helper module (e.g., face_analysis.py) for the MediaPipe logic.
A simple test_api.py script demonstrating the new Async workflow (Submit -> Poll -> Result).