mrRS18 commited on
Commit
4e6fa34
·
verified ·
1 Parent(s): 99f8363

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -19
app.py CHANGED
@@ -6,7 +6,6 @@ from transformers import pipeline
6
  from PIL import Image
7
  import torch
8
  import numpy as np
9
- import cv2
10
 
11
  # ======================================
12
  # LOAD REAL AI MODEL (CPU SAFE)
@@ -48,7 +47,7 @@ def analyze(image):
48
  img = Image.fromarray(image)
49
  results = detector(img)
50
 
51
- # Extract real and fake scores explicitly
52
  real_score = 0
53
  fake_score = 0
54
 
@@ -60,28 +59,37 @@ def analyze(image):
60
 
61
  confidence = round(max(real_score, fake_score), 2)
62
 
63
- # =============================
64
- # Smoothness Heuristic
65
- # =============================
66
- cv_img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2GRAY)
67
- laplacian_var = cv2.Laplacian(cv_img, cv2.CV_64F).var()
68
 
69
- # Low texture variance = more AI-like
70
- smooth_score = max(0, 100 - laplacian_var)
71
 
72
- # =============================
73
- # Combined Risk Scoring
74
- # =============================
 
75
 
76
- fake_probability = fake_score
 
 
 
 
 
77
 
78
- combined_risk = fake_probability + (smooth_score * 0.3)
 
 
 
 
 
 
79
 
80
- if combined_risk > 70:
81
  prediction = "DEEPFAKE"
82
  risk_level = "Forensic Risk Level: High"
83
  risk_color = "#FF3C5F"
84
- elif combined_risk > 40:
85
  prediction = "DEEPFAKE"
86
  risk_level = "Forensic Risk Level: Medium"
87
  risk_color = "#FFA500"
@@ -94,8 +102,9 @@ def analyze(image):
94
  • Vision Transformer inference complete
95
  • Real probability: {round(real_score,2)}%
96
  • Fake probability: {round(fake_score,2)}%
97
- • Texture variance score: {round(laplacian_var,2)}
98
- • Combined forensic risk index: {round(combined_risk,2)}
 
99
  • Multi-signal artifact evaluation enabled
100
  Conclusion: Image classified as {prediction}.
101
  """
@@ -269,7 +278,7 @@ def get_recent_records():
269
  return html
270
 
271
  # ======================================
272
- # UI
273
  # ======================================
274
 
275
  with gr.Blocks() as demo:
 
6
  from PIL import Image
7
  import torch
8
  import numpy as np
 
9
 
10
  # ======================================
11
  # LOAD REAL AI MODEL (CPU SAFE)
 
47
  img = Image.fromarray(image)
48
  results = detector(img)
49
 
50
+ # Extract real & fake scores explicitly
51
  real_score = 0
52
  fake_score = 0
53
 
 
59
 
60
  confidence = round(max(real_score, fake_score), 2)
61
 
62
+ # ======================================
63
+ # Multi-Signal Forensic Scoring
64
+ # ======================================
 
 
65
 
66
+ gray = np.mean(np.array(img), axis=2)
 
67
 
68
+ # Texture / Smoothness detection
69
+ grad_x, grad_y = np.gradient(gray)
70
+ laplacian_like = np.abs(grad_x) + np.abs(grad_y)
71
+ texture_variance = np.var(laplacian_like)
72
 
73
+ smooth_score = max(0, 150 - texture_variance)
74
+
75
+ # Facial symmetry heuristic
76
+ width = gray.shape[1]
77
+ left = gray[:, :width//2]
78
+ right = np.fliplr(gray[:, width//2:])
79
 
80
+ min_width = min(left.shape[1], right.shape[1])
81
+ symmetry_diff = np.mean(np.abs(left[:, :min_width] - right[:, :min_width]))
82
+ symmetry_score = max(0, 100 - symmetry_diff)
83
+
84
+ # Combined forensic index
85
+ fake_probability = fake_score
86
+ combined_risk = fake_probability + (smooth_score * 0.4) + (symmetry_score * 0.3)
87
 
88
+ if combined_risk > 120:
89
  prediction = "DEEPFAKE"
90
  risk_level = "Forensic Risk Level: High"
91
  risk_color = "#FF3C5F"
92
+ elif combined_risk > 80:
93
  prediction = "DEEPFAKE"
94
  risk_level = "Forensic Risk Level: Medium"
95
  risk_color = "#FFA500"
 
102
  • Vision Transformer inference complete
103
  • Real probability: {round(real_score,2)}%
104
  • Fake probability: {round(fake_score,2)}%
105
+ • Texture variance score: {round(texture_variance,2)}
106
+ • Symmetry anomaly score: {round(symmetry_score,2)}
107
+ • Combined forensic index: {round(combined_risk,2)}
108
  • Multi-signal artifact evaluation enabled
109
  Conclusion: Image classified as {prediction}.
110
  """
 
278
  return html
279
 
280
  # ======================================
281
+ # UI (UNCHANGED)
282
  # ======================================
283
 
284
  with gr.Blocks() as demo: