Spaces:
Running
Running
feat: integrate deepfake analysis and update XAI to use Qwen locally
Browse files- Linux - Shortcut.lnk +0 -0
- dashboard/src/App.jsx +2 -0
- dashboard/src/components/Sidebar.jsx +23 -4
- dashboard/src/pages/DeepfakePage.css +542 -0
- dashboard/src/pages/DeepfakePage.jsx +425 -0
- requirements.txt +5 -0
- scratch/benchmark_ensemble.py +250 -0
- scratch/benchmark_ensemble_v2.py +310 -0
- scratch/benchmark_temporal.py +190 -0
- scratch/benchmark_v4_gridsearch.py +173 -0
- scratch/regenerate_explanations.py +55 -0
- scratch/test_classifier.py +20 -0
- scratch/test_classifier2.py +20 -0
- scratch/test_community_model.py +43 -0
- scratch/test_cropping.py +27 -0
- scratch/test_cross.py +19 -0
- scratch/test_deberta.py +19 -0
- scratch/test_deepfake.py +24 -0
- scratch/test_deepfake_api.py +21 -0
- scratch/test_deepfake_video_unit.py +12 -0
- scratch/test_dima_model.py +43 -0
- scratch/test_fake_news.py +14 -0
- scratch/test_flan.py +17 -0
- scratch/test_flan2.py +16 -0
- scratch/test_hf_api.py +15 -0
- scratch/test_newspaper.py +13 -0
- scratch/test_qwen.py +25 -0
- scratch/test_qwen2.py +27 -0
- scratch/test_siglip2.py +43 -0
- scratch/test_videos_batch.py +16 -0
- scratch/test_xai.py +23 -0
- scratch/test_zero_shot.py +20 -0
- scratch/test_zero_shot2.py +18 -0
- scratch/validate_production.py +22 -0
- src/api/main.py +70 -1
- src/intelligence/deepfake_detector.py +506 -0
- src/intelligence/fake_news.py +45 -29
- src/maintenance/reprocess_all.py +1 -1
- stop.bat +30 -0
Linux - Shortcut.lnk
ADDED
|
Binary file (347 Bytes). View file
|
|
|
dashboard/src/App.jsx
CHANGED
|
@@ -5,6 +5,7 @@ import ArticleCard from './components/ArticleCard';
|
|
| 5 |
import DiscordLanding from './pages/DiscordLanding';
|
| 6 |
import WhatsAppLanding from './pages/WhatsAppLanding';
|
| 7 |
import VerifyPage from './pages/VerifyPage';
|
|
|
|
| 8 |
import './index.css';
|
| 9 |
|
| 10 |
function App() {
|
|
@@ -222,6 +223,7 @@ function App() {
|
|
| 222 |
<Route path="/discord" element={<DiscordLanding />} />
|
| 223 |
<Route path="/whatsapp" element={<WhatsAppLanding />} />
|
| 224 |
<Route path="/verify" element={<VerifyPage />} />
|
|
|
|
| 225 |
</Routes>
|
| 226 |
</main>
|
| 227 |
</div>
|
|
|
|
| 5 |
import DiscordLanding from './pages/DiscordLanding';
|
| 6 |
import WhatsAppLanding from './pages/WhatsAppLanding';
|
| 7 |
import VerifyPage from './pages/VerifyPage';
|
| 8 |
+
import DeepfakePage from './pages/DeepfakePage';
|
| 9 |
import './index.css';
|
| 10 |
|
| 11 |
function App() {
|
|
|
|
| 223 |
<Route path="/discord" element={<DiscordLanding />} />
|
| 224 |
<Route path="/whatsapp" element={<WhatsAppLanding />} />
|
| 225 |
<Route path="/verify" element={<VerifyPage />} />
|
| 226 |
+
<Route path="/deepfake" element={<DeepfakePage />} />
|
| 227 |
</Routes>
|
| 228 |
</main>
|
| 229 |
</div>
|
dashboard/src/components/Sidebar.jsx
CHANGED
|
@@ -15,6 +15,7 @@ const Sidebar = ({ stats, currentFilter, setCurrentFilter }) => {
|
|
| 15 |
const isDiscordActive = location.pathname === '/discord';
|
| 16 |
const isWhatsAppActive = location.pathname === '/whatsapp';
|
| 17 |
const isVerifyActive = location.pathname === '/verify';
|
|
|
|
| 18 |
|
| 19 |
return (
|
| 20 |
<aside className="sidebar">
|
|
@@ -26,21 +27,21 @@ const Sidebar = ({ stats, currentFilter, setCurrentFilter }) => {
|
|
| 26 |
<div className="filter-section">
|
| 27 |
<h3>Main Intelligence</h3>
|
| 28 |
<button
|
| 29 |
-
className={`filter-button ${(!isDiscordActive && !isWhatsAppActive && !isVerifyActive && currentFilter === 'all') ? 'active' : ''}`}
|
| 30 |
onClick={() => handleFilterClick('all')}
|
| 31 |
>
|
| 32 |
<span>Global Feed</span>
|
| 33 |
<span className="badge">{stats?.total_articles || 0}</span>
|
| 34 |
</button>
|
| 35 |
<button
|
| 36 |
-
className={`filter-button ${(!isDiscordActive && !isWhatsAppActive && !isVerifyActive && currentFilter === 'real') ? 'active' : ''}`}
|
| 37 |
onClick={() => handleFilterClick('real')}
|
| 38 |
>
|
| 39 |
<span>Verified Authentic</span>
|
| 40 |
<span className="badge">{stats?.real_articles || 0}</span>
|
| 41 |
</button>
|
| 42 |
<button
|
| 43 |
-
className={`filter-button ${(!isDiscordActive && !isWhatsAppActive && !isVerifyActive && currentFilter === 'fake') ? 'active' : ''}`}
|
| 44 |
onClick={() => handleFilterClick('fake')}
|
| 45 |
>
|
| 46 |
<span>Flagged Fake</span>
|
|
@@ -101,6 +102,24 @@ const Sidebar = ({ stats, currentFilter, setCurrentFilter }) => {
|
|
| 101 |
</svg>
|
| 102 |
<span>Verify URL</span>
|
| 103 |
</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
</div>
|
| 105 |
|
| 106 |
<div className="filter-section" style={{ marginTop: 'auto' }}>
|
|
@@ -108,7 +127,7 @@ const Sidebar = ({ stats, currentFilter, setCurrentFilter }) => {
|
|
| 108 |
{stats?.categories && Object.entries(stats.categories).map(([category, count]) => (
|
| 109 |
<button
|
| 110 |
key={category}
|
| 111 |
-
className={`filter-button ${(!isDiscordActive && !isWhatsAppActive && !isVerifyActive && currentFilter === category) ? 'active' : ''}`}
|
| 112 |
onClick={() => handleFilterClick(category)}
|
| 113 |
>
|
| 114 |
<span style={{ textTransform: 'capitalize' }}>{category}</span>
|
|
|
|
| 15 |
const isDiscordActive = location.pathname === '/discord';
|
| 16 |
const isWhatsAppActive = location.pathname === '/whatsapp';
|
| 17 |
const isVerifyActive = location.pathname === '/verify';
|
| 18 |
+
const isDeepfakeActive = location.pathname === '/deepfake';
|
| 19 |
|
| 20 |
return (
|
| 21 |
<aside className="sidebar">
|
|
|
|
| 27 |
<div className="filter-section">
|
| 28 |
<h3>Main Intelligence</h3>
|
| 29 |
<button
|
| 30 |
+
className={`filter-button ${(!isDiscordActive && !isWhatsAppActive && !isVerifyActive && !isDeepfakeActive && currentFilter === 'all') ? 'active' : ''}`}
|
| 31 |
onClick={() => handleFilterClick('all')}
|
| 32 |
>
|
| 33 |
<span>Global Feed</span>
|
| 34 |
<span className="badge">{stats?.total_articles || 0}</span>
|
| 35 |
</button>
|
| 36 |
<button
|
| 37 |
+
className={`filter-button ${(!isDiscordActive && !isWhatsAppActive && !isVerifyActive && !isDeepfakeActive && currentFilter === 'real') ? 'active' : ''}`}
|
| 38 |
onClick={() => handleFilterClick('real')}
|
| 39 |
>
|
| 40 |
<span>Verified Authentic</span>
|
| 41 |
<span className="badge">{stats?.real_articles || 0}</span>
|
| 42 |
</button>
|
| 43 |
<button
|
| 44 |
+
className={`filter-button ${(!isDiscordActive && !isWhatsAppActive && !isVerifyActive && !isDeepfakeActive && currentFilter === 'fake') ? 'active' : ''}`}
|
| 45 |
onClick={() => handleFilterClick('fake')}
|
| 46 |
>
|
| 47 |
<span>Flagged Fake</span>
|
|
|
|
| 102 |
</svg>
|
| 103 |
<span>Verify URL</span>
|
| 104 |
</button>
|
| 105 |
+
<button
|
| 106 |
+
className={`filter-button ${isDeepfakeActive ? 'active' : ''}`}
|
| 107 |
+
onClick={() => navigate('/deepfake')}
|
| 108 |
+
style={{
|
| 109 |
+
display: 'flex',
|
| 110 |
+
alignItems: 'center',
|
| 111 |
+
gap: '0.5rem',
|
| 112 |
+
color: isDeepfakeActive ? '#a855f7' : 'var(--text-muted)',
|
| 113 |
+
backgroundColor: isDeepfakeActive ? 'rgba(168, 85, 247, 0.1)' : 'transparent',
|
| 114 |
+
fontWeight: isDeepfakeActive ? '600' : '500'
|
| 115 |
+
}}
|
| 116 |
+
>
|
| 117 |
+
<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
| 118 |
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
|
| 119 |
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
|
| 120 |
+
</svg>
|
| 121 |
+
<span>Deepfake Detector</span>
|
| 122 |
+
</button>
|
| 123 |
</div>
|
| 124 |
|
| 125 |
<div className="filter-section" style={{ marginTop: 'auto' }}>
|
|
|
|
| 127 |
{stats?.categories && Object.entries(stats.categories).map(([category, count]) => (
|
| 128 |
<button
|
| 129 |
key={category}
|
| 130 |
+
className={`filter-button ${(!isDiscordActive && !isWhatsAppActive && !isVerifyActive && !isDeepfakeActive && currentFilter === category) ? 'active' : ''}`}
|
| 131 |
onClick={() => handleFilterClick(category)}
|
| 132 |
>
|
| 133 |
<span style={{ textTransform: 'capitalize' }}>{category}</span>
|
dashboard/src/pages/DeepfakePage.css
ADDED
|
@@ -0,0 +1,542 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 2 |
+
DeepfakePage β Premium dark UI for deepfake detection
|
| 3 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 4 |
+
|
| 5 |
+
.df-page {
|
| 6 |
+
padding: 2rem;
|
| 7 |
+
max-width: 900px;
|
| 8 |
+
margin: 0 auto;
|
| 9 |
+
animation: fadeInUp 0.5s ease;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
@keyframes fadeInUp {
|
| 13 |
+
from { opacity: 0; transform: translateY(20px); }
|
| 14 |
+
to { opacity: 1; transform: translateY(0); }
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
/* ββ Header ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 18 |
+
.df-header {
|
| 19 |
+
display: flex;
|
| 20 |
+
align-items: center;
|
| 21 |
+
gap: 1rem;
|
| 22 |
+
margin-bottom: 2rem;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
.df-header-icon {
|
| 26 |
+
width: 56px;
|
| 27 |
+
height: 56px;
|
| 28 |
+
border-radius: 16px;
|
| 29 |
+
background: linear-gradient(135deg, rgba(168, 85, 247, 0.15), rgba(139, 92, 246, 0.08));
|
| 30 |
+
border: 1px solid rgba(168, 85, 247, 0.2);
|
| 31 |
+
display: flex;
|
| 32 |
+
align-items: center;
|
| 33 |
+
justify-content: center;
|
| 34 |
+
color: #a855f7;
|
| 35 |
+
flex-shrink: 0;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
.df-title {
|
| 39 |
+
font-size: 1.6rem;
|
| 40 |
+
font-weight: 700;
|
| 41 |
+
color: var(--text-primary);
|
| 42 |
+
margin: 0;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
.df-subtitle {
|
| 46 |
+
font-size: 0.9rem;
|
| 47 |
+
color: var(--text-muted);
|
| 48 |
+
margin: 4px 0 0;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
/* ββ Dropzone ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 52 |
+
.df-dropzone {
|
| 53 |
+
border: 2px dashed rgba(168, 85, 247, 0.25);
|
| 54 |
+
border-radius: 20px;
|
| 55 |
+
padding: 3rem 2rem;
|
| 56 |
+
text-align: center;
|
| 57 |
+
cursor: pointer;
|
| 58 |
+
transition: all 0.3s ease;
|
| 59 |
+
background: rgba(168, 85, 247, 0.03);
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
.df-dropzone:hover,
|
| 63 |
+
.df-dropzone-active {
|
| 64 |
+
border-color: #a855f7;
|
| 65 |
+
background: rgba(168, 85, 247, 0.08);
|
| 66 |
+
transform: scale(1.01);
|
| 67 |
+
box-shadow: 0 0 40px rgba(168, 85, 247, 0.1);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
.df-dropzone-icon {
|
| 71 |
+
color: #a855f7;
|
| 72 |
+
margin-bottom: 1rem;
|
| 73 |
+
opacity: 0.7;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
.df-dropzone-text {
|
| 77 |
+
font-size: 1.1rem;
|
| 78 |
+
font-weight: 600;
|
| 79 |
+
color: var(--text-primary);
|
| 80 |
+
margin-bottom: 0.5rem;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
.df-dropzone-hint {
|
| 84 |
+
font-size: 0.8rem;
|
| 85 |
+
color: var(--text-muted);
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
/* ββ Preview Section βββββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 89 |
+
.df-preview-section {
|
| 90 |
+
animation: fadeInUp 0.4s ease;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
.df-preview-card {
|
| 94 |
+
background: var(--surface-card);
|
| 95 |
+
border: 1px solid var(--border-subtle);
|
| 96 |
+
border-radius: 16px;
|
| 97 |
+
overflow: hidden;
|
| 98 |
+
margin-bottom: 1.5rem;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
.df-preview-media {
|
| 102 |
+
width: 100%;
|
| 103 |
+
max-height: 400px;
|
| 104 |
+
object-fit: contain;
|
| 105 |
+
background: #0a0a0f;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
.df-preview-info {
|
| 109 |
+
padding: 1rem 1.25rem;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
.df-preview-name {
|
| 113 |
+
font-weight: 600;
|
| 114 |
+
color: var(--text-primary);
|
| 115 |
+
margin: 0 0 4px;
|
| 116 |
+
font-size: 0.95rem;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
.df-preview-meta {
|
| 120 |
+
font-size: 0.8rem;
|
| 121 |
+
color: var(--text-muted);
|
| 122 |
+
margin: 0;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
/* ββ Action Row ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 126 |
+
.df-action-row {
|
| 127 |
+
display: flex;
|
| 128 |
+
gap: 0.75rem;
|
| 129 |
+
justify-content: flex-end;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
.df-btn {
|
| 133 |
+
display: inline-flex;
|
| 134 |
+
align-items: center;
|
| 135 |
+
gap: 0.5rem;
|
| 136 |
+
padding: 0.7rem 1.5rem;
|
| 137 |
+
border-radius: 12px;
|
| 138 |
+
font-size: 0.9rem;
|
| 139 |
+
font-weight: 600;
|
| 140 |
+
border: none;
|
| 141 |
+
cursor: pointer;
|
| 142 |
+
transition: all 0.25s ease;
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
.df-btn-primary {
|
| 146 |
+
background: linear-gradient(135deg, #a855f7, #7c3aed);
|
| 147 |
+
color: #fff;
|
| 148 |
+
box-shadow: 0 4px 20px rgba(168, 85, 247, 0.3);
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
.df-btn-primary:hover {
|
| 152 |
+
transform: translateY(-2px);
|
| 153 |
+
box-shadow: 0 8px 30px rgba(168, 85, 247, 0.4);
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
.df-btn-secondary {
|
| 157 |
+
background: var(--surface-card);
|
| 158 |
+
color: var(--text-muted);
|
| 159 |
+
border: 1px solid var(--border-subtle);
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
.df-btn-secondary:hover {
|
| 163 |
+
background: var(--surface-hover);
|
| 164 |
+
color: var(--text-primary);
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
/* ββ Loading Skeleton ββββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 168 |
+
.df-loading {
|
| 169 |
+
animation: fadeInUp 0.4s ease;
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
.df-loading-card {
|
| 173 |
+
display: flex;
|
| 174 |
+
gap: 1.5rem;
|
| 175 |
+
background: var(--surface-card);
|
| 176 |
+
border: 1px solid var(--border-subtle);
|
| 177 |
+
border-radius: 16px;
|
| 178 |
+
padding: 1.5rem;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
.df-loading-preview {
|
| 182 |
+
width: 200px;
|
| 183 |
+
height: 150px;
|
| 184 |
+
border-radius: 12px;
|
| 185 |
+
flex-shrink: 0;
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
.df-loading-details {
|
| 189 |
+
flex: 1;
|
| 190 |
+
display: flex;
|
| 191 |
+
flex-direction: column;
|
| 192 |
+
justify-content: center;
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
/* ββ Error ββββββββββββββββββββββββββββββββββββββββββββββββββββοΏ½οΏ½οΏ½βββββββββ */
|
| 196 |
+
.df-error {
|
| 197 |
+
text-align: center;
|
| 198 |
+
padding: 3rem 2rem;
|
| 199 |
+
background: rgba(239, 68, 68, 0.05);
|
| 200 |
+
border: 1px solid rgba(239, 68, 68, 0.15);
|
| 201 |
+
border-radius: 16px;
|
| 202 |
+
animation: fadeInUp 0.4s ease;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
.df-error-icon {
|
| 206 |
+
font-size: 2.5rem;
|
| 207 |
+
margin-bottom: 1rem;
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
.df-error h3 {
|
| 211 |
+
color: #ef4444;
|
| 212 |
+
margin: 0 0 0.5rem;
|
| 213 |
+
font-size: 1.2rem;
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
.df-error p {
|
| 217 |
+
color: var(--text-muted);
|
| 218 |
+
margin: 0;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
/* ββ Results Card ββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 222 |
+
.df-results {
|
| 223 |
+
animation: fadeInUp 0.5s ease;
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
.df-result-card {
|
| 227 |
+
background: var(--surface-card);
|
| 228 |
+
border: 1px solid var(--border-subtle);
|
| 229 |
+
border-radius: 20px;
|
| 230 |
+
padding: 2rem;
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
/* Verdict Header */
|
| 234 |
+
.df-result-header {
|
| 235 |
+
display: flex;
|
| 236 |
+
align-items: center;
|
| 237 |
+
gap: 1.5rem;
|
| 238 |
+
margin-bottom: 1.5rem;
|
| 239 |
+
padding-bottom: 1.5rem;
|
| 240 |
+
border-bottom: 1px solid var(--border-subtle);
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
.df-verdict-ring-container {
|
| 244 |
+
position: relative;
|
| 245 |
+
width: 120px;
|
| 246 |
+
height: 120px;
|
| 247 |
+
flex-shrink: 0;
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
.df-ring-progress {
|
| 251 |
+
transition: stroke-dashoffset 1.2s ease-out;
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
.df-ring-label {
|
| 255 |
+
position: absolute;
|
| 256 |
+
top: 50%;
|
| 257 |
+
left: 50%;
|
| 258 |
+
transform: translate(-50%, -50%);
|
| 259 |
+
text-align: center;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
.df-ring-pct {
|
| 263 |
+
display: block;
|
| 264 |
+
font-size: 1.6rem;
|
| 265 |
+
font-weight: 800;
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
.df-ring-sublabel {
|
| 269 |
+
display: block;
|
| 270 |
+
font-size: 0.7rem;
|
| 271 |
+
color: var(--text-muted);
|
| 272 |
+
text-transform: uppercase;
|
| 273 |
+
letter-spacing: 1px;
|
| 274 |
+
margin-top: 2px;
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
.df-result-verdict {
|
| 278 |
+
font-size: 1.3rem;
|
| 279 |
+
font-weight: 700;
|
| 280 |
+
margin: 0 0 0.75rem;
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
.df-result-meta {
|
| 284 |
+
display: flex;
|
| 285 |
+
gap: 0.5rem;
|
| 286 |
+
flex-wrap: wrap;
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
.df-badge {
|
| 290 |
+
padding: 0.3rem 0.75rem;
|
| 291 |
+
border-radius: 8px;
|
| 292 |
+
font-size: 0.75rem;
|
| 293 |
+
font-weight: 600;
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
.df-badge-fake {
|
| 297 |
+
background: rgba(239, 68, 68, 0.12);
|
| 298 |
+
color: #ef4444;
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
.df-badge-real {
|
| 302 |
+
background: rgba(34, 197, 94, 0.12);
|
| 303 |
+
color: #22c55e;
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
.df-badge-type {
|
| 307 |
+
background: rgba(168, 85, 247, 0.1);
|
| 308 |
+
color: #a855f7;
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
.df-badge-file {
|
| 312 |
+
background: rgba(148, 163, 184, 0.1);
|
| 313 |
+
color: #94a3b8;
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
/* Media Preview in Results */
|
| 317 |
+
.df-result-preview {
|
| 318 |
+
margin-bottom: 1.5rem;
|
| 319 |
+
border-radius: 12px;
|
| 320 |
+
overflow: hidden;
|
| 321 |
+
background: #0a0a0f;
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
.df-result-media {
|
| 325 |
+
width: 100%;
|
| 326 |
+
max-height: 450px;
|
| 327 |
+
object-fit: contain;
|
| 328 |
+
display: block;
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
/* ββ Section Titles ββββββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 332 |
+
.df-section-title {
|
| 333 |
+
display: flex;
|
| 334 |
+
align-items: center;
|
| 335 |
+
gap: 0.5rem;
|
| 336 |
+
font-size: 1rem;
|
| 337 |
+
font-weight: 600;
|
| 338 |
+
color: var(--text-primary);
|
| 339 |
+
margin: 0 0 1rem;
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
/* ββ XAI Explanation βββββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 343 |
+
.df-explanation {
|
| 344 |
+
background: rgba(168, 85, 247, 0.04);
|
| 345 |
+
border: 1px solid rgba(168, 85, 247, 0.12);
|
| 346 |
+
border-radius: 14px;
|
| 347 |
+
padding: 1.25rem;
|
| 348 |
+
margin-bottom: 1.5rem;
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
.df-explanation-line {
|
| 352 |
+
color: var(--text-secondary, #cbd5e1);
|
| 353 |
+
font-size: 0.9rem;
|
| 354 |
+
line-height: 1.6;
|
| 355 |
+
margin: 0 0 0.5rem;
|
| 356 |
+
}
|
| 357 |
+
|
| 358 |
+
.df-explanation-line:last-child {
|
| 359 |
+
margin-bottom: 0;
|
| 360 |
+
}
|
| 361 |
+
|
| 362 |
+
/* ββ Frame Timeline ββββββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 363 |
+
.df-timeline {
|
| 364 |
+
background: rgba(30, 41, 59, 0.3);
|
| 365 |
+
border: 1px solid var(--border-subtle);
|
| 366 |
+
border-radius: 14px;
|
| 367 |
+
padding: 1.25rem;
|
| 368 |
+
margin-bottom: 1.5rem;
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
.df-timeline-chart {
|
| 372 |
+
display: flex;
|
| 373 |
+
align-items: flex-end;
|
| 374 |
+
gap: 2px;
|
| 375 |
+
height: 120px;
|
| 376 |
+
padding: 0 4px;
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
.df-timeline-bar-wrapper {
|
| 380 |
+
flex: 1;
|
| 381 |
+
display: flex;
|
| 382 |
+
flex-direction: column;
|
| 383 |
+
align-items: center;
|
| 384 |
+
height: 100%;
|
| 385 |
+
justify-content: flex-end;
|
| 386 |
+
position: relative;
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
.df-timeline-bar {
|
| 390 |
+
width: 100%;
|
| 391 |
+
min-height: 4px;
|
| 392 |
+
border-radius: 3px 3px 0 0;
|
| 393 |
+
transition: height 0.6s ease-out;
|
| 394 |
+
opacity: 0.85;
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
.df-timeline-bar:hover {
|
| 398 |
+
opacity: 1;
|
| 399 |
+
filter: brightness(1.2);
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
.df-timeline-label {
|
| 403 |
+
font-size: 0.55rem;
|
| 404 |
+
color: var(--text-muted);
|
| 405 |
+
margin-top: 4px;
|
| 406 |
+
white-space: nowrap;
|
| 407 |
+
}
|
| 408 |
+
|
| 409 |
+
.df-timeline-legend {
|
| 410 |
+
display: flex;
|
| 411 |
+
gap: 1rem;
|
| 412 |
+
margin-top: 0.75rem;
|
| 413 |
+
justify-content: center;
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
.df-legend-item {
|
| 417 |
+
display: flex;
|
| 418 |
+
align-items: center;
|
| 419 |
+
gap: 0.35rem;
|
| 420 |
+
font-size: 0.75rem;
|
| 421 |
+
color: var(--text-muted);
|
| 422 |
+
}
|
| 423 |
+
|
| 424 |
+
.df-legend-dot {
|
| 425 |
+
width: 8px;
|
| 426 |
+
height: 8px;
|
| 427 |
+
border-radius: 50%;
|
| 428 |
+
display: inline-block;
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
/* ββ Video Stats βββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 432 |
+
.df-video-stats {
|
| 433 |
+
display: flex;
|
| 434 |
+
gap: 1rem;
|
| 435 |
+
flex-wrap: wrap;
|
| 436 |
+
margin-bottom: 1.5rem;
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
.df-stat {
|
| 440 |
+
flex: 1;
|
| 441 |
+
min-width: 100px;
|
| 442 |
+
background: rgba(30, 41, 59, 0.4);
|
| 443 |
+
border: 1px solid var(--border-subtle);
|
| 444 |
+
border-radius: 12px;
|
| 445 |
+
padding: 0.75rem 1rem;
|
| 446 |
+
text-align: center;
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
.df-stat-value {
|
| 450 |
+
display: block;
|
| 451 |
+
font-size: 1.2rem;
|
| 452 |
+
font-weight: 700;
|
| 453 |
+
color: #a855f7;
|
| 454 |
+
}
|
| 455 |
+
|
| 456 |
+
.df-stat-label {
|
| 457 |
+
display: block;
|
| 458 |
+
font-size: 0.7rem;
|
| 459 |
+
color: var(--text-muted);
|
| 460 |
+
margin-top: 2px;
|
| 461 |
+
text-transform: uppercase;
|
| 462 |
+
letter-spacing: 0.5px;
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
/* ββ Raw Score Bars ββββββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 466 |
+
.df-raw-scores {
|
| 467 |
+
background: rgba(30, 41, 59, 0.3);
|
| 468 |
+
border: 1px solid var(--border-subtle);
|
| 469 |
+
border-radius: 14px;
|
| 470 |
+
padding: 1.25rem;
|
| 471 |
+
}
|
| 472 |
+
|
| 473 |
+
.df-score-bars {
|
| 474 |
+
display: flex;
|
| 475 |
+
flex-direction: column;
|
| 476 |
+
gap: 0.75rem;
|
| 477 |
+
}
|
| 478 |
+
|
| 479 |
+
.df-score-row {
|
| 480 |
+
display: flex;
|
| 481 |
+
align-items: center;
|
| 482 |
+
gap: 0.75rem;
|
| 483 |
+
}
|
| 484 |
+
|
| 485 |
+
.df-score-label {
|
| 486 |
+
width: 80px;
|
| 487 |
+
font-size: 0.8rem;
|
| 488 |
+
font-weight: 600;
|
| 489 |
+
color: var(--text-secondary, #cbd5e1);
|
| 490 |
+
text-align: right;
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
+
.df-score-bar-track {
|
| 494 |
+
flex: 1;
|
| 495 |
+
height: 10px;
|
| 496 |
+
background: rgba(255, 255, 255, 0.06);
|
| 497 |
+
border-radius: 5px;
|
| 498 |
+
overflow: hidden;
|
| 499 |
+
}
|
| 500 |
+
|
| 501 |
+
.df-score-bar-fill {
|
| 502 |
+
height: 100%;
|
| 503 |
+
border-radius: 5px;
|
| 504 |
+
transition: width 1s ease-out;
|
| 505 |
+
}
|
| 506 |
+
|
| 507 |
+
.df-score-value {
|
| 508 |
+
width: 40px;
|
| 509 |
+
font-size: 0.8rem;
|
| 510 |
+
font-weight: 700;
|
| 511 |
+
color: var(--text-primary);
|
| 512 |
+
}
|
| 513 |
+
|
| 514 |
+
/* ββ Responsive ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 515 |
+
@media (max-width: 640px) {
|
| 516 |
+
.df-page {
|
| 517 |
+
padding: 1rem;
|
| 518 |
+
}
|
| 519 |
+
|
| 520 |
+
.df-result-header {
|
| 521 |
+
flex-direction: column;
|
| 522 |
+
text-align: center;
|
| 523 |
+
}
|
| 524 |
+
|
| 525 |
+
.df-loading-card {
|
| 526 |
+
flex-direction: column;
|
| 527 |
+
}
|
| 528 |
+
|
| 529 |
+
.df-loading-preview {
|
| 530 |
+
width: 100%;
|
| 531 |
+
height: 120px;
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
.df-video-stats {
|
| 535 |
+
gap: 0.5rem;
|
| 536 |
+
}
|
| 537 |
+
|
| 538 |
+
.df-stat {
|
| 539 |
+
min-width: 70px;
|
| 540 |
+
padding: 0.5rem;
|
| 541 |
+
}
|
| 542 |
+
}
|
dashboard/src/pages/DeepfakePage.jsx
ADDED
|
@@ -0,0 +1,425 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState, useRef, useCallback } from 'react';
|
| 2 |
+
import './DeepfakePage.css';
|
| 3 |
+
|
| 4 |
+
/**
|
| 5 |
+
* DeepfakePage β Upload an image or video and get an AI-powered
|
| 6 |
+
* deepfake / AI-generation analysis with confidence scores,
|
| 7 |
+
* per-frame timelines (for video), and XAI explanations.
|
| 8 |
+
*/
|
| 9 |
+
const DeepfakePage = () => {
|
| 10 |
+
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000';
|
| 11 |
+
|
| 12 |
+
// ββ State ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
+
const [file, setFile] = useState(null);
|
| 14 |
+
const [preview, setPreview] = useState(null);
|
| 15 |
+
const [mediaType, setMediaType] = useState(null); // 'image' | 'video'
|
| 16 |
+
const [loading, setLoading] = useState(false);
|
| 17 |
+
const [result, setResult] = useState(null);
|
| 18 |
+
const [error, setError] = useState(null);
|
| 19 |
+
const [dragActive, setDragActive] = useState(false);
|
| 20 |
+
|
| 21 |
+
const fileInputRef = useRef(null);
|
| 22 |
+
|
| 23 |
+
// ββ Drag & Drop handlers βββββββββββββββββββββββββββββββββββββββββββ
|
| 24 |
+
const handleDrag = useCallback((e) => {
|
| 25 |
+
e.preventDefault();
|
| 26 |
+
e.stopPropagation();
|
| 27 |
+
if (e.type === 'dragenter' || e.type === 'dragover') {
|
| 28 |
+
setDragActive(true);
|
| 29 |
+
} else if (e.type === 'dragleave') {
|
| 30 |
+
setDragActive(false);
|
| 31 |
+
}
|
| 32 |
+
}, []);
|
| 33 |
+
|
| 34 |
+
const handleDrop = useCallback((e) => {
|
| 35 |
+
e.preventDefault();
|
| 36 |
+
e.stopPropagation();
|
| 37 |
+
setDragActive(false);
|
| 38 |
+
if (e.dataTransfer.files && e.dataTransfer.files[0]) {
|
| 39 |
+
handleFileSelect(e.dataTransfer.files[0]);
|
| 40 |
+
}
|
| 41 |
+
}, []);
|
| 42 |
+
|
| 43 |
+
// ββ File selection βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 44 |
+
const handleFileSelect = (selectedFile) => {
|
| 45 |
+
setResult(null);
|
| 46 |
+
setError(null);
|
| 47 |
+
|
| 48 |
+
const type = selectedFile.type;
|
| 49 |
+
const isImage = type.startsWith('image/');
|
| 50 |
+
const isVideo = type.startsWith('video/');
|
| 51 |
+
|
| 52 |
+
if (!isImage && !isVideo) {
|
| 53 |
+
setError('Please upload an image (jpg, png, webp) or video (mp4, avi, mov, webm).');
|
| 54 |
+
return;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
// Size checks
|
| 58 |
+
const maxSize = isVideo ? 50 * 1024 * 1024 : 10 * 1024 * 1024;
|
| 59 |
+
if (selectedFile.size > maxSize) {
|
| 60 |
+
setError(`File too large. Max ${isVideo ? '50' : '10'} MB.`);
|
| 61 |
+
return;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
setFile(selectedFile);
|
| 65 |
+
setMediaType(isImage ? 'image' : 'video');
|
| 66 |
+
|
| 67 |
+
// Generate preview URL
|
| 68 |
+
const previewUrl = URL.createObjectURL(selectedFile);
|
| 69 |
+
setPreview(previewUrl);
|
| 70 |
+
};
|
| 71 |
+
|
| 72 |
+
const handleInputChange = (e) => {
|
| 73 |
+
if (e.target.files && e.target.files[0]) {
|
| 74 |
+
handleFileSelect(e.target.files[0]);
|
| 75 |
+
}
|
| 76 |
+
};
|
| 77 |
+
|
| 78 |
+
// ββ Submit for analysis ββββββββββββββββββββββββββββββββββββββββββββ
|
| 79 |
+
const handleAnalyze = async () => {
|
| 80 |
+
if (!file) return;
|
| 81 |
+
|
| 82 |
+
setLoading(true);
|
| 83 |
+
setResult(null);
|
| 84 |
+
setError(null);
|
| 85 |
+
|
| 86 |
+
try {
|
| 87 |
+
const formData = new FormData();
|
| 88 |
+
formData.append('file', file);
|
| 89 |
+
|
| 90 |
+
const res = await fetch(`${API_BASE_URL}/api/deepfake/analyze`, {
|
| 91 |
+
method: 'POST',
|
| 92 |
+
body: formData,
|
| 93 |
+
});
|
| 94 |
+
|
| 95 |
+
const data = await res.json();
|
| 96 |
+
|
| 97 |
+
if (data.error) {
|
| 98 |
+
setError(data.error);
|
| 99 |
+
} else {
|
| 100 |
+
setResult(data);
|
| 101 |
+
}
|
| 102 |
+
} catch (err) {
|
| 103 |
+
setError(`Analysis failed: ${err.message}`);
|
| 104 |
+
} finally {
|
| 105 |
+
setLoading(false);
|
| 106 |
+
}
|
| 107 |
+
};
|
| 108 |
+
|
| 109 |
+
// ββ Reset ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 110 |
+
const handleReset = () => {
|
| 111 |
+
setFile(null);
|
| 112 |
+
setPreview(null);
|
| 113 |
+
setMediaType(null);
|
| 114 |
+
setResult(null);
|
| 115 |
+
setError(null);
|
| 116 |
+
if (fileInputRef.current) fileInputRef.current.value = '';
|
| 117 |
+
};
|
| 118 |
+
|
| 119 |
+
// ββ Credibility Ring (reused pattern from VerifyPage) ββββββββββββββ
|
| 120 |
+
const renderVerdictRing = (confidence, isFake) => {
|
| 121 |
+
const pct = Math.round(confidence * 100);
|
| 122 |
+
const radius = 52;
|
| 123 |
+
const circumference = 2 * Math.PI * radius;
|
| 124 |
+
const offset = circumference - (confidence * circumference);
|
| 125 |
+
const color = isFake
|
| 126 |
+
? (pct >= 70 ? '#ef4444' : '#f59e0b') // Red for high-confidence fake, amber for low
|
| 127 |
+
: (pct >= 70 ? '#22c55e' : '#3b82f6'); // Green for high-confidence real, blue for low
|
| 128 |
+
|
| 129 |
+
return (
|
| 130 |
+
<div className="df-verdict-ring-container">
|
| 131 |
+
<svg width="120" height="120" viewBox="0 0 120 120">
|
| 132 |
+
{/* Background track */}
|
| 133 |
+
<circle cx="60" cy="60" r={radius} fill="none" stroke="rgba(255,255,255,0.06)" strokeWidth="8" />
|
| 134 |
+
{/* Animated progress arc */}
|
| 135 |
+
<circle
|
| 136 |
+
cx="60" cy="60" r={radius}
|
| 137 |
+
fill="none"
|
| 138 |
+
stroke={color}
|
| 139 |
+
strokeWidth="8"
|
| 140 |
+
strokeLinecap="round"
|
| 141 |
+
strokeDasharray={circumference}
|
| 142 |
+
strokeDashoffset={offset}
|
| 143 |
+
transform="rotate(-90 60 60)"
|
| 144 |
+
className="df-ring-progress"
|
| 145 |
+
/>
|
| 146 |
+
</svg>
|
| 147 |
+
<div className="df-ring-label">
|
| 148 |
+
<span className="df-ring-pct" style={{ color }}>{pct}%</span>
|
| 149 |
+
<span className="df-ring-sublabel">{isFake ? 'Fake' : 'Real'}</span>
|
| 150 |
+
</div>
|
| 151 |
+
</div>
|
| 152 |
+
);
|
| 153 |
+
};
|
| 154 |
+
|
| 155 |
+
// ββ Frame Timeline (for video results) βββββββββββββββββββββββββββββ
|
| 156 |
+
const renderFrameTimeline = (frameResults) => {
|
| 157 |
+
if (!frameResults || frameResults.length === 0) return null;
|
| 158 |
+
|
| 159 |
+
const maxConf = Math.max(...frameResults.map(f => f.confidence));
|
| 160 |
+
|
| 161 |
+
return (
|
| 162 |
+
<div className="df-timeline">
|
| 163 |
+
<h3 className="df-section-title">
|
| 164 |
+
<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
| 165 |
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M7 4v16M17 4v16M3 8h4m10 0h4M3 12h18M3 16h4m10 0h4M4 20h16a1 1 0 001-1V5a1 1 0 00-1-1H4a1 1 0 00-1 1v14a1 1 0 001 1z" />
|
| 166 |
+
</svg>
|
| 167 |
+
Frame-by-Frame Analysis
|
| 168 |
+
</h3>
|
| 169 |
+
<div className="df-timeline-chart">
|
| 170 |
+
{frameResults.map((frame, idx) => {
|
| 171 |
+
const heightPct = (frame.confidence / maxConf) * 100;
|
| 172 |
+
const color = frame.is_fake ? '#ef4444' : '#22c55e';
|
| 173 |
+
return (
|
| 174 |
+
<div key={idx} className="df-timeline-bar-wrapper" title={`Frame ${frame.frame} (${frame.timestamp}s) β ${frame.label}: ${Math.round(frame.confidence * 100)}%`}>
|
| 175 |
+
<div
|
| 176 |
+
className="df-timeline-bar"
|
| 177 |
+
style={{
|
| 178 |
+
height: `${heightPct}%`,
|
| 179 |
+
backgroundColor: color,
|
| 180 |
+
}}
|
| 181 |
+
/>
|
| 182 |
+
{idx % Math.max(1, Math.floor(frameResults.length / 8)) === 0 && (
|
| 183 |
+
<span className="df-timeline-label">{frame.timestamp}s</span>
|
| 184 |
+
)}
|
| 185 |
+
</div>
|
| 186 |
+
);
|
| 187 |
+
})}
|
| 188 |
+
</div>
|
| 189 |
+
<div className="df-timeline-legend">
|
| 190 |
+
<span className="df-legend-item"><span className="df-legend-dot" style={{ background: '#22c55e' }} /> Real</span>
|
| 191 |
+
<span className="df-legend-item"><span className="df-legend-dot" style={{ background: '#ef4444' }} /> Fake</span>
|
| 192 |
+
</div>
|
| 193 |
+
</div>
|
| 194 |
+
);
|
| 195 |
+
};
|
| 196 |
+
|
| 197 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 198 |
+
// RENDER
|
| 199 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 200 |
+
return (
|
| 201 |
+
<div className="df-page">
|
| 202 |
+
{/* Header */}
|
| 203 |
+
<div className="df-header">
|
| 204 |
+
<div className="df-header-icon">
|
| 205 |
+
<svg width="28" height="28" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
| 206 |
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
|
| 207 |
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
|
| 208 |
+
</svg>
|
| 209 |
+
</div>
|
| 210 |
+
<div>
|
| 211 |
+
<h1 className="df-title">Deepfake Detector</h1>
|
| 212 |
+
<p className="df-subtitle">Upload an image or video to analyze it for AI-generated or manipulated content</p>
|
| 213 |
+
</div>
|
| 214 |
+
</div>
|
| 215 |
+
|
| 216 |
+
{/* Upload Zone */}
|
| 217 |
+
{!file && (
|
| 218 |
+
<div
|
| 219 |
+
className={`df-dropzone ${dragActive ? 'df-dropzone-active' : ''}`}
|
| 220 |
+
onDragEnter={handleDrag}
|
| 221 |
+
onDragLeave={handleDrag}
|
| 222 |
+
onDragOver={handleDrag}
|
| 223 |
+
onDrop={handleDrop}
|
| 224 |
+
onClick={() => fileInputRef.current?.click()}
|
| 225 |
+
>
|
| 226 |
+
<input
|
| 227 |
+
ref={fileInputRef}
|
| 228 |
+
type="file"
|
| 229 |
+
accept="image/jpeg,image/png,image/webp,video/mp4,video/avi,video/quicktime,video/webm"
|
| 230 |
+
onChange={handleInputChange}
|
| 231 |
+
style={{ display: 'none' }}
|
| 232 |
+
/>
|
| 233 |
+
<div className="df-dropzone-icon">
|
| 234 |
+
<svg width="48" height="48" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
| 235 |
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="1.5" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
|
| 236 |
+
</svg>
|
| 237 |
+
</div>
|
| 238 |
+
<p className="df-dropzone-text">Drag & drop a file here, or click to browse</p>
|
| 239 |
+
<p className="df-dropzone-hint">Supports JPG, PNG, WEBP images (max 10MB) and MP4, AVI, MOV, WEBM videos (max 50MB)</p>
|
| 240 |
+
</div>
|
| 241 |
+
)}
|
| 242 |
+
|
| 243 |
+
{/* Preview + Analyze */}
|
| 244 |
+
{file && !result && !loading && (
|
| 245 |
+
<div className="df-preview-section">
|
| 246 |
+
<div className="df-preview-card">
|
| 247 |
+
{mediaType === 'image' ? (
|
| 248 |
+
<img src={preview} alt="Preview" className="df-preview-media" />
|
| 249 |
+
) : (
|
| 250 |
+
<video src={preview} controls className="df-preview-media" />
|
| 251 |
+
)}
|
| 252 |
+
<div className="df-preview-info">
|
| 253 |
+
<p className="df-preview-name">{file.name}</p>
|
| 254 |
+
<p className="df-preview-meta">
|
| 255 |
+
{mediaType === 'image' ? 'πΌοΈ Image' : 'π¬ Video'} β’ {(file.size / (1024 * 1024)).toFixed(2)} MB
|
| 256 |
+
</p>
|
| 257 |
+
</div>
|
| 258 |
+
</div>
|
| 259 |
+
<div className="df-action-row">
|
| 260 |
+
<button className="df-btn df-btn-secondary" onClick={handleReset}>Cancel</button>
|
| 261 |
+
<button className="df-btn df-btn-primary" onClick={handleAnalyze}>
|
| 262 |
+
<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
| 263 |
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2.5" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z" />
|
| 264 |
+
</svg>
|
| 265 |
+
Analyze for Deepfake
|
| 266 |
+
</button>
|
| 267 |
+
</div>
|
| 268 |
+
</div>
|
| 269 |
+
)}
|
| 270 |
+
|
| 271 |
+
{/* Loading Skeleton */}
|
| 272 |
+
{loading && (
|
| 273 |
+
<div className="df-loading">
|
| 274 |
+
<div className="df-loading-card">
|
| 275 |
+
<div className="df-loading-preview skeleton-shimmer" />
|
| 276 |
+
<div className="df-loading-details">
|
| 277 |
+
<div className="skeleton-line skeleton-shimmer" style={{ width: '60%', height: '20px', marginBottom: '12px' }} />
|
| 278 |
+
<div className="skeleton-line skeleton-shimmer" style={{ width: '40%', height: '16px', marginBottom: '24px' }} />
|
| 279 |
+
<div className="skeleton-line skeleton-shimmer" style={{ width: '100%', height: '80px', borderRadius: '12px' }} />
|
| 280 |
+
</div>
|
| 281 |
+
</div>
|
| 282 |
+
<div style={{ textAlign: 'center', marginTop: '20px' }}>
|
| 283 |
+
<p style={{ color: 'var(--text-muted)', fontSize: '1rem', fontWeight: '500' }}>
|
| 284 |
+
{mediaType === 'video' ? 'Extracting & analyzing frames...' : 'Analyzing image...'}
|
| 285 |
+
</p>
|
| 286 |
+
<p style={{ color: '#94a3b8', fontSize: '0.85rem', marginTop: '4px' }}>
|
| 287 |
+
{mediaType === 'video'
|
| 288 |
+
? 'Frame Extraction β Per-Frame Classification β Score Aggregation'
|
| 289 |
+
: 'Preprocessing β Model Inference β Explanation Generation'}
|
| 290 |
+
</p>
|
| 291 |
+
</div>
|
| 292 |
+
</div>
|
| 293 |
+
)}
|
| 294 |
+
|
| 295 |
+
{/* Error */}
|
| 296 |
+
{error && !loading && (
|
| 297 |
+
<div className="df-error">
|
| 298 |
+
<div className="df-error-icon">β</div>
|
| 299 |
+
<h3>Analysis Failed</h3>
|
| 300 |
+
<p>{error}</p>
|
| 301 |
+
<button className="df-btn df-btn-secondary" onClick={handleReset} style={{ marginTop: '16px' }}>Try Again</button>
|
| 302 |
+
</div>
|
| 303 |
+
)}
|
| 304 |
+
|
| 305 |
+
{/* Results */}
|
| 306 |
+
{result && !loading && (
|
| 307 |
+
<div className="df-results">
|
| 308 |
+
<div className="df-result-card">
|
| 309 |
+
{/* Verdict Header */}
|
| 310 |
+
<div className="df-result-header">
|
| 311 |
+
{renderVerdictRing(result.confidence, result.is_fake)}
|
| 312 |
+
<div className="df-result-info">
|
| 313 |
+
<h2 className="df-result-verdict" style={{ color: result.is_fake ? '#ef4444' : '#22c55e' }}>
|
| 314 |
+
{result.is_fake ? 'β οΈ Likely Deepfake / AI-Generated' : 'β
Likely Authentic'}
|
| 315 |
+
</h2>
|
| 316 |
+
<div className="df-result-meta">
|
| 317 |
+
<span className={`df-badge ${result.is_fake ? 'df-badge-fake' : 'df-badge-real'}`}>
|
| 318 |
+
{result.label}
|
| 319 |
+
</span>
|
| 320 |
+
<span className="df-badge df-badge-type">
|
| 321 |
+
{result.media_type === 'image' ? 'πΌοΈ Image' : 'π¬ Video'}
|
| 322 |
+
</span>
|
| 323 |
+
{result.filename && (
|
| 324 |
+
<span className="df-badge df-badge-file">{result.filename}</span>
|
| 325 |
+
)}
|
| 326 |
+
</div>
|
| 327 |
+
</div>
|
| 328 |
+
</div>
|
| 329 |
+
|
| 330 |
+
{/* Media Preview */}
|
| 331 |
+
<div className="df-result-preview">
|
| 332 |
+
{mediaType === 'image' ? (
|
| 333 |
+
<img src={preview} alt="Analyzed" className="df-result-media" />
|
| 334 |
+
) : (
|
| 335 |
+
<video src={preview} controls className="df-result-media" />
|
| 336 |
+
)}
|
| 337 |
+
</div>
|
| 338 |
+
|
| 339 |
+
{/* XAI Explanation */}
|
| 340 |
+
{result.explanation && (
|
| 341 |
+
<div className="df-explanation">
|
| 342 |
+
<h3 className="df-section-title">
|
| 343 |
+
<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
| 344 |
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" />
|
| 345 |
+
</svg>
|
| 346 |
+
AI Analysis
|
| 347 |
+
</h3>
|
| 348 |
+
{result.explanation.split('\n').filter(l => l.trim()).map((line, i) => (
|
| 349 |
+
<p key={i} className="df-explanation-line">{line}</p>
|
| 350 |
+
))}
|
| 351 |
+
</div>
|
| 352 |
+
)}
|
| 353 |
+
|
| 354 |
+
{/* Video-specific: Frame Timeline */}
|
| 355 |
+
{result.media_type === 'video' && result.frame_results && (
|
| 356 |
+
<>
|
| 357 |
+
{renderFrameTimeline(result.frame_results)}
|
| 358 |
+
<div className="df-video-stats">
|
| 359 |
+
<div className="df-stat">
|
| 360 |
+
<span className="df-stat-value">{result.total_frames}</span>
|
| 361 |
+
<span className="df-stat-label">Total Frames</span>
|
| 362 |
+
</div>
|
| 363 |
+
<div className="df-stat">
|
| 364 |
+
<span className="df-stat-value">{result.analyzed_frames}</span>
|
| 365 |
+
<span className="df-stat-label">Analyzed</span>
|
| 366 |
+
</div>
|
| 367 |
+
<div className="df-stat">
|
| 368 |
+
<span className="df-stat-value">{result.fps}</span>
|
| 369 |
+
<span className="df-stat-label">FPS</span>
|
| 370 |
+
</div>
|
| 371 |
+
<div className="df-stat">
|
| 372 |
+
<span className="df-stat-value">{result.duration_seconds}s</span>
|
| 373 |
+
<span className="df-stat-label">Duration</span>
|
| 374 |
+
</div>
|
| 375 |
+
<div className="df-stat">
|
| 376 |
+
<span className="df-stat-value">{Math.round((result.fake_frame_ratio || 0) * 100)}%</span>
|
| 377 |
+
<span className="df-stat-label">Fake Frames</span>
|
| 378 |
+
</div>
|
| 379 |
+
</div>
|
| 380 |
+
</>
|
| 381 |
+
)}
|
| 382 |
+
|
| 383 |
+
{/* Raw Scores */}
|
| 384 |
+
{result.raw_scores && (
|
| 385 |
+
<div className="df-raw-scores">
|
| 386 |
+
<h3 className="df-section-title">
|
| 387 |
+
<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
| 388 |
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" />
|
| 389 |
+
</svg>
|
| 390 |
+
Model Confidence Breakdown
|
| 391 |
+
</h3>
|
| 392 |
+
<div className="df-score-bars">
|
| 393 |
+
{Object.entries(result.raw_scores).map(([label, score]) => (
|
| 394 |
+
<div key={label} className="df-score-row">
|
| 395 |
+
<span className="df-score-label">{label}</span>
|
| 396 |
+
<div className="df-score-bar-track">
|
| 397 |
+
<div
|
| 398 |
+
className="df-score-bar-fill"
|
| 399 |
+
style={{
|
| 400 |
+
width: `${Math.round(score * 100)}%`,
|
| 401 |
+
backgroundColor: label.toLowerCase().includes('fake') || label.toLowerCase().includes('ai') ? '#ef4444' : '#22c55e',
|
| 402 |
+
}}
|
| 403 |
+
/>
|
| 404 |
+
</div>
|
| 405 |
+
<span className="df-score-value">{Math.round(score * 100)}%</span>
|
| 406 |
+
</div>
|
| 407 |
+
))}
|
| 408 |
+
</div>
|
| 409 |
+
</div>
|
| 410 |
+
)}
|
| 411 |
+
|
| 412 |
+
{/* Reset Button */}
|
| 413 |
+
<div className="df-action-row" style={{ marginTop: '24px', justifyContent: 'center' }}>
|
| 414 |
+
<button className="df-btn df-btn-secondary" onClick={handleReset}>
|
| 415 |
+
Analyze Another File
|
| 416 |
+
</button>
|
| 417 |
+
</div>
|
| 418 |
+
</div>
|
| 419 |
+
</div>
|
| 420 |
+
)}
|
| 421 |
+
</div>
|
| 422 |
+
);
|
| 423 |
+
};
|
| 424 |
+
|
| 425 |
+
export default DeepfakePage;
|
requirements.txt
CHANGED
|
@@ -39,5 +39,10 @@ psycopg2-binary==2.9.9
|
|
| 39 |
discord.py==2.5.2
|
| 40 |
aiohttp>=3.9.0
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# WhatsApp Bot (Meta Cloud API)
|
| 43 |
httpx>=0.27.0
|
|
|
|
| 39 |
discord.py==2.5.2
|
| 40 |
aiohttp>=3.9.0
|
| 41 |
|
| 42 |
+
# Deepfake Detection
|
| 43 |
+
opencv-python>=4.8.0
|
| 44 |
+
Pillow>=10.0.0
|
| 45 |
+
|
| 46 |
+
|
| 47 |
# WhatsApp Bot (Meta Cloud API)
|
| 48 |
httpx>=0.27.0
|
scratch/benchmark_ensemble.py
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Comprehensive benchmark of the ensemble deepfake detection approach.
|
| 3 |
+
Tests original model with and without face cropping, frequency analysis,
|
| 4 |
+
and combined ensemble on the user's test dataset.
|
| 5 |
+
"""
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
+
import glob
|
| 9 |
+
import time
|
| 10 |
+
import cv2
|
| 11 |
+
import torch
|
| 12 |
+
import numpy as np
|
| 13 |
+
from PIL import Image
|
| 14 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 15 |
+
|
| 16 |
+
sys.path.insert(0, ".")
|
| 17 |
+
|
| 18 |
+
MODEL_ID = "prithivMLmods/deepfake-detector-model-v1"
|
| 19 |
+
|
| 20 |
+
print("Loading model...")
|
| 21 |
+
processor = AutoImageProcessor.from_pretrained(MODEL_ID)
|
| 22 |
+
model = AutoModelForImageClassification.from_pretrained(MODEL_ID)
|
| 23 |
+
model.eval()
|
| 24 |
+
id2label = model.config.id2label
|
| 25 |
+
print(f"Labels: {id2label}")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def get_face_crop(img_bgr):
|
| 29 |
+
"""Crop to largest face. Returns cropped BGR image or None if no face found."""
|
| 30 |
+
gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
|
| 31 |
+
cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 32 |
+
faces = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=4, minSize=(30, 30))
|
| 33 |
+
if len(faces) == 0:
|
| 34 |
+
return None
|
| 35 |
+
faces = sorted(faces, key=lambda x: x[2]*x[3], reverse=True)
|
| 36 |
+
x, y, w, h = faces[0]
|
| 37 |
+
margin = int(w * 0.3) # slightly larger margin
|
| 38 |
+
x1, y1 = max(0, x - margin), max(0, y - margin)
|
| 39 |
+
x2, y2 = min(img_bgr.shape[1], x + w + margin), min(img_bgr.shape[0], y + h + margin)
|
| 40 |
+
return img_bgr[y1:y2, x1:x2]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def classify_pil(pil_image):
|
| 44 |
+
"""Run the model on a PIL image, return (fake_prob, real_prob)."""
|
| 45 |
+
inputs = processor(images=pil_image, return_tensors="pt")
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
out = model(**inputs)
|
| 48 |
+
probs = torch.nn.functional.softmax(out.logits, dim=-1)[0]
|
| 49 |
+
# id2label: {0: 'Fake', 1: 'Real'}
|
| 50 |
+
return float(probs[0]), float(probs[1])
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def frequency_analysis(img_bgr):
|
| 54 |
+
"""
|
| 55 |
+
Analyze frequency-domain artifacts that indicate AI generation or face blending.
|
| 56 |
+
Returns a 'fakeness' score from 0-1.
|
| 57 |
+
"""
|
| 58 |
+
gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
|
| 59 |
+
|
| 60 |
+
# 1. FFT analysis - deepfakes often have unusual frequency patterns
|
| 61 |
+
f_transform = np.fft.fft2(gray.astype(np.float32))
|
| 62 |
+
f_shift = np.fft.fftshift(f_transform)
|
| 63 |
+
magnitude = np.log(1 + np.abs(f_shift))
|
| 64 |
+
|
| 65 |
+
# Normalize
|
| 66 |
+
magnitude = (magnitude - magnitude.min()) / (magnitude.max() - magnitude.min() + 1e-8)
|
| 67 |
+
|
| 68 |
+
h, w = magnitude.shape
|
| 69 |
+
cy, cx = h // 2, w // 2
|
| 70 |
+
|
| 71 |
+
# High-frequency energy ratio (deepfakes often have less high-freq detail)
|
| 72 |
+
r_mid = min(h, w) // 4
|
| 73 |
+
r_high = min(h, w) // 2
|
| 74 |
+
|
| 75 |
+
Y, X = np.ogrid[:h, :w]
|
| 76 |
+
dist = np.sqrt((X - cx)**2 + (Y - cy)**2)
|
| 77 |
+
|
| 78 |
+
mid_energy = magnitude[(dist >= r_mid) & (dist < r_high)].mean()
|
| 79 |
+
low_energy = magnitude[dist < r_mid].mean()
|
| 80 |
+
|
| 81 |
+
# Ratio of mid-to-low frequency energy
|
| 82 |
+
freq_ratio = mid_energy / (low_energy + 1e-8)
|
| 83 |
+
|
| 84 |
+
# 2. Laplacian variance (blur detection - deepfakes are often slightly blurrier)
|
| 85 |
+
laplacian_var = cv2.Laplacian(gray, cv2.CV_64F).var()
|
| 86 |
+
# Normalize: very sharp images have high variance
|
| 87 |
+
blur_score = 1.0 / (1.0 + laplacian_var / 500.0)
|
| 88 |
+
|
| 89 |
+
# 3. Color channel consistency check
|
| 90 |
+
b, g, r = cv2.split(img_bgr)
|
| 91 |
+
# Cross-channel correlation - deepfakes sometimes have inconsistent color channels
|
| 92 |
+
rg_corr = np.corrcoef(r.flatten(), g.flatten())[0, 1]
|
| 93 |
+
rb_corr = np.corrcoef(r.flatten(), b.flatten())[0, 1]
|
| 94 |
+
gb_corr = np.corrcoef(g.flatten(), b.flatten())[0, 1]
|
| 95 |
+
|
| 96 |
+
# High correlation between all channels is more natural
|
| 97 |
+
color_consistency = (rg_corr + rb_corr + gb_corr) / 3.0
|
| 98 |
+
color_anomaly = max(0, 1.0 - color_consistency)
|
| 99 |
+
|
| 100 |
+
# Combine signals
|
| 101 |
+
freq_score = max(0, min(1, 1.0 - freq_ratio * 2)) # Lower ratio = more fake
|
| 102 |
+
|
| 103 |
+
# Weighted combination
|
| 104 |
+
fakeness = 0.4 * freq_score + 0.35 * blur_score + 0.25 * color_anomaly
|
| 105 |
+
return round(fakeness, 4)
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def analyze_frame(img_bgr):
|
| 109 |
+
"""
|
| 110 |
+
Run ensemble analysis on a single BGR frame.
|
| 111 |
+
Returns dict with individual and combined scores.
|
| 112 |
+
"""
|
| 113 |
+
rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
|
| 114 |
+
pil_full = Image.fromarray(rgb)
|
| 115 |
+
|
| 116 |
+
# 1. Full-frame model prediction
|
| 117 |
+
fake_full, real_full = classify_pil(pil_full)
|
| 118 |
+
|
| 119 |
+
# 2. Face-cropped model prediction
|
| 120 |
+
face = get_face_crop(img_bgr)
|
| 121 |
+
if face is not None:
|
| 122 |
+
face_rgb = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
|
| 123 |
+
pil_face = Image.fromarray(face_rgb)
|
| 124 |
+
fake_face, real_face = classify_pil(pil_face)
|
| 125 |
+
has_face = True
|
| 126 |
+
else:
|
| 127 |
+
fake_face, real_face = fake_full, real_full # fallback
|
| 128 |
+
has_face = False
|
| 129 |
+
|
| 130 |
+
# 3. Frequency analysis
|
| 131 |
+
freq_fakeness = frequency_analysis(face if face is not None else img_bgr)
|
| 132 |
+
|
| 133 |
+
# 4. Ensemble: combine signals
|
| 134 |
+
# Full-frame model is good at catching fakes but flags too many reals as fake
|
| 135 |
+
# Face-crop model is good at confirming reals but misses some fakes
|
| 136 |
+
# Use full-frame as primary, face-crop as correction, freq as tiebreaker
|
| 137 |
+
|
| 138 |
+
if has_face:
|
| 139 |
+
# Weighted ensemble with both signals
|
| 140 |
+
ensemble_fake = (
|
| 141 |
+
0.45 * fake_full + # full-frame catches fakes well
|
| 142 |
+
0.35 * fake_face + # face-crop is more conservative
|
| 143 |
+
0.20 * freq_fakeness # frequency as supporting signal
|
| 144 |
+
)
|
| 145 |
+
else:
|
| 146 |
+
# No face detected - rely more on full frame + frequency
|
| 147 |
+
ensemble_fake = (
|
| 148 |
+
0.60 * fake_full +
|
| 149 |
+
0.40 * freq_fakeness
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
ensemble_real = 1.0 - ensemble_fake
|
| 153 |
+
|
| 154 |
+
return {
|
| 155 |
+
"full_frame": {"fake": fake_full, "real": real_full},
|
| 156 |
+
"face_crop": {"fake": fake_face, "real": real_face, "has_face": has_face},
|
| 157 |
+
"frequency": freq_fakeness,
|
| 158 |
+
"ensemble": {"fake": round(ensemble_fake, 4), "real": round(ensemble_real, 4)},
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def test_video(path, sample_rate=20):
|
| 163 |
+
"""Analyze a video and return the ensemble verdict."""
|
| 164 |
+
cap = cv2.VideoCapture(path)
|
| 165 |
+
if not cap.isOpened():
|
| 166 |
+
return None
|
| 167 |
+
|
| 168 |
+
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 169 |
+
frame_idx = 0
|
| 170 |
+
results = []
|
| 171 |
+
|
| 172 |
+
while True:
|
| 173 |
+
ret, frame = cap.read()
|
| 174 |
+
if not ret:
|
| 175 |
+
break
|
| 176 |
+
if frame_idx % sample_rate == 0:
|
| 177 |
+
r = analyze_frame(frame)
|
| 178 |
+
results.append(r)
|
| 179 |
+
frame_idx += 1
|
| 180 |
+
cap.release()
|
| 181 |
+
|
| 182 |
+
if not results:
|
| 183 |
+
return None
|
| 184 |
+
|
| 185 |
+
avg_fake = np.mean([r["ensemble"]["fake"] for r in results])
|
| 186 |
+
avg_real = np.mean([r["ensemble"]["real"] for r in results])
|
| 187 |
+
avg_full_fake = np.mean([r["full_frame"]["fake"] for r in results])
|
| 188 |
+
avg_face_fake = np.mean([r["face_crop"]["fake"] for r in results])
|
| 189 |
+
avg_freq = np.mean([r["frequency"] for r in results])
|
| 190 |
+
|
| 191 |
+
return {
|
| 192 |
+
"is_fake": avg_fake > 0.5,
|
| 193 |
+
"ensemble_fake": round(avg_fake, 4),
|
| 194 |
+
"ensemble_real": round(avg_real, 4),
|
| 195 |
+
"full_frame_fake": round(avg_full_fake, 4),
|
| 196 |
+
"face_crop_fake": round(avg_face_fake, 4),
|
| 197 |
+
"freq_score": round(avg_freq, 4),
|
| 198 |
+
"frames_analyzed": len(results),
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
# ββ Run benchmark βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 203 |
+
fake_dir = r"C:\Users\gamin\Downloads\videos_fake"
|
| 204 |
+
real_dir = r"C:\Users\gamin\Downloads\videos_real"
|
| 205 |
+
|
| 206 |
+
fake_videos = sorted(glob.glob(os.path.join(fake_dir, "*.mp4")))[:10]
|
| 207 |
+
real_videos = sorted(glob.glob(os.path.join(real_dir, "*.mp4")))[:10]
|
| 208 |
+
|
| 209 |
+
print(f"\n{'='*80}")
|
| 210 |
+
print(f"BENCHMARK: Testing {len(fake_videos)} fake + {len(real_videos)} real videos")
|
| 211 |
+
print(f"{'='*80}")
|
| 212 |
+
|
| 213 |
+
correct_fake = 0
|
| 214 |
+
correct_real = 0
|
| 215 |
+
|
| 216 |
+
print(f"\n--- FAKE VIDEOS (should detect as Fake) ---")
|
| 217 |
+
for v in fake_videos:
|
| 218 |
+
t0 = time.time()
|
| 219 |
+
r = test_video(v)
|
| 220 |
+
dt = time.time() - t0
|
| 221 |
+
if r is None:
|
| 222 |
+
print(f" {os.path.basename(v)}: SKIPPED")
|
| 223 |
+
continue
|
| 224 |
+
verdict = "FAKE" if r["is_fake"] else "REAL"
|
| 225 |
+
correct = "OK" if r["is_fake"] else "XX"
|
| 226 |
+
if r["is_fake"]:
|
| 227 |
+
correct_fake += 1
|
| 228 |
+
print(f" {correct} {os.path.basename(v)}: {verdict} (ens={r['ensemble_fake']:.3f}, full={r['full_frame_fake']:.3f}, face={r['face_crop_fake']:.3f}, freq={r['freq_score']:.3f}) [{dt:.1f}s]")
|
| 229 |
+
|
| 230 |
+
print(f"\n--- REAL VIDEOS (should detect as Real) ---")
|
| 231 |
+
for v in real_videos:
|
| 232 |
+
t0 = time.time()
|
| 233 |
+
r = test_video(v)
|
| 234 |
+
dt = time.time() - t0
|
| 235 |
+
if r is None:
|
| 236 |
+
print(f" {os.path.basename(v)}: SKIPPED")
|
| 237 |
+
continue
|
| 238 |
+
verdict = "FAKE" if r["is_fake"] else "REAL"
|
| 239 |
+
correct = "OK" if not r["is_fake"] else "XX"
|
| 240 |
+
if not r["is_fake"]:
|
| 241 |
+
correct_real += 1
|
| 242 |
+
print(f" {correct} {os.path.basename(v)}: {verdict} (ens={r['ensemble_fake']:.3f}, full={r['full_frame_fake']:.3f}, face={r['face_crop_fake']:.3f}, freq={r['freq_score']:.3f}) [{dt:.1f}s]")
|
| 243 |
+
|
| 244 |
+
total = len(fake_videos) + len(real_videos)
|
| 245 |
+
correct_total = correct_fake + correct_real
|
| 246 |
+
print(f"\n{'='*80}")
|
| 247 |
+
print(f"RESULTS: {correct_total}/{total} correct ({100*correct_total/total:.1f}%)")
|
| 248 |
+
print(f" Fake accuracy: {correct_fake}/{len(fake_videos)} ({100*correct_fake/len(fake_videos):.1f}%)")
|
| 249 |
+
print(f" Real accuracy: {correct_real}/{len(real_videos)} ({100*correct_real/len(real_videos):.1f}%)")
|
| 250 |
+
print(f"{'='*80}")
|
scratch/benchmark_ensemble_v2.py
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Improved ensemble benchmark v2 - adds forensic face-boundary and noise
|
| 3 |
+
analysis, and properly calibrates using the face-crop signal.
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
import glob
|
| 8 |
+
import time
|
| 9 |
+
import cv2
|
| 10 |
+
import torch
|
| 11 |
+
import numpy as np
|
| 12 |
+
from PIL import Image
|
| 13 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 14 |
+
|
| 15 |
+
sys.path.insert(0, ".")
|
| 16 |
+
|
| 17 |
+
MODEL_ID = "prithivMLmods/deepfake-detector-model-v1"
|
| 18 |
+
|
| 19 |
+
print("Loading model...")
|
| 20 |
+
processor = AutoImageProcessor.from_pretrained(MODEL_ID)
|
| 21 |
+
model = AutoModelForImageClassification.from_pretrained(MODEL_ID)
|
| 22 |
+
model.eval()
|
| 23 |
+
id2label = model.config.id2label
|
| 24 |
+
print(f"Labels: {id2label}")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def get_face_region(img_bgr):
|
| 28 |
+
"""Returns (face_crop, face_mask, face_rect) or (None, None, None)."""
|
| 29 |
+
gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
|
| 30 |
+
cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 31 |
+
faces = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=4, minSize=(30, 30))
|
| 32 |
+
if len(faces) == 0:
|
| 33 |
+
return None, None, None
|
| 34 |
+
|
| 35 |
+
faces = sorted(faces, key=lambda x: x[2]*x[3], reverse=True)
|
| 36 |
+
x, y, w, h = faces[0]
|
| 37 |
+
margin = int(w * 0.3)
|
| 38 |
+
x1, y1 = max(0, x - margin), max(0, y - margin)
|
| 39 |
+
x2, y2 = min(img_bgr.shape[1], x + w + margin), min(img_bgr.shape[0], y + h + margin)
|
| 40 |
+
|
| 41 |
+
face_crop = img_bgr[y1:y2, x1:x2]
|
| 42 |
+
|
| 43 |
+
# Create binary mask for face region (elliptical)
|
| 44 |
+
mask = np.zeros(img_bgr.shape[:2], dtype=np.uint8)
|
| 45 |
+
cx, cy = x + w // 2, y + h // 2
|
| 46 |
+
cv2.ellipse(mask, (cx, cy), (w // 2, int(h * 0.6)), 0, 0, 360, 255, -1)
|
| 47 |
+
|
| 48 |
+
return face_crop, mask, (x, y, w, h)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def classify_pil(pil_image):
|
| 52 |
+
"""Run model -> (fake_prob, real_prob)."""
|
| 53 |
+
inputs = processor(images=pil_image, return_tensors="pt")
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
out = model(**inputs)
|
| 56 |
+
probs = torch.nn.functional.softmax(out.logits, dim=-1)[0]
|
| 57 |
+
return float(probs[0]), float(probs[1])
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def face_boundary_analysis(img_bgr, face_mask):
|
| 61 |
+
"""
|
| 62 |
+
Analyze the boundary where a swapped face meets the original image.
|
| 63 |
+
Deepfakes have smoother/blurrier boundaries due to blending.
|
| 64 |
+
Returns fakeness score 0-1.
|
| 65 |
+
"""
|
| 66 |
+
if face_mask is None:
|
| 67 |
+
return 0.5 # neutral
|
| 68 |
+
|
| 69 |
+
gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY).astype(np.float32)
|
| 70 |
+
|
| 71 |
+
# Create boundary strip: dilated mask - eroded mask
|
| 72 |
+
kernel = np.ones((7, 7), np.uint8)
|
| 73 |
+
dilated = cv2.dilate(face_mask, kernel, iterations=3)
|
| 74 |
+
eroded = cv2.erode(face_mask, kernel, iterations=3)
|
| 75 |
+
boundary = cv2.subtract(dilated, eroded)
|
| 76 |
+
|
| 77 |
+
if boundary.sum() == 0:
|
| 78 |
+
return 0.5
|
| 79 |
+
|
| 80 |
+
# Compute gradient magnitude along the boundary
|
| 81 |
+
grad_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
|
| 82 |
+
grad_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
|
| 83 |
+
grad_mag = np.sqrt(grad_x**2 + grad_y**2)
|
| 84 |
+
|
| 85 |
+
# Average gradient at boundary vs. inside face
|
| 86 |
+
boundary_grad = grad_mag[boundary > 0].mean() if (boundary > 0).any() else 0
|
| 87 |
+
inside_grad = grad_mag[eroded > 0].mean() if (eroded > 0).any() else 1
|
| 88 |
+
|
| 89 |
+
# In deepfakes, boundary gradients are often LOWER (smoother blending)
|
| 90 |
+
# relative to interior detail. Genuine faces have natural transitions.
|
| 91 |
+
ratio = boundary_grad / (inside_grad + 1e-8)
|
| 92 |
+
|
| 93 |
+
# High ratio = sharp boundary (more natural), Low ratio = smooth blend (more fake)
|
| 94 |
+
# Typical range: 0.5 to 3.0
|
| 95 |
+
# Map to fakeness: lower ratio = more likely fake
|
| 96 |
+
fakeness = max(0, min(1, 1.0 - (ratio - 0.5) / 2.5))
|
| 97 |
+
|
| 98 |
+
return round(fakeness, 4)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def noise_inconsistency(img_bgr, face_mask):
|
| 102 |
+
"""
|
| 103 |
+
Check if the noise pattern in the face region differs from the background.
|
| 104 |
+
Face-swapped images have different noise characteristics in swapped regions.
|
| 105 |
+
Returns fakeness score 0-1.
|
| 106 |
+
"""
|
| 107 |
+
if face_mask is None:
|
| 108 |
+
return 0.5
|
| 109 |
+
|
| 110 |
+
gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY).astype(np.float64)
|
| 111 |
+
|
| 112 |
+
# High-pass filter to isolate noise
|
| 113 |
+
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 114 |
+
noise = gray - blurred
|
| 115 |
+
|
| 116 |
+
# Noise statistics inside vs outside face
|
| 117 |
+
face_pixels = noise[face_mask > 0]
|
| 118 |
+
bg_mask = cv2.bitwise_not(face_mask)
|
| 119 |
+
bg_pixels = noise[bg_mask > 0]
|
| 120 |
+
|
| 121 |
+
if len(face_pixels) < 100 or len(bg_pixels) < 100:
|
| 122 |
+
return 0.5
|
| 123 |
+
|
| 124 |
+
face_std = face_pixels.std()
|
| 125 |
+
bg_std = bg_pixels.std()
|
| 126 |
+
face_mean = abs(face_pixels.mean())
|
| 127 |
+
bg_mean = abs(bg_pixels.mean())
|
| 128 |
+
|
| 129 |
+
# Noise level difference - in genuine images, noise is more uniform
|
| 130 |
+
std_diff = abs(face_std - bg_std) / (max(face_std, bg_std) + 1e-8)
|
| 131 |
+
mean_diff = abs(face_mean - bg_mean) / (max(face_mean, bg_mean) + 1e-8)
|
| 132 |
+
|
| 133 |
+
# Higher difference = more likely manipulated
|
| 134 |
+
fakeness = min(1.0, (std_diff * 0.6 + mean_diff * 0.4) * 2.0)
|
| 135 |
+
|
| 136 |
+
return round(fakeness, 4)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def error_level_analysis(img_bgr, face_mask):
|
| 140 |
+
"""
|
| 141 |
+
Error Level Analysis (ELA): Re-save at known JPEG quality, compare
|
| 142 |
+
error patterns. Manipulated regions show different error levels.
|
| 143 |
+
Returns fakeness score 0-1.
|
| 144 |
+
"""
|
| 145 |
+
if face_mask is None:
|
| 146 |
+
return 0.5
|
| 147 |
+
|
| 148 |
+
# Encode as JPEG at quality 90, then decode
|
| 149 |
+
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
|
| 150 |
+
_, encoded = cv2.imencode('.jpg', img_bgr, encode_param)
|
| 151 |
+
decoded = cv2.imdecode(encoded, cv2.IMREAD_COLOR)
|
| 152 |
+
|
| 153 |
+
# ELA = absolute difference between original and re-saved
|
| 154 |
+
ela = cv2.absdiff(img_bgr, decoded).astype(np.float64)
|
| 155 |
+
ela_gray = cv2.cvtColor(ela.astype(np.uint8), cv2.COLOR_BGR2GRAY).astype(np.float64)
|
| 156 |
+
|
| 157 |
+
# Compare ELA levels inside face vs background
|
| 158 |
+
face_ela = ela_gray[face_mask > 0]
|
| 159 |
+
bg_mask = cv2.bitwise_not(face_mask)
|
| 160 |
+
bg_ela = ela_gray[bg_mask > 0]
|
| 161 |
+
|
| 162 |
+
if len(face_ela) < 100 or len(bg_ela) < 100:
|
| 163 |
+
return 0.5
|
| 164 |
+
|
| 165 |
+
face_ela_mean = face_ela.mean()
|
| 166 |
+
bg_ela_mean = bg_ela.mean()
|
| 167 |
+
|
| 168 |
+
# Different ELA levels between face and background indicate manipulation
|
| 169 |
+
ela_diff = abs(face_ela_mean - bg_ela_mean) / (max(face_ela_mean, bg_ela_mean) + 1e-8)
|
| 170 |
+
|
| 171 |
+
fakeness = min(1.0, ela_diff * 3.0)
|
| 172 |
+
return round(fakeness, 4)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def analyze_frame(img_bgr):
|
| 176 |
+
"""Ensemble analysis on a single BGR frame."""
|
| 177 |
+
rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
|
| 178 |
+
pil_full = Image.fromarray(rgb)
|
| 179 |
+
|
| 180 |
+
face_crop, face_mask, face_rect = get_face_region(img_bgr)
|
| 181 |
+
|
| 182 |
+
# 1. Full-frame model
|
| 183 |
+
fake_full, real_full = classify_pil(pil_full)
|
| 184 |
+
|
| 185 |
+
# 2. Face-crop model
|
| 186 |
+
if face_crop is not None:
|
| 187 |
+
face_rgb = cv2.cvtColor(face_crop, cv2.COLOR_BGR2RGB)
|
| 188 |
+
pil_face = Image.fromarray(face_rgb)
|
| 189 |
+
fake_face, real_face = classify_pil(pil_face)
|
| 190 |
+
has_face = True
|
| 191 |
+
else:
|
| 192 |
+
fake_face, real_face = fake_full, real_full
|
| 193 |
+
has_face = False
|
| 194 |
+
|
| 195 |
+
# 3. Forensic signals
|
| 196 |
+
boundary_score = face_boundary_analysis(img_bgr, face_mask)
|
| 197 |
+
noise_score = noise_inconsistency(img_bgr, face_mask)
|
| 198 |
+
ela_score = error_level_analysis(img_bgr, face_mask)
|
| 199 |
+
|
| 200 |
+
# 4. Calibrated ensemble
|
| 201 |
+
# Key insight from v1 benchmark: face_crop is the most discriminating signal.
|
| 202 |
+
# full_frame is biased toward Fake but useful as a secondary signal.
|
| 203 |
+
# Forensic signals add independent information.
|
| 204 |
+
if has_face:
|
| 205 |
+
ensemble_fake = (
|
| 206 |
+
0.15 * fake_full + # full-frame (biased, downweight it)
|
| 207 |
+
0.40 * fake_face + # face-crop (most discriminating)
|
| 208 |
+
0.15 * boundary_score + # face boundary artifacts
|
| 209 |
+
0.15 * noise_score + # noise inconsistency
|
| 210 |
+
0.15 * ela_score # error level analysis
|
| 211 |
+
)
|
| 212 |
+
else:
|
| 213 |
+
ensemble_fake = (
|
| 214 |
+
0.40 * fake_full +
|
| 215 |
+
0.20 * boundary_score +
|
| 216 |
+
0.20 * noise_score +
|
| 217 |
+
0.20 * ela_score
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
return {
|
| 221 |
+
"full_fake": fake_full,
|
| 222 |
+
"face_fake": fake_face,
|
| 223 |
+
"has_face": has_face,
|
| 224 |
+
"boundary": boundary_score,
|
| 225 |
+
"noise": noise_score,
|
| 226 |
+
"ela": ela_score,
|
| 227 |
+
"ensemble_fake": round(ensemble_fake, 4),
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
def test_video(path, sample_rate=20):
|
| 232 |
+
"""Analyze a video and return the ensemble verdict."""
|
| 233 |
+
cap = cv2.VideoCapture(path)
|
| 234 |
+
if not cap.isOpened():
|
| 235 |
+
return None
|
| 236 |
+
|
| 237 |
+
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 238 |
+
frame_idx = 0
|
| 239 |
+
results = []
|
| 240 |
+
|
| 241 |
+
while True:
|
| 242 |
+
ret, frame = cap.read()
|
| 243 |
+
if not ret:
|
| 244 |
+
break
|
| 245 |
+
if frame_idx % sample_rate == 0:
|
| 246 |
+
r = analyze_frame(frame)
|
| 247 |
+
results.append(r)
|
| 248 |
+
frame_idx += 1
|
| 249 |
+
cap.release()
|
| 250 |
+
|
| 251 |
+
if not results:
|
| 252 |
+
return None
|
| 253 |
+
|
| 254 |
+
avg = lambda key: round(np.mean([r[key] for r in results]), 4)
|
| 255 |
+
|
| 256 |
+
return {
|
| 257 |
+
"is_fake": avg("ensemble_fake") > 0.5,
|
| 258 |
+
"ens": avg("ensemble_fake"),
|
| 259 |
+
"full": avg("full_fake"),
|
| 260 |
+
"face": avg("face_fake"),
|
| 261 |
+
"bnd": avg("boundary"),
|
| 262 |
+
"noi": avg("noise"),
|
| 263 |
+
"ela": avg("ela"),
|
| 264 |
+
"frames": len(results),
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
# ββ Benchmark βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 269 |
+
fake_dir = r"C:\Users\gamin\Downloads\videos_fake"
|
| 270 |
+
real_dir = r"C:\Users\gamin\Downloads\videos_real"
|
| 271 |
+
|
| 272 |
+
fake_videos = sorted(glob.glob(os.path.join(fake_dir, "*.mp4")))[:10]
|
| 273 |
+
real_videos = sorted(glob.glob(os.path.join(real_dir, "*.mp4")))[:10]
|
| 274 |
+
|
| 275 |
+
print(f"\n{'='*90}")
|
| 276 |
+
print(f"BENCHMARK v2: {len(fake_videos)} fake + {len(real_videos)} real videos")
|
| 277 |
+
print(f"{'='*90}")
|
| 278 |
+
|
| 279 |
+
correct_fake = 0
|
| 280 |
+
correct_real = 0
|
| 281 |
+
|
| 282 |
+
print(f"\n--- FAKE VIDEOS (expect Fake) ---")
|
| 283 |
+
print(f" {'File':<12} {'Result':<6} {'Ens':>5} {'Full':>5} {'Face':>5} {'Bnd':>5} {'Noi':>5} {'ELA':>5}")
|
| 284 |
+
for v in fake_videos:
|
| 285 |
+
t0 = time.time()
|
| 286 |
+
r = test_video(v)
|
| 287 |
+
dt = time.time() - t0
|
| 288 |
+
if r is None: continue
|
| 289 |
+
ok = "OK" if r["is_fake"] else "XX"
|
| 290 |
+
if r["is_fake"]: correct_fake += 1
|
| 291 |
+
print(f" {ok} {os.path.basename(v):<10} {'FAKE' if r['is_fake'] else 'REAL':<6} {r['ens']:.3f} {r['full']:.3f} {r['face']:.3f} {r['bnd']:.3f} {r['noi']:.3f} {r['ela']:.3f} [{dt:.1f}s]")
|
| 292 |
+
|
| 293 |
+
print(f"\n--- REAL VIDEOS (expect Real) ---")
|
| 294 |
+
print(f" {'File':<12} {'Result':<6} {'Ens':>5} {'Full':>5} {'Face':>5} {'Bnd':>5} {'Noi':>5} {'ELA':>5}")
|
| 295 |
+
for v in real_videos:
|
| 296 |
+
t0 = time.time()
|
| 297 |
+
r = test_video(v)
|
| 298 |
+
dt = time.time() - t0
|
| 299 |
+
if r is None: continue
|
| 300 |
+
ok = "OK" if not r["is_fake"] else "XX"
|
| 301 |
+
if not r["is_fake"]: correct_real += 1
|
| 302 |
+
print(f" {ok} {os.path.basename(v):<10} {'FAKE' if r['is_fake'] else 'REAL':<6} {r['ens']:.3f} {r['full']:.3f} {r['face']:.3f} {r['bnd']:.3f} {r['noi']:.3f} {r['ela']:.3f} [{dt:.1f}s]")
|
| 303 |
+
|
| 304 |
+
total = len(fake_videos) + len(real_videos)
|
| 305 |
+
correct_total = correct_fake + correct_real
|
| 306 |
+
print(f"\n{'='*90}")
|
| 307 |
+
print(f"RESULTS: {correct_total}/{total} correct ({100*correct_total/total:.1f}%)")
|
| 308 |
+
print(f" Fake accuracy: {correct_fake}/{len(fake_videos)} ({100*correct_fake/len(fake_videos):.1f}%)")
|
| 309 |
+
print(f" Real accuracy: {correct_real}/{len(real_videos)} ({100*correct_real/len(real_videos):.1f}%)")
|
| 310 |
+
print(f"{'='*90}")
|
scratch/benchmark_temporal.py
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Benchmark v3: Temporal consistency analysis.
|
| 3 |
+
Deepfakes have MORE frame-to-frame variance in model scores and face position.
|
| 4 |
+
"""
|
| 5 |
+
import os, sys, glob, time, cv2, torch
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 9 |
+
|
| 10 |
+
sys.path.insert(0, ".")
|
| 11 |
+
|
| 12 |
+
MODEL_ID = "prithivMLmods/deepfake-detector-model-v1"
|
| 13 |
+
print("Loading model...")
|
| 14 |
+
processor = AutoImageProcessor.from_pretrained(MODEL_ID)
|
| 15 |
+
model = AutoModelForImageClassification.from_pretrained(MODEL_ID)
|
| 16 |
+
model.eval()
|
| 17 |
+
|
| 18 |
+
cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def get_face_rect(img_bgr):
|
| 22 |
+
gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
|
| 23 |
+
faces = cascade.detectMultiScale(gray, 1.1, 4, minSize=(30, 30))
|
| 24 |
+
if len(faces) == 0:
|
| 25 |
+
return None
|
| 26 |
+
faces = sorted(faces, key=lambda x: x[2]*x[3], reverse=True)
|
| 27 |
+
return faces[0] # (x, y, w, h)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def classify_bgr(img_bgr, face_rect=None):
|
| 31 |
+
"""Classify an image region. Returns fake_prob."""
|
| 32 |
+
if face_rect is not None:
|
| 33 |
+
x, y, w, h = face_rect
|
| 34 |
+
m = int(w * 0.3)
|
| 35 |
+
x1, y1 = max(0, x-m), max(0, y-m)
|
| 36 |
+
x2, y2 = min(img_bgr.shape[1], x+w+m), min(img_bgr.shape[0], y+h+m)
|
| 37 |
+
img_bgr = img_bgr[y1:y2, x1:x2]
|
| 38 |
+
|
| 39 |
+
rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
|
| 40 |
+
pil = Image.fromarray(rgb)
|
| 41 |
+
inputs = processor(images=pil, return_tensors="pt")
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
out = model(**inputs)
|
| 44 |
+
probs = torch.nn.functional.softmax(out.logits, dim=-1)[0]
|
| 45 |
+
return float(probs[0]) # fake prob
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def analyze_video_temporal(path, sample_rate=5):
|
| 49 |
+
"""Extract per-frame scores and face positions for temporal analysis."""
|
| 50 |
+
cap = cv2.VideoCapture(path)
|
| 51 |
+
if not cap.isOpened():
|
| 52 |
+
return None
|
| 53 |
+
|
| 54 |
+
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 55 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
|
| 56 |
+
|
| 57 |
+
# Cap frames analyzed
|
| 58 |
+
MAX_FRAMES = 30
|
| 59 |
+
effective_rate = max(sample_rate, total // MAX_FRAMES)
|
| 60 |
+
|
| 61 |
+
frame_idx = 0
|
| 62 |
+
face_scores = [] # face-crop model scores per frame
|
| 63 |
+
full_scores = [] # full-frame model scores per frame
|
| 64 |
+
face_positions = [] # (cx, cy, w, h) per frame
|
| 65 |
+
face_found_count = 0
|
| 66 |
+
|
| 67 |
+
while True:
|
| 68 |
+
ret, frame = cap.read()
|
| 69 |
+
if not ret:
|
| 70 |
+
break
|
| 71 |
+
if frame_idx % effective_rate == 0:
|
| 72 |
+
face = get_face_rect(frame)
|
| 73 |
+
|
| 74 |
+
# Full frame score
|
| 75 |
+
full_fake = classify_bgr(frame)
|
| 76 |
+
full_scores.append(full_fake)
|
| 77 |
+
|
| 78 |
+
if face is not None:
|
| 79 |
+
# Face-crop score
|
| 80 |
+
face_fake = classify_bgr(frame, face)
|
| 81 |
+
face_scores.append(face_fake)
|
| 82 |
+
|
| 83 |
+
x, y, w, h = face
|
| 84 |
+
cx, cy = x + w/2, y + h/2
|
| 85 |
+
face_positions.append((cx, cy, w, h))
|
| 86 |
+
face_found_count += 1
|
| 87 |
+
else:
|
| 88 |
+
face_scores.append(full_fake) # fallback
|
| 89 |
+
|
| 90 |
+
frame_idx += 1
|
| 91 |
+
cap.release()
|
| 92 |
+
|
| 93 |
+
if len(full_scores) < 3:
|
| 94 |
+
return None
|
| 95 |
+
|
| 96 |
+
full_arr = np.array(full_scores)
|
| 97 |
+
face_arr = np.array(face_scores)
|
| 98 |
+
|
| 99 |
+
# ββ Temporal signals ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 100 |
+
|
| 101 |
+
# 1. Score variance (deepfakes have more variable per-frame scores)
|
| 102 |
+
face_score_std = face_arr.std()
|
| 103 |
+
full_score_std = full_arr.std()
|
| 104 |
+
|
| 105 |
+
# 2. Face position jitter (deepfakes have jittery face tracking)
|
| 106 |
+
face_jitter = 0
|
| 107 |
+
if len(face_positions) >= 3:
|
| 108 |
+
pos_arr = np.array(face_positions)
|
| 109 |
+
# Compute frame-to-frame position deltas normalized by face size
|
| 110 |
+
deltas = np.diff(pos_arr[:, :2], axis=0) # cx, cy changes
|
| 111 |
+
face_sizes = pos_arr[:-1, 2] # w of face
|
| 112 |
+
normalized_deltas = np.sqrt((deltas**2).sum(axis=1)) / (face_sizes + 1e-8)
|
| 113 |
+
face_jitter = normalized_deltas.std() # std of normalized movement
|
| 114 |
+
|
| 115 |
+
# 3. Face size consistency (deepfakes have variable face sizes)
|
| 116 |
+
face_size_var = 0
|
| 117 |
+
if len(face_positions) >= 3:
|
| 118 |
+
sizes = np.array([p[2] * p[3] for p in face_positions])
|
| 119 |
+
face_size_var = sizes.std() / (sizes.mean() + 1e-8)
|
| 120 |
+
|
| 121 |
+
# 4. Score difference pattern: how much does face-crop differ from full-frame?
|
| 122 |
+
diff = full_arr - face_arr
|
| 123 |
+
avg_diff = diff.mean() # reals tend to have HIGHER diff (face looks more real than full frame)
|
| 124 |
+
|
| 125 |
+
# 5. Mean scores
|
| 126 |
+
face_mean = face_arr.mean()
|
| 127 |
+
full_mean = full_arr.mean()
|
| 128 |
+
|
| 129 |
+
return {
|
| 130 |
+
"face_mean": round(face_mean, 4),
|
| 131 |
+
"full_mean": round(full_mean, 4),
|
| 132 |
+
"face_std": round(face_score_std, 4),
|
| 133 |
+
"full_std": round(full_score_std, 4),
|
| 134 |
+
"face_jitter": round(face_jitter, 4),
|
| 135 |
+
"face_size_var": round(face_size_var, 4),
|
| 136 |
+
"score_diff": round(avg_diff, 4),
|
| 137 |
+
"frames": len(full_scores),
|
| 138 |
+
"faces_found": face_found_count,
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
# ββ Benchmark βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 143 |
+
fake_dir = r"C:\Users\gamin\Downloads\videos_fake"
|
| 144 |
+
real_dir = r"C:\Users\gamin\Downloads\videos_real"
|
| 145 |
+
|
| 146 |
+
fake_videos = sorted(glob.glob(os.path.join(fake_dir, "*.mp4")))[:10]
|
| 147 |
+
real_videos = sorted(glob.glob(os.path.join(real_dir, "*.mp4")))[:10]
|
| 148 |
+
|
| 149 |
+
print(f"\n{'='*100}")
|
| 150 |
+
print(f"TEMPORAL ANALYSIS: {len(fake_videos)} fake + {len(real_videos)} real videos")
|
| 151 |
+
print(f"{'='*100}")
|
| 152 |
+
|
| 153 |
+
header = f" {'File':<12} {'FaceMn':>6} {'FullMn':>6} {'FaceSD':>6} {'FullSD':>6} {'Jitter':>6} {'SzVar':>6} {'Diff':>6}"
|
| 154 |
+
|
| 155 |
+
print(f"\n--- FAKE VIDEOS ---")
|
| 156 |
+
print(header)
|
| 157 |
+
fake_results = []
|
| 158 |
+
for v in fake_videos:
|
| 159 |
+
t0 = time.time()
|
| 160 |
+
r = analyze_video_temporal(v)
|
| 161 |
+
dt = time.time() - t0
|
| 162 |
+
if r is None: continue
|
| 163 |
+
fake_results.append(r)
|
| 164 |
+
print(f" {os.path.basename(v):<12} {r['face_mean']:.4f} {r['full_mean']:.4f} {r['face_std']:.4f} {r['full_std']:.4f} {r['face_jitter']:.4f} {r['face_size_var']:.4f} {r['score_diff']:.4f} [{dt:.1f}s]")
|
| 165 |
+
|
| 166 |
+
print(f"\n--- REAL VIDEOS ---")
|
| 167 |
+
print(header)
|
| 168 |
+
real_results = []
|
| 169 |
+
for v in real_videos:
|
| 170 |
+
t0 = time.time()
|
| 171 |
+
r = analyze_video_temporal(v)
|
| 172 |
+
dt = time.time() - t0
|
| 173 |
+
if r is None: continue
|
| 174 |
+
real_results.append(r)
|
| 175 |
+
print(f" {os.path.basename(v):<12} {r['face_mean']:.4f} {r['full_mean']:.4f} {r['face_std']:.4f} {r['full_std']:.4f} {r['face_jitter']:.4f} {r['face_size_var']:.4f} {r['score_diff']:.4f} [{dt:.1f}s]")
|
| 176 |
+
|
| 177 |
+
# ββ Statistical comparison ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 178 |
+
print(f"\n{'='*100}")
|
| 179 |
+
print(f"SIGNAL ANALYSIS (mean +/- std for each group)")
|
| 180 |
+
print(f"{'='*100}")
|
| 181 |
+
|
| 182 |
+
for signal in ['face_mean', 'full_mean', 'face_std', 'full_std', 'face_jitter', 'face_size_var', 'score_diff']:
|
| 183 |
+
fake_vals = [r[signal] for r in fake_results]
|
| 184 |
+
real_vals = [r[signal] for r in real_results]
|
| 185 |
+
f_mean, f_std = np.mean(fake_vals), np.std(fake_vals)
|
| 186 |
+
r_mean, r_std = np.mean(real_vals), np.std(real_vals)
|
| 187 |
+
sep = abs(f_mean - r_mean) / (max(f_std, r_std) + 1e-8)
|
| 188 |
+
direction = "FAKE>REAL" if f_mean > r_mean else "REAL>FAKE"
|
| 189 |
+
quality = "***" if sep > 1.0 else "**" if sep > 0.5 else "*" if sep > 0.3 else ""
|
| 190 |
+
print(f" {signal:<14}: Fake={f_mean:.4f}+/-{f_std:.4f} Real={r_mean:.4f}+/-{r_std:.4f} Sep={sep:.2f} {direction} {quality}")
|
scratch/benchmark_v4_gridsearch.py
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Benchmark v4: Optimal ensemble using face_mean + score_diff.
|
| 3 |
+
Grid-searches weights and threshold on the test data.
|
| 4 |
+
"""
|
| 5 |
+
import os, sys, glob, time, cv2, torch
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 9 |
+
|
| 10 |
+
sys.path.insert(0, ".")
|
| 11 |
+
|
| 12 |
+
MODEL_ID = "prithivMLmods/deepfake-detector-model-v1"
|
| 13 |
+
print("Loading model...")
|
| 14 |
+
processor = AutoImageProcessor.from_pretrained(MODEL_ID)
|
| 15 |
+
model = AutoModelForImageClassification.from_pretrained(MODEL_ID)
|
| 16 |
+
model.eval()
|
| 17 |
+
|
| 18 |
+
cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def get_face_rect(img_bgr):
|
| 22 |
+
gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
|
| 23 |
+
faces = cascade.detectMultiScale(gray, 1.1, 4, minSize=(30, 30))
|
| 24 |
+
if len(faces) == 0:
|
| 25 |
+
return None
|
| 26 |
+
return sorted(faces, key=lambda x: x[2]*x[3], reverse=True)[0]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def classify_bgr(img_bgr, face_rect=None):
|
| 30 |
+
if face_rect is not None:
|
| 31 |
+
x, y, w, h = face_rect
|
| 32 |
+
m = int(w * 0.3)
|
| 33 |
+
x1, y1 = max(0, x-m), max(0, y-m)
|
| 34 |
+
x2, y2 = min(img_bgr.shape[1], x+w+m), min(img_bgr.shape[0], y+h+m)
|
| 35 |
+
img_bgr = img_bgr[y1:y2, x1:x2]
|
| 36 |
+
rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
|
| 37 |
+
pil = Image.fromarray(rgb)
|
| 38 |
+
inputs = processor(images=pil, return_tensors="pt")
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
out = model(**inputs)
|
| 41 |
+
probs = torch.nn.functional.softmax(out.logits, dim=-1)[0]
|
| 42 |
+
return float(probs[0])
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def analyze_video(path, sample_rate=5):
|
| 46 |
+
cap = cv2.VideoCapture(path)
|
| 47 |
+
if not cap.isOpened():
|
| 48 |
+
return None
|
| 49 |
+
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 50 |
+
MAX_FRAMES = 30
|
| 51 |
+
effective_rate = max(sample_rate, total // MAX_FRAMES)
|
| 52 |
+
|
| 53 |
+
frame_idx = 0
|
| 54 |
+
face_scores = []
|
| 55 |
+
full_scores = []
|
| 56 |
+
|
| 57 |
+
while True:
|
| 58 |
+
ret, frame = cap.read()
|
| 59 |
+
if not ret: break
|
| 60 |
+
if frame_idx % effective_rate == 0:
|
| 61 |
+
face = get_face_rect(frame)
|
| 62 |
+
full_fake = classify_bgr(frame)
|
| 63 |
+
full_scores.append(full_fake)
|
| 64 |
+
if face is not None:
|
| 65 |
+
face_fake = classify_bgr(frame, face)
|
| 66 |
+
face_scores.append(face_fake)
|
| 67 |
+
else:
|
| 68 |
+
face_scores.append(full_fake)
|
| 69 |
+
frame_idx += 1
|
| 70 |
+
cap.release()
|
| 71 |
+
|
| 72 |
+
if len(full_scores) < 2:
|
| 73 |
+
return None
|
| 74 |
+
|
| 75 |
+
face_mean = np.mean(face_scores)
|
| 76 |
+
full_mean = np.mean(full_scores)
|
| 77 |
+
score_diff = full_mean - face_mean
|
| 78 |
+
|
| 79 |
+
return {"face_mean": face_mean, "full_mean": full_mean, "score_diff": score_diff}
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# ββ Collect data ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 83 |
+
fake_dir = r"C:\Users\gamin\Downloads\videos_fake"
|
| 84 |
+
real_dir = r"C:\Users\gamin\Downloads\videos_real"
|
| 85 |
+
|
| 86 |
+
# Use first 15 of each for more data
|
| 87 |
+
fake_videos = sorted(glob.glob(os.path.join(fake_dir, "*.mp4")))[:15]
|
| 88 |
+
real_videos = sorted(glob.glob(os.path.join(real_dir, "*.mp4")))[:15]
|
| 89 |
+
|
| 90 |
+
print(f"\nAnalyzing {len(fake_videos)} fake + {len(real_videos)} real videos...")
|
| 91 |
+
|
| 92 |
+
fake_data = []
|
| 93 |
+
real_data = []
|
| 94 |
+
|
| 95 |
+
print("\n--- Fake videos ---")
|
| 96 |
+
for v in fake_videos:
|
| 97 |
+
t0 = time.time()
|
| 98 |
+
r = analyze_video(v)
|
| 99 |
+
dt = time.time() - t0
|
| 100 |
+
if r is None: continue
|
| 101 |
+
fake_data.append(r)
|
| 102 |
+
print(f" {os.path.basename(v):<12} face={r['face_mean']:.4f} full={r['full_mean']:.4f} diff={r['score_diff']:.4f} [{dt:.1f}s]")
|
| 103 |
+
|
| 104 |
+
print("\n--- Real videos ---")
|
| 105 |
+
for v in real_videos:
|
| 106 |
+
t0 = time.time()
|
| 107 |
+
r = analyze_video(v)
|
| 108 |
+
dt = time.time() - t0
|
| 109 |
+
if r is None: continue
|
| 110 |
+
real_data.append(r)
|
| 111 |
+
print(f" {os.path.basename(v):<12} face={r['face_mean']:.4f} full={r['full_mean']:.4f} diff={r['score_diff']:.4f} [{dt:.1f}s]")
|
| 112 |
+
|
| 113 |
+
# ββ Grid search βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 114 |
+
print(f"\n{'='*80}")
|
| 115 |
+
print(f"GRID SEARCH: Finding optimal weights and threshold")
|
| 116 |
+
print(f"{'='*80}")
|
| 117 |
+
print(f"Formula: fakeness = w * face_mean + (1-w) * (1 - score_diff)")
|
| 118 |
+
|
| 119 |
+
best_acc = 0
|
| 120 |
+
best_params = {}
|
| 121 |
+
|
| 122 |
+
for w in np.arange(0.3, 0.8, 0.05):
|
| 123 |
+
for thresh in np.arange(0.40, 0.70, 0.01):
|
| 124 |
+
correct = 0
|
| 125 |
+
total = len(fake_data) + len(real_data)
|
| 126 |
+
|
| 127 |
+
for d in fake_data:
|
| 128 |
+
score = w * d['face_mean'] + (1-w) * (1 - d['score_diff'])
|
| 129 |
+
if score > thresh: correct += 1
|
| 130 |
+
|
| 131 |
+
for d in real_data:
|
| 132 |
+
score = w * d['face_mean'] + (1-w) * (1 - d['score_diff'])
|
| 133 |
+
if score <= thresh: correct += 1
|
| 134 |
+
|
| 135 |
+
acc = correct / total
|
| 136 |
+
if acc > best_acc:
|
| 137 |
+
best_acc = acc
|
| 138 |
+
best_params = {"w": round(w, 2), "thresh": round(thresh, 2), "acc": round(acc, 4)}
|
| 139 |
+
|
| 140 |
+
print(f"\nBest: w={best_params['w']}, threshold={best_params['thresh']}, accuracy={best_params['acc']*100:.1f}%")
|
| 141 |
+
|
| 142 |
+
# ββ Show results with best params ββββββοΏ½οΏ½οΏ½ββββββββββββββββββββββββββββββββββ
|
| 143 |
+
w = best_params['w']
|
| 144 |
+
thresh = best_params['thresh']
|
| 145 |
+
|
| 146 |
+
print(f"\n--- Results with optimal params (w={w}, threshold={thresh}) ---")
|
| 147 |
+
|
| 148 |
+
fake_correct = 0
|
| 149 |
+
real_correct = 0
|
| 150 |
+
|
| 151 |
+
print(f"\nFake videos:")
|
| 152 |
+
for i, d in enumerate(fake_data):
|
| 153 |
+
score = w * d['face_mean'] + (1-w) * (1 - d['score_diff'])
|
| 154 |
+
is_fake = score > thresh
|
| 155 |
+
ok = "OK" if is_fake else "XX"
|
| 156 |
+
if is_fake: fake_correct += 1
|
| 157 |
+
print(f" {ok} score={score:.4f} (face={d['face_mean']:.4f}, diff={d['score_diff']:.4f})")
|
| 158 |
+
|
| 159 |
+
print(f"\nReal videos:")
|
| 160 |
+
for i, d in enumerate(real_data):
|
| 161 |
+
score = w * d['face_mean'] + (1-w) * (1 - d['score_diff'])
|
| 162 |
+
is_fake = score > thresh
|
| 163 |
+
ok = "OK" if not is_fake else "XX"
|
| 164 |
+
if not is_fake: real_correct += 1
|
| 165 |
+
print(f" {ok} score={score:.4f} (face={d['face_mean']:.4f}, diff={d['score_diff']:.4f})")
|
| 166 |
+
|
| 167 |
+
total = len(fake_data) + len(real_data)
|
| 168 |
+
total_correct = fake_correct + real_correct
|
| 169 |
+
print(f"\n{'='*80}")
|
| 170 |
+
print(f"FINAL: {total_correct}/{total} ({100*total_correct/total:.1f}%)")
|
| 171 |
+
print(f" Fake: {fake_correct}/{len(fake_data)} ({100*fake_correct/len(fake_data):.1f}%)")
|
| 172 |
+
print(f" Real: {real_correct}/{len(real_data)} ({100*real_correct/len(real_data):.1f}%)")
|
| 173 |
+
print(f"{'='*80}")
|
scratch/regenerate_explanations.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
| 6 |
+
|
| 7 |
+
from src.ingestion.database import get_session, Article
|
| 8 |
+
from src.intelligence.fake_news import generate_explanation
|
| 9 |
+
|
| 10 |
+
def run():
|
| 11 |
+
session = get_session()
|
| 12 |
+
articles = session.query(Article).all()
|
| 13 |
+
print(f"Found {len(articles)} articles to update.")
|
| 14 |
+
|
| 15 |
+
updated = 0
|
| 16 |
+
for a in articles:
|
| 17 |
+
if not a.score_details:
|
| 18 |
+
continue
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
details = json.loads(a.score_details)
|
| 22 |
+
except:
|
| 23 |
+
continue
|
| 24 |
+
|
| 25 |
+
score = a.credibility_score
|
| 26 |
+
if score is None:
|
| 27 |
+
continue
|
| 28 |
+
|
| 29 |
+
trust_factors = details.get('trust_factors', [])
|
| 30 |
+
risk_factors = details.get('risk_factors', [])
|
| 31 |
+
|
| 32 |
+
# Don't regenerate if it already has a long explanation (greater than 200 chars)
|
| 33 |
+
if details.get('explanation') and len(details['explanation']) > 200:
|
| 34 |
+
continue
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
print(f"Generating for ID {a.id}...")
|
| 38 |
+
new_exp = generate_explanation(score, trust_factors, risk_factors, a.title, a.source)
|
| 39 |
+
details['explanation'] = new_exp
|
| 40 |
+
a.score_details = json.dumps(details)
|
| 41 |
+
updated += 1
|
| 42 |
+
print(f"Updated ID {a.id}")
|
| 43 |
+
|
| 44 |
+
# Commit every 5 articles
|
| 45 |
+
if updated % 5 == 0:
|
| 46 |
+
session.commit()
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"Failed ID {a.id}: {e}")
|
| 49 |
+
|
| 50 |
+
session.commit()
|
| 51 |
+
session.close()
|
| 52 |
+
print(f"Successfully updated {updated} articles.")
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
run()
|
scratch/test_classifier.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
|
| 4 |
+
|
| 5 |
+
from src.intelligence.classifier import load_classifier, classify_article
|
| 6 |
+
|
| 7 |
+
model = load_classifier()
|
| 8 |
+
|
| 9 |
+
texts = [
|
| 10 |
+
("Random Ass Thoughts", "I was thinking about trimming my beard today, but then I realized it's too much work. Life is crazy right now."),
|
| 11 |
+
("New Swiffer Lemon Duster", "Introducing the new Swiffer Lemon Duster. Get the fresh scent of clean with no spray required. Buy now at your local store."),
|
| 12 |
+
("Parakram Gate 2026 Batch", "Join the Parakram GATE 2026 Batch for Computer Science. Enroll today to secure your future!"),
|
| 13 |
+
("Global Markets Fall Amid Tech Selloff", "Global stock markets tumbled on Thursday following a massive tech selloff on Wall Street. Investors remain cautious. The Dow Jones Industrial Average dropped 500 points."),
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
for title, text in texts:
|
| 17 |
+
full_text = f"{title}. {text}"
|
| 18 |
+
cat, conf = classify_article(full_text, model=model)
|
| 19 |
+
print(f"\n--- {title} ---")
|
| 20 |
+
print(f"Category: {cat}, Confidence: {conf:.4f}")
|
scratch/test_classifier2.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
|
| 4 |
+
|
| 5 |
+
from src.intelligence.classifier import load_classifier, classify_article
|
| 6 |
+
|
| 7 |
+
model = load_classifier()
|
| 8 |
+
|
| 9 |
+
texts = [
|
| 10 |
+
("Local Firefighters Rescue Cat", "A local firefighter team in downtown Seattle successfully rescued a cat stuck in a 40-foot oak tree this morning. The cat, named Whiskers, was returned safely to its owner."),
|
| 11 |
+
("Scientists Discover New Exoplanet", "Astronomers using the James Webb Space Telescope have discovered a new exoplanet with potential signs of water in its atmosphere, located 40 light-years away."),
|
| 12 |
+
("President Signs Historic Climate Bill", "The President signed a historic climate bill into law today, allocating $300 billion for renewable energy initiatives over the next decade."),
|
| 13 |
+
("Nadal Wins French Open", "Rafael Nadal secured his 15th French Open title on Sunday after defeating his opponent in straight sets. The match lasted only two hours."),
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
for title, text in texts:
|
| 17 |
+
full_text = f"{title}. {text}"
|
| 18 |
+
cat, conf = classify_article(full_text, model=model)
|
| 19 |
+
print(f"\n--- {title} ---")
|
| 20 |
+
print(f"Category: {cat}, Confidence: {conf:.4f}")
|
scratch/test_community_model.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os, glob
|
| 5 |
+
|
| 6 |
+
model_id = "buildborderless/CommunityForensics-DeepfakeDet-ViT"
|
| 7 |
+
processor = AutoImageProcessor.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForImageClassification.from_pretrained(model_id)
|
| 9 |
+
model.eval()
|
| 10 |
+
|
| 11 |
+
def test_file(path):
|
| 12 |
+
import cv2
|
| 13 |
+
img = cv2.imread(path)
|
| 14 |
+
if img is None:
|
| 15 |
+
cap = cv2.VideoCapture(path)
|
| 16 |
+
ret, img = cap.read()
|
| 17 |
+
if not ret: return
|
| 18 |
+
|
| 19 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 20 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 21 |
+
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
|
| 22 |
+
if len(faces) > 0:
|
| 23 |
+
faces = sorted(faces, key=lambda x: x[2]*x[3], reverse=True)
|
| 24 |
+
x, y, w, h = faces[0]
|
| 25 |
+
margin = int(w * 0.2)
|
| 26 |
+
x1, y1 = max(0, x - margin), max(0, y - margin)
|
| 27 |
+
x2, y2 = min(img.shape[1], x + w + margin), min(img.shape[0], y + h + margin)
|
| 28 |
+
img = img[y1:y2, x1:x2]
|
| 29 |
+
|
| 30 |
+
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 31 |
+
image = Image.fromarray(rgb)
|
| 32 |
+
|
| 33 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
out = model(**inputs)
|
| 36 |
+
probs = torch.nn.functional.softmax(out.logits, dim=-1)[0]
|
| 37 |
+
idx = probs.argmax().item()
|
| 38 |
+
print(f"{os.path.basename(path)} -> Label: {idx}, conf: {probs[idx]:.4f} (Prob 0: {probs[0]:.4f}, Prob 1: {probs[1]:.4f})")
|
| 39 |
+
|
| 40 |
+
test_file(r"C:\Users\gamin\Downloads\videos_fake\vs1.mp4")
|
| 41 |
+
test_file(r"C:\Users\gamin\Downloads\videos_fake\vs10.mp4")
|
| 42 |
+
test_file(r"C:\Users\gamin\Downloads\videos_real\v1.mp4")
|
| 43 |
+
test_file(r"C:\Users\gamin\Downloads\videos_real\v10.mp4")
|
scratch/test_cropping.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
def get_face(image_path):
|
| 6 |
+
img = cv2.imread(image_path)
|
| 7 |
+
if img is None:
|
| 8 |
+
return None
|
| 9 |
+
|
| 10 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 11 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 12 |
+
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
|
| 13 |
+
if len(faces) == 0:
|
| 14 |
+
return Image.open(image_path).convert("RGB") # Fallback to full image
|
| 15 |
+
|
| 16 |
+
# Get largest face
|
| 17 |
+
faces = sorted(faces, key=lambda x: x[2]*x[3], reverse=True)
|
| 18 |
+
x, y, w, h = faces[0]
|
| 19 |
+
|
| 20 |
+
# Add margin
|
| 21 |
+
margin = int(w * 0.2)
|
| 22 |
+
x1, y1 = max(0, x - margin), max(0, y - margin)
|
| 23 |
+
x2, y2 = min(img.shape[1], x + w + margin), min(img.shape[0], y + h + margin)
|
| 24 |
+
|
| 25 |
+
face_img = img[y1:y2, x1:x2]
|
| 26 |
+
face_rgb = cv2.cvtColor(face_img, cv2.COLOR_BGR2RGB)
|
| 27 |
+
return Image.fromarray(face_rgb)
|
scratch/test_cross.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
classifier = pipeline("zero-shot-classification", model="cross-encoder/nli-distilroberta-base")
|
| 4 |
+
candidate_labels = ["journalistic news article", "personal blog post", "product advertisement", "educational course"]
|
| 5 |
+
|
| 6 |
+
texts = [
|
| 7 |
+
("Random Ass Thoughts", "I was thinking about trimming my beard today, but then I realized it's too much work. Life is crazy right now."),
|
| 8 |
+
("New Swiffer Lemon Duster", "Introducing the new Swiffer Lemon Duster. Get the fresh scent of clean with no spray required. Buy now at your local store."),
|
| 9 |
+
("Parakram Gate 2026 Batch", "Join the Parakram GATE 2026 Batch for Computer Science. Enroll today to secure your future!"),
|
| 10 |
+
("Global Markets Fall Amid Tech Selloff", "Global stock markets tumbled on Thursday following a massive tech selloff on Wall Street. Investors remain cautious."),
|
| 11 |
+
("Local Firefighters Rescue Cat", "A local firefighter team in downtown Seattle successfully rescued a cat stuck in a 40-foot oak tree this morning. The cat, named Whiskers, was returned safely to its owner.")
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
for title, text in texts:
|
| 15 |
+
full_text = f"{title}. {text}"
|
| 16 |
+
result = classifier(full_text, candidate_labels)
|
| 17 |
+
print(f"\n--- {title} ---")
|
| 18 |
+
for i in range(len(result['labels'])):
|
| 19 |
+
print(f"{result['labels'][i]}: {result['scores'][i]:.4f}")
|
scratch/test_deberta.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
classifier = pipeline("zero-shot-classification", model="MoritzLaurer/mDeBERTa-v3-base-mnli-xnli")
|
| 4 |
+
candidate_labels = ["news report", "personal opinion blog", "product advertisement", "educational material"]
|
| 5 |
+
|
| 6 |
+
texts = [
|
| 7 |
+
("Random Ass Thoughts", "I was thinking about trimming my beard today, but then I realized it's too much work. Life is crazy right now."),
|
| 8 |
+
("New Swiffer Lemon Duster", "Introducing the new Swiffer Lemon Duster. Get the fresh scent of clean with no spray required. Buy now at your local store."),
|
| 9 |
+
("Parakram Gate 2026 Batch", "Join the Parakram GATE 2026 Batch for Computer Science. Enroll today to secure your future!"),
|
| 10 |
+
("Global Markets Fall Amid Tech Selloff", "Global stock markets tumbled on Thursday following a massive tech selloff on Wall Street. Investors remain cautious."),
|
| 11 |
+
("Local Firefighters Rescue Cat", "A local firefighter team in downtown Seattle successfully rescued a cat stuck in a 40-foot oak tree this morning. The cat, named Whiskers, was returned safely to its owner.")
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
for title, text in texts:
|
| 15 |
+
full_text = f"{title}. {text}"
|
| 16 |
+
result = classifier(full_text, candidate_labels)
|
| 17 |
+
print(f"\n--- {title} ---")
|
| 18 |
+
for i in range(len(result['labels'])):
|
| 19 |
+
print(f"{result['labels'][i]}: {result['scores'][i]:.4f}")
|
scratch/test_deepfake.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from urllib.request import urlretrieve
|
| 3 |
+
from src.intelligence.deepfake_detector import detect_deepfake_image
|
| 4 |
+
|
| 5 |
+
# Let's download a known real image and known fake image from standard datasets or wikipedia
|
| 6 |
+
real_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/200px-React-icon.svg.png"
|
| 7 |
+
# For a fake image, we can try to use a known AI image URL from wikimedia or just test the model on the real one.
|
| 8 |
+
fake_url = "https://upload.wikimedia.org/wikipedia/commons/2/23/Ai-generated-8314150_1280.jpg"
|
| 9 |
+
|
| 10 |
+
os.makedirs("scratch", exist_ok=True)
|
| 11 |
+
real_path = "scratch/test_real.png"
|
| 12 |
+
fake_path = "scratch/test_fake.jpg"
|
| 13 |
+
|
| 14 |
+
print("Downloading images...")
|
| 15 |
+
urlretrieve(real_url, real_path)
|
| 16 |
+
urlretrieve(fake_url, fake_path)
|
| 17 |
+
|
| 18 |
+
print("\n--- Testing Real Image ---")
|
| 19 |
+
res1 = detect_deepfake_image(real_path)
|
| 20 |
+
print(res1)
|
| 21 |
+
|
| 22 |
+
print("\n--- Testing Fake Image ---")
|
| 23 |
+
res2 = detect_deepfake_image(fake_path)
|
| 24 |
+
print(res2)
|
scratch/test_deepfake_api.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import sys
|
| 3 |
+
import glob
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
url = "http://localhost:8000/api/deepfake/analyze"
|
| 7 |
+
videos = glob.glob(r"C:\Users\gamin\Downloads\videos_fake\*.mp4")
|
| 8 |
+
if not videos:
|
| 9 |
+
print("No fake videos found in Downloads")
|
| 10 |
+
sys.exit(1)
|
| 11 |
+
|
| 12 |
+
test_video = videos[0]
|
| 13 |
+
print(f"Uploading {os.path.basename(test_video)} to {url}...")
|
| 14 |
+
try:
|
| 15 |
+
with open(test_video, "rb") as f:
|
| 16 |
+
files = {"file": (os.path.basename(test_video), f, "video/mp4")}
|
| 17 |
+
res = requests.post(url, files=files, timeout=120)
|
| 18 |
+
print(f"Status Code: {res.status_code}")
|
| 19 |
+
print("Response:", res.text)
|
| 20 |
+
except requests.exceptions.RequestException as e:
|
| 21 |
+
print(f"Request failed: {e}")
|
scratch/test_deepfake_video_unit.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
sys.path.insert(0, ".")
|
| 3 |
+
from src.intelligence.deepfake_detector import detect_deepfake_video
|
| 4 |
+
|
| 5 |
+
video_path = r"C:\Users\gamin\Downloads\videos_fake\vs1.mp4"
|
| 6 |
+
try:
|
| 7 |
+
print(f"Testing {video_path}")
|
| 8 |
+
res = detect_deepfake_video(video_path)
|
| 9 |
+
print(res)
|
| 10 |
+
except Exception as e:
|
| 11 |
+
import traceback
|
| 12 |
+
traceback.print_exc()
|
scratch/test_dima_model.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os, glob
|
| 5 |
+
|
| 6 |
+
model_id = "dima806/deepfake_vs_real_image_detection"
|
| 7 |
+
processor = AutoImageProcessor.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForImageClassification.from_pretrained(model_id)
|
| 9 |
+
model.eval()
|
| 10 |
+
|
| 11 |
+
def test_file(path):
|
| 12 |
+
import cv2
|
| 13 |
+
img = cv2.imread(path)
|
| 14 |
+
if img is None:
|
| 15 |
+
cap = cv2.VideoCapture(path)
|
| 16 |
+
ret, img = cap.read()
|
| 17 |
+
if not ret: return
|
| 18 |
+
|
| 19 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 20 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 21 |
+
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
|
| 22 |
+
if len(faces) > 0:
|
| 23 |
+
faces = sorted(faces, key=lambda x: x[2]*x[3], reverse=True)
|
| 24 |
+
x, y, w, h = faces[0]
|
| 25 |
+
margin = int(w * 0.2)
|
| 26 |
+
x1, y1 = max(0, x - margin), max(0, y - margin)
|
| 27 |
+
x2, y2 = min(img.shape[1], x + w + margin), min(img.shape[0], y + h + margin)
|
| 28 |
+
img = img[y1:y2, x1:x2]
|
| 29 |
+
|
| 30 |
+
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 31 |
+
image = Image.fromarray(rgb)
|
| 32 |
+
|
| 33 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
out = model(**inputs)
|
| 36 |
+
probs = torch.nn.functional.softmax(out.logits, dim=-1)[0]
|
| 37 |
+
idx = probs.argmax().item()
|
| 38 |
+
print(f"{os.path.basename(path)} -> Label: {model.config.id2label[idx]}, conf: {probs[idx]:.4f}")
|
| 39 |
+
|
| 40 |
+
test_file(r"C:\Users\gamin\Downloads\videos_fake\vs1.mp4")
|
| 41 |
+
test_file(r"C:\Users\gamin\Downloads\videos_fake\vs10.mp4")
|
| 42 |
+
test_file(r"C:\Users\gamin\Downloads\videos_real\v1.mp4")
|
| 43 |
+
test_file(r"C:\Users\gamin\Downloads\videos_real\v10.mp4")
|
scratch/test_fake_news.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
|
| 4 |
+
|
| 5 |
+
from src.intelligence.fake_news import detect_fake_news, load_fake_news_detector
|
| 6 |
+
|
| 7 |
+
title = "Random Ass Thoughts"
|
| 8 |
+
content = "I was thinking about trimming my beard today, but then I realized it's too much work. Life is crazy right now."
|
| 9 |
+
|
| 10 |
+
model, tokenizer = load_fake_news_detector()
|
| 11 |
+
is_fake, score, breakdown = detect_fake_news(title, content, model=model, tokenizer=tokenizer)
|
| 12 |
+
|
| 13 |
+
print("Score:", score)
|
| 14 |
+
print("Breakdown:", breakdown)
|
scratch/test_flan.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
print("Loading flan-t5-base...")
|
| 4 |
+
classifier = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 5 |
+
|
| 6 |
+
texts = [
|
| 7 |
+
("Random Ass Thoughts", "I was thinking about trimming my beard today, but then I realized it's too much work. Life is crazy right now."),
|
| 8 |
+
("New Swiffer Lemon Duster", "Introducing the new Swiffer Lemon Duster. Get the fresh scent of clean with no spray required. Buy now at your local store."),
|
| 9 |
+
("Parakram Gate 2026 Batch", "Join the Parakram GATE 2026 Batch for Computer Science. Enroll today to secure your future!"),
|
| 10 |
+
("Global Markets Fall Amid Tech Selloff", "Global stock markets tumbled on Thursday following a massive tech selloff on Wall Street. Investors remain cautious."),
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
for title, text in texts:
|
| 14 |
+
full_text = f"Title: {title}\nText: {text}\n\nIs this text a professional journalistic news article? Answer yes or no."
|
| 15 |
+
result = classifier(full_text, max_new_tokens=5)
|
| 16 |
+
print(f"\n--- {title} ---")
|
| 17 |
+
print("Result:", result[0]['generated_text'])
|
scratch/test_flan2.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
classifier = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 4 |
+
|
| 5 |
+
texts = [
|
| 6 |
+
("Random Ass Thoughts", "I was thinking about trimming my beard today, but then I realized it's too much work. Life is crazy right now."),
|
| 7 |
+
("New Swiffer Lemon Duster", "Introducing the new Swiffer Lemon Duster. Get the fresh scent of clean with no spray required. Buy now at your local store."),
|
| 8 |
+
("Parakram Gate 2026 Batch", "Join the Parakram GATE 2026 Batch for Computer Science. Enroll today to secure your future!"),
|
| 9 |
+
("Global Markets Fall Amid Tech Selloff", "Global stock markets tumbled on Thursday following a massive tech selloff on Wall Street. Investors remain cautious. The Dow Jones Industrial Average dropped 500 points."),
|
| 10 |
+
]
|
| 11 |
+
|
| 12 |
+
for title, text in texts:
|
| 13 |
+
full_text = f"Classify the following text as either 'News Article' or 'Not News'.\n\nTitle: {title}\nText: {text}\n\nClassification:"
|
| 14 |
+
result = classifier(full_text, max_new_tokens=5)
|
| 15 |
+
print(f"\n--- {title} ---")
|
| 16 |
+
print("Result:", result[0]['generated_text'])
|
scratch/test_hf_api.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import InferenceClient
|
| 2 |
+
|
| 3 |
+
client = InferenceClient()
|
| 4 |
+
|
| 5 |
+
prompt = "Article: Global Markets Fall Amid Tech Selloff\nScore: 85%\nPositive indicators: established news outlet.\nWrite a short, professional 2-sentence explanation for this credibility score."
|
| 6 |
+
|
| 7 |
+
try:
|
| 8 |
+
response = client.chat_completion(
|
| 9 |
+
model="Qwen/Qwen2.5-72B-Instruct",
|
| 10 |
+
messages=[{"role": "user", "content": prompt}],
|
| 11 |
+
max_tokens=60,
|
| 12 |
+
)
|
| 13 |
+
print("API Result:", response.choices[0].message.content)
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print("Error:", e)
|
scratch/test_newspaper.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import newspaper
|
| 2 |
+
|
| 3 |
+
url = "https://speakeasyofstrength.com/random-ass-thoughts/"
|
| 4 |
+
article = newspaper.Article(url)
|
| 5 |
+
article.download()
|
| 6 |
+
article.parse()
|
| 7 |
+
article.nlp()
|
| 8 |
+
|
| 9 |
+
print("Title:", article.title)
|
| 10 |
+
print("Keywords:", article.keywords)
|
| 11 |
+
print("Summary:", article.summary)
|
| 12 |
+
print("Meta keywords:", article.meta_keywords)
|
| 13 |
+
print("Meta description:", article.meta_description)
|
scratch/test_qwen.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 5 |
+
print("Loading Qwen...")
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16).to(device)
|
| 9 |
+
|
| 10 |
+
prompt = """You are an AI news verifier. Write a 2-sentence professional explanation for why an article received a specific credibility score based on the factors provided.
|
| 11 |
+
|
| 12 |
+
Article: Global Markets Fall Amid Tech Selloff
|
| 13 |
+
Score: 85%
|
| 14 |
+
Positive indicators: published by Reuters, a recognised and established news outlet.
|
| 15 |
+
|
| 16 |
+
Explanation:"""
|
| 17 |
+
|
| 18 |
+
messages = [{"role": "system", "content": "You are a professional AI news verification assistant."}, {"role": "user", "content": prompt}]
|
| 19 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 20 |
+
inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 21 |
+
|
| 22 |
+
print("Generating...")
|
| 23 |
+
outputs = model.generate(**inputs, max_new_tokens=60, temperature=0.7, do_sample=True)
|
| 24 |
+
result = tokenizer.decode(outputs[0][len(inputs.input_ids[0]):], skip_special_tokens=True)
|
| 25 |
+
print("Result:", result)
|
scratch/test_qwen2.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
print("Starting script...")
|
| 3 |
+
try:
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
+
|
| 7 |
+
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 8 |
+
print("Loading tokenizer...")
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
+
print("Loading model onto CPU with float32...")
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32).to("cpu")
|
| 12 |
+
print("Model loaded.")
|
| 13 |
+
|
| 14 |
+
prompt = "Hello, write a 2 sentence story."
|
| 15 |
+
messages = [{"role": "user", "content": prompt}]
|
| 16 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 17 |
+
inputs = tokenizer([text], return_tensors="pt").to("cpu")
|
| 18 |
+
|
| 19 |
+
print("Generating...")
|
| 20 |
+
outputs = model.generate(**inputs, max_new_tokens=50)
|
| 21 |
+
print("Generated:", tokenizer.decode(outputs[0]))
|
| 22 |
+
print("Done!")
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"Caught Exception: {e}")
|
| 25 |
+
except BaseException as be:
|
| 26 |
+
print(f"Caught BaseException: {be}")
|
| 27 |
+
sys.exit(0)
|
scratch/test_siglip2.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os, glob
|
| 5 |
+
|
| 6 |
+
model_id = "prithivMLmods/Deepfake-Detect-Siglip2"
|
| 7 |
+
processor = AutoImageProcessor.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForImageClassification.from_pretrained(model_id)
|
| 9 |
+
model.eval()
|
| 10 |
+
|
| 11 |
+
def test_file(path):
|
| 12 |
+
import cv2
|
| 13 |
+
img = cv2.imread(path)
|
| 14 |
+
if img is None:
|
| 15 |
+
cap = cv2.VideoCapture(path)
|
| 16 |
+
ret, img = cap.read()
|
| 17 |
+
if not ret: return
|
| 18 |
+
|
| 19 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 20 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 21 |
+
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
|
| 22 |
+
if len(faces) > 0:
|
| 23 |
+
faces = sorted(faces, key=lambda x: x[2]*x[3], reverse=True)
|
| 24 |
+
x, y, w, h = faces[0]
|
| 25 |
+
margin = int(w * 0.2)
|
| 26 |
+
x1, y1 = max(0, x - margin), max(0, y - margin)
|
| 27 |
+
x2, y2 = min(img.shape[1], x + w + margin), min(img.shape[0], y + h + margin)
|
| 28 |
+
img = img[y1:y2, x1:x2]
|
| 29 |
+
|
| 30 |
+
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 31 |
+
image = Image.fromarray(rgb)
|
| 32 |
+
|
| 33 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
out = model(**inputs)
|
| 36 |
+
probs = torch.nn.functional.softmax(out.logits, dim=-1)[0]
|
| 37 |
+
idx = probs.argmax().item()
|
| 38 |
+
print(f"{os.path.basename(path)} -> Label: {model.config.id2label[idx]}, conf: {probs[idx]:.4f}")
|
| 39 |
+
|
| 40 |
+
test_file(r"C:\Users\gamin\Downloads\videos_fake\vs1.mp4")
|
| 41 |
+
test_file(r"C:\Users\gamin\Downloads\videos_fake\vs10.mp4")
|
| 42 |
+
test_file(r"C:\Users\gamin\Downloads\videos_real\v1.mp4")
|
| 43 |
+
test_file(r"C:\Users\gamin\Downloads\videos_real\v10.mp4")
|
scratch/test_videos_batch.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import glob
|
| 4 |
+
sys.path.append(".")
|
| 5 |
+
from src.intelligence.deepfake_detector import detect_deepfake_video
|
| 6 |
+
|
| 7 |
+
def test_batch(folder, label):
|
| 8 |
+
videos = glob.glob(os.path.join(folder, "*.mp4"))[:3]
|
| 9 |
+
print(f"\n--- Testing {len(videos)} videos from {folder} (Expected: {label}) ---")
|
| 10 |
+
for v in videos:
|
| 11 |
+
res = detect_deepfake_video(v, sample_rate=20) # use 20 to make it faster for testing
|
| 12 |
+
print(f"File: {os.path.basename(v)} -> is_fake: {res.get('is_fake')}, label: {res.get('label')}, conf: {res.get('confidence')}")
|
| 13 |
+
|
| 14 |
+
if __name__ == "__main__":
|
| 15 |
+
test_batch(r"C:\Users\gamin\Downloads\videos_fake", "Fake")
|
| 16 |
+
test_batch(r"C:\Users\gamin\Downloads\videos_real", "Real")
|
scratch/test_xai.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 2 |
+
|
| 3 |
+
tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
|
| 4 |
+
model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
|
| 5 |
+
|
| 6 |
+
def generate(prompt):
|
| 7 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 8 |
+
outputs = model.generate(**inputs, max_new_tokens=60, do_sample=True, temperature=0.7)
|
| 9 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 10 |
+
|
| 11 |
+
prompt = """Article: Global Markets Fall Amid Tech Selloff
|
| 12 |
+
Source: Reuters
|
| 13 |
+
Credibility Score: 85%
|
| 14 |
+
Positive indicators: published by Reuters, a recognised and established news outlet, neutral objective tone.
|
| 15 |
+
Write a short, 2-sentence professional explanation for this credibility score."""
|
| 16 |
+
print("Result 1:", generate(prompt))
|
| 17 |
+
|
| 18 |
+
prompt2 = """Article: ALIENS LAND IN CENTRAL PARK!!!
|
| 19 |
+
Source: Unknown
|
| 20 |
+
Credibility Score: 15%
|
| 21 |
+
Risk factors: highly sensationalized title, excessive punctuation, high density of emotionally charged language.
|
| 22 |
+
Write a short, 2-sentence professional explanation for this credibility score."""
|
| 23 |
+
print("Result 2:", generate(prompt2))
|
scratch/test_zero_shot.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
print("Loading zero-shot classifier...")
|
| 4 |
+
classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
|
| 5 |
+
|
| 6 |
+
candidate_labels = ["news reporting", "personal blog post", "product advertisement", "educational course"]
|
| 7 |
+
|
| 8 |
+
texts = [
|
| 9 |
+
("Random Ass Thoughts", "I was thinking about trimming my beard today, but then I realized it's too much work. Life is crazy right now."),
|
| 10 |
+
("New Swiffer Lemon Duster", "Introducing the new Swiffer Lemon Duster. Get the fresh scent of clean with no spray required. Buy now at your local store."),
|
| 11 |
+
("Parakram Gate 2026 Batch", "Join the Parakram GATE 2026 Batch for Computer Science. Enroll today to secure your future!"),
|
| 12 |
+
("Global Markets Fall Amid Tech Selloff", "Global stock markets tumbled on Thursday following a massive tech selloff on Wall Street. Investors remain cautious."),
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
for title, text in texts:
|
| 16 |
+
full_text = f"{title}. {text}"
|
| 17 |
+
result = classifier(full_text, candidate_labels)
|
| 18 |
+
print(f"\n--- {title} ---")
|
| 19 |
+
for i in range(len(result['labels'])):
|
| 20 |
+
print(f"{result['labels'][i]}: {result['scores'][i]:.4f}")
|
scratch/test_zero_shot2.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
|
| 4 |
+
candidate_labels = ["professional news reporting", "personal blog post or corporate advertisement"]
|
| 5 |
+
|
| 6 |
+
texts = [
|
| 7 |
+
("Random Ass Thoughts", "I was thinking about trimming my beard today, but then I realized it's too much work. Life is crazy right now."),
|
| 8 |
+
("New Swiffer Lemon Duster", "Introducing the new Swiffer Lemon Duster. Get the fresh scent of clean with no spray required. Buy now at your local store."),
|
| 9 |
+
("Parakram Gate 2026 Batch", "Join the Parakram GATE 2026 Batch for Computer Science. Enroll today to secure your future!"),
|
| 10 |
+
("Global Markets Fall Amid Tech Selloff", "Global stock markets tumbled on Thursday following a massive tech selloff on Wall Street. Investors remain cautious."),
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
for title, text in texts:
|
| 14 |
+
full_text = f"{title}. {text}"
|
| 15 |
+
result = classifier(full_text, candidate_labels)
|
| 16 |
+
print(f"\n--- {title} ---")
|
| 17 |
+
for i in range(len(result['labels'])):
|
| 18 |
+
print(f"{result['labels'][i]}: {result['scores'][i]:.4f}")
|
scratch/validate_production.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Quick validation: test the production deepfake_detector.py on a few files."""
|
| 2 |
+
import os, sys, glob
|
| 3 |
+
sys.path.insert(0, ".")
|
| 4 |
+
from src.intelligence.deepfake_detector import detect_deepfake_video
|
| 5 |
+
|
| 6 |
+
fake_dir = r"C:\Users\gamin\Downloads\videos_fake"
|
| 7 |
+
real_dir = r"C:\Users\gamin\Downloads\videos_real"
|
| 8 |
+
|
| 9 |
+
fake_videos = sorted(glob.glob(os.path.join(fake_dir, "*.mp4")))[:5]
|
| 10 |
+
real_videos = sorted(glob.glob(os.path.join(real_dir, "*.mp4")))[:5]
|
| 11 |
+
|
| 12 |
+
print("--- FAKE (expect Fake) ---")
|
| 13 |
+
for v in fake_videos:
|
| 14 |
+
r = detect_deepfake_video(v, sample_rate=10)
|
| 15 |
+
ok = "OK" if r["is_fake"] else "XX"
|
| 16 |
+
print(f" {ok} {os.path.basename(v)}: {r['label']} conf={r['confidence']:.3f} band={r['confidence_band']} ens={r['raw_scores']['Fake']:.3f}")
|
| 17 |
+
|
| 18 |
+
print("\n--- REAL (expect Real) ---")
|
| 19 |
+
for v in real_videos:
|
| 20 |
+
r = detect_deepfake_video(v, sample_rate=10)
|
| 21 |
+
ok = "OK" if not r["is_fake"] else "XX"
|
| 22 |
+
print(f" {ok} {os.path.basename(v)}: {r['label']} conf={r['confidence']:.3f} band={r['confidence_band']} ens={r['raw_scores']['Fake']:.3f}")
|
src/api/main.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI, Query, Request, Response
|
| 2 |
import asyncio
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -461,4 +461,73 @@ def get_whatsapp_info():
|
|
| 461 |
bot_number = os.getenv("WHATSAPP_BOT_NUMBER", "")
|
| 462 |
return {"bot_number": bot_number, "available": bool(bot_number)}
|
| 463 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
# ββ Intelligence Pipeline Trigger (Optional Internal) βββββββββββββββββββββββββ
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query, Request, Response, UploadFile, File
|
| 2 |
import asyncio
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 461 |
bot_number = os.getenv("WHATSAPP_BOT_NUMBER", "")
|
| 462 |
return {"bot_number": bot_number, "available": bool(bot_number)}
|
| 463 |
|
| 464 |
+
# ββ Deepfake Detection ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 465 |
+
|
| 466 |
+
# Allowed file types and size limits for deepfake analysis
|
| 467 |
+
ALLOWED_IMAGE_TYPES = {"image/jpeg", "image/png", "image/webp", "image/jpg"}
|
| 468 |
+
ALLOWED_VIDEO_TYPES = {"video/mp4", "video/avi", "video/quicktime", "video/x-msvideo", "video/webm"}
|
| 469 |
+
MAX_IMAGE_SIZE = 10 * 1024 * 1024 # 10 MB
|
| 470 |
+
MAX_VIDEO_SIZE = 50 * 1024 * 1024 # 50 MB
|
| 471 |
+
|
| 472 |
+
@app.post("/api/deepfake/analyze")
|
| 473 |
+
async def analyze_deepfake(file: UploadFile = File(...)):
|
| 474 |
+
"""
|
| 475 |
+
Accepts an uploaded image or video file, runs it through the
|
| 476 |
+
deepfake detection AI model, and returns a verdict with confidence
|
| 477 |
+
scores and a human-readable explanation.
|
| 478 |
+
|
| 479 |
+
Supported formats:
|
| 480 |
+
Images: jpg, png, webp (max 10 MB)
|
| 481 |
+
Videos: mp4, avi, mov, webm (max 50 MB)
|
| 482 |
+
"""
|
| 483 |
+
import tempfile
|
| 484 |
+
|
| 485 |
+
content_type = file.content_type or ""
|
| 486 |
+
is_image = content_type in ALLOWED_IMAGE_TYPES
|
| 487 |
+
is_video = content_type in ALLOWED_VIDEO_TYPES
|
| 488 |
+
|
| 489 |
+
# ββ Validate file type ββββββββββββββββββββββββββββββββββββββββββββ
|
| 490 |
+
if not is_image and not is_video:
|
| 491 |
+
return {
|
| 492 |
+
"error": f"Unsupported file type: {content_type}. "
|
| 493 |
+
f"Please upload an image (jpg, png, webp) or video (mp4, avi, mov, webm)."
|
| 494 |
+
}
|
| 495 |
+
|
| 496 |
+
# ββ Validate file size ββββββββββββββββββββββββββββββββββββββββββββ
|
| 497 |
+
contents = await file.read()
|
| 498 |
+
max_size = MAX_VIDEO_SIZE if is_video else MAX_IMAGE_SIZE
|
| 499 |
+
if len(contents) > max_size:
|
| 500 |
+
limit_mb = max_size // (1024 * 1024)
|
| 501 |
+
return {"error": f"File too large. Maximum size is {limit_mb} MB."}
|
| 502 |
+
|
| 503 |
+
# ββ Save to temp file and analyze βββββββββββββββββββββββββββββββββ
|
| 504 |
+
suffix = os.path.splitext(file.filename or "upload")[1] or (".png" if is_image else ".mp4")
|
| 505 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
|
| 506 |
+
try:
|
| 507 |
+
tmp.write(contents)
|
| 508 |
+
tmp.close()
|
| 509 |
+
|
| 510 |
+
if is_image:
|
| 511 |
+
from src.intelligence.deepfake_detector import detect_deepfake_image
|
| 512 |
+
result = detect_deepfake_image(tmp.name)
|
| 513 |
+
else:
|
| 514 |
+
from src.intelligence.deepfake_detector import detect_deepfake_video
|
| 515 |
+
result = detect_deepfake_video(tmp.name)
|
| 516 |
+
|
| 517 |
+
# Tag the result with the media type for the frontend
|
| 518 |
+
result["media_type"] = "image" if is_image else "video"
|
| 519 |
+
result["filename"] = file.filename
|
| 520 |
+
return result
|
| 521 |
+
|
| 522 |
+
except Exception as e:
|
| 523 |
+
logging.error("Deepfake analysis failed: %s", e)
|
| 524 |
+
return {"error": f"Analysis failed: {str(e)}"}
|
| 525 |
+
|
| 526 |
+
finally:
|
| 527 |
+
# Always clean up the temp file
|
| 528 |
+
try:
|
| 529 |
+
os.unlink(tmp.name)
|
| 530 |
+
except OSError:
|
| 531 |
+
pass
|
| 532 |
+
|
| 533 |
# ββ Intelligence Pipeline Trigger (Optional Internal) βββββββββββββββββββββββββ
|
src/intelligence/deepfake_detector.py
ADDED
|
@@ -0,0 +1,506 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Deepfake Detection Module β Ensemble Approach
|
| 3 |
+
==============================================
|
| 4 |
+
Uses a pre-trained SigLIP-based Vision Transformer combined with a
|
| 5 |
+
dual-pass ensemble (full-frame + face-crop) to classify images and
|
| 6 |
+
video frames as "Real" or "Fake" (AI-generated / deepfake).
|
| 7 |
+
|
| 8 |
+
Model: prithivMLmods/deepfake-detector-model-v1
|
| 9 |
+
Architecture: google/siglip-base-patch16-512 (fine-tuned)
|
| 10 |
+
|
| 11 |
+
Ensemble Strategy:
|
| 12 |
+
The SigLIP model has a strong bias toward "Fake" on video frames due to
|
| 13 |
+
compression artifacts. To counteract this, we run TWO passes:
|
| 14 |
+
1. Full-frame β captures overall synthetic patterns (biased toward Fake)
|
| 15 |
+
2. Face-crop β focuses on the face region (more discriminating)
|
| 16 |
+
The final score is:
|
| 17 |
+
fakeness = 0.5 * face_fake + 0.5 * (1 - score_diff)
|
| 18 |
+
where score_diff = full_fake - face_fake. A higher gap between full-frame
|
| 19 |
+
and face-crop indicates the face looks genuine (full-frame is biased by
|
| 20 |
+
compression but the face itself is real).
|
| 21 |
+
|
| 22 |
+
Pipeline:
|
| 23 |
+
1. Image β Full-frame + Face-crop inference β Ensemble β Verdict
|
| 24 |
+
2. Video β Sample every Nth frame β Run (1) on each β Aggregate
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
import os
|
| 28 |
+
import logging
|
| 29 |
+
import tempfile
|
| 30 |
+
from typing import Optional
|
| 31 |
+
|
| 32 |
+
logger = logging.getLogger(__name__)
|
| 33 |
+
|
| 34 |
+
# ββ Global model cache (lazy-loaded once, reused across requests) βββββββββ
|
| 35 |
+
_deepfake_model = None
|
| 36 |
+
_deepfake_processor = None
|
| 37 |
+
_face_cascade = None
|
| 38 |
+
|
| 39 |
+
# Model identifier on HuggingFace
|
| 40 |
+
DEEPFAKE_MODEL_ID = "prithivMLmods/deepfake-detector-model-v1"
|
| 41 |
+
|
| 42 |
+
# Ensemble parameters (calibrated via grid search on 30 test videos)
|
| 43 |
+
ENSEMBLE_WEIGHT = 0.5 # Weight for face_mean vs (1 - score_diff)
|
| 44 |
+
ENSEMBLE_THRESHOLD = 0.56 # Scores above this are classified as Fake
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def _ensure_model_loaded():
|
| 48 |
+
"""
|
| 49 |
+
Lazy-loads the deepfake detection model and processor into memory.
|
| 50 |
+
Called automatically on first inference request; subsequent calls
|
| 51 |
+
return the cached instances instantly.
|
| 52 |
+
"""
|
| 53 |
+
global _deepfake_model, _deepfake_processor
|
| 54 |
+
|
| 55 |
+
if _deepfake_model is not None and _deepfake_processor is not None:
|
| 56 |
+
return _deepfake_model, _deepfake_processor
|
| 57 |
+
|
| 58 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 59 |
+
import torch
|
| 60 |
+
|
| 61 |
+
logger.info("Loading deepfake detection model: %s ...", DEEPFAKE_MODEL_ID)
|
| 62 |
+
|
| 63 |
+
_deepfake_processor = AutoImageProcessor.from_pretrained(DEEPFAKE_MODEL_ID)
|
| 64 |
+
_deepfake_model = AutoModelForImageClassification.from_pretrained(DEEPFAKE_MODEL_ID)
|
| 65 |
+
_deepfake_model.eval() # Set to evaluation mode (no dropout, etc.)
|
| 66 |
+
|
| 67 |
+
logger.info("Deepfake detection model loaded successfully.")
|
| 68 |
+
return _deepfake_model, _deepfake_processor
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def _ensure_face_cascade():
|
| 72 |
+
"""Lazy-load the Haar Cascade face detector (cached globally)."""
|
| 73 |
+
global _face_cascade
|
| 74 |
+
if _face_cascade is None:
|
| 75 |
+
import cv2
|
| 76 |
+
cascade_path = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
|
| 77 |
+
_face_cascade = cv2.CascadeClassifier(cascade_path)
|
| 78 |
+
return _face_cascade
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def _get_face_rect(img_bgr):
|
| 82 |
+
"""
|
| 83 |
+
Detect the largest face in a BGR image.
|
| 84 |
+
Returns (x, y, w, h) tuple or None if no face found.
|
| 85 |
+
"""
|
| 86 |
+
import cv2
|
| 87 |
+
cascade = _ensure_face_cascade()
|
| 88 |
+
gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
|
| 89 |
+
faces = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=4, minSize=(30, 30))
|
| 90 |
+
if len(faces) == 0:
|
| 91 |
+
return None
|
| 92 |
+
# Return the largest face by area
|
| 93 |
+
return max(faces, key=lambda f: f[2] * f[3])
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def _crop_face(img_bgr, face_rect, margin_ratio=0.3):
|
| 97 |
+
"""
|
| 98 |
+
Crop the face region from a BGR image with a margin.
|
| 99 |
+
Returns cropped BGR image.
|
| 100 |
+
"""
|
| 101 |
+
x, y, w, h = face_rect
|
| 102 |
+
margin = int(w * margin_ratio)
|
| 103 |
+
x1, y1 = max(0, x - margin), max(0, y - margin)
|
| 104 |
+
x2, y2 = min(img_bgr.shape[1], x + w + margin), min(img_bgr.shape[0], y + h + margin)
|
| 105 |
+
return img_bgr[y1:y2, x1:x2]
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def _classify_bgr(img_bgr):
|
| 109 |
+
"""
|
| 110 |
+
Run the SigLIP model on a BGR image.
|
| 111 |
+
Returns fake_prob (float, 0-1).
|
| 112 |
+
"""
|
| 113 |
+
import torch
|
| 114 |
+
import cv2
|
| 115 |
+
from PIL import Image
|
| 116 |
+
|
| 117 |
+
model, processor = _ensure_model_loaded()
|
| 118 |
+
rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
|
| 119 |
+
pil = Image.fromarray(rgb)
|
| 120 |
+
inputs = processor(images=pil, return_tensors="pt")
|
| 121 |
+
with torch.no_grad():
|
| 122 |
+
out = model(**inputs)
|
| 123 |
+
probs = torch.nn.functional.softmax(out.logits, dim=-1)[0]
|
| 124 |
+
# id2label: {0: 'Fake', 1: 'Real'}
|
| 125 |
+
return float(probs[0])
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def _compute_ensemble_score(full_fake, face_fake):
|
| 129 |
+
"""
|
| 130 |
+
Compute the calibrated ensemble fakeness score.
|
| 131 |
+
|
| 132 |
+
Uses the insight that the gap between full-frame and face-crop scores
|
| 133 |
+
is a strong indicator: real faces have a LARGER gap (full frame biased
|
| 134 |
+
by compression, but face itself looks genuine), while deepfakes have
|
| 135 |
+
a SMALLER gap (both full frame and face look synthetic).
|
| 136 |
+
|
| 137 |
+
Returns: float (0-1), where higher = more likely fake.
|
| 138 |
+
"""
|
| 139 |
+
score_diff = full_fake - face_fake
|
| 140 |
+
# Combine face_mean (direct signal) with inverted score_diff (gap signal)
|
| 141 |
+
fakeness = ENSEMBLE_WEIGHT * face_fake + (1 - ENSEMBLE_WEIGHT) * (1.0 - score_diff)
|
| 142 |
+
# Clamp to [0, 1]
|
| 143 |
+
return max(0.0, min(1.0, fakeness))
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def _classify_confidence_band(ensemble_score):
|
| 147 |
+
"""
|
| 148 |
+
Map ensemble score to a human-readable confidence band.
|
| 149 |
+
Calibrated against benchmark results to be honest about uncertainty.
|
| 150 |
+
"""
|
| 151 |
+
if ensemble_score >= 0.80:
|
| 152 |
+
return "high"
|
| 153 |
+
elif ensemble_score >= 0.65:
|
| 154 |
+
return "moderate"
|
| 155 |
+
elif ensemble_score >= ENSEMBLE_THRESHOLD:
|
| 156 |
+
return "low"
|
| 157 |
+
elif ensemble_score >= (1.0 - 0.65): # mirror for "real"
|
| 158 |
+
return "low"
|
| 159 |
+
elif ensemble_score >= (1.0 - 0.80):
|
| 160 |
+
return "moderate"
|
| 161 |
+
else:
|
| 162 |
+
return "high"
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 166 |
+
# IMAGE ANALYSIS
|
| 167 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 168 |
+
|
| 169 |
+
def detect_deepfake_image(image_path: str) -> dict:
|
| 170 |
+
"""
|
| 171 |
+
Analyze a single image for deepfake / AI-generation indicators using
|
| 172 |
+
the dual-pass ensemble approach.
|
| 173 |
+
|
| 174 |
+
Args:
|
| 175 |
+
image_path: Absolute path to the image file (jpg, png, webp).
|
| 176 |
+
|
| 177 |
+
Returns:
|
| 178 |
+
dict with keys:
|
| 179 |
+
- is_fake (bool): True if the ensemble classifies as deepfake.
|
| 180 |
+
- confidence (float): 0.0-1.0 ensemble confidence.
|
| 181 |
+
- label (str): Human-readable label ("Real" or "Fake").
|
| 182 |
+
- confidence_band (str): "high", "moderate", or "low".
|
| 183 |
+
- raw_scores (dict): Breakdown of individual signals.
|
| 184 |
+
- explanation (str): XAI-style human-readable reasoning.
|
| 185 |
+
"""
|
| 186 |
+
import cv2
|
| 187 |
+
from PIL import Image as PILImage
|
| 188 |
+
|
| 189 |
+
model, processor = _ensure_model_loaded()
|
| 190 |
+
|
| 191 |
+
# Load the image as BGR for OpenCV processing
|
| 192 |
+
img_bgr = cv2.imread(image_path)
|
| 193 |
+
if img_bgr is None:
|
| 194 |
+
# Fallback: try loading with PIL and converting
|
| 195 |
+
pil_img = PILImage.open(image_path).convert("RGB")
|
| 196 |
+
import numpy as np
|
| 197 |
+
img_bgr = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
|
| 198 |
+
|
| 199 |
+
# ββ Pass 1: Full-frame classification βββββββββββββββββββββββββββββ
|
| 200 |
+
full_fake = _classify_bgr(img_bgr)
|
| 201 |
+
|
| 202 |
+
# ββ Pass 2: Face-crop classification ββββββββββββββββββββββββββββββ
|
| 203 |
+
face_rect = _get_face_rect(img_bgr)
|
| 204 |
+
if face_rect is not None:
|
| 205 |
+
face_crop = _crop_face(img_bgr, face_rect)
|
| 206 |
+
face_fake = _classify_bgr(face_crop)
|
| 207 |
+
has_face = True
|
| 208 |
+
else:
|
| 209 |
+
face_fake = full_fake # No face found β fallback to full-frame
|
| 210 |
+
has_face = False
|
| 211 |
+
|
| 212 |
+
# ββ Ensemble scoring ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 213 |
+
ensemble_score = _compute_ensemble_score(full_fake, face_fake)
|
| 214 |
+
is_fake = ensemble_score > ENSEMBLE_THRESHOLD
|
| 215 |
+
|
| 216 |
+
# Compute a "confidence in the verdict" (distance from threshold)
|
| 217 |
+
if is_fake:
|
| 218 |
+
confidence = min(1.0, 0.5 + (ensemble_score - ENSEMBLE_THRESHOLD) / (1.0 - ENSEMBLE_THRESHOLD) * 0.5)
|
| 219 |
+
else:
|
| 220 |
+
confidence = min(1.0, 0.5 + (ENSEMBLE_THRESHOLD - ensemble_score) / ENSEMBLE_THRESHOLD * 0.5)
|
| 221 |
+
|
| 222 |
+
label = "Fake" if is_fake else "Real"
|
| 223 |
+
band = _classify_confidence_band(ensemble_score)
|
| 224 |
+
|
| 225 |
+
# Build raw scores for transparency
|
| 226 |
+
raw_scores = {
|
| 227 |
+
"Fake": round(ensemble_score, 4),
|
| 228 |
+
"Real": round(1.0 - ensemble_score, 4),
|
| 229 |
+
"full_frame_fake": round(full_fake, 4),
|
| 230 |
+
"face_crop_fake": round(face_fake, 4),
|
| 231 |
+
"score_diff": round(full_fake - face_fake, 4),
|
| 232 |
+
"face_detected": has_face,
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
h, w = img_bgr.shape[:2]
|
| 236 |
+
explanation = _generate_image_explanation(label, confidence, band, raw_scores, (w, h))
|
| 237 |
+
|
| 238 |
+
return {
|
| 239 |
+
"is_fake": is_fake,
|
| 240 |
+
"confidence": round(confidence, 4),
|
| 241 |
+
"label": label,
|
| 242 |
+
"confidence_band": band,
|
| 243 |
+
"raw_scores": raw_scores,
|
| 244 |
+
"explanation": explanation,
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
def _generate_image_explanation(label: str, confidence: float, band: str,
|
| 249 |
+
raw_scores: dict, image_size: tuple) -> str:
|
| 250 |
+
"""
|
| 251 |
+
Produce a rich, human-readable explanation of the deepfake analysis result.
|
| 252 |
+
Honest about uncertainty levels.
|
| 253 |
+
"""
|
| 254 |
+
pct = round(confidence * 100, 1)
|
| 255 |
+
w, h = image_size
|
| 256 |
+
is_fake = label == "Fake"
|
| 257 |
+
face_detected = raw_scores.get("face_detected", False)
|
| 258 |
+
|
| 259 |
+
# Confidence-calibrated verdicts
|
| 260 |
+
if is_fake:
|
| 261 |
+
if band == "high":
|
| 262 |
+
verdict = f"This image shows strong indicators of AI generation or manipulation ({pct}% confidence)."
|
| 263 |
+
detail = "Multiple analysis passes detected consistent synthetic patterns in both the full image and facial region."
|
| 264 |
+
elif band == "moderate":
|
| 265 |
+
verdict = f"This image shows moderate indicators of possible manipulation ({pct}% confidence)."
|
| 266 |
+
detail = "Some synthetic patterns were detected. This could indicate AI generation, heavy filtering, or face-swap manipulation."
|
| 267 |
+
else:
|
| 268 |
+
verdict = f"This image shows mild indicators of possible manipulation ({pct}% confidence)."
|
| 269 |
+
detail = "The analysis detected borderline signals. The result is uncertain -- manual review is recommended."
|
| 270 |
+
else:
|
| 271 |
+
if band == "high":
|
| 272 |
+
verdict = f"This image appears authentic ({pct}% confidence)."
|
| 273 |
+
detail = "The image exhibits natural patterns consistent with real photography across all analysis passes."
|
| 274 |
+
elif band == "moderate":
|
| 275 |
+
verdict = f"This image appears likely authentic ({pct}% confidence)."
|
| 276 |
+
detail = "The image shows predominantly natural characteristics, with some minor ambiguous elements."
|
| 277 |
+
else:
|
| 278 |
+
verdict = f"This image shows uncertain results ({pct}% confidence)."
|
| 279 |
+
detail = "The analysis produced borderline scores. The image may be authentic or subtly manipulated. Manual review is recommended."
|
| 280 |
+
|
| 281 |
+
# Add context notes
|
| 282 |
+
notes = []
|
| 283 |
+
resolution_note = f"Image resolution: {w}x{h}px."
|
| 284 |
+
if w < 256 or h < 256:
|
| 285 |
+
notes.append("Low resolution may reduce detection accuracy.")
|
| 286 |
+
if not face_detected:
|
| 287 |
+
notes.append("No face was detected -- analysis was performed on the full image only.")
|
| 288 |
+
|
| 289 |
+
context = resolution_note
|
| 290 |
+
if notes:
|
| 291 |
+
context += " " + " ".join(notes)
|
| 292 |
+
|
| 293 |
+
return f"{verdict}\n\n{detail}\n\n{context}"
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 297 |
+
# VIDEO ANALYSIS
|
| 298 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 299 |
+
|
| 300 |
+
def detect_deepfake_video(video_path: str, sample_rate: int = 10) -> dict:
|
| 301 |
+
"""
|
| 302 |
+
Analyze a video for deepfake indicators by sampling every Nth frame
|
| 303 |
+
and running the dual-pass ensemble on each.
|
| 304 |
+
|
| 305 |
+
Args:
|
| 306 |
+
video_path: Absolute path to the video file (mp4, avi, mov).
|
| 307 |
+
sample_rate: Analyze every Nth frame (default: every 10th frame).
|
| 308 |
+
|
| 309 |
+
Returns:
|
| 310 |
+
dict with keys:
|
| 311 |
+
- is_fake (bool): Overall verdict based on ensemble scoring.
|
| 312 |
+
- confidence (float): Ensemble confidence in the verdict.
|
| 313 |
+
- label (str): "Real" or "Fake" overall verdict.
|
| 314 |
+
- confidence_band (str): "high", "moderate", or "low".
|
| 315 |
+
- total_frames (int): Total frames in the video.
|
| 316 |
+
- analyzed_frames (int): Frames actually analyzed.
|
| 317 |
+
- fps (float): Video frames per second.
|
| 318 |
+
- duration_seconds (float): Video duration in seconds.
|
| 319 |
+
- frame_results (list): Per-frame results for timeline.
|
| 320 |
+
- raw_scores (dict): Aggregated signal breakdown.
|
| 321 |
+
- explanation (str): XAI-style human-readable reasoning.
|
| 322 |
+
"""
|
| 323 |
+
import cv2
|
| 324 |
+
import torch
|
| 325 |
+
import numpy as np
|
| 326 |
+
from PIL import Image
|
| 327 |
+
|
| 328 |
+
model, processor = _ensure_model_loaded()
|
| 329 |
+
id2label = model.config.id2label
|
| 330 |
+
|
| 331 |
+
# Open the video file
|
| 332 |
+
cap = cv2.VideoCapture(video_path)
|
| 333 |
+
if not cap.isOpened():
|
| 334 |
+
return {"error": "Failed to open video file. The format may not be supported."}
|
| 335 |
+
|
| 336 |
+
# Extract video metadata
|
| 337 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 338 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
|
| 339 |
+
duration = total_frames / fps if fps > 0 else 0
|
| 340 |
+
|
| 341 |
+
# Cap the maximum number of frames to analyze
|
| 342 |
+
MAX_FRAMES_TO_ANALYZE = 30
|
| 343 |
+
effective_sample_rate = max(sample_rate, total_frames // MAX_FRAMES_TO_ANALYZE) \
|
| 344 |
+
if total_frames > MAX_FRAMES_TO_ANALYZE * sample_rate else sample_rate
|
| 345 |
+
|
| 346 |
+
frame_results = []
|
| 347 |
+
full_scores = []
|
| 348 |
+
face_scores = []
|
| 349 |
+
frame_idx = 0
|
| 350 |
+
|
| 351 |
+
logger.info(
|
| 352 |
+
"Analyzing video: %d total frames, %.1f fps, %.1f sec, sampling every %d frames",
|
| 353 |
+
total_frames, fps, duration, effective_sample_rate
|
| 354 |
+
)
|
| 355 |
+
|
| 356 |
+
while True:
|
| 357 |
+
ret, frame = cap.read()
|
| 358 |
+
if not ret:
|
| 359 |
+
break
|
| 360 |
+
|
| 361 |
+
if frame_idx % effective_sample_rate == 0:
|
| 362 |
+
# ββ Pass 1: Full-frame ββββββββββββββββββββββββββββββββββββ
|
| 363 |
+
full_fake = _classify_bgr(frame)
|
| 364 |
+
full_scores.append(full_fake)
|
| 365 |
+
|
| 366 |
+
# ββ Pass 2: Face-crop βββββββββββββββββββββββββββββββββββββ
|
| 367 |
+
face_rect = _get_face_rect(frame)
|
| 368 |
+
if face_rect is not None:
|
| 369 |
+
face_crop = _crop_face(frame, face_rect)
|
| 370 |
+
face_fake = _classify_bgr(face_crop)
|
| 371 |
+
else:
|
| 372 |
+
face_fake = full_fake # fallback
|
| 373 |
+
face_scores.append(face_fake)
|
| 374 |
+
|
| 375 |
+
# ββ Per-frame ensemble score ββββββββββββββββββββββββββββββ
|
| 376 |
+
frame_ensemble = _compute_ensemble_score(full_fake, face_fake)
|
| 377 |
+
is_frame_fake = frame_ensemble > ENSEMBLE_THRESHOLD
|
| 378 |
+
|
| 379 |
+
frame_results.append({
|
| 380 |
+
"frame": frame_idx,
|
| 381 |
+
"timestamp": round(frame_idx / fps, 2),
|
| 382 |
+
"label": "Fake" if is_frame_fake else "Real",
|
| 383 |
+
"confidence": round(frame_ensemble, 4),
|
| 384 |
+
"is_fake": is_frame_fake,
|
| 385 |
+
})
|
| 386 |
+
|
| 387 |
+
frame_idx += 1
|
| 388 |
+
|
| 389 |
+
cap.release()
|
| 390 |
+
|
| 391 |
+
if not frame_results:
|
| 392 |
+
return {"error": "No frames could be extracted from the video."}
|
| 393 |
+
|
| 394 |
+
# ββ Aggregate across all frames using the ensemble ββββββββββββββββ
|
| 395 |
+
face_mean = float(np.mean(face_scores)) if face_scores else 0.0
|
| 396 |
+
full_mean = float(np.mean(full_scores)) if full_scores else 0.0
|
| 397 |
+
score_diff = full_mean - face_mean
|
| 398 |
+
|
| 399 |
+
# Overall ensemble score (aggregated, not per-frame average)
|
| 400 |
+
overall_ensemble = _compute_ensemble_score(full_mean, face_mean)
|
| 401 |
+
overall_is_fake = overall_ensemble > ENSEMBLE_THRESHOLD
|
| 402 |
+
|
| 403 |
+
# Confidence in the verdict
|
| 404 |
+
if overall_is_fake:
|
| 405 |
+
confidence = min(1.0, 0.5 + (overall_ensemble - ENSEMBLE_THRESHOLD) / (1.0 - ENSEMBLE_THRESHOLD) * 0.5)
|
| 406 |
+
else:
|
| 407 |
+
confidence = min(1.0, 0.5 + (ENSEMBLE_THRESHOLD - overall_ensemble) / ENSEMBLE_THRESHOLD * 0.5)
|
| 408 |
+
|
| 409 |
+
label = "Fake" if overall_is_fake else "Real"
|
| 410 |
+
band = _classify_confidence_band(overall_ensemble)
|
| 411 |
+
|
| 412 |
+
# Frame-level stats for timeline
|
| 413 |
+
fake_count = sum(1 for f in frame_results if f["is_fake"])
|
| 414 |
+
real_count = len(frame_results) - fake_count
|
| 415 |
+
fake_ratio = fake_count / len(frame_results)
|
| 416 |
+
|
| 417 |
+
raw_scores = {
|
| 418 |
+
"Fake": round(overall_ensemble, 4),
|
| 419 |
+
"Real": round(1.0 - overall_ensemble, 4),
|
| 420 |
+
"full_frame_fake": round(full_mean, 4),
|
| 421 |
+
"face_crop_fake": round(face_mean, 4),
|
| 422 |
+
"score_diff": round(score_diff, 4),
|
| 423 |
+
"fake_frame_ratio": round(fake_ratio, 4),
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
+
explanation = _generate_video_explanation(
|
| 427 |
+
label, confidence, band, fake_count, real_count,
|
| 428 |
+
len(frame_results), total_frames, duration, raw_scores
|
| 429 |
+
)
|
| 430 |
+
|
| 431 |
+
return {
|
| 432 |
+
"is_fake": overall_is_fake,
|
| 433 |
+
"confidence": round(confidence, 4),
|
| 434 |
+
"label": label,
|
| 435 |
+
"confidence_band": band,
|
| 436 |
+
"total_frames": total_frames,
|
| 437 |
+
"analyzed_frames": len(frame_results),
|
| 438 |
+
"fps": round(fps, 2),
|
| 439 |
+
"duration_seconds": round(duration, 2),
|
| 440 |
+
"fake_frame_ratio": round(fake_ratio, 4),
|
| 441 |
+
"frame_results": frame_results,
|
| 442 |
+
"raw_scores": raw_scores,
|
| 443 |
+
"explanation": explanation,
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
|
| 447 |
+
def _generate_video_explanation(
|
| 448 |
+
label: str, confidence: float, band: str,
|
| 449 |
+
fake_count: int, real_count: int,
|
| 450 |
+
analyzed: int, total: int, duration: float,
|
| 451 |
+
raw_scores: dict
|
| 452 |
+
) -> str:
|
| 453 |
+
"""
|
| 454 |
+
Produce a rich, honest explanation for video deepfake analysis.
|
| 455 |
+
"""
|
| 456 |
+
pct = round(confidence * 100, 1)
|
| 457 |
+
is_fake = label == "Fake"
|
| 458 |
+
ratio_pct = round((fake_count / analyzed) * 100, 1) if analyzed > 0 else 0
|
| 459 |
+
|
| 460 |
+
if is_fake:
|
| 461 |
+
if band == "high":
|
| 462 |
+
verdict = f"This video shows strong indicators of deepfake manipulation ({pct}% confidence)."
|
| 463 |
+
detail = (
|
| 464 |
+
f"Across {analyzed} sampled frames (from {total} total, {round(duration, 1)}s), "
|
| 465 |
+
f"the dual-pass analysis consistently detected synthetic facial patterns."
|
| 466 |
+
)
|
| 467 |
+
elif band == "moderate":
|
| 468 |
+
verdict = f"This video shows moderate indicators of possible manipulation ({pct}% confidence)."
|
| 469 |
+
detail = (
|
| 470 |
+
f"Across {analyzed} sampled frames, the analysis detected mixed signals "
|
| 471 |
+
f"with {fake_count} frames ({ratio_pct}%) flagged as potentially manipulated."
|
| 472 |
+
)
|
| 473 |
+
else:
|
| 474 |
+
verdict = f"This video shows mild indicators of possible manipulation ({pct}% confidence)."
|
| 475 |
+
detail = (
|
| 476 |
+
f"The analysis produced borderline scores across {analyzed} frames. "
|
| 477 |
+
f"The result is uncertain -- manual review is recommended."
|
| 478 |
+
)
|
| 479 |
+
else:
|
| 480 |
+
if band == "high":
|
| 481 |
+
verdict = f"This video appears authentic ({pct}% confidence)."
|
| 482 |
+
detail = (
|
| 483 |
+
f"Across {analyzed} sampled frames (from {total} total, {round(duration, 1)}s), "
|
| 484 |
+
f"the analysis found consistent natural patterns in both full-frame and facial regions."
|
| 485 |
+
)
|
| 486 |
+
elif band == "moderate":
|
| 487 |
+
verdict = f"This video appears likely authentic ({pct}% confidence)."
|
| 488 |
+
detail = (
|
| 489 |
+
f"Across {analyzed} sampled frames, the analysis found predominantly "
|
| 490 |
+
f"natural characteristics with some minor ambiguous elements."
|
| 491 |
+
)
|
| 492 |
+
else:
|
| 493 |
+
verdict = f"This video shows uncertain results ({pct}% confidence)."
|
| 494 |
+
detail = (
|
| 495 |
+
f"The analysis produced borderline scores across {analyzed} frames. "
|
| 496 |
+
f"The video may be authentic or subtly manipulated. Manual review is recommended."
|
| 497 |
+
)
|
| 498 |
+
|
| 499 |
+
# Technical note about the ensemble approach
|
| 500 |
+
tech_note = (
|
| 501 |
+
f"Analysis method: Dual-pass ensemble (full-frame + face-crop) with "
|
| 502 |
+
f"calibrated scoring. Full-frame signal: {raw_scores.get('full_frame_fake', 0):.2f}, "
|
| 503 |
+
f"Face-crop signal: {raw_scores.get('face_crop_fake', 0):.2f}."
|
| 504 |
+
)
|
| 505 |
+
|
| 506 |
+
return f"{verdict}\n\n{detail}\n\n{tech_note}"
|
src/intelligence/fake_news.py
CHANGED
|
@@ -491,36 +491,52 @@ def generate_explanation(score: float, title: str = "", content: str = "",
|
|
| 491 |
elif total_outlets == 0:
|
| 492 |
risk_factors.append("no other major outlets are reporting this story, raising exclusivity concerns")
|
| 493 |
|
| 494 |
-
# ββ Build the explanation ββββββββββββββββββββββββββββββββββ
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
if
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
parts.append(f"This article scores {int(score*100)}% credibility β likely authentic but with some caveats.")
|
| 502 |
-
elif score >= 0.40:
|
| 503 |
-
parts.append(f"This article scores {int(score*100)}% credibility, placing it in the uncertain zone where neither authenticity nor misinformation can be confidently determined.")
|
| 504 |
-
elif score >= 0.20:
|
| 505 |
-
parts.append(f"This article scores only {int(score*100)}% credibility, indicating a significant risk of misinformation.")
|
| 506 |
-
else:
|
| 507 |
-
parts.append(f"This article scores just {int(score*100)}% credibility β our models strongly flag this as potential misinformation.")
|
| 508 |
-
|
| 509 |
-
# Trust signals
|
| 510 |
-
if trust_factors:
|
| 511 |
-
parts.append("Positive indicators: " + "; ".join(trust_factors) + ".")
|
| 512 |
-
|
| 513 |
-
# Risk signals
|
| 514 |
-
if risk_factors:
|
| 515 |
-
parts.append("Risk factors: " + "; ".join(risk_factors) + ".")
|
| 516 |
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 524 |
|
| 525 |
|
| 526 |
def detect_fake_news(title: str, content: str, model=None, tokenizer=None, source: str = None, verification_result: dict = None) -> tuple:
|
|
|
|
| 491 |
elif total_outlets == 0:
|
| 492 |
risk_factors.append("no other major outlets are reporting this story, raising exclusivity concerns")
|
| 493 |
|
| 494 |
+
# ββ Build the explanation prompt ββββββββββββββββββββββββββββββββββ
|
| 495 |
+
prompt = f"Article Title: {title}\n"
|
| 496 |
+
if source: prompt += f"Source: {source}\n"
|
| 497 |
+
prompt += f"Credibility Score: {int(score*100)}%\n"
|
| 498 |
+
if trust_factors: prompt += f"Positive indicators: {', '.join(trust_factors)}.\n"
|
| 499 |
+
if risk_factors: prompt += f"Risk factors: {', '.join(risk_factors)}.\n"
|
| 500 |
+
prompt += "\nWrite a detailed, dynamic explanation of exactly what the AI models think about this article's credibility based on the given indicators. Your explanation must be between 5 and 7 sentences long and professionally explain the reasoning."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
|
| 502 |
+
try:
|
| 503 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 504 |
+
import torch
|
| 505 |
+
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 506 |
+
|
| 507 |
+
# We load it lazily to save startup time if XAI isn't hit
|
| 508 |
+
global _xai_model, _xai_tokenizer
|
| 509 |
+
if '_xai_model' not in globals():
|
| 510 |
+
_xai_tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 511 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 512 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 513 |
+
_xai_model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=dtype).to(device)
|
| 514 |
+
_xai_model.eval()
|
| 515 |
+
|
| 516 |
+
messages = [
|
| 517 |
+
{"role": "system", "content": "You are a professional AI news verification assistant. You provide detailed, analytical reasoning for credibility scores."},
|
| 518 |
+
{"role": "user", "content": prompt}
|
| 519 |
+
]
|
| 520 |
+
text = _xai_tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 521 |
+
inputs = _xai_tokenizer([text], return_tensors="pt").to(_xai_model.device)
|
| 522 |
+
|
| 523 |
+
with torch.no_grad():
|
| 524 |
+
outputs = _xai_model.generate(**inputs, max_new_tokens=250, temperature=0.75, do_sample=True)
|
| 525 |
+
explanation = _xai_tokenizer.decode(outputs[0][len(inputs.input_ids[0]):], skip_special_tokens=True).strip()
|
| 526 |
+
|
| 527 |
+
# Fallback if generated string is empty
|
| 528 |
+
if len(explanation) < 10:
|
| 529 |
+
raise ValueError("Empty explanation generated.")
|
| 530 |
+
|
| 531 |
+
return explanation
|
| 532 |
+
except Exception as e:
|
| 533 |
+
print(f"XAI Generation Error: {e}")
|
| 534 |
+
# Fallback to a basic template if the LLM fails or is downloading
|
| 535 |
+
if score >= 0.60:
|
| 536 |
+
return f"This article scores {int(score*100)}% credibility, indicating it is likely authentic."
|
| 537 |
+
elif score >= 0.40:
|
| 538 |
+
return f"This article scores {int(score*100)}% credibility, placing it in an uncertain zone."
|
| 539 |
+
return f"This article scores {int(score*100)}% credibility, indicating potential misinformation."
|
| 540 |
|
| 541 |
|
| 542 |
def detect_fake_news(title: str, content: str, model=None, tokenizer=None, source: str = None, verification_result: dict = None) -> tuple:
|
src/maintenance/reprocess_all.py
CHANGED
|
@@ -22,7 +22,7 @@ def reprocess_all():
|
|
| 22 |
|
| 23 |
print("\n--- Reprocessing ALL Articles with New XAI Explanations ---")
|
| 24 |
|
| 25 |
-
all_articles = session.query(Article).all()
|
| 26 |
print(f"Found {len(all_articles)} total articles.")
|
| 27 |
if not all_articles:
|
| 28 |
return
|
|
|
|
| 22 |
|
| 23 |
print("\n--- Reprocessing ALL Articles with New XAI Explanations ---")
|
| 24 |
|
| 25 |
+
all_articles = session.query(Article).limit(5).all()
|
| 26 |
print(f"Found {len(all_articles)} total articles.")
|
| 27 |
if not all_articles:
|
| 28 |
return
|
stop.bat
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
@echo off
|
| 2 |
+
echo Stopping AI News Monitor Services gracefully...
|
| 3 |
+
echo.
|
| 4 |
+
|
| 5 |
+
:: We use the /FI filter to match the exact window titles we set in run.bat
|
| 6 |
+
:: The /T switch kills any child processes, but we don't use /F (force) so they have a chance to shut down cleanly.
|
| 7 |
+
:: If you need a forced kill, you can add /F
|
| 8 |
+
|
| 9 |
+
echo Stopping Data Ingestion...
|
| 10 |
+
taskkill /FI "WINDOWTITLE eq Data Ingestion*" /T
|
| 11 |
+
|
| 12 |
+
echo Stopping Intelligence Pipeline...
|
| 13 |
+
taskkill /FI "WINDOWTITLE eq Intelligence Pipeline*" /T
|
| 14 |
+
|
| 15 |
+
echo Stopping API Backend...
|
| 16 |
+
taskkill /FI "WINDOWTITLE eq API Backend*" /T
|
| 17 |
+
|
| 18 |
+
echo Stopping Frontend Dashboard...
|
| 19 |
+
taskkill /FI "WINDOWTITLE eq Frontend Dashboard*" /T
|
| 20 |
+
|
| 21 |
+
echo Stopping Background Scheduler...
|
| 22 |
+
taskkill /FI "WINDOWTITLE eq Background Scheduler*" /T
|
| 23 |
+
|
| 24 |
+
echo Stopping Reprocessor...
|
| 25 |
+
taskkill /FI "WINDOWTITLE eq Reprocessor*" /T
|
| 26 |
+
|
| 27 |
+
echo.
|
| 28 |
+
echo All AI News Monitor services have been instructed to shut down.
|
| 29 |
+
echo If a window prompts you to terminate batch jobs, type 'Y'.
|
| 30 |
+
pause
|