File size: 3,180 Bytes
c9721cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 🧠 DeepFake Detector V15

**Self-Learning Deepfake Detector with Web Search Integration**

## ✨ Features

- πŸ” **Real Web Search** - SerpAPI reverse image + Serper text search
- 🧠 **Self-Learning** - Improves from user feedback
- πŸ›‘οΈ **EWC Protection** - Never forgets old knowledge
- πŸ“ˆ **Progressive** - Gets smarter over time

## πŸ“Š Architecture

| Component | Parameters | Trainable |
|-----------|------------|-----------|
| Swin-Large Backbone | 197M | ❌ Frozen |
| Adapter Layers | 1.5M | βœ… Yes |
| **Total** | **198.5M** | 1.5M |

## πŸš€ Quick Start

```python
import torch
import timm
from safetensors.torch import load_file
from torchvision import transforms
from PIL import Image

class DeepfakeDetector(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.backbone = timm.create_model('swin_large_patch4_window7_224', 
                                          pretrained=False, num_classes=0)
        feat_dim = 1536
        
        self.adapter = torch.nn.Sequential(
            torch.nn.Linear(feat_dim, 512),
            torch.nn.LayerNorm(512),
            torch.nn.ReLU(),
            torch.nn.Dropout(0.1),
            torch.nn.Linear(512, feat_dim)
        )
        
        self.classifier = torch.nn.Sequential(
            torch.nn.Linear(feat_dim, 512),
            torch.nn.BatchNorm1d(512),
            torch.nn.GELU(),
            torch.nn.Dropout(0.3),
            torch.nn.Linear(512, 128),
            torch.nn.BatchNorm1d(128),
            torch.nn.GELU(),
            torch.nn.Dropout(0.15),
            torch.nn.Linear(128, 1)
        )
    
    def forward(self, x):
        features = self.backbone(x)
        adapted = features + 0.1 * self.adapter(features)
        return self.classifier(adapted).squeeze(-1)

# Load
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = DeepfakeDetector()
model.load_state_dict(load_file("model.safetensors"))
model = model.to(device)
model.eval()

# Preprocess
transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

# Predict
image = Image.open("test.jpg").convert("RGB")
with torch.no_grad():
    prob = torch.sigmoid(model(transform(image).unsqueeze(0).to(device))).item()

print(f"Fake: {prob:.1%}" if prob > 0.5 else f"Real: {1-prob:.1%}")
```

## πŸ“ˆ Performance

| Version | F1 Score | Improvement |
|---------|----------|-------------|
| V14 Base | 0.9586 | - |
| V15 (+50 samples) | ~0.962 | +0.3% |
| V15 (+200 samples) | ~0.968 | +1.0% |
| V15 (+500 samples) | ~0.975 | +1.6% |

## 🌐 Web Search Integration

V15 uses two APIs for verification:

- **SerpAPI** - Google reverse image search (finds where image exists online)
- **Serper.dev** - Text search (finds deepfake mentions)

## πŸ”§ Self-Learning

Uses **Elastic Weight Consolidation (EWC)** to:
- Learn from new user feedback
- Without forgetting previous knowledge
- Only trains adapter layers (fast!)

## πŸ“š Model Lineage

`V12 β†’ V13 β†’ V14 β†’ V15 (Self-Learning)`

## πŸ“„ License

MIT

---
**Built with PyTorch, timm, and Gradio**