File size: 7,573 Bytes
66a6dab 08b59bf f5d1d5c 66a6dab 08b59bf 66a6dab 08b59bf 66a6dab 08b59bf f5d1d5c 66a6dab f5d1d5c 66a6dab 08b59bf 66a6dab 08b59bf 66a6dab 08b59bf f5d1d5c 08b59bf 66a6dab 08b59bf 1772308 66a6dab 08b59bf 66a6dab 08b59bf 66a6dab 08b59bf 66a6dab f5d1d5c 66a6dab f5d1d5c 66a6dab 08b59bf 66a6dab 08b59bf 66a6dab f5d1d5c 66a6dab f5d1d5c 66a6dab f5d1d5c 66a6dab f5d1d5c 66a6dab f5d1d5c 66a6dab f5d1d5c 66a6dab 08b59bf f5d1d5c 08b59bf 66a6dab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | import gradio as gr
import base64
import json
import numpy as np
import torch
import librosa
import spaces # <-- Required for Hugging Face ZeroGPU infrastructure
from fastapi import FastAPI, WebSocket, Request
from fastapi.responses import HTMLResponse
from transformers import AutoFeatureExtractor, Wav2Vec2ForSequenceClassification
# ==========================================
# 1. LOAD MODEL & FEATURE EXTRACTOR
# ==========================================
MODEL_ID = "Bgeorge/deepfake-audio-detector-wav2vec2"
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"π₯ Loading AI Model ({MODEL_ID}) onto {device}...")
feature_extractor = AutoFeatureExtractor.from_pretrained(MODEL_ID)
model = Wav2Vec2ForSequenceClassification.from_pretrained(MODEL_ID).to(device)
model.eval()
# Global list to track active Flutter dashboard client connections
connected_flutter_clients = []
# ==========================================
# 2. ZERO-GPU WRAPPED INFERENCE FUNCTION
# ==========================================
@spaces.GPU(duration=30)
def run_model_inference(audio_buffer):
"""Executes transformer inference securely under Hugging Face's ZeroGPU dynamic allocator."""
inputs = feature_extractor(audio_buffer, sampling_rate=16000, return_tensors="pt", padding=True)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
probs = torch.softmax(outputs.logits, dim=-1).squeeze().tolist()
return probs
# ==========================================
# 3. GRADIO WEB INTERFACE (For Browser Testing)
# ==========================================
def predict_audio_file(audio_file):
if audio_file is None:
return "No audio uploaded.", 0.0
sr, y = audio_file
if y.ndim > 1:
y = np.mean(y, axis=1) # Convert stereo to mono
y = y.astype(np.float32)
# Resample if browser audio isn't 16kHz
if sr != 16000:
y = librosa.resample(y, orig_sr=sr, target_sr=16000)
probs = run_model_inference(y)
# Label 0 = REAL, Label 1 = FAKE
fake_prob = probs[1] if isinstance(probs, list) and len(probs) > 1 else probs
fake_confidence = round(fake_prob * 100, 2)
real_confidence = round((1.0 - fake_prob) * 100, 2)
status = "π¨ FAKE / SYNTHETIC VOICE" if fake_prob < 0.60 else "β
REAL / HUMAN VOICE"
score = fake_confidence if fake_prob > 0.60 else real_confidence
return f"{status} | Confidence Score: {score}%", score
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# π‘οΈ VoiceGuard AI: Live Telephony Deepfake Shield")
gr.Markdown("Test audio files via browser below. Live phone calls connect automatically through background Twilio webhooks.")
with gr.Row():
audio_input = gr.Audio(sources=["upload", "microphone"], type="numpy", label="Upload Voice Sample")
with gr.Row():
output_text = gr.Textbox(label="Classification Result")
output_score = gr.Number(label="Confidence Score (%)")
analyze_btn = gr.Button("Analyze Audio", variant="primary")
analyze_btn.click(fn=predict_audio_file, inputs=audio_input, outputs=[output_text, output_score])
# ==========================================
# 4. TWILIO & FLUTTER BACKEND (FastAPI)
# ==========================================
app = demo.app
@app.post("/incoming-call")
async def incoming_call(request: Request):
"""Twilio hits this webhook when an incoming call hits your phone number."""
host = request.url.hostname
ws_url = f"wss://{host}/media-stream"
twiml_response = f"""<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Security system active. Analyzing voice biometrics in real time.</Say>
<Connect>
<Stream url="{ws_url}" />
</Connect>
</Response>
"""
return HTMLResponse(content=twiml_response, media_type="application/xml")
@app.websocket("/media-stream")
async def handle_media_stream(websocket: WebSocket):
"""Twilio streams live raw call audio chunks here over WebSockets."""
await websocket.accept()
print("π LIVE CALL CONNECTED: Streaming carrier audio...")
audio_buffer = np.array([], dtype=np.float32)
EVAL_INTERVAL = 16000 * 2 # Evaluate every 2 seconds of accumulated audio
try:
while True:
message = await websocket.receive_text()
data = json.loads(message)
if data.get("event") == "media":
chunk_bytes = base64.b64decode(data["media"]["payload"])
pcm_chunk = np.frombuffer(chunk_bytes, dtype=np.int16).astype(np.float32) / 32768.0
audio_16k = librosa.resample(pcm_chunk, orig_sr=8000, target_sr=16000)
audio_buffer = np.concatenate([audio_buffer, audio_16k])
# Run sliding window evaluation when buffer hits interval threshold
if len(audio_buffer) >= EVAL_INTERVAL:
probs = run_model_inference(audio_buffer)
fake_prob = probs[1] if isinstance(probs, list) and len(probs) > 1 else probs
fake_confidence = round(fake_prob * 100, 2)
real_confidence = round((1.0 - fake_prob) * 100, 2)
is_fake = fake_prob > 0.60
score = fake_confidence if is_fake else real_confidence
if is_fake:
print(f"π¨ [LIVE FRAUD DETECTED] Synthetic Voice! Confidence: {score}%")
else:
print(f"β
[CLEAN CALL] Real Human Voice. Confidence: {score}%")
# Construct telemetry payload for Flutter dashboard
telemetry_payload = json.dumps({
"is_fake": is_fake,
"confidence_score": score
})
# Broadcast telemetry to all connected Flutter simulator clients
for client in list(connected_flutter_clients):
try:
await client.send_text(telemetry_payload)
except Exception:
connected_flutter_clients.remove(client)
# Slide window forward: keep last 0.5 seconds for context overlap
OVERLAP = int(16000 * 0.5)
audio_buffer = audio_buffer[-OVERLAP:]
elif data.get("event") == "stop":
print("π Call hung up.")
break
except Exception as e:
print(f"WebSocket error: {e}")
@app.websocket("/ui-dashboard-feed")
async def flutter_dashboard_feed(websocket: WebSocket):
"""WebSocket endpoint that connects your online Flutter simulator to receive live telemetry."""
await websocket.accept()
connected_flutter_clients.append(websocket)
print("π± Flutter Dashboard Client Connected!")
try:
while True:
await websocket.receive_text()
except Exception:
if websocket in connected_flutter_clients:
connected_flutter_clients.remove(websocket)
print("π± Flutter Dashboard Client Disconnected.")
# ==========================================
# 5. LAUNCH APPLICATION
# ==========================================
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860) |