lukhsaankumar commited on
Commit
7ee6359
Β·
1 Parent(s): 47b4cf3

Deploy DeepFake Detector API - 2026-04-20 00:17:18

Browse files
Files changed (2) hide show
  1. Dockerfile +47 -47
  2. README.md +130 -182
Dockerfile CHANGED
@@ -1,47 +1,47 @@
1
- # DeepFake Detector API - Hugging Face Spaces Docker Image
2
- # Optimized for HF Spaces deployment with GPU support
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 HF Spaces port
44
- EXPOSE 7860
45
-
46
- # Run the application (start.sh already defaults to port 7860)
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
- title: DeepFake Detector API
3
- emoji: 🎭
4
- colorFrom: blue
5
- colorTo: purple
6
- sdk: docker
7
- app_port: 7860
8
- ---
9
-
10
- # 🎭 DeepFake Detector API
11
-
12
- FastAPI backend for detecting AI-generated (deepfake) images using an ensemble of state-of-the-art deep learning models.
13
-
14
- ## πŸ€– Models
15
-
16
- This API uses a fusion ensemble of 5 deep learning models:
17
-
18
- - **CNN Transfer** (EfficientNet-B0) - Transfer learning from ImageNet
19
- - **ViT Base** (Vision Transformer) - Attention-based architecture
20
- - **DeiT Distilled** (Data-efficient Image Transformer) - Distilled ViT variant
21
- - **Gradient Field CNN** - Custom architecture analyzing gradient patterns
22
- - **FFT CNN** - Frequency domain analysis using Fast Fourier Transform
23
-
24
- All models are combined using a **Logistic Regression stacking ensemble** for optimal accuracy.
25
-
26
- ## πŸ”— API Endpoints
27
-
28
- | Endpoint | Method | Description |
29
- |----------|--------|-------------|
30
- | `/health` | GET | Health check - returns API status |
31
- | `/ready` | GET | Model readiness check - confirms models are loaded |
32
- | `/models` | GET | List all loaded models with metadata |
33
- | `/predict` | POST | Predict if an image is real or AI-generated |
34
- | `/docs` | GET | Interactive Swagger API documentation |
35
- | `/redoc` | GET | Alternative API documentation |
36
-
37
- ## πŸš€ Usage Example
38
-
39
- ### Using cURL
40
-
41
- ```bash
42
- # Check if API is ready
43
- curl https://lukhsaankumar-deepfakedetectorbackend.hf.space/ready
44
-
45
- # Make a prediction
46
- curl -X POST "https://lukhsaankumar-deepfakedetectorbackend.hf.space/predict" \
47
- -F "file=@image.jpg" \
48
- -F "explain=true"
49
- ```
50
-
51
- ### Using Python
52
-
53
- ```python
54
- import requests
55
-
56
- # Upload an image for prediction
57
- url = "https://lukhsaankumar-deepfakedetectorbackend.hf.space/predict"
58
- files = {"file": open("image.jpg", "rb")}
59
- data = {"explain": True}
60
-
61
- response = requests.post(url, files=files, data=data)
62
- result = response.json()
63
-
64
- print(f"Prediction: {result['prediction']}")
65
- print(f"Confidence: {result['confidence']:.2%}")
66
- print(f"Explanation: {result['explanation']}")
67
- ```
68
-
69
- ## 🎯 Response Format
70
-
71
- ```json
72
- {
73
- "prediction": "fake",
74
- "confidence": 0.8734,
75
- "probabilities": {
76
- "real": 0.1266,
77
- "fake": 0.8734
78
- },
79
- "model_predictions": {
80
- "cnn_transfer": {"prediction": "fake", "confidence": 0.89},
81
- "vit_base": {"prediction": "fake", "confidence": 0.92},
82
- "deit": {"prediction": "fake", "confidence": 0.85},
83
- "gradient_field": {"prediction": "real", "confidence": 0.55},
84
- "fft_cnn": {"prediction": "fake", "confidence": 0.78}
85
- },
86
- "fusion_confidence": 0.8734,
87
- "explanation": "AI-powered analysis of the prediction...",
88
- "processing_time_ms": 342
89
- }
90
- ```
91
-
92
- ## πŸ”§ Configuration
93
-
94
- ### Required Secrets
95
-
96
- Set these in your Space Settings β†’ Repository secrets:
97
-
98
- | Secret | Description | Required |
99
- |--------|-------------|----------|
100
- | `GOOGLE_API_KEY` | Google Gemini API key for AI explanations | Yes |
101
- | `HF_TOKEN` | Hugging Face token (auto-set by Spaces) | No |
102
-
103
- ### Optional Environment Variables
104
-
105
- | Variable | Default | Description |
106
- |----------|---------|-------------|
107
- | `HF_FUSION_REPO_ID` | `DeepFakeDetector/fusion-logreg-final` | Hugging Face model repository |
108
- | `CORS_ORIGINS` | Multiple defaults | Comma-separated allowed CORS origins |
109
- | `GEMINI_MODEL` | `gemini-2.5-flash` | Gemini model for explanations |
110
-
111
- ## πŸ—οΈ Architecture
112
-
113
- ```
114
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
115
- β”‚ Client β”‚
116
- β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
117
- β”‚
118
- β–Ό
119
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
120
- β”‚ FastAPI Backend β”‚
121
- β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
122
- β”‚ β”‚ Model Registry β”‚ β”‚
123
- β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
124
- β”‚ β”‚ β”‚ CNN Transfer β”‚ β”‚ β”‚
125
- β”‚ β”‚ β”‚ ViT Base β”‚ β”‚ β”‚
126
- β”‚ β”‚ β”‚ DeiT Distilled β”‚ β”‚ β”‚
127
- β”‚ β”‚ β”‚ Gradient Field β”‚ β”‚ β”‚
128
- β”‚ β”‚ β”‚ FFT CNN β”‚ β”‚ β”‚
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