File size: 17,360 Bytes
689b21b
 
38d1107
 
689b21b
 
 
87d15c0
 
f40c811
 
8af68c4
f40c811
689b21b
 
9889d4f
38d1107
9889d4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8af68c4
 
 
 
 
 
38d1107
9889d4f
 
 
 
 
 
38d1107
9889d4f
689b21b
 
 
 
87d15c0
689b21b
 
 
87d15c0
689b21b
 
 
 
87d15c0
 
 
689b21b
87d15c0
689b21b
 
 
87d15c0
689b21b
 
87d15c0
 
 
 
 
 
9889d4f
 
 
87d15c0
 
f40c811
 
8af68c4
87d15c0
 
f40c811
87d15c0
 
f40c811
 
 
 
f3f345f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d15c0
 
 
 
 
247f7f7
f40c811
 
 
 
 
 
 
 
 
 
8af68c4
 
f40c811
 
 
 
 
 
 
8af68c4
 
 
 
f40c811
 
87d15c0
247f7f7
 
87d15c0
 
247f7f7
 
0333b4a
247f7f7
87d15c0
 
9889d4f
 
 
 
 
 
87d15c0
689b21b
 
f40c811
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9889d4f
f40c811
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9889d4f
 
 
 
 
8af68c4
9889d4f
 
689b21b
87d15c0
 
 
 
 
 
 
 
 
 
 
 
 
 
247f7f7
87d15c0
 
 
 
 
 
 
 
 
 
9889d4f
 
87d15c0
 
 
 
 
 
 
 
689b21b
87d15c0
 
 
9889d4f
 
 
 
 
 
 
 
 
87d15c0
 
247f7f7
87d15c0
 
 
 
247f7f7
 
8af68c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f40c811
8af68c4
f40c811
 
 
 
8af68c4
 
 
 
 
38d1107
 
 
8af68c4
38d1107
 
 
8af68c4
 
 
 
 
 
38d1107
 
 
8af68c4
38d1107
 
 
8af68c4
 
 
 
9889d4f
8af68c4
f40c811
 
 
7ac5a08
f40c811
 
 
 
 
 
9889d4f
f40c811
 
 
 
 
 
 
247f7f7
f40c811
247f7f7
 
8af68c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ac5a08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247f7f7
87d15c0
 
 
 
689b21b
 
 
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
"use client";
import styles from "./UploadBox.module.css";
import React, { useRef, useState, useEffect, useCallback } from "react";
import Image from "next/image";

