add app file
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import torch
|
| 3 |
+
import librosa
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import AutoFeatureExtractor, Wav2Vec2ForSequenceClassification
|
| 6 |
+
|
| 7 |
+
# 1. Load Model & Feature Extractor (Use direct HF loading or your exported ONNX model)
|
| 8 |
+
model_id = "Bgeorge/deepfake-audio-detector-wav2vec2"
|
| 9 |
+
print("📥 Loading deepfake detector for live streaming...")
|
| 10 |
+
|
| 11 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
|
| 12 |
+
model = Wav2Vec2ForSequenceClassification.from_pretrained(model_id)
|
| 13 |
+
model.eval()
|
| 14 |
+
|
| 15 |
+
# Global buffer to store the rolling window of audio (e.g., up to 3 seconds at 16kHz = 48,000 samples)
|
| 16 |
+
AUDIO_BUFFER_SIZE = 16000 * 3
|
| 17 |
+
audio_buffer = np.array([], dtype=np.float32)
|
| 18 |
+
|
| 19 |
+
def predict_stream(stream_chunk):
|
| 20 |
+
global audio_buffer
|
| 21 |
+
|
| 22 |
+
if stream_chunk is None:
|
| 23 |
+
return "Listening..."
|
| 24 |
+
|
| 25 |
+
# Gradio streaming provides a tuple: (sample_rate, numpy_array)
|
| 26 |
+
sr, y = stream_chunk
|
| 27 |
+
|
| 28 |
+
# Ensure audio is mono and float32
|
| 29 |
+
if y.ndim > 1:
|
| 30 |
+
y = np.mean(y, axis=1)
|
| 31 |
+
y = y.astype(np.float32)
|
| 32 |
+
|
| 33 |
+
# Resample to 16kHz if the mic stream is a different sample rate (e.g., 44.1kHz or 48kHz)
|
| 34 |
+
if sr != 16000:
|
| 35 |
+
y = librosa.resample(y, orig_sr=sr, target_sr=16000)
|
| 36 |
+
|
| 37 |
+
# Append new chunk to the rolling buffer
|
| 38 |
+
audio_buffer = np.concatenate([audio_buffer, y])
|
| 39 |
+
|
| 40 |
+
# Keep only the latest N samples (sliding window)
|
| 41 |
+
if len(audio_buffer) > AUDIO_BUFFER_SIZE:
|
| 42 |
+
audio_buffer = audio_buffer[-AUDIO_BUFFER_SIZE:]
|
| 43 |
+
|
| 44 |
+
# Wait until we have at least 1 second of audio before running inference
|
| 45 |
+
if len(audio_buffer) < 16000:
|
| 46 |
+
return "🎙️ Gathering audio context..."
|
| 47 |
+
|
| 48 |
+
# Extract features and run inference
|
| 49 |
+
inputs = feature_extractor(audio_buffer, sampling_rate=16000, return_tensors="pt", padding=True)
|
| 50 |
+
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
outputs = model(**inputs)
|
| 53 |
+
logits = outputs.logits
|
| 54 |
+
probabilities = torch.softmax(logits, dim=-1).squeeze().tolist()
|
| 55 |
+
|
| 56 |
+
# Assuming index 1 is Fake / Spoof, index 0 is Real
|
| 57 |
+
fake_prob = probabilities[1] if len(probabilities) > 1 else probabilities[0]
|
| 58 |
+
real_prob = probabilities[0] if len(probabilities) > 1 else (1 - probabilities[0])
|
| 59 |
+
|
| 60 |
+
if fake_prob > 0.5:
|
| 61 |
+
return f"🚨 ALERT: Suspicious Speech Detected!\nConfidence (Fake): {fake_prob * 100:.1f}%"
|
| 62 |
+
else:
|
| 63 |
+
return f"✅ Status: Clean / Real Voice\nConfidence (Real): {real_prob * 100:.1f}%"
|
| 64 |
+
|
| 65 |
+
# 2. Build Gradio Live Interface
|
| 66 |
+
demo = gr.Interface(
|
| 67 |
+
fn=predict_stream,
|
| 68 |
+
inputs=gr.Audio(sources=["microphone"], streaming=True, type="numpy", label="Live Microphone Stream"),
|
| 69 |
+
outputs=gr.Textbox(label="Live Fraud Detection Feed", lines=3),
|
| 70 |
+
live=True,
|
| 71 |
+
title="Real-Time Voice Deepfake Detector",
|
| 72 |
+
description="Speak into your microphone to simulate a live call stream. The model analyzes rolling audio chunks to flag synthetic speech."
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
demo.launch()
|