MrTsp commited on
Commit
77037c2
·
1 Parent(s): 64ca17e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -5
app.py CHANGED
@@ -233,15 +233,32 @@ def run_inference(model: DeepfakeDetector, frame_paths: list) -> dict:
233
  if not fake_probs:
234
  raise ValueError("No frames could be processed.")
235
 
236
- avg_fake = float(np.mean(fake_probs))
237
- avg_real = 1.0 - avg_fake
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
 
239
  return {
240
- "verdict": "FAKE" if avg_fake > 0.5 else "REAL",
241
- "fake_probability": round(avg_fake * 100, 1),
242
  "real_probability": round(avg_real * 100, 1),
243
  "frame_count": len(fake_probs),
244
- "confidence": round(max(avg_fake, avg_real) * 100, 1),
245
  "per_frame_scores": [round(p * 100, 1) for p in fake_probs],
246
  }
247
 
 
233
  if not fake_probs:
234
  raise ValueError("No frames could be processed.")
235
 
236
+ # 1. Advanced Aggregation (Top 50% Mean)
237
+ # Deepfake artifacts might only appear in parts of the video.
238
+ # Averaging all frames dilutes the score. We take the top 50% most suspicious frames.
239
+ sorted_probs = sorted(fake_probs, reverse=True)
240
+ top_k = max(1, len(sorted_probs) // 2)
241
+ video_fake_prob = float(np.mean(sorted_probs[:top_k]))
242
+
243
+ # 2. Ratio Check
244
+ # If at least 30% of frames are distinctly flagged as Fake, mark the whole video as Fake.
245
+ fake_frame_count = sum(1 for p in fake_probs if p > 0.5)
246
+ fake_ratio = fake_frame_count / len(fake_probs)
247
+
248
+ is_fake = (video_fake_prob > 0.5) or (fake_ratio >= 0.3)
249
+
250
+ # Ensure UI consistency: If flagged as FAKE by ratio, but probability is low, boost it to 51%
251
+ if is_fake and video_fake_prob <= 0.5:
252
+ video_fake_prob = 0.51
253
+
254
+ avg_real = 1.0 - video_fake_prob
255
 
256
  return {
257
+ "verdict": "FAKE" if is_fake else "REAL",
258
+ "fake_probability": round(video_fake_prob * 100, 1),
259
  "real_probability": round(avg_real * 100, 1),
260
  "frame_count": len(fake_probs),
261
+ "confidence": round(max(video_fake_prob, avg_real) * 100, 1),
262
  "per_frame_scores": [round(p * 100, 1) for p in fake_probs],
263
  }
264