S-Vetrivel commited on
Commit
f95b090
·
1 Parent(s): 04f3da2

Implement Conservative Consensus: SNR Analysis and Human Liveness Veto to eliminate false positives

Browse files
Files changed (1) hide show
  1. app/infer.py +109 -53
app/infer.py CHANGED
@@ -29,6 +29,30 @@ class VoiceClassifier:
29
  print(f"Error loading model: {e}")
30
  self.model = None
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  def predict(self, waveform: torch.Tensor, language: str = "Unknown"):
33
  if self.model is None:
34
  return {"error": "Model not loaded"}
@@ -38,9 +62,12 @@ class VoiceClassifier:
38
  wav_np = waveform.squeeze().cpu().numpy()
39
  sr = 16000
40
 
41
- # --- ADVANCED FEATURE EXTRACTION ---
42
  t0 = time.time()
43
 
 
 
 
 
44
  # A. Pitch Analysis
45
  f0, voiced_flag, voiced_probs = librosa.pyin(
46
  wav_np, fmin=librosa.note_to_hz('C2'), fmax=librosa.note_to_hz('C7'), sr=sr
@@ -48,27 +75,31 @@ class VoiceClassifier:
48
  f0_clean = f0[~np.isnan(f0)]
49
  pitch_var = np.std(f0_clean) if len(f0_clean) > 0 else 0.0
50
 
51
- # B. Spectral Flatness (Detects vocoder buzz)
52
  flatness = np.mean(librosa.feature.spectral_flatness(y=wav_np))
53
 
54
- # C. RMS Energy Variance (Detects flattened volume envelopes)
55
  rms = librosa.feature.rms(y=wav_np)[0]
56
- rms_var = np.std(rms) / (np.mean(rms) + 1e-6) # Normalized variance
57
 
58
- # D. Zero Crossing Rate Variance (Detects robotic vowel transitions)
59
- zcr = librosa.feature.zero_crossing_rate(wav_np)[0]
60
- zcr_var = np.std(zcr)
 
 
 
 
 
 
 
61
 
62
- # --- TEMPORAL CONSISTENCY (SLIDING WINDOW) ---
63
- chunk_size = 2 * sr # 2 seconds
64
- stride = 1 * sr # 1 second overlap
65
  chunks = []
66
  for i in range(0, len(wav_np) - chunk_size + 1, stride):
67
  chunks.append(wav_np[i : i + chunk_size])
68
-
69
- # If audio too short for stride, just use whole thing
70
- if not chunks:
71
- chunks = [wav_np]
72
 
73
  chunk_probs = []
74
  for chunk in chunks:
@@ -77,61 +108,86 @@ class VoiceClassifier:
77
  with torch.no_grad():
78
  outputs = self.model(**inputs)
79
  probs = torch.softmax(outputs.logits, dim=-1)
80
- chunk_probs.append(probs[0][0].item()) # Probability of 'fake'
81
 
82
- # Authority calculation
83
- # We take the MAX probability across chunks to catch 'slips' in AI generation
84
  prob_fake = np.max(chunk_probs)
85
- prob_real = 1.0 - prob_fake
86
 
87
  t1 = time.time()
88
- print(f"DEBUG: Analysis took {t1 - t0:.3f}s. Multi-chunk prob_fake: {prob_fake:.4f}")
89
- print(f"DEBUG: Features - PitchVar: {pitch_var:.1f}, Flatness: {flatness:.4f}, RMS_Var: {rms_var:.4f}")
90
 
91
- # --- HYBRID HEURISTIC STRENGTHENING ---
92
- # AI Voices often have VERY low flatness or VERY low pitch variance
93
- ai_signal_flags = 0
94
- if pitch_var < 15.0: ai_signal_flags += 1
95
- if flatness < 0.005: ai_signal_flags += 1 # Very tonal/melodic
96
- if rms_var < 0.1: ai_signal_flags += 1 # Robotic volume
97
-
98
- # Final Verdict Logic
99
- # If the model is unsure (0.4-0.6) but signal flags are high, tip to AI
100
- if 0.4 < prob_fake < 0.6 and ai_signal_flags >= 2:
101
- prob_fake = 0.75
102
-
103
- prediction = "AI_GENERATED" if prob_fake > 0.5 else "HUMAN"
104
- confidence = prob_fake if prediction == "AI_GENERATED" else prob_real
105
-
106
- # --- LANGUAGE AWARENESS ---
107
- # If it's a non-English language, the model might be slightly less reliable
108
- # We dampen confidence on low-resource languages to prevent false accusations
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  is_english = language.lower() in ["english", "en"]
110
- if not is_english and confidence < 0.85:
111
- confidence *= 0.95 # Slight dampening
112
 
113
  # Construct Explanation
114
  if prediction == "AI_GENERATED":
115
- reasons = []
116
- if ai_signal_flags >= 2: reasons.append("synthetic spectral characteristics")
117
- if pitch_var < 20: reasons.append("lack of natural prosody")
118
- if not reasons: reasons.append("digital vocoder artifacts")
119
- explanation = f"AI detected with {confidence*100:.1f}% confidence. Evidence: {', '.join(reasons)}."
120
  else:
121
- if pitch_var > 25.0:
122
- explanation = f"Human verified with {confidence*100:.1f}% confidence. Strong natural pitch variance and human vocal dynamics detected."
123
- else:
124
- explanation = f"Audio likely Human ({confidence*100:.1f}%). Detected natural speech fluctuations despite localized artifacts."
125
 
126
  return {
127
  "prediction": prediction,
128
- "probability_ai": float(f"{prob_fake:.4f}"),
129
  "confidence": float(f"{confidence:.4f}"),
130
  "features": {
131
  "pitch_variance": float(f"{pitch_var:.2f}"),
132
- "spectral_flatness": float(f"{flatness:.6f}"),
133
- "rms_variance": float(f"{rms_var:.4f}"),
134
- "zcr_variance": float(f"{zcr_var:.4f}")
135
  },
136
  "explanation": explanation
137
  }
 
29
  print(f"Error loading model: {e}")
30
  self.model = None
31
 
32
+ def calculate_snr(self, audio_np):
33
+ """
34
+ Estimate Signal-to-Noise Ratio (SNR) in dB.
35
+ Assumes the quietest 10% of frames represent the noise floor.
36
+ """
37
+ try:
38
+ # Frame-based RMS energy
39
+ rms = librosa.feature.rms(y=audio_np)[0]
40
+ if len(rms) < 10: return 50.0 # Too short, assume clean
41
+
42
+ # Sort RMS values to find noise floor
43
+ sorted_rms = np.sort(rms)
44
+ noise_len = max(1, int(0.1 * len(rms)))
45
+ noise_floor_rms = np.mean(sorted_rms[:noise_len]) + 1e-9
46
+
47
+ # Signal RMS (approximate as top 50% energy average)
48
+ signal_len = max(1, int(0.5 * len(rms)))
49
+ signal_rms = np.mean(sorted_rms[-signal_len:])
50
+
51
+ snr = 20 * np.log10(signal_rms / noise_floor_rms)
52
+ return snr
53
+ except Exception:
54
+ return 30.0 # Default to decent SNR if calculation fails
55
+
56
  def predict(self, waveform: torch.Tensor, language: str = "Unknown"):
57
  if self.model is None:
58
  return {"error": "Model not loaded"}
 
62
  wav_np = waveform.squeeze().cpu().numpy()
63
  sr = 16000
64
 
 
65
  t0 = time.time()
66
 
67
+ # --- SIGNAL QUALITY CHECKS ---
68
+ snr_db = self.calculate_snr(wav_np)
69
+
70
+ # --- ADVANCED FEATURE EXTRACTION ---
71
  # A. Pitch Analysis
72
  f0, voiced_flag, voiced_probs = librosa.pyin(
73
  wav_np, fmin=librosa.note_to_hz('C2'), fmax=librosa.note_to_hz('C7'), sr=sr
 
75
  f0_clean = f0[~np.isnan(f0)]
76
  pitch_var = np.std(f0_clean) if len(f0_clean) > 0 else 0.0
77
 
78
+ # B. Spectral Flatness
79
  flatness = np.mean(librosa.feature.spectral_flatness(y=wav_np))
80
 
81
+ # C. RMS Energy Variance
82
  rms = librosa.feature.rms(y=wav_np)[0]
83
+ rms_var = np.std(rms) / (np.mean(rms) + 1e-6)
84
 
85
+ # D. Liveness (Pause) Detection
86
+ # Count distinct silent intervals (>0.1s)
87
+ silent_intervals = librosa.effects.split(wav_np, top_db=20, frame_length=2048, hop_length=512)
88
+ num_pauses = 0
89
+ if len(silent_intervals) > 1:
90
+ # Calculate gaps between speech segments
91
+ for i in range(len(silent_intervals)-1):
92
+ gap_samples = silent_intervals[i+1][0] - silent_intervals[i][1]
93
+ if gap_samples > sr * 0.1: # >100ms
94
+ num_pauses += 1
95
 
96
+ # --- TEMPORAL CONSISTENCY ---
97
+ chunk_size = 2 * sr
98
+ stride = 1 * sr
99
  chunks = []
100
  for i in range(0, len(wav_np) - chunk_size + 1, stride):
101
  chunks.append(wav_np[i : i + chunk_size])
102
+ if not chunks: chunks = [wav_np]
 
 
 
103
 
104
  chunk_probs = []
105
  for chunk in chunks:
 
108
  with torch.no_grad():
109
  outputs = self.model(**inputs)
110
  probs = torch.softmax(outputs.logits, dim=-1)
111
+ chunk_probs.append(probs[0][0].item()) # Prob fake
112
 
113
+ # Initial Raw Confidence (Max across chunks)
 
114
  prob_fake = np.max(chunk_probs)
 
115
 
116
  t1 = time.time()
117
+ print(f"DEBUG: Analysis took {t1 - t0:.3f}s. Raw prob_fake: {prob_fake:.4f}")
118
+ print(f"DEBUG: Features - SNR: {snr_db:.1f}dB, Pauses: {num_pauses}, PitchVar: {pitch_var:.1f}, Flatness: {flatness:.4f}")
119
 
120
+ # --- CONSERVATIVE CONSENSUS LOGIC ---
121
+
122
+ # 1. Initialize Flags (Relaxed thresholds)
123
+ ai_flags = []
124
+ human_flags = []
125
+
126
+ # AI Indicators
127
+ if pitch_var < 10.0: ai_flags.append("Low pitch variance") # Relaxed from 15
128
+ if flatness < 0.002: ai_flags.append("Unnatural spectral flatness") # Relaxed from 0.005
129
+ if rms_var < 0.1: ai_flags.append("Robotic volume consistency")
130
+
131
+ # Human Indicators (VETO Power)
132
+ if snr_db < 15.0: human_flags.append("High Background Noise")
133
+ if num_pauses >= 2: human_flags.append("Natural breathing pauses")
134
+ if pitch_var > 35.0: human_flags.append("High expressive variation")
135
+
136
+ # 2. Apply Penalties / Vetoes
137
+ confidence_penalty = 1.0
138
+
139
+ # VETO 1: NOISE
140
+ # If noisy, the model's "Fake" detection is untrustworthy. Cap it.
141
+ if snr_db < 15.0:
142
+ print("DEBUG: Low SNR detected. Applying penalty.")
143
+ confidence_penalty *= 0.6 # Reduce confidence by 40%
144
+
145
+ # VETO 2: LIVENESS
146
+ if num_pauses >= 2 and prob_fake < 0.95:
147
+ print("DEBUG: Natural pauses detected. Applying penalty.")
148
+ confidence_penalty *= 0.8 # Reduce confidence by 20%
149
+
150
+ # Apply penalty to the probability of being fake
151
+ prob_fake_adjusted = prob_fake * confidence_penalty
152
+
153
+ # 3. Final Decision
154
+ # We demand HIGHER evidence for AI (Conservatism)
155
+ threshold = 0.65 # Default threshold (requires >65% certainty)
156
+
157
+ # If we have AI flags, we can trust the model more (lower threshold)
158
+ if len(ai_flags) >= 2:
159
+ threshold = 0.55
160
+
161
+ if prob_fake_adjusted > threshold:
162
+ prediction = "AI_GENERATED"
163
+ confidence = prob_fake_adjusted
164
+ else:
165
+ prediction = "HUMAN"
166
+ confidence = 1.0 - prob_fake_adjusted
167
+
168
+ # 4. Language Awareness Dampening
169
  is_english = language.lower() in ["english", "en"]
170
+ if prediction == "AI_GENERATED" and not is_english:
171
+ confidence *= 0.9 # Extra caution for non-English
172
 
173
  # Construct Explanation
174
  if prediction == "AI_GENERATED":
175
+ reasons = ai_flags
176
+ if not reasons: reasons.append("high confidence from deepfake classifier")
177
+ explanation = f"AI detected ({confidence*100:.1f}%). Indicators: {', '.join(reasons)}."
 
 
178
  else:
179
+ reasons = human_flags
180
+ if not reasons: reasons.append("insufficient evidence of synthesis")
181
+ explanation = f"Verified Human ({confidence*100:.1f}%). Evidence: {', '.join(reasons)}."
 
182
 
183
  return {
184
  "prediction": prediction,
185
+ "probability_ai": float(f"{prob_fake_adjusted:.4f}"),
186
  "confidence": float(f"{confidence:.4f}"),
187
  "features": {
188
  "pitch_variance": float(f"{pitch_var:.2f}"),
189
+ "snr_db": float(f"{snr_db:.1f}"),
190
+ "pauses": num_pauses
 
191
  },
192
  "explanation": explanation
193
  }