Commit Β·
7ee6359
1
Parent(s): 47b4cf3
Deploy DeepFake Detector API - 2026-04-20 00:17:18
Browse files- Dockerfile +47 -47
- README.md +130 -182
Dockerfile
CHANGED
|
@@ -1,47 +1,47 @@
|
|
| 1 |
-
# DeepFake Detector API -
|
| 2 |
-
#
|
| 3 |
-
|
| 4 |
-
FROM python:3.11-slim
|
| 5 |
-
|
| 6 |
-
# Set working directory
|
| 7 |
-
WORKDIR /app
|
| 8 |
-
|
| 9 |
-
# Set environment variables
|
| 10 |
-
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 11 |
-
PYTHONUNBUFFERED=1 \
|
| 12 |
-
PIP_NO_CACHE_DIR=1 \
|
| 13 |
-
PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
| 14 |
-
PORT=7860
|
| 15 |
-
|
| 16 |
-
# Install system dependencies
|
| 17 |
-
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 18 |
-
curl \
|
| 19 |
-
git \
|
| 20 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 21 |
-
|
| 22 |
-
# Create non-root user (HF Spaces requirement)
|
| 23 |
-
RUN useradd -m -u 1000 user
|
| 24 |
-
USER user
|
| 25 |
-
|
| 26 |
-
# Set PATH for user-installed packages
|
| 27 |
-
ENV PATH="/home/user/.local/bin:$PATH"
|
| 28 |
-
|
| 29 |
-
# Copy requirements and install dependencies as user
|
| 30 |
-
COPY --chown=user:user requirements.txt .
|
| 31 |
-
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 32 |
-
|
| 33 |
-
# Copy application code
|
| 34 |
-
COPY --chown=user:user . /app
|
| 35 |
-
|
| 36 |
-
# Switch to root to create cache directory and set permissions
|
| 37 |
-
USER root
|
| 38 |
-
RUN mkdir -p /app/.hf_cache && chown -R user:user /app/.hf_cache && chmod +x /app/start.sh
|
| 39 |
-
|
| 40 |
-
# Switch back to user
|
| 41 |
-
USER user
|
| 42 |
-
|
| 43 |
-
# Expose
|
| 44 |
-
EXPOSE 7860
|
| 45 |
-
|
| 46 |
-
# Run the application (start.sh
|
| 47 |
-
CMD ["./start.sh"]
|
|
|
|
| 1 |
+
# DeepFake Detector API - Docker Image
|
| 2 |
+
# Unified Dockerfile for local, Railway, and HF Spaces
|
| 3 |
+
|
| 4 |
+
FROM python:3.11-slim
|
| 5 |
+
|
| 6 |
+
# Set working directory
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
# Set environment variables
|
| 10 |
+
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 11 |
+
PYTHONUNBUFFERED=1 \
|
| 12 |
+
PIP_NO_CACHE_DIR=1 \
|
| 13 |
+
PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
| 14 |
+
PORT=7860
|
| 15 |
+
|
| 16 |
+
# Install system dependencies
|
| 17 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 18 |
+
curl \
|
| 19 |
+
git \
|
| 20 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 21 |
+
|
| 22 |
+
# Create non-root user (HF Spaces requirement)
|
| 23 |
+
RUN useradd -m -u 1000 user
|
| 24 |
+
USER user
|
| 25 |
+
|
| 26 |
+
# Set PATH for user-installed packages
|
| 27 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 28 |
+
|
| 29 |
+
# Copy requirements and install dependencies as user
|
| 30 |
+
COPY --chown=user:user requirements.txt .
|
| 31 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 32 |
+
|
| 33 |
+
# Copy application code
|
| 34 |
+
COPY --chown=user:user . /app
|
| 35 |
+
|
| 36 |
+
# Switch to root to create cache directory and set permissions
|
| 37 |
+
USER root
|
| 38 |
+
RUN mkdir -p /app/.hf_cache && chown -R user:user /app/.hf_cache && chmod +x /app/start.sh
|
| 39 |
+
|
| 40 |
+
# Switch back to user
|
| 41 |
+
USER user
|
| 42 |
+
|
| 43 |
+
# Expose default app port
|
| 44 |
+
EXPOSE 7860
|
| 45 |
+
|
| 46 |
+
# Run the application (start.sh uses PORT env var)
|
| 47 |
+
CMD ["./start.sh"]
|
README.md
CHANGED
|
@@ -1,182 +1,130 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
--
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
``
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
β β β Fusion Ensemble β β β
|
| 132 |
-
β β β (LogReg Stacking) β β β
|
| 133 |
-
β β ββββββββββββββββββββββ β β
|
| 134 |
-
β ββββββββββββββββββββββββββββ β
|
| 135 |
-
β ββββββββββββββββββββββββββββ β
|
| 136 |
-
β β Gemini Explainer β β
|
| 137 |
-
β ββββββββββββββββββββββββββββ β
|
| 138 |
-
βββββββββββββββββββββββββββββββοΏ½οΏ½βββ
|
| 139 |
-
```
|
| 140 |
-
|
| 141 |
-
## π Performance
|
| 142 |
-
|
| 143 |
-
- **Accuracy**: ~87% on test set (OpenFake dataset)
|
| 144 |
-
- **Inference Time**: ~200-500ms per image (with GPU)
|
| 145 |
-
- **Model Size**: ~500MB total
|
| 146 |
-
- **Supported Formats**: JPG, PNG, WEBP
|
| 147 |
-
|
| 148 |
-
## π Troubleshooting
|
| 149 |
-
|
| 150 |
-
### Models not loading?
|
| 151 |
-
- Check the Logs tab for specific errors
|
| 152 |
-
- Verify `HF_FUSION_REPO_ID` points to a valid repository
|
| 153 |
-
- Ensure the repository is public or `HF_TOKEN` is set
|
| 154 |
-
|
| 155 |
-
### Explanations not working?
|
| 156 |
-
- Verify `GOOGLE_API_KEY` is set in Space Settings
|
| 157 |
-
- Check if you have Gemini API quota remaining
|
| 158 |
-
- Review logs for API errors
|
| 159 |
-
|
| 160 |
-
### CORS errors?
|
| 161 |
-
- Add your frontend domain to `CORS_ORIGINS` in Space Settings
|
| 162 |
-
- Format: `https://yourdomain.com,https://www.yourdomain.com`
|
| 163 |
-
|
| 164 |
-
## π Documentation
|
| 165 |
-
|
| 166 |
-
- **Interactive Docs**: Visit `/docs` for Swagger UI
|
| 167 |
-
- **ReDoc**: Visit `/redoc` for alternative documentation
|
| 168 |
-
- **Source Code**: [GitHub Repository](https://github.com/lukhsaankumar/DeepFakeDetector)
|
| 169 |
-
|
| 170 |
-
## π License
|
| 171 |
-
|
| 172 |
-
This project is part of the MacAI Society research initiative.
|
| 173 |
-
|
| 174 |
-
## π Acknowledgments
|
| 175 |
-
|
| 176 |
-
- Models trained on OpenFake, ImageNet, and custom datasets
|
| 177 |
-
- Powered by PyTorch, Hugging Face, and FastAPI
|
| 178 |
-
- AI explanations by Google Gemini
|
| 179 |
-
|
| 180 |
-
---
|
| 181 |
-
|
| 182 |
-
**Built with β€οΈ by MacAI Society**
|
|
|
|
| 1 |
+
# DeepFake Detector Backend
|
| 2 |
+
|
| 3 |
+
FastAPI backend for detecting AI-generated (deepfake) images using a multi-model fusion pipeline.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- Multi-model ensemble: CNN Transfer, ViT Base, DeiT Distilled, Gradient Field CNN
|
| 8 |
+
- Fusion prediction: Logistic Regression and meta-classifier variants
|
| 9 |
+
- Explainability: Grad-CAM and attention-based heatmaps
|
| 10 |
+
- Optional Gemini-powered interpretation layer
|
| 11 |
+
- Hugging Face Hub model download/caching
|
| 12 |
+
|
| 13 |
+
## Prerequisites
|
| 14 |
+
|
| 15 |
+
- Python 3.11+
|
| 16 |
+
- pip
|
| 17 |
+
|
| 18 |
+
## Quick Start (Local)
|
| 19 |
+
docker run -p 7860:7860 deepfake-detector-api
|
| 20 |
+
```bash
|
| 21 |
+
cd backend
|
| 22 |
+
|
| 23 |
+
# Create virtual environment
|
| 24 |
+
python -m venv venv
|
| 25 |
+
source venv/bin/activate # Windows PowerShell: .\\venv\\Scripts\\Activate.ps1
|
| 26 |
+
|
| 27 |
+
# Install dependencies
|
| 28 |
+
pip install -r requirements.txt
|
| 29 |
+
|
| 30 |
+
# Configure env
|
| 31 |
+
cp .env.example .env # Windows PowerShell: Copy-Item .env.example .env
|
| 32 |
+
|
| 33 |
+
# Run API
|
| 34 |
+
uvicorn app.main:app --reload
|
| 35 |
+
- copy backend files as-is (single `Dockerfile` setup)
|
| 36 |
+
- `http://localhost:8000/redoc`
|
| 37 |
+
|
| 38 |
+
## Environment Configuration
|
| 39 |
+
|
| 40 |
+
Use [backend/.env.example](.env.example) as the source of truth.
|
| 41 |
+
|
| 42 |
+
Common runtime variables:
|
| 43 |
+
- `HF_FUSION_REPO_ID` (default: `DeepFakeDetector/fusion-logreg-final`)
|
| 44 |
+
- `HF_CACHE_DIR` (default: `.hf_cache`)
|
| 45 |
+
- `HF_TOKEN` (optional; required for private model repos or non-interactive HF auth)
|
| 46 |
+
- `GOOGLE_API_KEY` (optional; required for Gemini explanations)
|
| 47 |
+
- `HOST` (default: `0.0.0.0`)
|
| 48 |
+
- `PORT` (default: `8000`)
|
| 49 |
+
- `CORS_ORIGINS` (comma-separated origins)
|
| 50 |
+
- `ENABLE_DEBUG`, `LOG_LEVEL`
|
| 51 |
+
|
| 52 |
+
HF Spaces deploy variables (used by [backend/deploy-to-hf.sh](deploy-to-hf.sh)):
|
| 53 |
+
|
| 54 |
+
- `HF_SPACE_URL`
|
| 55 |
+
- `HF_SPACE_WEB_URL`
|
| 56 |
+
- `HF_SPACE_APP_URL`
|
| 57 |
+
- `HF_DEPLOY_DIR`
|
| 58 |
+
|
| 59 |
+
## API Endpoints
|
| 60 |
+
|
| 61 |
+
- `GET /health` - service health
|
| 62 |
+
- `GET /ready` - readiness (includes model load state)
|
| 63 |
+
- `GET /models` - loaded model metadata
|
| 64 |
+
- `POST /predict` - real/fake prediction
|
| 65 |
+
- `GET /docs` - Swagger UI
|
| 66 |
+
|
| 67 |
+
Example:
|
| 68 |
+
|
| 69 |
+
```bash
|
| 70 |
+
curl -X POST "http://localhost:8000/predict" \
|
| 71 |
+
-F "image=@/path/to/image.jpg"
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
## Docker
|
| 75 |
+
|
| 76 |
+
Build and run locally:
|
| 77 |
+
|
| 78 |
+
```bash
|
| 79 |
+
docker build -t deepfake-detector-api .
|
| 80 |
+
docker run -p 8000:8000 deepfake-detector-api
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
## Deploy to Hugging Face Spaces
|
| 84 |
+
|
| 85 |
+
Recommended path is the Bash deploy script:
|
| 86 |
+
|
| 87 |
+
1. Configure [backend/.env](.env) from [backend/.env.example](.env.example)
|
| 88 |
+
2. Ensure `HF_SPACE_URL` and related deploy variables are set
|
| 89 |
+
3. Run from repo root:
|
| 90 |
+
|
| 91 |
+
```bash
|
| 92 |
+
bash ./deploy-to-hf.sh
|
| 93 |
+
```
|
| 94 |
+
|
| 95 |
+
The script will:
|
| 96 |
+
|
| 97 |
+
- install Hugging Face CLI if needed
|
| 98 |
+
- prompt/authenticate with HF (`hf auth login`) when required
|
| 99 |
+
- clone Space repo into a separate temp deploy directory
|
| 100 |
+
- commit and push to the HF Space
|
| 101 |
+
|
| 102 |
+
After deploy, set Space secrets in Hugging Face:
|
| 103 |
+
|
| 104 |
+
- `GOOGLE_API_KEY` (if using explanation endpoints)
|
| 105 |
+
- `CORS_ORIGINS` (frontend domains)
|
| 106 |
+
|
| 107 |
+
## Troubleshooting
|
| 108 |
+
|
| 109 |
+
- Build fails: inspect Space/Railway logs first
|
| 110 |
+
- HF auth fails: run `hf auth login` and/or set `HF_TOKEN` in `.env`
|
| 111 |
+
- Model loading issues: verify fusion/submodel repo IDs and access
|
| 112 |
+
- CORS issues: ensure frontend domains are in `CORS_ORIGINS`
|
| 113 |
+
|
| 114 |
+
## Project Structure
|
| 115 |
+
|
| 116 |
+
```text
|
| 117 |
+
backend/
|
| 118 |
+
βββ app/
|
| 119 |
+
βββ tests/
|
| 120 |
+
βββ Dockerfile
|
| 121 |
+
βββ Dockerfile.huggingface
|
| 122 |
+
βββ deploy-to-hf.sh
|
| 123 |
+
βββ deploy-to-hf.ps1
|
| 124 |
+
βββ requirements.txt
|
| 125 |
+
βββ README.md
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
+
## License
|
| 129 |
+
|
| 130 |
+
MIT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|