export default function UploadBox() {
  const [dragActive, setDragActive] = useState(false);
  const [uploading, setUploading] = useState(false);
  const [result, setResult] = useState(null);
  const [explanation, setExplanation] = useState(null);
  const [gradcamImage, setGradcamImage] = useState(null);
  const [originalImage, setOriginalImage] = useState(null);
  const [loadingExplanation, setLoadingExplanation] = useState(false);
  const fileInputRef = useRef(null);

  // Memory cleanup function
  const cleanupMemory = useCallback(() => {
    // Reset file input
    if (fileInputRef.current) {
      fileInputRef.current.value = '';
    }
    
    // Force garbage collection if available (browser dependent)
    if (window.gc) {
      window.gc();
    }
    
    // Clear any cached URLs
    if (gradcamImage) {
      // If gradcamImage was a blob URL, revoke it
      if (gradcamImage.startsWith('blob:')) {
        URL.revokeObjectURL(gradcamImage);
      }
    }
    if (originalImage) {
      // If originalImage was a blob URL, revoke it
      if (originalImage.startsWith('blob:')) {
        URL.revokeObjectURL(originalImage);
      }
    }
  }, [gradcamImage, originalImage]);

  // Cleanup effect when component unmounts
  useEffect(() => {
    return () => {
      cleanupMemory();
    };
  }, [cleanupMemory]);

  const handleDragOver = e => {
    e.preventDefault();
    setDragActive(true);
  };

  const handleDragLeave = e => {
    setDragActive(false);
  };

  const handleDrop = e => {
    e.preventDefault();
    setDragActive(false);
    const files = e.dataTransfer.files;
    if (files[0]) {
      uploadFile(files[0]);
    }
  };

  const handleClick = () => {
    fileInputRef.current.click();
  };

  const handleChange = e => {
    const file = e.target.files[0];
    if (file) {
      uploadFile(file);
    }
  };

  const uploadFile = async (file) => {
    // Clean up previous analysis memory
    cleanupMemory();
    
    setUploading(true);
    setResult(null);
    setExplanation(null);
    setGradcamImage(null);
    setOriginalImage(null);

    const formData = new FormData();
    formData.append('file', file);

    try {
      // Determine endpoint based on file type
      const isVideo = file.type.startsWith('video/');
      const endpoint = isVideo ? 'analyze/video' : 'analyze/image';
      
      // Derive backend base URL with better dev detection:
      // 1) NEXT_PUBLIC_API_BASE (explicit)
      // 2) If running in Next dev (localhost:3000) prefer local backend at http://localhost:8000
      // 3) Otherwise use same-origin (useful when frontend is served by backend container)
      // 4) Final fallback to http://localhost:8000
      let apiBase = process.env.NEXT_PUBLIC_API_BASE;
      if (!apiBase) {
        if (typeof window !== 'undefined' && window.location) {
          const { hostname, port, origin } = window.location;
          const isLocalhost = hostname === 'localhost' || hostname === '127.0.0.1';
          // If running frontend dev (common port 3000) -> use backend local port 8000
          if (isLocalhost && port && port !== '' && port !== '8000' && port !== '7860') {
            apiBase = 'http://localhost:8000';
          } else {
            apiBase = origin;
          }
        } else {
          apiBase = 'http://localhost:8000';
        }
      }
      const response = await fetch(`${apiBase.replace(/\/$/, '')}/${endpoint}`, {
        method: 'POST',
        body: formData,
      });

      if (response.ok) {
        const data = await response.json();
        // Transform the response to match the frontend's expected format
        const transformedResult = {
          is_deepfake: data.label === 'fake',
          confidence: data.label === 'fake' ? data.confidence : (1 - data.confidence), // How confident we are in the prediction
          fake_probability: data.confidence, // Raw fake probability from model
          faces_detected: data.faces_detected || 1,
          processing_time: data.processing_time || 'N/A',
          models_used: [`${data.details}`],
          file_type: isVideo ? 'video' : 'image',
          fake_frames: data.fake_frames || [], // Frames detected as fake
          gradcam_image: data.gradcam_image || null, // Base64 encoded Grad-CAM image
          original_image: data.original_image || null // Base64 encoded original image
        };
        setResult(transformedResult);
        
        if (data.gradcam_image) {
          setGradcamImage(data.gradcam_image);
        }
        
        if (data.original_image) {
          setOriginalImage(data.original_image);
        }
        
        // Get AI explanation after getting the result
        generateExplanation(transformedResult);
      } else {
        const errorData = await response.json();
        setResult({ error: errorData.error || 'Detection failed' });
      }
    } catch (error) {
      console.error('Backend error:', error);
      setResult({ 
        error: 'Backend connection failed. Ensure API container is up (check port 7860 or set NEXT_PUBLIC_API_BASE).' 
      });
    } finally {
      setUploading(false);
      // Additional cleanup after upload completes
      setTimeout(() => {
        if (window.gc) {
          window.gc();
        }
      }, 1000);
    }
  };

  const generateExplanation = async (analysisResult) => {
    setLoadingExplanation(true);
    try {
      const prompt = `Based on this deepfake analysis result, provide a clear explanation of why this ${analysisResult.file_type} was classified as ${analysisResult.is_deepfake ? 'fake' : 'real'}:

- Status: ${analysisResult.is_deepfake ? 'DEEPFAKE DETECTED' : 'APPEARS AUTHENTIC'}
- Confidence: ${(analysisResult.confidence * 100).toFixed(1)}%
- Fake Probability: ${(analysisResult.fake_probability * 100).toFixed(1)}%
- File Type: ${analysisResult.file_type}
${analysisResult.fake_frames.length > 0 ? `- Suspicious Frames: ${analysisResult.fake_frames.join(', ')}` : ''}

Please explain in simple terms what factors likely contributed to this classification and what users should look for when detecting deepfakes.`;

      const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${process.env.NEXT_PUBLIC_OPENROUTER_API_KEY}`,
          'HTTP-Referer': window.location.origin,
          'X-Title': 'UnMask Deepfake Detector',
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          model: 'meta-llama/llama-3.3-70b-instruct:free',
          messages: [
            {
              role: 'system',
              content: 'You are an AI expert in deepfake detection. Provide clear, educational explanations about deepfake analysis results in 2-3 sentences.'
            },
            {
              role: 'user',
              content: prompt
            }
          ],
          max_tokens: 150,
          temperature: 0.7,
        }),
      });

      console.log('OpenRouter response status:', response.status);
      
      if (response.ok) {
        const data = await response.json();
        console.log('OpenRouter full response:', JSON.stringify(data, null, 2));
        
        if (data.choices && Array.isArray(data.choices) && data.choices.length > 0) {
          const firstChoice = data.choices[0];
          console.log('First choice:', firstChoice);
          
          if (firstChoice.message && firstChoice.message.content) {
            const content = firstChoice.message.content.trim();
            console.log('Extracted content:', content);
            setExplanation(content);
          } else {
            console.error('Missing message content in choice:', firstChoice);
            setExplanation('AI explanation received but content was empty.');
          }
        } else {
          console.error('No choices in response:', data);
          setExplanation('AI explanation service returned no results.');
        }
      } else {
        const errorData = await response.text();
        console.error('OpenRouter error:', response.status, errorData);
        setExplanation(`Unable to generate AI explanation (Error ${response.status}). Please check your API key.`);
      }
    } catch (error) {
      console.error('Explanation generation error:', error);
      setExplanation('Unable to generate AI explanation at this time.');
    } finally {
      setLoadingExplanation(false);
    }
  };

  const clearResults = () => {
    cleanupMemory();
    setResult(null);
    setExplanation(null);
    setGradcamImage(null);
    setOriginalImage(null);
  };

  return (
    <div className={styles.container}>
      <div
        className={dragActive ? `${styles["upload-box"]} ${styles["dragover"]}` : styles["upload-box"]}
        onDragOver={handleDragOver}
        onDragLeave={handleDragLeave}
        onDrop={handleDrop}
        onClick={handleClick}
      >
        <input
          ref={fileInputRef}
          id="fileInput"
          type="file"
          style={{ display: "none" }}
          onChange={handleChange}
          accept="video/*,image/*"
        />
        <div className={styles["upload-box-content"]}>
          {uploading ? (
            <div className={styles.loading}>
              <div className={styles.spinner}></div>
              <p>Analyzing file...</p>
            </div>
          ) : (
            <>
              <svg width="48" height="48" fill="none" viewBox="0 0 48 48" className={styles["upload-box-icon"]}>
                <rect width="48" height="48" rx="12" fill="#334155" />
                <path d="M24 14v20M24 14l-6 6M24 14l6 6" stroke="#60a5fa" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
              </svg>
              <p className={styles["upload-box-title"]}>Drag & drop a file here</p>
              <p className={styles["upload-box-desc"]}>
                or <span className={styles["upload-box-browse"]}>click to browse</span>
              </p>
            </>
          )}
        </div>
      </div>

      {result && (
        <div className={styles.result}>
          <div className={styles.resultHeader}>
            <button 
              onClick={clearResults} 
              className={styles.clearButton}
              title="Clear results and free memory"
            >
              πŸ—‘οΈ Clear Results
            </button>
          </div>
          {result.error ? (
            <div className={styles.error}>
              <h3>❌ Error</h3>
              <p>{result.error}</p>
            </div>
          ) : (
            <div className={styles.success}>
              <h3>🎯 Detection Result</h3>
              <div className={styles.resultGrid}>
                
                {/* Basic Results - Always Show */}
                <div className={styles.mainResult}>
                  <p><strong>Status:</strong> 
                    <span className={result.is_deepfake ? styles.fake : styles.real}>
                      {result.is_deepfake ? '🚨 DEEPFAKE DETECTED' : 'βœ… APPEARS AUTHENTIC'}
                    </span>
                  </p>
                  <p><strong>Confidence:</strong> {(result.confidence * 100).toFixed(1)}%</p>
                  <p><strong>Fake Probability:</strong> {(result.fake_probability * 100).toFixed(1)}%</p>
                  <p><strong>File Type:</strong> {result.file_type}</p>
                  <p><strong>Faces Detected:</strong> {result.faces_detected}</p>
                  <p><strong>Processing Time:</strong> {result.processing_time}</p>
                </div>

                {/* Grad-CAM Visualization with AI Explanation */}
                {(gradcamImage || originalImage) && (
                  <div className={styles.gradcam}>
                    <h4>πŸ” Visual Explanation (Grad-CAM)</h4>
                    <div style={{ display: 'flex', alignItems: 'flex-start', gap: '32px' }}>
                      <div>
                        {/* Image comparison - Original vs Grad-CAM */}
                        <div style={{ display: 'flex', gap: '16px', marginBottom: '16px' }}>
                          {originalImage && (
                            <div>
                              <h5 style={{ margin: '0 0 8px 0', color: '#c7d2fe', fontSize: '0.9rem' }}>Original Frame</h5>
                              <Image
                                src={`data:image/png;base64,${originalImage}`}
                                alt="Original frame"
                                className={styles.gradcamImage}
                                width={400}
                                height={400}
                                style={{ height: 'auto', border: '2px solid #4ade80' }}
                              />
                            </div>
                          )}
                          {gradcamImage && (
                            <div>
                              <h5 style={{ margin: '0 0 8px 0', color: '#c7d2fe', fontSize: '0.9rem' }}>Grad-CAM Analysis</h5>
                              <Image
                                src={`data:image/png;base64,${gradcamImage}`}
                                alt="Grad-CAM visualization"
                                className={styles.gradcamImage}
                                width={400}
                                height={400}
                                style={{ height: 'auto', border: '2px solid #f87171' }}
                              />
                            </div>
                          )}
                        </div>
                        <p style={{ marginTop: '8px', color: '#c7d2fe', fontWeight: 500 }}>
                          <span style={{ fontWeight: 600 }}>Red areas</span> in the Grad-CAM show regions the AI focused on when making its decision.
                        </p>
                      </div>
                      <div style={{ flex: 1 }}>
                        <h4 style={{paddingTop:'20px', margin: '0 0 8px 0', color: '#5eead4', fontSize: '1.05rem', fontWeight: 600 }}>πŸ€– AI Explanation</h4>
                        {loadingExplanation ? (
                          <div className={styles.loadingExplain}>
                            <div className={styles.loadingSpinner}></div>
                            <span>Generating explanation...</span>
                          </div>
                        ) : explanation ? (
                          <div style={{ background: '#0f766e', padding: '12px 16px', borderRadius: '8px', border: '1px solid #14b8a6', color: '#7dd3fc', fontSize: '0.98rem', fontWeight: 500, lineHeight: 1.7 }}>
                            {explanation.split(/\n|\r/).map((line, idx) => (
                              <p key={idx} style={{ margin: '8px 0' }}>{line.trim()}</p>
                            ))}
                          </div>
                        ) : (
                          <p>AI explanation will appear here after analysis.</p>
                        )}
                      </div>
                    </div>
                  </div>
                )}

                {/* AI Explanation when no Grad-CAM available */}
                {!(gradcamImage || originalImage) && (
                  <div className={styles.explanation}>
                    <h4 style={{ margin: '16px 0 8px 0', color: '#5eead4', fontSize: '1.05rem', fontWeight: 600 }}>πŸ€– AI Explanation</h4>
                    {loadingExplanation ? (
                      <div className={styles.loadingExplain}>
                        <div className={styles.loadingSpinner}></div>
                        <span>Generating explanation...</span>
                      </div>
                    ) : explanation ? (
                      <div style={{ background: '#0f766e', padding: '12px 16px', borderRadius: '8px', border: '1px solid #14b8a6', color: '#7dd3fc', fontSize: '0.98rem', fontWeight: 500, lineHeight: 1.7 }}>
                        {explanation.split(/\n|\r/).map((line, idx) => (
                          <p key={idx} style={{ margin: '8px 0' }}>{line.trim()}</p>
                        ))}
                      </div>
                    ) : (
                      <p>AI explanation will appear here after analysis.</p>
                    )}
                  </div>
                )}
                
                
                

                {/* Frame Analysis for Videos */}
                {result.file_type === 'video' && result.fake_frames && result.fake_frames.length > 0 && (
                  <div className={styles.frameAnalysis}>
                    <h4>🎬 Suspicious Frames Detected</h4>
                    <p>The following frames were flagged as potentially manipulated:</p>
                    <div className={styles.frameList}>
                      {result.fake_frames.map((frame, index) => (
                        <div key={index} className={styles.frameItem}>
                          Frame {frame}
                        </div>
                      ))}
                    </div>
                  </div>
                )}

                
              </div>
            </div>
          )}
        </div>
      )}
    </div>
  );
}