FastAPI backend for detecting AI-manipulated images and videos using an EfficientNet-B4 CNN classifier with GradCAM explainability.
- Image analysis — face-aware deepfake classification (JPEG, PNG, WebP)
- Video analysis — temporal frame sampling + inconsistency scoring (MP4, AVI, MOV, WebM)
- GradCAM heatmaps — visualize which facial regions triggered the model
- PostgreSQL history with pagination, filtering, and aggregate stats
- Docker Compose — single command to run API + database
POST /detect/image ─► Face Detection (OpenCV Haar)
─► EfficientNet-B4 (timm) → sigmoid → fake_probability
─► GradCAM heatmap → /uploads/gradcam/*.png
─► Persist to DB → DetectionResult
POST /detect/video ─► FFmpeg frame extraction (uniform sampling)
─► Per-frame face detection + classification
─► Temporal std-dev boost for high-variance sequences
─► Persist to DB → DetectionResult
GET /history/ ─► Paginated detection log
GET /history/stats ─► Aggregate stats (fake_rate, avg_fake_probability)
| fake_probability | Verdict |
|---|---|
| ≥ 0.65 | FAKE |
| ≤ 0.35 | REAL |
| 0.35 – 0.65 | UNCERTAIN |
cp .env.example .env
# Add your Neon/PostgreSQL connection string to .env
docker-compose up --buildAPI available at http://localhost:8000
Swagger docs at http://localhost:8000/docs
# Requires Python 3.11 + ffmpeg
./setup.sh
source ./activate
cp .env.example .env
uvicorn app.main:app --reloadThe API runs without weights (random predictions) for development. For production:
- Download a pretrained EfficientNet-B4 deepfake checkpoint (e.g. from FaceForensics++ trained models)
- Place it at
model_weights/efficientnet_deepfake.pth - Update
MODEL_WEIGHTS_PATHin.env
The model head expects a binary classifier (1 output unit, sigmoid activation). Fine-tune on FaceForensics++ or DFDC datasets.
| Variable | Default | Description |
|---|---|---|
DATABASE_URL |
SQLite local | PostgreSQL connection string |
MODEL_WEIGHTS_PATH |
./model_weights/efficientnet_deepfake.pth |
Path to .pth checkpoint |
UPLOAD_DIR |
./uploads |
Storage for uploaded media + GradCAM images |
MAX_IMAGE_SIZE_MB |
10 | Upload size limit for images |
MAX_VIDEO_SIZE_MB |
200 | Upload size limit for videos |
VIDEO_FRAMES_TO_SAMPLE |
30 | Frames extracted per video |
FFMPEG_PATH |
ffmpeg |
Path to ffmpeg binary |
Upload a JPEG/PNG/WebP image. Returns verdict + confidence + GradCAM URL.
Upload MP4/AVI/MOV/WebM. Returns per-frame scores + temporal analysis.
List past detections. Query params: skip, limit, media_type, verdict.
Returns { total_analyzed, fake_detected, real_detected, uncertain, avg_fake_probability, fake_rate }.
Full result for a single detection including frame_scores and gradcam_url.
source ./activate
pip install pytest httpx
pytest tests/ -v- FastAPI + Uvicorn
- PyTorch 2.3 + timm (EfficientNet-B4)
- OpenCV (face detection, frame I/O)
- FFmpeg (video decoding)
- SQLAlchemy 2.0 + Alembic
- PostgreSQL (production) / SQLite (dev/test)