Dev-ML12's picture
Upload app.py with huggingface_hub
f64d8fb verified
Raw History Blame Contribute Delete
3.53 kB
import json
import base64
import numpy as np
import audioop
from scipy.signal import resample
import onnxruntime as ort
from fastapi import FastAPI, WebSocket
import gradio as gr
app = FastAPI()
session = ort.InferenceSession("model.onnx")
@app.get("/")
def home():
return {"status": "Fraud Shield AI Engine is Active via Gradio Wrapper"}
# 3. Real-Time Telephony WebSocket Endpoint
@app.websocket("/media-stream")
async def handle_twilio_stream(websocket: WebSocket):
await websocket.accept()
print("🚀 Twilio Call Connected via Live Media Stream WebSocket")
audio_buffer = bytearray()
try:
while True:
# Twilio streams packets continuously over the WebSocket
message = await websocket.receive_text()
data = json.loads(message)
if data['event'] == 'media':
# Extract the 20ms base64 chunk
payload = data['media']['payload']
chunk = base64.b64decode(payload)
# Step A: Convert 8-bit Mu-Law telecom audio to 16-bit Linear PCM
pcm_data = audioop.ulaw2lin(chunk, 2)
audio_buffer.extend(pcm_data)
# Process audio continuously in 1-second sliding windows (16,000 bytes at 8kHz)
if len(audio_buffer) >= 16000:
raw_audio = np.frombuffer(audio_buffer, dtype=np.int16).astype(np.float32)
audio_buffer = bytearray() # Reset buffer for the next sliding frame
# Step B: Standardize the magnitude bounds
max_val = np.max(np.abs(raw_audio))
if max_val > 0:
raw_audio /= max_val
# Step C: Upsample the 8kHz Twilio track to the model's native 16kHz structure
num_samples = int(len(raw_audio) * (16000 / 8000))
resampled_audio = resample(raw_audio, num_samples).astype(np.float32)
# Step D: Run ONNX Real-Time Inference
inputs = {session.get_inputs()[0].name: np.expand_dims(resampled_audio, axis=0)}
outputs = session.run(None, inputs)
logits = outputs[0]
# Step E: Apply Softmax to extract the absolute Deepfake Confidence Score
exp_logits = np.exp(logits - np.max(logits))
probabilities = exp_logits / np.sum(exp_logits)
# Index 0 = Real (Bona Fide), Index 1 = Fake (Synthetic Deepfake)
fake_confidence = float(probabilities[0][1])
# Broadcast the real-time score out to the terminal console
verdict = "⚠️ DEEPFAKE DETECTED" if fake_confidence > 0.80 else "✅ HUMAN VERIFIED"
print(f"[{verdict}] Confidence Score: {fake_confidence * 100:.2f}%")
except Exception as e:
print(f"Call disconnected or stream ended: {e}")
finally:
await websocket.close()
with gr.Blocks() as ui:
gr.Markdown("# 🛡️ Fraud Shield Live Deepfake Detection Backend")
gr.Markdown("The background server is up and listening for Twilio WebSocket pipelines.")
app = gr.routes.mount_gradio_app(app, ui, path="/")