RT-test / app.py
Dev-ML12's picture
fixed probability issues on gradio
1772308
Raw History Blame Contribute Delete
7.57 kB
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)