File size: 7,826 Bytes
0af1032
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""
HuggingFace Deepfake Detector
Real pre-trained model for deepfake detection

Installation:
pip install transformers torch torchvision pillow

Usage:
from huggingface_detector import HuggingFaceDeepfakeDetector
detector = HuggingFaceDeepfakeDetector()
result = detector.predict('image.jpg')
"""

from transformers import AutoModelForImageClassification, AutoImageProcessor, AutoFeatureExtractor
import torch
from PIL import Image
import numpy as np
import os
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class HuggingFaceDeepfakeDetector:
    """
    Real deepfake detection using pre-trained models from HuggingFace
    
    Supports multiple pre-trained models:
    1. dima806/deepfake_vs_real_image_detection - Good general purpose
    2. abhinavtripathi/deepfake-detection - Alternative
    3. rizvandwiki/deepfakes-image-detection - Another option
    """
    
    def __init__(self, model_name=None):
        """
        Initialize the detector
        
        Args:
            model_name: HuggingFace model name. If None, tries multiple models.
        """
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        logger.info(f"Using device: {self.device}")
        
        # List of available models to try
        self.available_models = [
            "dima806/deepfake_vs_real_image_detection",
            "abhinavtripathi/deepfake-detection",
            "rizvandwiki/deepfakes-image-detection"
        ]
        
        self.model = None
        self.processor = None
        self.loaded = False
        
        # Try to load model
        if model_name:
            self._load_model(model_name)
        else:
            # Try each model until one works
            for model_name in self.available_models:
                if self._load_model(model_name):
                    break
    
    def _load_model(self, model_name):
        """Load a specific model"""
        try:
            logger.info(f"Loading model: {model_name}")
            
            # Load processor and model
            self.processor = AutoImageProcessor.from_pretrained(model_name)
            self.model = AutoModelForImageClassification.from_pretrained(model_name)
            
            # Move to device
            self.model.to(self.device)
            self.model.eval()
            
            self.loaded = True
            self.model_name = model_name
            logger.info(f"✓ Model loaded successfully: {model_name}")
            return True
            
        except Exception as e:
            logger.warning(f"Failed to load {model_name}: {e}")
            return False
    
    def predict(self, image_path):
        """
        Predict if an image is a deepfake
        
        Args:
            image_path: Path to image file
            
        Returns:
            dict with prediction results
        """
        if not self.loaded:
            logger.error("No model loaded!")
            return {
                'is_deepfake': False,
                'fake_probability': 50.0,
                'real_probability': 50.0,
                'confidence': 0.0,
                'error': 'Model not loaded'
            }
        
        try:
            # Load and preprocess image
            image = Image.open(image_path).convert('RGB')
            
            # Preprocess
            inputs = self.processor(images=image, return_tensors="pt")
            inputs = {k: v.to(self.device) for k, v in inputs.items()}
            
            # Predict
            with torch.no_grad():
                outputs = self.model(**inputs)
                logits = outputs.logits
                probs = torch.softmax(logits, dim=1)
            
            # Get probabilities
            real_prob = probs[0][0].item()
            fake_prob = probs[0][1].item()
            
            # Determine prediction
            is_deepfake = fake_prob > 0.5
            confidence = max(real_prob, fake_prob) * 100
            
            result = {
                'is_deepfake': bool(is_deepfake),
                'fake_probability': float(fake_prob * 100),
                'real_probability': float(real_prob * 100),
                'confidence': float(confidence),
                'model_used': self.model_name
            }
            
            logger.info(f"Prediction: {'FAKE' if is_deepfake else 'REAL'} ({confidence:.1f}% confident)")
            return result
            
        except Exception as e:
            logger.error(f"Prediction failed: {e}")
            return {
                'is_deepfake': False,
                'fake_probability': 50.0,
                'real_probability': 50.0,
                'confidence': 0.0,
                'error': str(e)
            }
    
    def predict_from_array(self, image_array):
        """
        Predict from numpy array (for integration with OpenCV)
        
        Args:
            image_array: numpy array (H, W, C) in BGR format
            
        Returns:
            dict with prediction results
        """
        if not self.loaded:
            return {
                'is_deepfake': False,
                'fake_probability': 50.0,
                'real_probability': 50.0,
                'confidence': 0.0,
                'error': 'Model not loaded'
            }
        
        try:
            # Convert BGR to RGB
            import cv2
            if len(image_array.shape) == 3 and image_array.shape[2] == 3:
                image_array = cv2.cvtColor(image_array, cv2.COLOR_BGR2RGB)
            
            # Convert to PIL Image
            image = Image.fromarray(image_array)
            
            # Preprocess
            inputs = self.processor(images=image, return_tensors="pt")
            inputs = {k: v.to(self.device) for k, v in inputs.items()}
            
            # Predict
            with torch.no_grad():
                outputs = self.model(**inputs)
                probs = torch.softmax(outputs.logits, dim=1)
            
            real_prob = probs[0][0].item()
            fake_prob = probs[0][1].item()
            is_deepfake = fake_prob > 0.5
            confidence = max(real_prob, fake_prob) * 100
            
            return {
                'is_deepfake': bool(is_deepfake),
                'fake_probability': float(fake_prob * 100),
                'real_probability': float(real_prob * 100),
                'confidence': float(confidence),
                'model_used': self.model_name
            }
            
        except Exception as e:
            logger.error(f"Prediction failed: {e}")
            return {
                'is_deepfake': False,
                'fake_probability': 50.0,
                'real_probability': 50.0,
                'confidence': 0.0,
                'error': str(e)
            }


# Example usage
if __name__ == "__main__":
    # Initialize detector
    print("Initializing detector...")
    detector = HuggingFaceDeepfakeDetector()
    
    if detector.loaded:
        print(f"✓ Detector ready! Using model: {detector.model_name}")
        print(f"Device: {detector.device}")
        
        # Test prediction
        test_image = "test_image.jpg"
        if os.path.exists(test_image):
            print(f"\nTesting with {test_image}...")
            result = detector.predict(test_image)
            
            print("\nResults:")
            print(f"  Is Deepfake: {result['is_deepfake']}")
            print(f"  Fake Probability: {result['fake_probability']:.2f}%")
            print(f"  Real Probability: {result['real_probability']:.2f}%")
            print(f"  Confidence: {result['confidence']:.2f}%")
        else:
            print(f"Test image not found: {test_image}")
    else:
        print("✗ Failed to load detector")