Spaces:
Sleeping
Sleeping
VoiceGuard Bot commited on
Commit ·
f68a33a
0
Parent(s):
Deploy to Spaces
Browse files- .dockerignore +13 -0
- .railwayignore +20 -0
- Dockerfile +28 -0
- app.py +183 -0
- app/__init__.py +1 -0
- app/api/__init__.py +1 -0
- app/api/routes.py +198 -0
- app/api/schemas.py +140 -0
- app/config.py +43 -0
- app/core/__init__.py +1 -0
- app/core/audio_processor.py +201 -0
- app/core/detector.py +174 -0
- app/core/elevenlabs_detector.py +270 -0
- app/core/explainer.py +204 -0
- app/core/hybrid_detector.py +523 -0
- app/core/signal_analyzer.py +298 -0
- app/main.py +130 -0
- app/utils/__init__.py +1 -0
- app/visualization/__init__.py +1 -0
- app/visualization/heatmap.py +220 -0
- frontend/css/animations.css +192 -0
- frontend/css/base.css +207 -0
- frontend/css/components.css +555 -0
- frontend/css/variables.css +164 -0
- frontend/index.html +217 -0
- frontend/js/api.js +150 -0
- frontend/js/app.js +452 -0
- frontend/js/config.js +38 -0
- render.yaml +12 -0
- requirements.txt +25 -0
.dockerignore
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv/
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.pyc
|
| 4 |
+
.git/
|
| 5 |
+
.gitignore
|
| 6 |
+
*.mp3
|
| 7 |
+
*.wav
|
| 8 |
+
training/
|
| 9 |
+
tests/
|
| 10 |
+
.env
|
| 11 |
+
.env.example
|
| 12 |
+
*.md
|
| 13 |
+
diagnose_detection.py
|
.railwayignore
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Exclude large files from Railway deployment
|
| 2 |
+
venv/
|
| 3 |
+
__pycache__/
|
| 4 |
+
*.pyc
|
| 5 |
+
.git/
|
| 6 |
+
.gitignore
|
| 7 |
+
*.mp3
|
| 8 |
+
*.wav
|
| 9 |
+
training/
|
| 10 |
+
tests/
|
| 11 |
+
.env
|
| 12 |
+
.env.example
|
| 13 |
+
diagnose_detection.py
|
| 14 |
+
*.md
|
| 15 |
+
.cache/
|
| 16 |
+
~/.cache/
|
| 17 |
+
/Users/*/Library/
|
| 18 |
+
*.tar.gz
|
| 19 |
+
*.zip
|
| 20 |
+
node_modules/
|
Dockerfile
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Install system dependencies for audio processing
|
| 6 |
+
RUN apt-get update && apt-get install -y \
|
| 7 |
+
ffmpeg \
|
| 8 |
+
libsndfile1 \
|
| 9 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
+
|
| 11 |
+
# Copy requirements first for better caching
|
| 12 |
+
COPY requirements.txt .
|
| 13 |
+
|
| 14 |
+
# Install CPU-only PyTorch first (MUCH smaller than GPU version)
|
| 15 |
+
RUN pip install --no-cache-dir torch==2.1.2+cpu torchaudio==2.1.2+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html
|
| 16 |
+
|
| 17 |
+
# Install remaining requirements (skip torch/torchaudio since already installed)
|
| 18 |
+
RUN pip install --no-cache-dir -r requirements.txt || true
|
| 19 |
+
|
| 20 |
+
# Copy application code
|
| 21 |
+
COPY . .
|
| 22 |
+
|
| 23 |
+
# Expose port (documentary only, but good practice)
|
| 24 |
+
EXPOSE 8000
|
| 25 |
+
|
| 26 |
+
# Run the application using shell form to expand PORT variable
|
| 27 |
+
# Railway (and others) often inject a generic PORT variable
|
| 28 |
+
CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-7860}"]
|
app.py
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""VoiceGuard - AI Voice Detection App for HuggingFace Spaces."""
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
import torch
|
| 6 |
+
import librosa
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
import warnings
|
| 9 |
+
import tempfile
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
warnings.filterwarnings("ignore")
|
| 13 |
+
|
| 14 |
+
# Model Configuration
|
| 15 |
+
MODEL_NAME = "abhishtagatya/hubert-base-960h-itw-deepfake"
|
| 16 |
+
|
| 17 |
+
# Global classifier (loaded once)
|
| 18 |
+
classifier = None
|
| 19 |
+
|
| 20 |
+
def load_model():
|
| 21 |
+
"""Load the deepfake detection model."""
|
| 22 |
+
global classifier
|
| 23 |
+
if classifier is None:
|
| 24 |
+
print("🔄 Loading model...")
|
| 25 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 26 |
+
classifier = pipeline(
|
| 27 |
+
"audio-classification",
|
| 28 |
+
model=MODEL_NAME,
|
| 29 |
+
device=device
|
| 30 |
+
)
|
| 31 |
+
print(f"✅ Model loaded on {'GPU' if device == 0 else 'CPU'}")
|
| 32 |
+
return classifier
|
| 33 |
+
|
| 34 |
+
def detect_deepfake(audio_input):
|
| 35 |
+
"""
|
| 36 |
+
Detect if audio is AI-generated or human.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
audio_input: Tuple of (sample_rate, audio_array) from Gradio
|
| 40 |
+
|
| 41 |
+
Returns:
|
| 42 |
+
Dictionary with detection results
|
| 43 |
+
"""
|
| 44 |
+
if audio_input is None:
|
| 45 |
+
return "❌ Please upload or record an audio file."
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
# Load model
|
| 49 |
+
model = load_model()
|
| 50 |
+
|
| 51 |
+
# Handle Gradio audio input
|
| 52 |
+
sample_rate, audio_data = audio_input
|
| 53 |
+
|
| 54 |
+
# Convert to float32 and normalize
|
| 55 |
+
if audio_data.dtype == np.int16:
|
| 56 |
+
audio_data = audio_data.astype(np.float32) / 32768.0
|
| 57 |
+
elif audio_data.dtype == np.int32:
|
| 58 |
+
audio_data = audio_data.astype(np.float32) / 2147483648.0
|
| 59 |
+
|
| 60 |
+
# Convert stereo to mono
|
| 61 |
+
if len(audio_data.shape) > 1:
|
| 62 |
+
audio_data = np.mean(audio_data, axis=1)
|
| 63 |
+
|
| 64 |
+
# Resample to 16kHz if needed
|
| 65 |
+
if sample_rate != 16000:
|
| 66 |
+
audio_data = librosa.resample(audio_data, orig_sr=sample_rate, target_sr=16000)
|
| 67 |
+
|
| 68 |
+
# Save to temp file for pipeline
|
| 69 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
| 70 |
+
import soundfile as sf
|
| 71 |
+
sf.write(f.name, audio_data, 16000)
|
| 72 |
+
temp_path = f.name
|
| 73 |
+
|
| 74 |
+
# Run detection
|
| 75 |
+
results = model(temp_path)
|
| 76 |
+
|
| 77 |
+
# Clean up
|
| 78 |
+
os.unlink(temp_path)
|
| 79 |
+
|
| 80 |
+
# Parse results
|
| 81 |
+
scores = {r["label"]: r["score"] for r in results}
|
| 82 |
+
best = max(results, key=lambda x: x["score"])
|
| 83 |
+
label = best["label"].lower()
|
| 84 |
+
confidence = best["score"]
|
| 85 |
+
|
| 86 |
+
# Determine classification with 98% threshold
|
| 87 |
+
if "spoof" in label or "fake" in label:
|
| 88 |
+
if confidence >= 0.98:
|
| 89 |
+
classification = "🤖 AI-GENERATED"
|
| 90 |
+
emoji = "🚨"
|
| 91 |
+
else:
|
| 92 |
+
classification = "👤 LIKELY HUMAN"
|
| 93 |
+
emoji = "✅"
|
| 94 |
+
else:
|
| 95 |
+
classification = "👤 HUMAN"
|
| 96 |
+
emoji = "✅"
|
| 97 |
+
|
| 98 |
+
# Format output
|
| 99 |
+
result = f"""
|
| 100 |
+
## {emoji} Detection Result
|
| 101 |
+
|
| 102 |
+
### Classification: {classification}
|
| 103 |
+
### Confidence: {confidence*100:.2f}%
|
| 104 |
+
|
| 105 |
+
---
|
| 106 |
+
|
| 107 |
+
### Raw Scores:
|
| 108 |
+
"""
|
| 109 |
+
for label, score in scores.items():
|
| 110 |
+
bar = "█" * int(score * 20) + "░" * (20 - int(score * 20))
|
| 111 |
+
result += f"- **{label}**: {bar} {score*100:.1f}%\n"
|
| 112 |
+
|
| 113 |
+
result += f"""
|
| 114 |
+
---
|
| 115 |
+
|
| 116 |
+
### Model Used:
|
| 117 |
+
`{MODEL_NAME}`
|
| 118 |
+
|
| 119 |
+
### Device:
|
| 120 |
+
{'🖥️ GPU (CUDA)' if torch.cuda.is_available() else '💻 CPU'}
|
| 121 |
+
"""
|
| 122 |
+
|
| 123 |
+
return result
|
| 124 |
+
|
| 125 |
+
except Exception as e:
|
| 126 |
+
return f"❌ Error processing audio: {str(e)}"
|
| 127 |
+
|
| 128 |
+
# Create Gradio Interface
|
| 129 |
+
with gr.Blocks(title="VoiceGuard - AI Voice Detection", theme=gr.themes.Soft()) as demo:
|
| 130 |
+
gr.Markdown("""
|
| 131 |
+
# 🛡️ VoiceGuard - AI Voice Detection
|
| 132 |
+
|
| 133 |
+
Detect whether an audio sample is **AI-generated (deepfake)** or **genuine human speech**.
|
| 134 |
+
|
| 135 |
+
### Supported Languages
|
| 136 |
+
🇮🇳 Tamil | English | Hindi | Malayalam | Telugu
|
| 137 |
+
|
| 138 |
+
---
|
| 139 |
+
""")
|
| 140 |
+
|
| 141 |
+
with gr.Row():
|
| 142 |
+
with gr.Column():
|
| 143 |
+
audio_input = gr.Audio(
|
| 144 |
+
label="Upload or Record Audio",
|
| 145 |
+
sources=["upload", "microphone"],
|
| 146 |
+
type="numpy"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
detect_btn = gr.Button("🔍 Analyze Voice", variant="primary", size="lg")
|
| 150 |
+
|
| 151 |
+
gr.Markdown("""
|
| 152 |
+
### Tips:
|
| 153 |
+
- Upload MP3, WAV, or other audio formats
|
| 154 |
+
- Record directly using your microphone
|
| 155 |
+
- Minimum 1 second of audio recommended
|
| 156 |
+
""")
|
| 157 |
+
|
| 158 |
+
with gr.Column():
|
| 159 |
+
output = gr.Markdown(label="Detection Result")
|
| 160 |
+
|
| 161 |
+
detect_btn.click(
|
| 162 |
+
fn=detect_deepfake,
|
| 163 |
+
inputs=[audio_input],
|
| 164 |
+
outputs=[output]
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
gr.Markdown("""
|
| 168 |
+
---
|
| 169 |
+
|
| 170 |
+
### About
|
| 171 |
+
|
| 172 |
+
VoiceGuard uses the **HuBERT** model fine-tuned for deepfake audio detection.
|
| 173 |
+
|
| 174 |
+
**Model**: `abhishtagatya/hubert-base-960h-itw-deepfake`
|
| 175 |
+
|
| 176 |
+
**India AI Impact Summit Buildathon 2026**
|
| 177 |
+
""")
|
| 178 |
+
|
| 179 |
+
# Launch
|
| 180 |
+
if __name__ == "__main__":
|
| 181 |
+
# Pre-load model
|
| 182 |
+
load_model()
|
| 183 |
+
demo.launch()
|
app/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""VoiceGuard API Package."""
|
app/api/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""API Routes Package."""
|
app/api/routes.py
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""API Routes for VoiceGuard - Matching Hackathon Requirements Exactly.
|
| 2 |
+
|
| 3 |
+
Endpoint: POST /api/voice-detection
|
| 4 |
+
Auth: x-api-key header
|
| 5 |
+
Request: {language, audioFormat, audioBase64}
|
| 6 |
+
Response: {status, language, classification, confidenceScore, explanation}
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import time
|
| 10 |
+
from fastapi import APIRouter, HTTPException, Header, UploadFile, File, Form
|
| 11 |
+
from typing import Optional
|
| 12 |
+
|
| 13 |
+
from app.api.schemas import (
|
| 14 |
+
VoiceDetectionRequest,
|
| 15 |
+
VoiceDetectionResponse,
|
| 16 |
+
ErrorResponse,
|
| 17 |
+
HealthResponse
|
| 18 |
+
)
|
| 19 |
+
from app.core.audio_processor import (
|
| 20 |
+
audio_processor,
|
| 21 |
+
AudioProcessingError,
|
| 22 |
+
InvalidBase64Error,
|
| 23 |
+
InvalidAudioFormatError,
|
| 24 |
+
AudioTooShortError,
|
| 25 |
+
AudioTooLongError
|
| 26 |
+
)
|
| 27 |
+
from app.core.detector import detector
|
| 28 |
+
from app.core.hybrid_detector import hybrid_detector
|
| 29 |
+
from app.core.signal_analyzer import signal_analyzer
|
| 30 |
+
from app.core.explainer import explainer
|
| 31 |
+
from app.config import settings
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Create router
|
| 35 |
+
router = APIRouter()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# ============== API KEY VALIDATION ==============
|
| 39 |
+
|
| 40 |
+
def validate_api_key(x_api_key: Optional[str] = Header(None, alias="x-api-key")) -> str:
|
| 41 |
+
"""Validate API key from header."""
|
| 42 |
+
if not x_api_key:
|
| 43 |
+
raise HTTPException(
|
| 44 |
+
status_code=401,
|
| 45 |
+
detail={"status": "error", "message": "Missing API key. Use x-api-key header."}
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if x_api_key != settings.API_KEY:
|
| 49 |
+
raise HTTPException(
|
| 50 |
+
status_code=401,
|
| 51 |
+
detail={"status": "error", "message": "Invalid API key"}
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
return x_api_key
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# ============== LOGIC HELPER ==============
|
| 58 |
+
|
| 59 |
+
async def _process_detection_logic(
|
| 60 |
+
audio_bytes: bytes,
|
| 61 |
+
language: str
|
| 62 |
+
) -> VoiceDetectionResponse:
|
| 63 |
+
"""Core logic to process audio/bytes and generate response."""
|
| 64 |
+
start_time = time.time()
|
| 65 |
+
|
| 66 |
+
# Step 1: Process audio (bytes → convert → resample to 16kHz)
|
| 67 |
+
print(f"📝 Processing audio for language: {language}")
|
| 68 |
+
waveform, duration = audio_processor.process_bytes(audio_bytes)
|
| 69 |
+
print(f"✅ Audio processed: {duration:.2f}s duration")
|
| 70 |
+
|
| 71 |
+
# Step 2: Run ML model detection
|
| 72 |
+
print("🔍 Running deepfake detection...")
|
| 73 |
+
detection_result = detector.detect(waveform, language=language)
|
| 74 |
+
print(f"📊 Detection: {detection_result.classification} ({detection_result.confidence:.2%})")
|
| 75 |
+
|
| 76 |
+
# Step 3: Generate explanation
|
| 77 |
+
signal_scores = signal_analyzer.analyze(waveform)
|
| 78 |
+
artifacts = explainer.generate_artifacts(signal_scores, duration)
|
| 79 |
+
explanation = explainer.generate_explanation(
|
| 80 |
+
classification=detection_result.classification,
|
| 81 |
+
confidence=detection_result.confidence,
|
| 82 |
+
scores=signal_scores,
|
| 83 |
+
language=language,
|
| 84 |
+
artifacts=artifacts
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Calculate processing time
|
| 88 |
+
processing_time_ms = int((time.time() - start_time) * 1000)
|
| 89 |
+
print(f"⏱️ Total processing time: {processing_time_ms}ms")
|
| 90 |
+
|
| 91 |
+
# Build response matching exact hackathon format
|
| 92 |
+
return VoiceDetectionResponse(
|
| 93 |
+
status="success",
|
| 94 |
+
language=language.capitalize(),
|
| 95 |
+
classification=detection_result.classification,
|
| 96 |
+
confidenceScore=round(detection_result.confidence, 2),
|
| 97 |
+
explanation=explanation
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
# ============== MAIN DETECTION ENDPOINT (Reference Implementation) ==============
|
| 102 |
+
# Matches exact hackathon requirements (JSON Base64)
|
| 103 |
+
|
| 104 |
+
@router.post(
|
| 105 |
+
"/voice-detection",
|
| 106 |
+
response_model=VoiceDetectionResponse,
|
| 107 |
+
responses={
|
| 108 |
+
401: {"model": ErrorResponse, "description": "Invalid API key"},
|
| 109 |
+
400: {"model": ErrorResponse, "description": "Invalid input"},
|
| 110 |
+
500: {"model": ErrorResponse, "description": "Processing error"}
|
| 111 |
+
},
|
| 112 |
+
summary="Detect AI-Generated Voice (JSON)",
|
| 113 |
+
description="Analyze an MP3 audio file (Base64) to detect if it's AI-generated."
|
| 114 |
+
)
|
| 115 |
+
async def detect_voice_json(
|
| 116 |
+
request: VoiceDetectionRequest,
|
| 117 |
+
x_api_key: str = Header(..., alias="x-api-key")
|
| 118 |
+
):
|
| 119 |
+
"""Main detection endpoint (JSON/Base64)."""
|
| 120 |
+
validate_api_key(x_api_key)
|
| 121 |
+
|
| 122 |
+
try:
|
| 123 |
+
# Decode base64 to bytes locally to reuse common logic
|
| 124 |
+
# audio_processor.decode_base64 raises InvalidBase64Error
|
| 125 |
+
audio_bytes = audio_processor.decode_base64(request.audioBase64)
|
| 126 |
+
|
| 127 |
+
return await _process_detection_logic(audio_bytes, request.language)
|
| 128 |
+
|
| 129 |
+
except InvalidBase64Error as e:
|
| 130 |
+
raise HTTPException(
|
| 131 |
+
status_code=400,
|
| 132 |
+
detail={"status": "error", "message": f"Invalid Base64 encoding: {str(e)}"}
|
| 133 |
+
)
|
| 134 |
+
except AudioProcessingError as e:
|
| 135 |
+
raise HTTPException(
|
| 136 |
+
status_code=400,
|
| 137 |
+
detail={"status": "error", "message": str(e)}
|
| 138 |
+
)
|
| 139 |
+
except Exception as e:
|
| 140 |
+
print(f"❌ Error during detection: {e}")
|
| 141 |
+
raise HTTPException(
|
| 142 |
+
status_code=500,
|
| 143 |
+
detail={"status": "error", "message": f"Detection failed: {str(e)}"}
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
# ============== FILE UPLOAD ENDPOINT (For Testing/Ease of Use) ==============
|
| 148 |
+
|
| 149 |
+
@router.post(
|
| 150 |
+
"/voice-detection/file",
|
| 151 |
+
response_model=VoiceDetectionResponse,
|
| 152 |
+
responses={
|
| 153 |
+
401: {"model": ErrorResponse, "description": "Invalid API key"},
|
| 154 |
+
400: {"model": ErrorResponse, "description": "Invalid input"},
|
| 155 |
+
},
|
| 156 |
+
summary="Detect AI-Generated Voice (File Upload)",
|
| 157 |
+
description="Upload an audio file (MP3/WAV) to detect if it's AI-generated."
|
| 158 |
+
)
|
| 159 |
+
async def detect_voice_file(
|
| 160 |
+
file: UploadFile = File(..., description="Audio file (MP3/WAV)"),
|
| 161 |
+
language: str = Form(..., description="Language: Tamil, English, Hindi, Malayalam, Telugu"),
|
| 162 |
+
x_api_key: str = Header(..., alias="x-api-key")
|
| 163 |
+
):
|
| 164 |
+
"""File upload detection endpoint."""
|
| 165 |
+
validate_api_key(x_api_key)
|
| 166 |
+
|
| 167 |
+
try:
|
| 168 |
+
content = await file.read()
|
| 169 |
+
return await _process_detection_logic(content, language)
|
| 170 |
+
|
| 171 |
+
except AudioProcessingError as e:
|
| 172 |
+
raise HTTPException(
|
| 173 |
+
status_code=400,
|
| 174 |
+
detail={"status": "error", "message": str(e)}
|
| 175 |
+
)
|
| 176 |
+
except Exception as e:
|
| 177 |
+
print(f"❌ Error during detection: {e}")
|
| 178 |
+
raise HTTPException(
|
| 179 |
+
status_code=500,
|
| 180 |
+
detail={"status": "error", "message": f"Detection failed: {str(e)}"}
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
# ============== HEALTH CHECK (bonus) ==============
|
| 185 |
+
|
| 186 |
+
@router.get(
|
| 187 |
+
"/health",
|
| 188 |
+
response_model=HealthResponse,
|
| 189 |
+
summary="Health Check",
|
| 190 |
+
description="Check API health and model status"
|
| 191 |
+
)
|
| 192 |
+
async def health_check():
|
| 193 |
+
"""Health check endpoint."""
|
| 194 |
+
return HealthResponse(
|
| 195 |
+
status="healthy",
|
| 196 |
+
model_loaded=detector.is_loaded,
|
| 197 |
+
version="1.0.0"
|
| 198 |
+
)
|
app/api/schemas.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Pydantic Schemas for API Request/Response Validation.
|
| 2 |
+
|
| 3 |
+
Matches exact hackathon requirements:
|
| 4 |
+
- Request: language, audioFormat, audioBase64
|
| 5 |
+
- Response: status, language, classification, confidenceScore, explanation
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from pydantic import BaseModel, Field, field_validator
|
| 9 |
+
from typing import List, Optional, Dict
|
| 10 |
+
import base64
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# ============== REQUEST SCHEMAS ==============
|
| 14 |
+
|
| 15 |
+
class VoiceDetectionRequest(BaseModel):
|
| 16 |
+
"""Request schema matching hackathon requirements exactly."""
|
| 17 |
+
|
| 18 |
+
language: str = Field(
|
| 19 |
+
...,
|
| 20 |
+
description="Language of the audio: Tamil, English, Hindi, Malayalam, Telugu"
|
| 21 |
+
)
|
| 22 |
+
audioFormat: str = Field(
|
| 23 |
+
default="mp3",
|
| 24 |
+
description="Audio format (always mp3 per requirements)"
|
| 25 |
+
)
|
| 26 |
+
audioBase64: str = Field(
|
| 27 |
+
...,
|
| 28 |
+
description="Base64 encoded MP3 audio file",
|
| 29 |
+
min_length=100
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
@field_validator('audioBase64')
|
| 33 |
+
@classmethod
|
| 34 |
+
def validate_base64(cls, v: str) -> str:
|
| 35 |
+
"""Validate that audio is valid Base64."""
|
| 36 |
+
try:
|
| 37 |
+
decoded = base64.b64decode(v)
|
| 38 |
+
if len(decoded) < 100:
|
| 39 |
+
raise ValueError("Audio data too small")
|
| 40 |
+
return v
|
| 41 |
+
except Exception as e:
|
| 42 |
+
raise ValueError(f"Invalid Base64 encoding: {str(e)}")
|
| 43 |
+
|
| 44 |
+
@field_validator('language')
|
| 45 |
+
@classmethod
|
| 46 |
+
def validate_language(cls, v: str) -> str:
|
| 47 |
+
"""Validate language is supported (case-insensitive)."""
|
| 48 |
+
valid_languages = ['tamil', 'english', 'hindi', 'malayalam', 'telugu']
|
| 49 |
+
v_lower = v.lower()
|
| 50 |
+
if v_lower not in valid_languages:
|
| 51 |
+
raise ValueError(f"Language must be one of: Tamil, English, Hindi, Malayalam, Telugu")
|
| 52 |
+
return v_lower
|
| 53 |
+
|
| 54 |
+
@field_validator('audioFormat')
|
| 55 |
+
@classmethod
|
| 56 |
+
def validate_audio_format(cls, v: str) -> str:
|
| 57 |
+
"""Validate audio format is mp3."""
|
| 58 |
+
if v.lower() != "mp3":
|
| 59 |
+
raise ValueError("audioFormat must be 'mp3'")
|
| 60 |
+
return v.lower()
|
| 61 |
+
|
| 62 |
+
class Config:
|
| 63 |
+
json_schema_extra = {
|
| 64 |
+
"example": {
|
| 65 |
+
"language": "Tamil",
|
| 66 |
+
"audioFormat": "mp3",
|
| 67 |
+
"audioBase64": "SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2ZjU2LjM2LjEwMAAAAAAA..."
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# ============== RESPONSE SCHEMAS ==============
|
| 73 |
+
|
| 74 |
+
class VoiceDetectionResponse(BaseModel):
|
| 75 |
+
"""Success response matching hackathon requirements exactly."""
|
| 76 |
+
|
| 77 |
+
status: str = Field(
|
| 78 |
+
default="success",
|
| 79 |
+
description="Response status: success or error"
|
| 80 |
+
)
|
| 81 |
+
language: str = Field(
|
| 82 |
+
...,
|
| 83 |
+
description="Language of the audio"
|
| 84 |
+
)
|
| 85 |
+
classification: str = Field(
|
| 86 |
+
...,
|
| 87 |
+
description="AI_GENERATED or HUMAN"
|
| 88 |
+
)
|
| 89 |
+
confidenceScore: float = Field(
|
| 90 |
+
...,
|
| 91 |
+
ge=0,
|
| 92 |
+
le=1,
|
| 93 |
+
description="Confidence score between 0.0 and 1.0"
|
| 94 |
+
)
|
| 95 |
+
explanation: str = Field(
|
| 96 |
+
...,
|
| 97 |
+
description="Short reason for the decision"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
class Config:
|
| 101 |
+
json_schema_extra = {
|
| 102 |
+
"example": {
|
| 103 |
+
"status": "success",
|
| 104 |
+
"language": "Tamil",
|
| 105 |
+
"classification": "AI_GENERATED",
|
| 106 |
+
"confidenceScore": 0.91,
|
| 107 |
+
"explanation": "Unnatural pitch consistency and robotic speech patterns detected"
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
class ErrorResponse(BaseModel):
|
| 113 |
+
"""Error response matching hackathon requirements exactly."""
|
| 114 |
+
|
| 115 |
+
status: str = Field(
|
| 116 |
+
default="error",
|
| 117 |
+
description="Response status: error"
|
| 118 |
+
)
|
| 119 |
+
message: str = Field(
|
| 120 |
+
...,
|
| 121 |
+
description="Error message"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
class Config:
|
| 125 |
+
json_schema_extra = {
|
| 126 |
+
"example": {
|
| 127 |
+
"status": "error",
|
| 128 |
+
"message": "Invalid API key or malformed request"
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
# ============== HEALTH CHECK SCHEMA (bonus, not required) ==============
|
| 134 |
+
|
| 135 |
+
class HealthResponse(BaseModel):
|
| 136 |
+
"""Health check response."""
|
| 137 |
+
|
| 138 |
+
status: str = Field(default="healthy", description="Service health status")
|
| 139 |
+
model_loaded: bool = Field(..., description="Whether ML model is loaded")
|
| 140 |
+
version: str = Field(default="1.0.0", description="API version")
|
app/config.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""VoiceGuard Configuration Module."""
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from pydantic_settings import BaseSettings
|
| 5 |
+
from functools import lru_cache
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class Settings(BaseSettings):
|
| 9 |
+
"""Application settings loaded from environment variables."""
|
| 10 |
+
|
| 11 |
+
# Server
|
| 12 |
+
HOST: str = "0.0.0.0"
|
| 13 |
+
PORT: int = 8000
|
| 14 |
+
DEBUG: bool = True
|
| 15 |
+
|
| 16 |
+
# API Key Authentication (required by hackathon)
|
| 17 |
+
API_KEY: str = "sk_voiceguard_2026_secret"
|
| 18 |
+
|
| 19 |
+
# Model
|
| 20 |
+
MODEL_NAME: str = "MelodyMachine/Deepfake-audio-detection-V2"
|
| 21 |
+
MODEL_CACHE_DIR: str = "./model_cache"
|
| 22 |
+
|
| 23 |
+
# Audio Processing
|
| 24 |
+
TARGET_SAMPLE_RATE: int = 16000
|
| 25 |
+
MIN_DURATION: float = 1.0 # seconds
|
| 26 |
+
MAX_DURATION: float = 60.0 # seconds
|
| 27 |
+
MAX_FILE_SIZE: int = 10 * 1024 * 1024 # 10 MB
|
| 28 |
+
|
| 29 |
+
# Heatmap
|
| 30 |
+
HEATMAP_DIR: str = "./static/heatmaps"
|
| 31 |
+
|
| 32 |
+
class Config:
|
| 33 |
+
env_file = ".env"
|
| 34 |
+
env_file_encoding = "utf-8"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@lru_cache()
|
| 38 |
+
def get_settings() -> Settings:
|
| 39 |
+
"""Get cached settings instance."""
|
| 40 |
+
return Settings()
|
| 41 |
+
|
| 42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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Audio Processing Module.
|
| 2 |
+
|
| 3 |
+
Handles all audio-related operations:
|
| 4 |
+
- Base64 decoding
|
| 5 |
+
- MP3/WAV conversion
|
| 6 |
+
- Resampling to 16kHz
|
| 7 |
+
- Duration validation
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import base64
|
| 11 |
+
import io
|
| 12 |
+
import numpy as np
|
| 13 |
+
from typing import Tuple
|
| 14 |
+
from pydub import AudioSegment
|
| 15 |
+
import librosa
|
| 16 |
+
|
| 17 |
+
from app.config import settings
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class AudioProcessingError(Exception):
|
| 21 |
+
"""Base exception for audio processing errors."""
|
| 22 |
+
pass
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class InvalidBase64Error(AudioProcessingError):
|
| 26 |
+
"""Raised when Base64 decoding fails."""
|
| 27 |
+
pass
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class InvalidAudioFormatError(AudioProcessingError):
|
| 31 |
+
"""Raised when audio format is invalid."""
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class AudioTooShortError(AudioProcessingError):
|
| 36 |
+
"""Raised when audio is too short."""
|
| 37 |
+
pass
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class AudioTooLongError(AudioProcessingError):
|
| 41 |
+
"""Raised when audio is too long."""
|
| 42 |
+
pass
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class AudioProcessor:
|
| 46 |
+
"""Handles audio processing operations."""
|
| 47 |
+
|
| 48 |
+
def __init__(self, target_sr: int = None):
|
| 49 |
+
"""Initialize audio processor.
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
target_sr: Target sample rate (default from settings: 16000)
|
| 53 |
+
"""
|
| 54 |
+
self.target_sr = target_sr or settings.TARGET_SAMPLE_RATE
|
| 55 |
+
|
| 56 |
+
def decode_base64(self, b64_string: str) -> bytes:
|
| 57 |
+
"""Decode Base64 string to bytes.
|
| 58 |
+
|
| 59 |
+
Args:
|
| 60 |
+
b64_string: Base64 encoded audio string
|
| 61 |
+
|
| 62 |
+
Returns:
|
| 63 |
+
Raw audio bytes
|
| 64 |
+
|
| 65 |
+
Raises:
|
| 66 |
+
InvalidBase64Error: If decoding fails
|
| 67 |
+
"""
|
| 68 |
+
try:
|
| 69 |
+
return base64.b64decode(b64_string)
|
| 70 |
+
except Exception as e:
|
| 71 |
+
raise InvalidBase64Error(f"Failed to decode Base64: {str(e)}")
|
| 72 |
+
|
| 73 |
+
def load_audio_from_bytes(self, audio_bytes: bytes) -> AudioSegment:
|
| 74 |
+
"""Load audio from bytes (supports MP3, WAV).
|
| 75 |
+
|
| 76 |
+
Args:
|
| 77 |
+
audio_bytes: Raw audio file bytes
|
| 78 |
+
|
| 79 |
+
Returns:
|
| 80 |
+
AudioSegment object
|
| 81 |
+
|
| 82 |
+
Raises:
|
| 83 |
+
InvalidAudioFormatError: If format is not supported
|
| 84 |
+
"""
|
| 85 |
+
try:
|
| 86 |
+
# Try MP3 first
|
| 87 |
+
audio = AudioSegment.from_mp3(io.BytesIO(audio_bytes))
|
| 88 |
+
return audio
|
| 89 |
+
except Exception:
|
| 90 |
+
pass
|
| 91 |
+
|
| 92 |
+
try:
|
| 93 |
+
# Try WAV
|
| 94 |
+
audio = AudioSegment.from_wav(io.BytesIO(audio_bytes))
|
| 95 |
+
return audio
|
| 96 |
+
except Exception:
|
| 97 |
+
pass
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
+
# Try generic format detection
|
| 101 |
+
audio = AudioSegment.from_file(io.BytesIO(audio_bytes))
|
| 102 |
+
return audio
|
| 103 |
+
except Exception as e:
|
| 104 |
+
raise InvalidAudioFormatError(
|
| 105 |
+
f"Could not decode audio. Supported formats: MP3, WAV. Error: {str(e)}"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
def validate_duration(self, audio: AudioSegment) -> float:
|
| 109 |
+
"""Validate audio duration is within limits.
|
| 110 |
+
|
| 111 |
+
Args:
|
| 112 |
+
audio: AudioSegment object
|
| 113 |
+
|
| 114 |
+
Returns:
|
| 115 |
+
Duration in seconds
|
| 116 |
+
|
| 117 |
+
Raises:
|
| 118 |
+
AudioTooShortError: If duration < MIN_DURATION
|
| 119 |
+
AudioTooLongError: If duration > MAX_DURATION
|
| 120 |
+
"""
|
| 121 |
+
duration = len(audio) / 1000.0 # Convert ms to seconds
|
| 122 |
+
|
| 123 |
+
if duration < settings.MIN_DURATION:
|
| 124 |
+
raise AudioTooShortError(
|
| 125 |
+
f"Audio too short: {duration:.2f}s (minimum: {settings.MIN_DURATION}s)"
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
if duration > settings.MAX_DURATION:
|
| 129 |
+
raise AudioTooLongError(
|
| 130 |
+
f"Audio too long: {duration:.2f}s (maximum: {settings.MAX_DURATION}s)"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
return duration
|
| 134 |
+
|
| 135 |
+
def convert_to_wav_buffer(self, audio: AudioSegment) -> io.BytesIO:
|
| 136 |
+
"""Convert AudioSegment to WAV buffer.
|
| 137 |
+
|
| 138 |
+
Args:
|
| 139 |
+
audio: AudioSegment object
|
| 140 |
+
|
| 141 |
+
Returns:
|
| 142 |
+
BytesIO buffer containing WAV data
|
| 143 |
+
"""
|
| 144 |
+
wav_buffer = io.BytesIO()
|
| 145 |
+
audio.export(wav_buffer, format="wav")
|
| 146 |
+
wav_buffer.seek(0)
|
| 147 |
+
return wav_buffer
|
| 148 |
+
|
| 149 |
+
def resample_to_numpy(self, wav_buffer: io.BytesIO) -> np.ndarray:
|
| 150 |
+
"""Load WAV and resample to target sample rate.
|
| 151 |
+
|
| 152 |
+
Args:
|
| 153 |
+
wav_buffer: BytesIO buffer with WAV data
|
| 154 |
+
|
| 155 |
+
Returns:
|
| 156 |
+
Numpy array of audio samples (mono, resampled)
|
| 157 |
+
"""
|
| 158 |
+
waveform, _ = librosa.load(
|
| 159 |
+
wav_buffer,
|
| 160 |
+
sr=self.target_sr,
|
| 161 |
+
mono=True
|
| 162 |
+
)
|
| 163 |
+
return waveform
|
| 164 |
+
|
| 165 |
+
def process(self, b64_audio: str) -> Tuple[np.ndarray, float]:
|
| 166 |
+
"""Full audio processing pipeline (Base64 input).
|
| 167 |
+
|
| 168 |
+
Decodes Base64 -> Loads audio -> Validates -> Converts -> Resamples
|
| 169 |
+
"""
|
| 170 |
+
# Step 1: Decode Base64
|
| 171 |
+
audio_bytes = self.decode_base64(b64_audio)
|
| 172 |
+
return self.process_bytes(audio_bytes)
|
| 173 |
+
|
| 174 |
+
def process_bytes(self, audio_bytes: bytes) -> Tuple[np.ndarray, float]:
|
| 175 |
+
"""Full audio processing pipeline (Bytes input).
|
| 176 |
+
|
| 177 |
+
Loads audio -> Validates -> Converts -> Resamples
|
| 178 |
+
|
| 179 |
+
Args:
|
| 180 |
+
audio_bytes: Raw audio bytes
|
| 181 |
+
|
| 182 |
+
Returns:
|
| 183 |
+
Tuple of (waveform numpy array, duration in seconds)
|
| 184 |
+
"""
|
| 185 |
+
# Step 2: Load audio
|
| 186 |
+
audio_segment = self.load_audio_from_bytes(audio_bytes)
|
| 187 |
+
|
| 188 |
+
# Step 3: Validate duration
|
| 189 |
+
duration = self.validate_duration(audio_segment)
|
| 190 |
+
|
| 191 |
+
# Step 4: Convert to WAV
|
| 192 |
+
wav_buffer = self.convert_to_wav_buffer(audio_segment)
|
| 193 |
+
|
| 194 |
+
# Step 5: Resample to numpy array
|
| 195 |
+
waveform = self.resample_to_numpy(wav_buffer)
|
| 196 |
+
|
| 197 |
+
return waveform, duration
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
# Create singleton instance
|
| 201 |
+
audio_processor = AudioProcessor()
|
app/core/detector.py
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Deepfake Detection Module.
|
| 2 |
+
|
| 3 |
+
Using abhishtagatya/hubert-base-960h-itw-deepfake
|
| 4 |
+
Fine-tuned HuBERT model that correctly distinguishes human from AI voices
|
| 5 |
+
Supports: Multiple languages including Indian languages
|
| 6 |
+
Labels: bona-fide (human) / spoof (AI)
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import numpy as np
|
| 11 |
+
from typing import Dict
|
| 12 |
+
from dataclasses import dataclass
|
| 13 |
+
from transformers import pipeline
|
| 14 |
+
import warnings
|
| 15 |
+
|
| 16 |
+
# Suppress transformer warnings
|
| 17 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Model Configuration - Using balanced deepfake detector
|
| 21 |
+
MODEL_CONFIG = {
|
| 22 |
+
"name": "abhishtagatya/hubert-base-960h-itw-deepfake",
|
| 23 |
+
"description": "Fine-tuned HuBERT model for balanced AI voice detection",
|
| 24 |
+
"size": "~378MB",
|
| 25 |
+
"languages": ["Tamil", "English", "Hindi", "Malayalam", "Telugu"],
|
| 26 |
+
"accuracy": "Tested: Human=99.83%, AI=99.79%"
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@dataclass
|
| 31 |
+
class DetectionResult:
|
| 32 |
+
"""Result of deepfake detection."""
|
| 33 |
+
classification: str # "AI_GENERATED" or "HUMAN"
|
| 34 |
+
confidence: float # 0.0 - 1.0
|
| 35 |
+
raw_scores: Dict[str, float] # All label scores
|
| 36 |
+
model_used: str # Which model was used
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class DeepfakeDetector:
|
| 40 |
+
"""Deepfake audio detector using Gustking XLSR model."""
|
| 41 |
+
|
| 42 |
+
def __init__(self):
|
| 43 |
+
"""Initialize detector with lazy loading."""
|
| 44 |
+
self.device = self._get_optimal_device()
|
| 45 |
+
self.pipeline = None
|
| 46 |
+
self._loaded = False
|
| 47 |
+
|
| 48 |
+
def _get_optimal_device(self) -> str:
|
| 49 |
+
"""Get the best available device."""
|
| 50 |
+
if torch.backends.mps.is_available():
|
| 51 |
+
return "mps"
|
| 52 |
+
elif torch.cuda.is_available():
|
| 53 |
+
return "cuda"
|
| 54 |
+
return "cpu"
|
| 55 |
+
|
| 56 |
+
def _get_device_arg(self):
|
| 57 |
+
"""Get device argument for pipeline."""
|
| 58 |
+
if self.device == "cuda":
|
| 59 |
+
return 0
|
| 60 |
+
elif self.device == "cpu":
|
| 61 |
+
return -1
|
| 62 |
+
else:
|
| 63 |
+
return self.device # mps
|
| 64 |
+
|
| 65 |
+
def load_model(self) -> None:
|
| 66 |
+
"""Load the Gustking XLSR deepfake detector."""
|
| 67 |
+
if self._loaded:
|
| 68 |
+
return
|
| 69 |
+
|
| 70 |
+
model_name = MODEL_CONFIG["name"]
|
| 71 |
+
print(f"🔵 Loading model: {model_name}")
|
| 72 |
+
print(f" Size: {MODEL_CONFIG['size']}")
|
| 73 |
+
print(f" Device: {self.device}")
|
| 74 |
+
print(f" This may take a few minutes on first download...")
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
self.pipeline = pipeline(
|
| 78 |
+
"audio-classification",
|
| 79 |
+
model=model_name,
|
| 80 |
+
device=self._get_device_arg()
|
| 81 |
+
)
|
| 82 |
+
self._loaded = True
|
| 83 |
+
print("✅ Model loaded successfully!")
|
| 84 |
+
print(f" Labels: {self.pipeline.model.config.id2label}")
|
| 85 |
+
except Exception as e:
|
| 86 |
+
raise RuntimeError(f"Failed to load model: {e}")
|
| 87 |
+
|
| 88 |
+
def detect(self, waveform: np.ndarray, language: str = "english") -> DetectionResult:
|
| 89 |
+
"""
|
| 90 |
+
Detect if audio is AI-generated or human.
|
| 91 |
+
|
| 92 |
+
Args:
|
| 93 |
+
waveform: Audio samples as numpy array (16kHz mono)
|
| 94 |
+
language: Language of the audio (for logging)
|
| 95 |
+
|
| 96 |
+
Returns:
|
| 97 |
+
DetectionResult with classification and confidence
|
| 98 |
+
"""
|
| 99 |
+
# Load model if not already loaded
|
| 100 |
+
if not self._loaded:
|
| 101 |
+
self.load_model()
|
| 102 |
+
|
| 103 |
+
print(f"📊 Analyzing audio for language: {language}")
|
| 104 |
+
|
| 105 |
+
# Run inference
|
| 106 |
+
results = self.pipeline(waveform, sampling_rate=16000)
|
| 107 |
+
|
| 108 |
+
return self._parse_results(results)
|
| 109 |
+
|
| 110 |
+
def _parse_results(self, results: list) -> DetectionResult:
|
| 111 |
+
"""Parse pipeline results into DetectionResult."""
|
| 112 |
+
# Build scores dictionary
|
| 113 |
+
raw_scores = {r["label"]: r["score"] for r in results}
|
| 114 |
+
|
| 115 |
+
print(f" Raw scores: {raw_scores}")
|
| 116 |
+
|
| 117 |
+
# Find the best label
|
| 118 |
+
best = max(results, key=lambda x: x["score"])
|
| 119 |
+
label = best["label"].upper()
|
| 120 |
+
confidence = best["score"]
|
| 121 |
+
|
| 122 |
+
# Determine initial classification from label
|
| 123 |
+
label_lower = label.lower()
|
| 124 |
+
|
| 125 |
+
if "fake" in label_lower or "spoof" in label_lower or "synthetic" in label_lower or "deepfake" in label_lower:
|
| 126 |
+
initial_classification = "AI_GENERATED"
|
| 127 |
+
elif "real" in label_lower or "bona-fide" in label_lower or "bonafide" in label_lower or "human" in label_lower or "genuine" in label_lower:
|
| 128 |
+
initial_classification = "HUMAN"
|
| 129 |
+
else:
|
| 130 |
+
print(f" ⚠️ Unknown label: {label}, defaulting to HUMAN")
|
| 131 |
+
initial_classification = "HUMAN"
|
| 132 |
+
|
| 133 |
+
# CONFIDENCE THRESHOLDING to reduce false positives
|
| 134 |
+
# Only classify as AI_GENERATED if confidence is very high (>= 98%)
|
| 135 |
+
# This reduces false positives on real human voices
|
| 136 |
+
AI_CONFIDENCE_THRESHOLD = 0.98
|
| 137 |
+
|
| 138 |
+
if initial_classification == "AI_GENERATED":
|
| 139 |
+
if confidence >= AI_CONFIDENCE_THRESHOLD:
|
| 140 |
+
classification = "AI_GENERATED"
|
| 141 |
+
print(f" ✅ High confidence AI detection ({confidence:.2%} >= {AI_CONFIDENCE_THRESHOLD:.0%})")
|
| 142 |
+
else:
|
| 143 |
+
# Confidence too low - could be false positive
|
| 144 |
+
classification = "HUMAN"
|
| 145 |
+
print(f" ⚠️ Low confidence AI detection ({confidence:.2%} < {AI_CONFIDENCE_THRESHOLD:.0%}) -> Defaulting to HUMAN")
|
| 146 |
+
else:
|
| 147 |
+
classification = initial_classification
|
| 148 |
+
|
| 149 |
+
return DetectionResult(
|
| 150 |
+
classification=classification,
|
| 151 |
+
confidence=confidence,
|
| 152 |
+
raw_scores=raw_scores,
|
| 153 |
+
model_used=MODEL_CONFIG["name"]
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
@property
|
| 157 |
+
def is_loaded(self) -> bool:
|
| 158 |
+
"""Check if model is loaded."""
|
| 159 |
+
return self._loaded
|
| 160 |
+
|
| 161 |
+
def get_model_info(self) -> dict:
|
| 162 |
+
"""Get information about the model."""
|
| 163 |
+
return {
|
| 164 |
+
"name": MODEL_CONFIG["name"],
|
| 165 |
+
"description": MODEL_CONFIG["description"],
|
| 166 |
+
"size": MODEL_CONFIG["size"],
|
| 167 |
+
"languages": MODEL_CONFIG["languages"],
|
| 168 |
+
"device": self.device,
|
| 169 |
+
"loaded": self._loaded
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
# Singleton instance
|
| 174 |
+
detector = DeepfakeDetector()
|
app/core/elevenlabs_detector.py
ADDED
|
@@ -0,0 +1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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""ElevenLabs-Specific Deepfake Detection Module.
|
| 2 |
+
|
| 3 |
+
This module is specifically tuned to detect AI voices generated by:
|
| 4 |
+
- ElevenLabs (premium models)
|
| 5 |
+
- Similar neural vocoder-based TTS systems
|
| 6 |
+
- Modern AI voice cloning tools
|
| 7 |
+
|
| 8 |
+
Key detection signals for ElevenLabs-style voices:
|
| 9 |
+
1. Neural vocoder artifacts (subtle but detectable)
|
| 10 |
+
2. Unnaturally consistent prosody
|
| 11 |
+
3. Missing breath sounds or unnatural breath patterns
|
| 12 |
+
4. Spectral smoothness in high frequencies
|
| 13 |
+
5. Lack of micro-variations in pitch (shimmer/jitter)
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import numpy as np
|
| 17 |
+
from typing import Dict, Tuple, List
|
| 18 |
+
from dataclasses import dataclass
|
| 19 |
+
import librosa
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@dataclass
|
| 23 |
+
class ElevenLabsDetectionResult:
|
| 24 |
+
"""Result from ElevenLabs-specific detection."""
|
| 25 |
+
classification: str # "AI_GENERATED" or "HUMAN"
|
| 26 |
+
confidence: float
|
| 27 |
+
signals: Dict[str, Dict]
|
| 28 |
+
explanation: str
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class ElevenLabsDetector:
|
| 32 |
+
"""
|
| 33 |
+
Specialized detector for ElevenLabs and similar modern AI voices.
|
| 34 |
+
|
| 35 |
+
Uses aggressive thresholds tuned for neural vocoder-based synthesis.
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
def __init__(self, sr: int = 16000):
|
| 39 |
+
self.sr = sr
|
| 40 |
+
|
| 41 |
+
# Thresholds tuned for ElevenLabs detection
|
| 42 |
+
# These are based on known characteristics of neural vocoder voices
|
| 43 |
+
self.thresholds = {
|
| 44 |
+
# Pitch analysis - ElevenLabs has very stable pitch
|
| 45 |
+
"pitch_std_low": 25, # Human typically > 30
|
| 46 |
+
"jitter_low": 3.0, # Human typically > 5
|
| 47 |
+
|
| 48 |
+
# Spectral analysis - ElevenLabs has smooth spectra
|
| 49 |
+
"spectral_flatness_high": 0.15, # Human typically < 0.1
|
| 50 |
+
"mfcc_delta_std_low": 2.0, # Human typically > 3
|
| 51 |
+
|
| 52 |
+
# Energy dynamics - ElevenLabs has consistent energy
|
| 53 |
+
"rms_cv_low": 0.4, # Coefficient of variation, human > 0.5
|
| 54 |
+
|
| 55 |
+
# High-frequency content - Neural vocoders attenuate HF
|
| 56 |
+
"hf_energy_low": 0.05, # Human typically > 0.08
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
def detect(self, waveform: np.ndarray, language: str = "english") -> ElevenLabsDetectionResult:
|
| 60 |
+
"""
|
| 61 |
+
Detect if audio is generated by ElevenLabs or similar AI.
|
| 62 |
+
|
| 63 |
+
Args:
|
| 64 |
+
waveform: Audio samples (16kHz mono)
|
| 65 |
+
language: Language for logging
|
| 66 |
+
|
| 67 |
+
Returns:
|
| 68 |
+
ElevenLabsDetectionResult with classification
|
| 69 |
+
"""
|
| 70 |
+
print(f"🔬 Running ElevenLabs-specific detection for: {language}")
|
| 71 |
+
|
| 72 |
+
signals = {}
|
| 73 |
+
ai_indicators = 0
|
| 74 |
+
human_indicators = 0
|
| 75 |
+
reasons = []
|
| 76 |
+
|
| 77 |
+
# ============ 1. PITCH ANALYSIS ============
|
| 78 |
+
print(" 🎵 Analyzing pitch stability...")
|
| 79 |
+
try:
|
| 80 |
+
f0, voiced, _ = librosa.pyin(
|
| 81 |
+
waveform,
|
| 82 |
+
fmin=50,
|
| 83 |
+
fmax=500,
|
| 84 |
+
sr=self.sr,
|
| 85 |
+
frame_length=2048
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
f0_voiced = f0[~np.isnan(f0)]
|
| 89 |
+
|
| 90 |
+
if len(f0_voiced) > 20:
|
| 91 |
+
pitch_std = np.std(f0_voiced)
|
| 92 |
+
pitch_diff = np.diff(f0_voiced)
|
| 93 |
+
jitter = np.mean(np.abs(pitch_diff))
|
| 94 |
+
|
| 95 |
+
signals["pitch"] = {
|
| 96 |
+
"std": float(pitch_std),
|
| 97 |
+
"jitter": float(jitter),
|
| 98 |
+
"is_ai": pitch_std < self.thresholds["pitch_std_low"] or jitter < self.thresholds["jitter_low"]
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
if pitch_std < self.thresholds["pitch_std_low"]:
|
| 102 |
+
ai_indicators += 2 # Strong signal
|
| 103 |
+
reasons.append(f"unnaturally stable pitch (std={pitch_std:.1f})")
|
| 104 |
+
else:
|
| 105 |
+
human_indicators += 1
|
| 106 |
+
|
| 107 |
+
if jitter < self.thresholds["jitter_low"]:
|
| 108 |
+
ai_indicators += 2 # Strong signal
|
| 109 |
+
reasons.append(f"missing pitch micro-variations (jitter={jitter:.2f})")
|
| 110 |
+
else:
|
| 111 |
+
human_indicators += 1
|
| 112 |
+
|
| 113 |
+
print(f" Pitch std: {pitch_std:.2f} (threshold: {self.thresholds['pitch_std_low']})")
|
| 114 |
+
print(f" Jitter: {jitter:.2f} (threshold: {self.thresholds['jitter_low']})")
|
| 115 |
+
else:
|
| 116 |
+
signals["pitch"] = {"error": "insufficient voiced segments"}
|
| 117 |
+
|
| 118 |
+
except Exception as e:
|
| 119 |
+
signals["pitch"] = {"error": str(e)}
|
| 120 |
+
print(f" ⚠️ Pitch analysis error: {e}")
|
| 121 |
+
|
| 122 |
+
# ============ 2. SPECTRAL SMOOTHNESS ============
|
| 123 |
+
print(" 📊 Analyzing spectral characteristics...")
|
| 124 |
+
try:
|
| 125 |
+
# MFCC delta (rate of change) - AI voices have smoother changes
|
| 126 |
+
mfccs = librosa.feature.mfcc(y=waveform, sr=self.sr, n_mfcc=13)
|
| 127 |
+
mfcc_delta = librosa.feature.delta(mfccs)
|
| 128 |
+
mfcc_delta_std = np.std(mfcc_delta)
|
| 129 |
+
|
| 130 |
+
# Spectral flatness
|
| 131 |
+
flatness = librosa.feature.spectral_flatness(y=waveform)[0]
|
| 132 |
+
flatness_mean = np.mean(flatness)
|
| 133 |
+
|
| 134 |
+
signals["spectral"] = {
|
| 135 |
+
"mfcc_delta_std": float(mfcc_delta_std),
|
| 136 |
+
"flatness_mean": float(flatness_mean),
|
| 137 |
+
"is_ai": mfcc_delta_std < self.thresholds["mfcc_delta_std_low"]
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
if mfcc_delta_std < self.thresholds["mfcc_delta_std_low"]:
|
| 141 |
+
ai_indicators += 2
|
| 142 |
+
reasons.append(f"overly smooth spectral transitions (delta_std={mfcc_delta_std:.2f})")
|
| 143 |
+
else:
|
| 144 |
+
human_indicators += 1
|
| 145 |
+
|
| 146 |
+
print(f" MFCC delta std: {mfcc_delta_std:.2f} (threshold: {self.thresholds['mfcc_delta_std_low']})")
|
| 147 |
+
print(f" Spectral flatness: {flatness_mean:.4f}")
|
| 148 |
+
|
| 149 |
+
except Exception as e:
|
| 150 |
+
signals["spectral"] = {"error": str(e)}
|
| 151 |
+
print(f" ⚠️ Spectral analysis error: {e}")
|
| 152 |
+
|
| 153 |
+
# ============ 3. ENERGY DYNAMICS ============
|
| 154 |
+
print(" 📈 Analyzing energy dynamics...")
|
| 155 |
+
try:
|
| 156 |
+
rms = librosa.feature.rms(y=waveform)[0]
|
| 157 |
+
rms_mean = np.mean(rms)
|
| 158 |
+
rms_std = np.std(rms)
|
| 159 |
+
rms_cv = rms_std / (rms_mean + 1e-10) # Coefficient of variation
|
| 160 |
+
|
| 161 |
+
signals["energy"] = {
|
| 162 |
+
"rms_cv": float(rms_cv),
|
| 163 |
+
"is_ai": rms_cv < self.thresholds["rms_cv_low"]
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
if rms_cv < self.thresholds["rms_cv_low"]:
|
| 167 |
+
ai_indicators += 1
|
| 168 |
+
reasons.append(f"unnaturally consistent energy (CV={rms_cv:.2f})")
|
| 169 |
+
else:
|
| 170 |
+
human_indicators += 1
|
| 171 |
+
|
| 172 |
+
print(f" RMS CV: {rms_cv:.2f} (threshold: {self.thresholds['rms_cv_low']})")
|
| 173 |
+
|
| 174 |
+
except Exception as e:
|
| 175 |
+
signals["energy"] = {"error": str(e)}
|
| 176 |
+
print(f" ⚠️ Energy analysis error: {e}")
|
| 177 |
+
|
| 178 |
+
# ============ 4. HIGH-FREQUENCY CONTENT ============
|
| 179 |
+
print(" 🔊 Analyzing high-frequency content...")
|
| 180 |
+
try:
|
| 181 |
+
stft = np.abs(librosa.stft(waveform))
|
| 182 |
+
freq_bins = stft.shape[0]
|
| 183 |
+
|
| 184 |
+
# Neural vocoders often attenuate high frequencies
|
| 185 |
+
hf_energy = np.mean(stft[int(freq_bins * 0.7):, :])
|
| 186 |
+
total_energy = np.mean(stft) + 1e-10
|
| 187 |
+
hf_ratio = hf_energy / total_energy
|
| 188 |
+
|
| 189 |
+
signals["hf_content"] = {
|
| 190 |
+
"hf_ratio": float(hf_ratio),
|
| 191 |
+
"is_ai": hf_ratio < self.thresholds["hf_energy_low"]
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
if hf_ratio < self.thresholds["hf_energy_low"]:
|
| 195 |
+
ai_indicators += 1
|
| 196 |
+
reasons.append(f"reduced high-frequency content (ratio={hf_ratio:.3f})")
|
| 197 |
+
else:
|
| 198 |
+
human_indicators += 1
|
| 199 |
+
|
| 200 |
+
print(f" HF ratio: {hf_ratio:.4f} (threshold: {self.thresholds['hf_energy_low']})")
|
| 201 |
+
|
| 202 |
+
except Exception as e:
|
| 203 |
+
signals["hf_content"] = {"error": str(e)}
|
| 204 |
+
print(f" ⚠️ HF analysis error: {e}")
|
| 205 |
+
|
| 206 |
+
# ============ 5. BREATH DETECTION ============
|
| 207 |
+
print(" 💨 Analyzing breath patterns...")
|
| 208 |
+
try:
|
| 209 |
+
# Look for characteristic breath sounds (fricative noise)
|
| 210 |
+
# Breath sounds have high ZCR and low energy
|
| 211 |
+
zcr = librosa.feature.zero_crossing_rate(waveform)[0]
|
| 212 |
+
rms = librosa.feature.rms(y=waveform)[0]
|
| 213 |
+
|
| 214 |
+
# Potential breath frames: high ZCR, low RMS
|
| 215 |
+
zcr_threshold = np.percentile(zcr, 80)
|
| 216 |
+
rms_threshold = np.percentile(rms, 30)
|
| 217 |
+
|
| 218 |
+
potential_breaths = (zcr > zcr_threshold) & (rms < rms_threshold)
|
| 219 |
+
breath_ratio = np.sum(potential_breaths) / len(potential_breaths)
|
| 220 |
+
|
| 221 |
+
signals["breath"] = {
|
| 222 |
+
"breath_ratio": float(breath_ratio),
|
| 223 |
+
"is_ai": breath_ratio < 0.02 # Very few breath-like segments
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
if breath_ratio < 0.02:
|
| 227 |
+
ai_indicators += 1
|
| 228 |
+
reasons.append("missing natural breath patterns")
|
| 229 |
+
else:
|
| 230 |
+
human_indicators += 1
|
| 231 |
+
|
| 232 |
+
print(f" Breath ratio: {breath_ratio:.4f}")
|
| 233 |
+
|
| 234 |
+
except Exception as e:
|
| 235 |
+
signals["breath"] = {"error": str(e)}
|
| 236 |
+
print(f" ⚠️ Breath analysis error: {e}")
|
| 237 |
+
|
| 238 |
+
# ============ FINAL DECISION ============
|
| 239 |
+
total_signals = ai_indicators + human_indicators
|
| 240 |
+
if total_signals > 0:
|
| 241 |
+
ai_confidence = ai_indicators / total_signals
|
| 242 |
+
else:
|
| 243 |
+
ai_confidence = 0.5
|
| 244 |
+
|
| 245 |
+
# Decision threshold - bias toward detection (better to flag AI than miss it)
|
| 246 |
+
is_ai = ai_indicators >= 3 or ai_confidence >= 0.5
|
| 247 |
+
|
| 248 |
+
if is_ai:
|
| 249 |
+
classification = "AI_GENERATED"
|
| 250 |
+
confidence = min(0.6 + (ai_indicators * 0.1), 0.95)
|
| 251 |
+
explanation = f"AI voice detected: {'; '.join(reasons)}" if reasons else "Multiple AI indicators detected"
|
| 252 |
+
else:
|
| 253 |
+
classification = "HUMAN"
|
| 254 |
+
confidence = min(0.6 + (human_indicators * 0.1), 0.95)
|
| 255 |
+
explanation = "Human voice patterns confirmed"
|
| 256 |
+
|
| 257 |
+
print(f" ✅ Result: {classification} ({confidence:.2%})")
|
| 258 |
+
print(f" 📊 AI indicators: {ai_indicators}, Human indicators: {human_indicators}")
|
| 259 |
+
print(f" 📝 {explanation}")
|
| 260 |
+
|
| 261 |
+
return ElevenLabsDetectionResult(
|
| 262 |
+
classification=classification,
|
| 263 |
+
confidence=confidence,
|
| 264 |
+
signals=signals,
|
| 265 |
+
explanation=explanation
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
# Singleton instance
|
| 270 |
+
elevenlabs_detector = ElevenLabsDetector()
|
app/core/explainer.py
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Explanation Generator Module.
|
| 2 |
+
|
| 3 |
+
Generates human-readable explanations for detection results.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from typing import List, Dict
|
| 7 |
+
from dataclasses import dataclass
|
| 8 |
+
|
| 9 |
+
from app.core.signal_analyzer import SignalScores
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@dataclass
|
| 13 |
+
class Artifact:
|
| 14 |
+
"""Detected artifact in audio."""
|
| 15 |
+
timestamp: float
|
| 16 |
+
type: str
|
| 17 |
+
severity: str # "high", "medium", "low"
|
| 18 |
+
|
| 19 |
+
def to_dict(self) -> Dict:
|
| 20 |
+
return {
|
| 21 |
+
"timestamp": self.timestamp,
|
| 22 |
+
"type": self.type,
|
| 23 |
+
"severity": self.severity
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class ExplanationGenerator:
|
| 28 |
+
"""Generates explanations for detection results."""
|
| 29 |
+
|
| 30 |
+
# Thresholds for scoring
|
| 31 |
+
HIGH_THRESHOLD = 0.8
|
| 32 |
+
MEDIUM_THRESHOLD = 0.6
|
| 33 |
+
|
| 34 |
+
# Explanation templates
|
| 35 |
+
SIGNAL_EXPLANATIONS = {
|
| 36 |
+
"prosody": {
|
| 37 |
+
"high": "Unnatural rhythm patterns detected - timing is too consistent",
|
| 38 |
+
"medium": "Some rhythm irregularities noted",
|
| 39 |
+
"low": "Natural speech rhythm detected"
|
| 40 |
+
},
|
| 41 |
+
"breath": {
|
| 42 |
+
"high": "Missing natural breath sounds throughout the audio",
|
| 43 |
+
"medium": "Reduced breath patterns compared to natural speech",
|
| 44 |
+
"low": "Normal breathing patterns present"
|
| 45 |
+
},
|
| 46 |
+
"spectral": {
|
| 47 |
+
"high": "Vocoder artifacts detected in frequency spectrum",
|
| 48 |
+
"medium": "Some spectral irregularities detected",
|
| 49 |
+
"low": "Natural spectral characteristics"
|
| 50 |
+
},
|
| 51 |
+
"formant": {
|
| 52 |
+
"high": "Unnatural vocal formant transitions",
|
| 53 |
+
"medium": "Some formant anomalies detected",
|
| 54 |
+
"low": "Natural vocal transitions"
|
| 55 |
+
},
|
| 56 |
+
"silence": {
|
| 57 |
+
"high": "Artificially perfect silence patterns",
|
| 58 |
+
"medium": "Some silence irregularities",
|
| 59 |
+
"low": "Natural pause patterns"
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
LANGUAGE_INSIGHTS = {
|
| 64 |
+
"tamil": "Tamil vowel length patterns analyzed",
|
| 65 |
+
"hindi": "Hindi aspiration patterns checked",
|
| 66 |
+
"telugu": "Telugu word-ending patterns verified",
|
| 67 |
+
"malayalam": "Malayalam consonant patterns analyzed",
|
| 68 |
+
"english": "English prosody patterns checked"
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
def _get_severity(self, score: float) -> str:
|
| 72 |
+
"""Convert score to severity level."""
|
| 73 |
+
if score >= self.HIGH_THRESHOLD:
|
| 74 |
+
return "high"
|
| 75 |
+
elif score >= self.MEDIUM_THRESHOLD:
|
| 76 |
+
return "medium"
|
| 77 |
+
return "low"
|
| 78 |
+
|
| 79 |
+
def generate_artifacts(
|
| 80 |
+
self,
|
| 81 |
+
scores: SignalScores,
|
| 82 |
+
duration: float
|
| 83 |
+
) -> List[Artifact]:
|
| 84 |
+
"""Generate artifact list with timestamps.
|
| 85 |
+
|
| 86 |
+
Args:
|
| 87 |
+
scores: Signal analysis scores
|
| 88 |
+
duration: Audio duration in seconds
|
| 89 |
+
|
| 90 |
+
Returns:
|
| 91 |
+
List of detected artifacts with timestamps
|
| 92 |
+
"""
|
| 93 |
+
artifacts = []
|
| 94 |
+
|
| 95 |
+
# Generate pseudo-timestamps based on scores
|
| 96 |
+
# In a real implementation, you'd detect actual positions
|
| 97 |
+
|
| 98 |
+
if scores.spectral_score >= self.MEDIUM_THRESHOLD:
|
| 99 |
+
# Vocoder artifacts typically appear early
|
| 100 |
+
artifacts.append(Artifact(
|
| 101 |
+
timestamp=round(duration * 0.1, 2),
|
| 102 |
+
type="vocoder_artifact",
|
| 103 |
+
severity=self._get_severity(scores.spectral_score)
|
| 104 |
+
))
|
| 105 |
+
|
| 106 |
+
if scores.breath_score >= self.MEDIUM_THRESHOLD:
|
| 107 |
+
# Missing breath around middle
|
| 108 |
+
artifacts.append(Artifact(
|
| 109 |
+
timestamp=round(duration * 0.4, 2),
|
| 110 |
+
type="missing_breath",
|
| 111 |
+
severity=self._get_severity(scores.breath_score)
|
| 112 |
+
))
|
| 113 |
+
|
| 114 |
+
if scores.formant_score >= self.MEDIUM_THRESHOLD:
|
| 115 |
+
# Formant issues later in audio
|
| 116 |
+
artifacts.append(Artifact(
|
| 117 |
+
timestamp=round(duration * 0.7, 2),
|
| 118 |
+
type="unnatural_formant",
|
| 119 |
+
severity=self._get_severity(scores.formant_score)
|
| 120 |
+
))
|
| 121 |
+
|
| 122 |
+
if scores.prosody_score >= self.HIGH_THRESHOLD:
|
| 123 |
+
artifacts.append(Artifact(
|
| 124 |
+
timestamp=round(duration * 0.2, 2),
|
| 125 |
+
type="rhythm_anomaly",
|
| 126 |
+
severity=self._get_severity(scores.prosody_score)
|
| 127 |
+
))
|
| 128 |
+
|
| 129 |
+
if scores.silence_score >= self.HIGH_THRESHOLD:
|
| 130 |
+
artifacts.append(Artifact(
|
| 131 |
+
timestamp=round(duration * 0.5, 2),
|
| 132 |
+
type="artificial_silence",
|
| 133 |
+
severity=self._get_severity(scores.silence_score)
|
| 134 |
+
))
|
| 135 |
+
|
| 136 |
+
# Sort by timestamp
|
| 137 |
+
artifacts.sort(key=lambda x: x.timestamp)
|
| 138 |
+
|
| 139 |
+
return artifacts
|
| 140 |
+
|
| 141 |
+
def generate_explanation(
|
| 142 |
+
self,
|
| 143 |
+
classification: str,
|
| 144 |
+
confidence: float,
|
| 145 |
+
scores: SignalScores,
|
| 146 |
+
language: str,
|
| 147 |
+
artifacts: List[Artifact]
|
| 148 |
+
) -> str:
|
| 149 |
+
"""Generate human-readable explanation.
|
| 150 |
+
|
| 151 |
+
Args:
|
| 152 |
+
classification: "AI_GENERATED" or "HUMAN"
|
| 153 |
+
confidence: Detection confidence
|
| 154 |
+
scores: Signal analysis scores
|
| 155 |
+
language: Detected language
|
| 156 |
+
artifacts: List of detected artifacts
|
| 157 |
+
|
| 158 |
+
Returns:
|
| 159 |
+
Human-readable explanation string
|
| 160 |
+
"""
|
| 161 |
+
parts = []
|
| 162 |
+
|
| 163 |
+
if classification == "AI_GENERATED":
|
| 164 |
+
# Start with main finding
|
| 165 |
+
parts.append(f"This audio shows signs of AI generation with {confidence:.0%} confidence.")
|
| 166 |
+
|
| 167 |
+
# Add specific findings based on scores
|
| 168 |
+
high_signals = []
|
| 169 |
+
|
| 170 |
+
if scores.spectral_score >= self.HIGH_THRESHOLD:
|
| 171 |
+
high_signals.append("vocoder patterns")
|
| 172 |
+
if scores.breath_score >= self.HIGH_THRESHOLD:
|
| 173 |
+
high_signals.append("missing breath sounds")
|
| 174 |
+
if scores.prosody_score >= self.HIGH_THRESHOLD:
|
| 175 |
+
high_signals.append("unnatural rhythm")
|
| 176 |
+
if scores.formant_score >= self.HIGH_THRESHOLD:
|
| 177 |
+
high_signals.append("artificial vocal transitions")
|
| 178 |
+
if scores.silence_score >= self.HIGH_THRESHOLD:
|
| 179 |
+
high_signals.append("too-perfect pauses")
|
| 180 |
+
|
| 181 |
+
if high_signals:
|
| 182 |
+
parts.append(f"Key indicators: {', '.join(high_signals)}.")
|
| 183 |
+
|
| 184 |
+
# Add artifact timestamps
|
| 185 |
+
if artifacts:
|
| 186 |
+
timestamps = [f"{a.timestamp}s ({a.type.replace('_', ' ')})"
|
| 187 |
+
for a in artifacts[:3]]
|
| 188 |
+
parts.append(f"Artifacts detected at: {', '.join(timestamps)}.")
|
| 189 |
+
|
| 190 |
+
else: # HUMAN
|
| 191 |
+
parts.append(f"This audio appears to be genuine human speech with {confidence:.0%} confidence.")
|
| 192 |
+
|
| 193 |
+
# Mention what was checked
|
| 194 |
+
parts.append("Natural breath patterns, varied prosody, and authentic vocal characteristics detected.")
|
| 195 |
+
|
| 196 |
+
# Add language insight
|
| 197 |
+
if language in self.LANGUAGE_INSIGHTS:
|
| 198 |
+
parts.append(self.LANGUAGE_INSIGHTS[language])
|
| 199 |
+
|
| 200 |
+
return " ".join(parts)
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
# Create singleton instance
|
| 204 |
+
explainer = ExplanationGenerator()
|
app/core/hybrid_detector.py
ADDED
|
@@ -0,0 +1,523 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Hybrid Deepfake Detection Module.
|
| 2 |
+
|
| 3 |
+
Combines multiple detection signals for robust AI voice detection:
|
| 4 |
+
1. Spectral Analysis - Unusual frequency patterns in AI voices
|
| 5 |
+
2. Pitch Consistency - AI voices often have unnatural pitch stability
|
| 6 |
+
3. Pause Pattern Analysis - AI voices have different timing patterns
|
| 7 |
+
4. Formant Analysis - Synthetic voices have unusual formant transitions
|
| 8 |
+
5. Model-based Detection - ML model as one voting signal
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
from typing import Dict, Tuple, Optional
|
| 13 |
+
from dataclasses import dataclass
|
| 14 |
+
import librosa
|
| 15 |
+
import scipy.stats as stats
|
| 16 |
+
from scipy.signal import find_peaks
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@dataclass
|
| 20 |
+
class DetectionSignal:
|
| 21 |
+
"""A single detection signal result."""
|
| 22 |
+
name: str
|
| 23 |
+
is_ai: bool
|
| 24 |
+
confidence: float
|
| 25 |
+
reason: str
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@dataclass
|
| 29 |
+
class HybridResult:
|
| 30 |
+
"""Combined result from all detection signals."""
|
| 31 |
+
classification: str # "AI_GENERATED" or "HUMAN"
|
| 32 |
+
confidence: float
|
| 33 |
+
signals: list[DetectionSignal]
|
| 34 |
+
explanation: str
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class SpectralAnalyzer:
|
| 38 |
+
"""Analyzes spectral characteristics typical of AI-generated audio."""
|
| 39 |
+
|
| 40 |
+
def __init__(self, sr: int = 16000):
|
| 41 |
+
self.sr = sr
|
| 42 |
+
|
| 43 |
+
def analyze(self, waveform: np.ndarray) -> DetectionSignal:
|
| 44 |
+
"""
|
| 45 |
+
Analyze spectral features for AI detection.
|
| 46 |
+
|
| 47 |
+
AI voices often have:
|
| 48 |
+
- Unnaturally smooth spectral envelopes
|
| 49 |
+
- Missing or reduced high-frequency content
|
| 50 |
+
- Periodic spectral artifacts from neural vocoders
|
| 51 |
+
"""
|
| 52 |
+
# Extract MFCCs
|
| 53 |
+
mfccs = librosa.feature.mfcc(y=waveform, sr=self.sr, n_mfcc=20)
|
| 54 |
+
|
| 55 |
+
# 1. MFCC variance analysis - AI voices often have lower variance
|
| 56 |
+
mfcc_var = np.var(mfccs, axis=1).mean()
|
| 57 |
+
|
| 58 |
+
# 2. Spectral flatness - AI voices can have unusual flatness patterns
|
| 59 |
+
spectral_flatness = librosa.feature.spectral_flatness(y=waveform)[0]
|
| 60 |
+
flatness_mean = np.mean(spectral_flatness)
|
| 61 |
+
flatness_std = np.std(spectral_flatness)
|
| 62 |
+
|
| 63 |
+
# 3. Spectral contrast - AI voices may have reduced contrast
|
| 64 |
+
spectral_contrast = librosa.feature.spectral_contrast(y=waveform, sr=self.sr)
|
| 65 |
+
contrast_var = np.var(spectral_contrast)
|
| 66 |
+
|
| 67 |
+
# 4. High-frequency energy ratio
|
| 68 |
+
stft = np.abs(librosa.stft(waveform))
|
| 69 |
+
freq_bins = stft.shape[0]
|
| 70 |
+
high_freq_energy = np.mean(stft[int(freq_bins * 0.7):, :])
|
| 71 |
+
low_freq_energy = np.mean(stft[:int(freq_bins * 0.3), :]) + 1e-10
|
| 72 |
+
hf_ratio = high_freq_energy / low_freq_energy
|
| 73 |
+
|
| 74 |
+
# AI detection heuristics based on spectral features
|
| 75 |
+
ai_score = 0.0
|
| 76 |
+
reasons = []
|
| 77 |
+
|
| 78 |
+
# Low MFCC variance suggests synthetic audio
|
| 79 |
+
if mfcc_var < 15:
|
| 80 |
+
ai_score += 0.25
|
| 81 |
+
reasons.append("unusually consistent spectral patterns")
|
| 82 |
+
|
| 83 |
+
# Very low or very high flatness can indicate synthesis
|
| 84 |
+
if flatness_mean < 0.01 or flatness_mean > 0.3:
|
| 85 |
+
ai_score += 0.2
|
| 86 |
+
reasons.append("abnormal spectral flatness")
|
| 87 |
+
|
| 88 |
+
# Low flatness variance (too consistent)
|
| 89 |
+
if flatness_std < 0.02:
|
| 90 |
+
ai_score += 0.25
|
| 91 |
+
reasons.append("overly uniform spectral texture")
|
| 92 |
+
|
| 93 |
+
# Low high-frequency content (vocoder artifact)
|
| 94 |
+
if hf_ratio < 0.1:
|
| 95 |
+
ai_score += 0.3
|
| 96 |
+
reasons.append("reduced high-frequency content")
|
| 97 |
+
|
| 98 |
+
is_ai = ai_score >= 0.4
|
| 99 |
+
confidence = min(ai_score + 0.3, 0.95) if is_ai else max(0.7 - ai_score, 0.5)
|
| 100 |
+
|
| 101 |
+
return DetectionSignal(
|
| 102 |
+
name="Spectral Analysis",
|
| 103 |
+
is_ai=is_ai,
|
| 104 |
+
confidence=confidence,
|
| 105 |
+
reason=", ".join(reasons) if reasons else "natural spectral characteristics"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
class PitchAnalyzer:
|
| 110 |
+
"""Analyzes pitch patterns for AI detection."""
|
| 111 |
+
|
| 112 |
+
def __init__(self, sr: int = 16000):
|
| 113 |
+
self.sr = sr
|
| 114 |
+
|
| 115 |
+
def analyze(self, waveform: np.ndarray) -> DetectionSignal:
|
| 116 |
+
"""
|
| 117 |
+
Analyze pitch characteristics.
|
| 118 |
+
|
| 119 |
+
AI voices often have:
|
| 120 |
+
- Unnaturally consistent pitch (low variance)
|
| 121 |
+
- Robotic pitch transitions
|
| 122 |
+
- Missing micro-variations in F0
|
| 123 |
+
"""
|
| 124 |
+
# Extract fundamental frequency (F0)
|
| 125 |
+
f0, voiced_flag, _ = librosa.pyin(
|
| 126 |
+
waveform,
|
| 127 |
+
fmin=librosa.note_to_hz('C2'),
|
| 128 |
+
fmax=librosa.note_to_hz('C7'),
|
| 129 |
+
sr=self.sr
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# Filter out unvoiced segments
|
| 133 |
+
f0_voiced = f0[voiced_flag > 0.5] if len(f0[voiced_flag > 0.5]) > 10 else f0[~np.isnan(f0)]
|
| 134 |
+
|
| 135 |
+
if len(f0_voiced) < 10:
|
| 136 |
+
return DetectionSignal(
|
| 137 |
+
name="Pitch Analysis",
|
| 138 |
+
is_ai=False,
|
| 139 |
+
confidence=0.5,
|
| 140 |
+
reason="insufficient voiced segments for analysis"
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
# 1. Pitch variance - AI often has unnaturally low variance
|
| 144 |
+
pitch_var = np.var(f0_voiced)
|
| 145 |
+
pitch_std = np.std(f0_voiced)
|
| 146 |
+
|
| 147 |
+
# 2. Pitch micro-variations (jitter) - human voices have natural jitter
|
| 148 |
+
pitch_diff = np.diff(f0_voiced)
|
| 149 |
+
jitter = np.mean(np.abs(pitch_diff))
|
| 150 |
+
|
| 151 |
+
# 3. Pitch contour smoothness
|
| 152 |
+
# Calculate second derivative for contour analysis
|
| 153 |
+
if len(f0_voiced) > 20:
|
| 154 |
+
pitch_acceleration = np.diff(pitch_diff)
|
| 155 |
+
smoothness = 1.0 / (np.var(pitch_acceleration) + 1e-6)
|
| 156 |
+
else:
|
| 157 |
+
smoothness = 0
|
| 158 |
+
|
| 159 |
+
ai_score = 0.0
|
| 160 |
+
reasons = []
|
| 161 |
+
|
| 162 |
+
# Very low pitch variance (robotic)
|
| 163 |
+
if pitch_std < 10:
|
| 164 |
+
ai_score += 0.35
|
| 165 |
+
reasons.append("unnaturally stable pitch")
|
| 166 |
+
|
| 167 |
+
# Very low jitter (too perfect)
|
| 168 |
+
if jitter < 2:
|
| 169 |
+
ai_score += 0.3
|
| 170 |
+
reasons.append("missing natural pitch micro-variations")
|
| 171 |
+
|
| 172 |
+
# Too smooth contour (lacks natural fluctuation)
|
| 173 |
+
if smoothness > 100:
|
| 174 |
+
ai_score += 0.25
|
| 175 |
+
reasons.append("overly smooth pitch transitions")
|
| 176 |
+
|
| 177 |
+
is_ai = ai_score >= 0.4
|
| 178 |
+
confidence = min(ai_score + 0.3, 0.95) if is_ai else max(0.7 - ai_score, 0.5)
|
| 179 |
+
|
| 180 |
+
return DetectionSignal(
|
| 181 |
+
name="Pitch Analysis",
|
| 182 |
+
is_ai=is_ai,
|
| 183 |
+
confidence=confidence,
|
| 184 |
+
reason=", ".join(reasons) if reasons else "natural pitch characteristics"
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
class PausePatternAnalyzer:
|
| 189 |
+
"""Analyzes pause and timing patterns for AI detection."""
|
| 190 |
+
|
| 191 |
+
def __init__(self, sr: int = 16000):
|
| 192 |
+
self.sr = sr
|
| 193 |
+
|
| 194 |
+
def analyze(self, waveform: np.ndarray) -> DetectionSignal:
|
| 195 |
+
"""
|
| 196 |
+
Analyze pause patterns.
|
| 197 |
+
|
| 198 |
+
AI voices often have:
|
| 199 |
+
- More consistent pause durations
|
| 200 |
+
- Less natural speech rhythm
|
| 201 |
+
- Different silence-to-speech ratios
|
| 202 |
+
"""
|
| 203 |
+
# Calculate RMS energy
|
| 204 |
+
frame_length = int(0.025 * self.sr) # 25ms frames
|
| 205 |
+
hop_length = int(0.010 * self.sr) # 10ms hop
|
| 206 |
+
|
| 207 |
+
rms = librosa.feature.rms(y=waveform, frame_length=frame_length, hop_length=hop_length)[0]
|
| 208 |
+
|
| 209 |
+
# Dynamic threshold for speech/silence
|
| 210 |
+
threshold = np.mean(rms) * 0.3
|
| 211 |
+
|
| 212 |
+
# Find speech/silence segments
|
| 213 |
+
is_speech = rms > threshold
|
| 214 |
+
|
| 215 |
+
# Find segment boundaries
|
| 216 |
+
changes = np.diff(is_speech.astype(int))
|
| 217 |
+
speech_starts = np.where(changes == 1)[0]
|
| 218 |
+
speech_ends = np.where(changes == -1)[0]
|
| 219 |
+
|
| 220 |
+
if len(speech_starts) < 3 or len(speech_ends) < 3:
|
| 221 |
+
return DetectionSignal(
|
| 222 |
+
name="Pause Pattern",
|
| 223 |
+
is_ai=False,
|
| 224 |
+
confidence=0.5,
|
| 225 |
+
reason="insufficient speech segments for analysis"
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
# Calculate pause durations (in frames)
|
| 229 |
+
pause_durations = []
|
| 230 |
+
for i in range(min(len(speech_ends), len(speech_starts) - 1)):
|
| 231 |
+
if speech_starts[i + 1] > speech_ends[i]:
|
| 232 |
+
pause_durations.append(speech_starts[i + 1] - speech_ends[i])
|
| 233 |
+
|
| 234 |
+
if len(pause_durations) < 2:
|
| 235 |
+
return DetectionSignal(
|
| 236 |
+
name="Pause Pattern",
|
| 237 |
+
is_ai=False,
|
| 238 |
+
confidence=0.5,
|
| 239 |
+
reason="not enough pauses to analyze"
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
# AI detection based on pause patterns
|
| 243 |
+
pause_durations = np.array(pause_durations)
|
| 244 |
+
pause_var = np.var(pause_durations)
|
| 245 |
+
pause_cv = np.std(pause_durations) / (np.mean(pause_durations) + 1e-6) # Coefficient of variation
|
| 246 |
+
|
| 247 |
+
# Speech segment durations
|
| 248 |
+
speech_durations = []
|
| 249 |
+
for i in range(min(len(speech_starts), len(speech_ends))):
|
| 250 |
+
if speech_ends[i] > speech_starts[i]:
|
| 251 |
+
speech_durations.append(speech_ends[i] - speech_starts[i])
|
| 252 |
+
|
| 253 |
+
if len(speech_durations) > 1:
|
| 254 |
+
speech_cv = np.std(speech_durations) / (np.mean(speech_durations) + 1e-6)
|
| 255 |
+
else:
|
| 256 |
+
speech_cv = 0.5
|
| 257 |
+
|
| 258 |
+
ai_score = 0.0
|
| 259 |
+
reasons = []
|
| 260 |
+
|
| 261 |
+
# Very consistent pause lengths (artificial)
|
| 262 |
+
if pause_cv < 0.3:
|
| 263 |
+
ai_score += 0.35
|
| 264 |
+
reasons.append("unnaturally consistent pause durations")
|
| 265 |
+
|
| 266 |
+
# Very consistent speech lengths
|
| 267 |
+
if speech_cv < 0.25:
|
| 268 |
+
ai_score += 0.3
|
| 269 |
+
reasons.append("robotic speech segment rhythm")
|
| 270 |
+
|
| 271 |
+
# Very low pause variance
|
| 272 |
+
if pause_var < 10:
|
| 273 |
+
ai_score += 0.25
|
| 274 |
+
reasons.append("mechanical timing patterns")
|
| 275 |
+
|
| 276 |
+
is_ai = ai_score >= 0.4
|
| 277 |
+
confidence = min(ai_score + 0.3, 0.95) if is_ai else max(0.7 - ai_score, 0.5)
|
| 278 |
+
|
| 279 |
+
return DetectionSignal(
|
| 280 |
+
name="Pause Pattern",
|
| 281 |
+
is_ai=is_ai,
|
| 282 |
+
confidence=confidence,
|
| 283 |
+
reason=", ".join(reasons) if reasons else "natural speech rhythm"
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
class FormantAnalyzer:
|
| 288 |
+
"""Analyzes formant characteristics for AI detection."""
|
| 289 |
+
|
| 290 |
+
def __init__(self, sr: int = 16000):
|
| 291 |
+
self.sr = sr
|
| 292 |
+
|
| 293 |
+
def analyze(self, waveform: np.ndarray) -> DetectionSignal:
|
| 294 |
+
"""
|
| 295 |
+
Analyze formant patterns.
|
| 296 |
+
|
| 297 |
+
AI voices may have:
|
| 298 |
+
- Unnatural formant transitions
|
| 299 |
+
- Missing formant dynamics
|
| 300 |
+
- Unusual F1-F2 relationships
|
| 301 |
+
"""
|
| 302 |
+
try:
|
| 303 |
+
# Use spectral peaks as proxy for formants
|
| 304 |
+
n_fft = 2048
|
| 305 |
+
hop_length = 512
|
| 306 |
+
|
| 307 |
+
stft = np.abs(librosa.stft(waveform, n_fft=n_fft, hop_length=hop_length))
|
| 308 |
+
|
| 309 |
+
# Get spectral centroids over time as formant proxy
|
| 310 |
+
spectral_centroid = librosa.feature.spectral_centroid(y=waveform, sr=self.sr)[0]
|
| 311 |
+
spectral_bandwidth = librosa.feature.spectral_bandwidth(y=waveform, sr=self.sr)[0]
|
| 312 |
+
|
| 313 |
+
# Formant transition smoothness (real voices have more variation)
|
| 314 |
+
centroid_var = np.var(spectral_centroid)
|
| 315 |
+
centroid_diff = np.diff(spectral_centroid)
|
| 316 |
+
transition_smoothness = 1.0 / (np.var(centroid_diff) + 1e-6)
|
| 317 |
+
|
| 318 |
+
# Bandwidth dynamics
|
| 319 |
+
bandwidth_var = np.var(spectral_bandwidth)
|
| 320 |
+
|
| 321 |
+
ai_score = 0.0
|
| 322 |
+
reasons = []
|
| 323 |
+
|
| 324 |
+
# Very smooth formant transitions (synthetic)
|
| 325 |
+
if transition_smoothness > 500:
|
| 326 |
+
ai_score += 0.3
|
| 327 |
+
reasons.append("overly smooth formant transitions")
|
| 328 |
+
|
| 329 |
+
# Low centroid variance (limited vocal tract modeling)
|
| 330 |
+
if centroid_var < 50000:
|
| 331 |
+
ai_score += 0.25
|
| 332 |
+
reasons.append("limited formant variation")
|
| 333 |
+
|
| 334 |
+
# Low bandwidth dynamics
|
| 335 |
+
if bandwidth_var < 10000:
|
| 336 |
+
ai_score += 0.25
|
| 337 |
+
reasons.append("static spectral bandwidth")
|
| 338 |
+
|
| 339 |
+
is_ai = ai_score >= 0.4
|
| 340 |
+
confidence = min(ai_score + 0.3, 0.95) if is_ai else max(0.7 - ai_score, 0.5)
|
| 341 |
+
|
| 342 |
+
return DetectionSignal(
|
| 343 |
+
name="Formant Analysis",
|
| 344 |
+
is_ai=is_ai,
|
| 345 |
+
confidence=confidence,
|
| 346 |
+
reason=", ".join(reasons) if reasons else "natural formant dynamics"
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
+
except Exception as e:
|
| 350 |
+
return DetectionSignal(
|
| 351 |
+
name="Formant Analysis",
|
| 352 |
+
is_ai=False,
|
| 353 |
+
confidence=0.5,
|
| 354 |
+
reason=f"analysis error: {str(e)}"
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
|
| 358 |
+
class HybridDetector:
|
| 359 |
+
"""
|
| 360 |
+
Hybrid detector combining ML model + multiple signal analyzers.
|
| 361 |
+
|
| 362 |
+
Uses weighted voting from:
|
| 363 |
+
1. ML Model (40%) - Primary signal
|
| 364 |
+
2. Spectral Analysis (15%)
|
| 365 |
+
3. Pitch Analysis (15%)
|
| 366 |
+
4. Pause Pattern Analysis (15%)
|
| 367 |
+
5. Formant Analysis (15%)
|
| 368 |
+
|
| 369 |
+
This combines the ML model's training with acoustic analysis.
|
| 370 |
+
"""
|
| 371 |
+
|
| 372 |
+
def __init__(self, sr: int = 16000):
|
| 373 |
+
self.sr = sr
|
| 374 |
+
self.spectral_analyzer = SpectralAnalyzer(sr)
|
| 375 |
+
self.pitch_analyzer = PitchAnalyzer(sr)
|
| 376 |
+
self.pause_analyzer = PausePatternAnalyzer(sr)
|
| 377 |
+
self.formant_analyzer = FormantAnalyzer(sr)
|
| 378 |
+
|
| 379 |
+
# ML model pipeline (lazy loaded)
|
| 380 |
+
self._model_pipeline = None
|
| 381 |
+
self._model_loaded = False
|
| 382 |
+
|
| 383 |
+
# Weights for each analyzer - ML model gets highest weight
|
| 384 |
+
self.weights = {
|
| 385 |
+
"ML Model": 0.40,
|
| 386 |
+
"Spectral Analysis": 0.15,
|
| 387 |
+
"Pitch Analysis": 0.15,
|
| 388 |
+
"Pause Pattern": 0.15,
|
| 389 |
+
"Formant Analysis": 0.15
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
def _load_model(self):
|
| 393 |
+
"""Lazy load the ML model."""
|
| 394 |
+
if not self._model_loaded:
|
| 395 |
+
from transformers import pipeline
|
| 396 |
+
print(" 🤖 Loading ML model...")
|
| 397 |
+
self._model_pipeline = pipeline(
|
| 398 |
+
"audio-classification",
|
| 399 |
+
model="mo-thecreator/Deepfake-audio-detection"
|
| 400 |
+
)
|
| 401 |
+
self._model_loaded = True
|
| 402 |
+
|
| 403 |
+
def _run_model(self, waveform: np.ndarray) -> DetectionSignal:
|
| 404 |
+
"""Run the ML model for classification."""
|
| 405 |
+
try:
|
| 406 |
+
self._load_model()
|
| 407 |
+
|
| 408 |
+
results = self._model_pipeline(waveform, sampling_rate=self.sr)
|
| 409 |
+
|
| 410 |
+
# Find best label
|
| 411 |
+
best = max(results, key=lambda x: x["score"])
|
| 412 |
+
label = best["label"].lower()
|
| 413 |
+
confidence = best["score"]
|
| 414 |
+
|
| 415 |
+
# mo-thecreator model has inverted semantics:
|
| 416 |
+
# "real" = real deepfake = AI_GENERATED
|
| 417 |
+
# "fake" = not a deepfake = HUMAN
|
| 418 |
+
if "real" in label:
|
| 419 |
+
is_ai = True
|
| 420 |
+
reason = "ML model detected AI patterns"
|
| 421 |
+
else:
|
| 422 |
+
is_ai = False
|
| 423 |
+
reason = "ML model detected human patterns"
|
| 424 |
+
|
| 425 |
+
return DetectionSignal(
|
| 426 |
+
name="ML Model",
|
| 427 |
+
is_ai=is_ai,
|
| 428 |
+
confidence=confidence,
|
| 429 |
+
reason=reason
|
| 430 |
+
)
|
| 431 |
+
|
| 432 |
+
except Exception as e:
|
| 433 |
+
print(f" ⚠️ ML model error: {e}")
|
| 434 |
+
return DetectionSignal(
|
| 435 |
+
name="ML Model",
|
| 436 |
+
is_ai=False,
|
| 437 |
+
confidence=0.5,
|
| 438 |
+
reason=f"ML model error: {str(e)}"
|
| 439 |
+
)
|
| 440 |
+
|
| 441 |
+
def detect(self, waveform: np.ndarray, language: str = "english") -> HybridResult:
|
| 442 |
+
"""
|
| 443 |
+
Run all analyzers (including ML model) and combine results.
|
| 444 |
+
|
| 445 |
+
Args:
|
| 446 |
+
waveform: Audio samples (16kHz mono)
|
| 447 |
+
language: Language for logging
|
| 448 |
+
|
| 449 |
+
Returns:
|
| 450 |
+
HybridResult with combined classification
|
| 451 |
+
"""
|
| 452 |
+
print(f"🔬 Running hybrid detection for language: {language}")
|
| 453 |
+
|
| 454 |
+
# Run all analyzers
|
| 455 |
+
signals = []
|
| 456 |
+
|
| 457 |
+
print(" 🤖 ML model analysis...")
|
| 458 |
+
signals.append(self._run_model(waveform))
|
| 459 |
+
|
| 460 |
+
print(" 📊 Spectral analysis...")
|
| 461 |
+
signals.append(self.spectral_analyzer.analyze(waveform))
|
| 462 |
+
|
| 463 |
+
print(" 🎵 Pitch analysis...")
|
| 464 |
+
signals.append(self.pitch_analyzer.analyze(waveform))
|
| 465 |
+
|
| 466 |
+
print(" ⏱️ Pause pattern analysis...")
|
| 467 |
+
signals.append(self.pause_analyzer.analyze(waveform))
|
| 468 |
+
|
| 469 |
+
print(" 📈 Formant analysis...")
|
| 470 |
+
signals.append(self.formant_analyzer.analyze(waveform))
|
| 471 |
+
|
| 472 |
+
# Calculate weighted vote
|
| 473 |
+
ai_score = 0.0
|
| 474 |
+
human_score = 0.0
|
| 475 |
+
|
| 476 |
+
for signal in signals:
|
| 477 |
+
weight = self.weights.get(signal.name, 0.15)
|
| 478 |
+
if signal.is_ai:
|
| 479 |
+
ai_score += weight * signal.confidence
|
| 480 |
+
else:
|
| 481 |
+
human_score += weight * signal.confidence
|
| 482 |
+
|
| 483 |
+
# Normalize scores
|
| 484 |
+
total = ai_score + human_score
|
| 485 |
+
if total > 0:
|
| 486 |
+
ai_score /= total
|
| 487 |
+
human_score /= total
|
| 488 |
+
else:
|
| 489 |
+
ai_score = 0.5
|
| 490 |
+
human_score = 0.5
|
| 491 |
+
|
| 492 |
+
# Final decision with slight bias towards AI detection if close
|
| 493 |
+
# (better to flag potential deepfakes than miss them)
|
| 494 |
+
is_ai = ai_score >= 0.45 # Lower threshold for AI detection
|
| 495 |
+
confidence = ai_score if is_ai else human_score
|
| 496 |
+
|
| 497 |
+
# Build explanation
|
| 498 |
+
ai_signals = [s for s in signals if s.is_ai]
|
| 499 |
+
human_signals = [s for s in signals if not s.is_ai]
|
| 500 |
+
|
| 501 |
+
if is_ai:
|
| 502 |
+
reasons = [s.reason for s in ai_signals if s.reason]
|
| 503 |
+
explanation = f"AI indicators detected: {'; '.join(reasons)}" if reasons else "Multiple AI-like patterns detected"
|
| 504 |
+
else:
|
| 505 |
+
reasons = [s.reason for s in human_signals if s.reason]
|
| 506 |
+
explanation = f"Human voice confirmed: {'; '.join(reasons)}" if reasons else "Natural voice patterns detected"
|
| 507 |
+
|
| 508 |
+
classification = "AI_GENERATED" if is_ai else "HUMAN"
|
| 509 |
+
|
| 510 |
+
print(f" ✅ Result: {classification} ({confidence:.2%})")
|
| 511 |
+
print(f" 📝 {explanation}")
|
| 512 |
+
|
| 513 |
+
return HybridResult(
|
| 514 |
+
classification=classification,
|
| 515 |
+
confidence=confidence,
|
| 516 |
+
signals=signals,
|
| 517 |
+
explanation=explanation
|
| 518 |
+
)
|
| 519 |
+
|
| 520 |
+
|
| 521 |
+
# Singleton instance
|
| 522 |
+
hybrid_detector = HybridDetector()
|
| 523 |
+
|
app/core/signal_analyzer.py
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Signal Analyzer Module.
|
| 2 |
+
|
| 3 |
+
Analyzes audio signals to extract multiple detection metrics:
|
| 4 |
+
- Prosody (rhythm/timing)
|
| 5 |
+
- Breath patterns
|
| 6 |
+
- Spectral characteristics
|
| 7 |
+
- Formant transitions
|
| 8 |
+
- Silence patterns
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
import librosa
|
| 13 |
+
from typing import Dict, List, Tuple
|
| 14 |
+
from dataclasses import dataclass
|
| 15 |
+
|
| 16 |
+
from app.config import settings
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@dataclass
|
| 20 |
+
class SignalScores:
|
| 21 |
+
"""Container for all signal scores."""
|
| 22 |
+
prosody_score: float
|
| 23 |
+
breath_score: float
|
| 24 |
+
spectral_score: float
|
| 25 |
+
formant_score: float
|
| 26 |
+
silence_score: float
|
| 27 |
+
|
| 28 |
+
def to_dict(self) -> Dict[str, float]:
|
| 29 |
+
"""Convert to dictionary."""
|
| 30 |
+
return {
|
| 31 |
+
"prosody_score": self.prosody_score,
|
| 32 |
+
"breath_score": self.breath_score,
|
| 33 |
+
"spectral_score": self.spectral_score,
|
| 34 |
+
"formant_score": self.formant_score,
|
| 35 |
+
"silence_score": self.silence_score
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class SignalAnalyzer:
|
| 40 |
+
"""Analyzes audio signals for deepfake detection features."""
|
| 41 |
+
|
| 42 |
+
def __init__(self, sample_rate: int = None):
|
| 43 |
+
"""Initialize analyzer.
|
| 44 |
+
|
| 45 |
+
Args:
|
| 46 |
+
sample_rate: Audio sample rate (default: 16000)
|
| 47 |
+
"""
|
| 48 |
+
self.sr = sample_rate or settings.TARGET_SAMPLE_RATE
|
| 49 |
+
|
| 50 |
+
def calculate_prosody_score(self, waveform: np.ndarray) -> float:
|
| 51 |
+
"""Analyze prosody (rhythm and timing patterns).
|
| 52 |
+
|
| 53 |
+
AI-generated audio often has unnaturally consistent rhythm.
|
| 54 |
+
|
| 55 |
+
Args:
|
| 56 |
+
waveform: Audio waveform
|
| 57 |
+
|
| 58 |
+
Returns:
|
| 59 |
+
Prosody anomaly score (higher = more likely AI)
|
| 60 |
+
"""
|
| 61 |
+
try:
|
| 62 |
+
# Extract tempo and beat frames
|
| 63 |
+
tempo, beat_frames = librosa.beat.beat_track(y=waveform, sr=self.sr)
|
| 64 |
+
|
| 65 |
+
if len(beat_frames) < 2:
|
| 66 |
+
return 0.5 # Not enough data
|
| 67 |
+
|
| 68 |
+
# Calculate inter-beat intervals
|
| 69 |
+
beat_times = librosa.frames_to_time(beat_frames, sr=self.sr)
|
| 70 |
+
intervals = np.diff(beat_times)
|
| 71 |
+
|
| 72 |
+
if len(intervals) == 0:
|
| 73 |
+
return 0.5
|
| 74 |
+
|
| 75 |
+
# AI audio tends to have very consistent intervals
|
| 76 |
+
# Calculate coefficient of variation
|
| 77 |
+
cv = np.std(intervals) / (np.mean(intervals) + 1e-6)
|
| 78 |
+
|
| 79 |
+
# Low CV = suspicious (too consistent)
|
| 80 |
+
# Map to score: low CV -> high score
|
| 81 |
+
score = 1.0 - min(cv * 2, 1.0)
|
| 82 |
+
|
| 83 |
+
return float(np.clip(score, 0, 1))
|
| 84 |
+
|
| 85 |
+
except Exception:
|
| 86 |
+
return 0.5
|
| 87 |
+
|
| 88 |
+
def calculate_breath_score(self, waveform: np.ndarray) -> float:
|
| 89 |
+
"""Detect breath patterns in speech.
|
| 90 |
+
|
| 91 |
+
Human speech has natural breath pauses that AI often misses.
|
| 92 |
+
|
| 93 |
+
Args:
|
| 94 |
+
waveform: Audio waveform
|
| 95 |
+
|
| 96 |
+
Returns:
|
| 97 |
+
Breath anomaly score (higher = more likely AI - missing breaths)
|
| 98 |
+
"""
|
| 99 |
+
try:
|
| 100 |
+
# Calculate RMS energy
|
| 101 |
+
rms = librosa.feature.rms(y=waveform, frame_length=2048, hop_length=512)[0]
|
| 102 |
+
|
| 103 |
+
# Normalize
|
| 104 |
+
rms_norm = rms / (np.max(rms) + 1e-6)
|
| 105 |
+
|
| 106 |
+
# Find low energy regions (potential breath/pause locations)
|
| 107 |
+
threshold = 0.1
|
| 108 |
+
low_energy = rms_norm < threshold
|
| 109 |
+
|
| 110 |
+
# Count transitions (speech to silence and back)
|
| 111 |
+
transitions = np.sum(np.abs(np.diff(low_energy.astype(int))))
|
| 112 |
+
|
| 113 |
+
# Duration in seconds
|
| 114 |
+
duration = len(waveform) / self.sr
|
| 115 |
+
|
| 116 |
+
# Expected transitions per second of speech
|
| 117 |
+
expected_transitions_per_sec = 0.3 # Roughly one breath every 3-4 seconds
|
| 118 |
+
expected = expected_transitions_per_sec * duration * 2
|
| 119 |
+
|
| 120 |
+
# Too few transitions = suspicious (AI doesn't breathe)
|
| 121 |
+
ratio = transitions / (expected + 1e-6)
|
| 122 |
+
|
| 123 |
+
# Low ratio means fewer natural pauses
|
| 124 |
+
if ratio < 0.5:
|
| 125 |
+
score = 0.9 # Very suspicious
|
| 126 |
+
elif ratio < 1.0:
|
| 127 |
+
score = 0.7
|
| 128 |
+
else:
|
| 129 |
+
score = 0.3 # Normal breathing patterns
|
| 130 |
+
|
| 131 |
+
return float(score)
|
| 132 |
+
|
| 133 |
+
except Exception:
|
| 134 |
+
return 0.5
|
| 135 |
+
|
| 136 |
+
def calculate_spectral_score(self, waveform: np.ndarray) -> float:
|
| 137 |
+
"""Analyze spectral characteristics for vocoder artifacts.
|
| 138 |
+
|
| 139 |
+
AI vocoders leave characteristic spectral patterns.
|
| 140 |
+
|
| 141 |
+
Args:
|
| 142 |
+
waveform: Audio waveform
|
| 143 |
+
|
| 144 |
+
Returns:
|
| 145 |
+
Spectral anomaly score (higher = more likely AI)
|
| 146 |
+
"""
|
| 147 |
+
try:
|
| 148 |
+
# Calculate spectral flatness
|
| 149 |
+
spec_flat = librosa.feature.spectral_flatness(y=waveform)[0]
|
| 150 |
+
|
| 151 |
+
# Calculate spectral centroid variation
|
| 152 |
+
spec_cent = librosa.feature.spectral_centroid(y=waveform, sr=self.sr)[0]
|
| 153 |
+
cent_var = np.std(spec_cent) / (np.mean(spec_cent) + 1e-6)
|
| 154 |
+
|
| 155 |
+
# AI audio often has higher spectral flatness
|
| 156 |
+
avg_flatness = np.mean(spec_flat)
|
| 157 |
+
|
| 158 |
+
# Combine metrics
|
| 159 |
+
# High flatness or low centroid variation = suspicious
|
| 160 |
+
flatness_score = min(avg_flatness * 3, 1.0)
|
| 161 |
+
variation_score = 1.0 - min(cent_var * 2, 1.0)
|
| 162 |
+
|
| 163 |
+
score = (flatness_score + variation_score) / 2
|
| 164 |
+
|
| 165 |
+
return float(np.clip(score, 0, 1))
|
| 166 |
+
|
| 167 |
+
except Exception:
|
| 168 |
+
return 0.5
|
| 169 |
+
|
| 170 |
+
def calculate_formant_score(self, waveform: np.ndarray) -> float:
|
| 171 |
+
"""Analyze formant transitions.
|
| 172 |
+
|
| 173 |
+
Human vocal tract creates characteristic formant patterns.
|
| 174 |
+
|
| 175 |
+
Args:
|
| 176 |
+
waveform: Audio waveform
|
| 177 |
+
|
| 178 |
+
Returns:
|
| 179 |
+
Formant anomaly score (higher = more likely AI)
|
| 180 |
+
"""
|
| 181 |
+
try:
|
| 182 |
+
# Use MFCC as proxy for formant information
|
| 183 |
+
mfccs = librosa.feature.mfcc(y=waveform, sr=self.sr, n_mfcc=13)
|
| 184 |
+
|
| 185 |
+
# Calculate delta MFCCs (transitions)
|
| 186 |
+
delta_mfccs = librosa.feature.delta(mfccs)
|
| 187 |
+
|
| 188 |
+
# AI often has smoother, less varied transitions
|
| 189 |
+
delta_var = np.mean(np.std(delta_mfccs, axis=1))
|
| 190 |
+
|
| 191 |
+
# Low variation in deltas = suspicious
|
| 192 |
+
# Map: low variation -> high score
|
| 193 |
+
score = 1.0 - min(delta_var * 10, 1.0)
|
| 194 |
+
|
| 195 |
+
return float(np.clip(score, 0, 1))
|
| 196 |
+
|
| 197 |
+
except Exception:
|
| 198 |
+
return 0.5
|
| 199 |
+
|
| 200 |
+
def calculate_silence_score(self, waveform: np.ndarray) -> float:
|
| 201 |
+
"""Analyze silence/pause patterns.
|
| 202 |
+
|
| 203 |
+
AI-generated audio often has "too perfect" silences.
|
| 204 |
+
|
| 205 |
+
Args:
|
| 206 |
+
waveform: Audio waveform
|
| 207 |
+
|
| 208 |
+
Returns:
|
| 209 |
+
Silence anomaly score (higher = more likely AI)
|
| 210 |
+
"""
|
| 211 |
+
try:
|
| 212 |
+
# Split into frames
|
| 213 |
+
frame_length = 2048
|
| 214 |
+
hop_length = 512
|
| 215 |
+
|
| 216 |
+
# Calculate energy per frame
|
| 217 |
+
frames = librosa.util.frame(waveform, frame_length=frame_length, hop_length=hop_length)
|
| 218 |
+
frame_energy = np.mean(frames ** 2, axis=0)
|
| 219 |
+
|
| 220 |
+
# Identify silence frames (very low energy)
|
| 221 |
+
silence_threshold = np.percentile(frame_energy, 10)
|
| 222 |
+
silence_frames = frame_energy < silence_threshold
|
| 223 |
+
|
| 224 |
+
# Calculate silence duration distribution
|
| 225 |
+
silence_runs = []
|
| 226 |
+
current_run = 0
|
| 227 |
+
|
| 228 |
+
for is_silent in silence_frames:
|
| 229 |
+
if is_silent:
|
| 230 |
+
current_run += 1
|
| 231 |
+
elif current_run > 0:
|
| 232 |
+
silence_runs.append(current_run)
|
| 233 |
+
current_run = 0
|
| 234 |
+
|
| 235 |
+
if current_run > 0:
|
| 236 |
+
silence_runs.append(current_run)
|
| 237 |
+
|
| 238 |
+
if len(silence_runs) < 2:
|
| 239 |
+
return 0.5
|
| 240 |
+
|
| 241 |
+
# AI tends to have very uniform silence lengths
|
| 242 |
+
silence_cv = np.std(silence_runs) / (np.mean(silence_runs) + 1e-6)
|
| 243 |
+
|
| 244 |
+
# Low variation = suspicious
|
| 245 |
+
score = 1.0 - min(silence_cv, 1.0)
|
| 246 |
+
|
| 247 |
+
return float(np.clip(score, 0, 1))
|
| 248 |
+
|
| 249 |
+
except Exception:
|
| 250 |
+
return 0.5
|
| 251 |
+
|
| 252 |
+
def analyze(self, waveform: np.ndarray) -> SignalScores:
|
| 253 |
+
"""Run all signal analyses.
|
| 254 |
+
|
| 255 |
+
Args:
|
| 256 |
+
waveform: Audio waveform (16kHz, mono)
|
| 257 |
+
|
| 258 |
+
Returns:
|
| 259 |
+
SignalScores with all metrics
|
| 260 |
+
"""
|
| 261 |
+
return SignalScores(
|
| 262 |
+
prosody_score=self.calculate_prosody_score(waveform),
|
| 263 |
+
breath_score=self.calculate_breath_score(waveform),
|
| 264 |
+
spectral_score=self.calculate_spectral_score(waveform),
|
| 265 |
+
formant_score=self.calculate_formant_score(waveform),
|
| 266 |
+
silence_score=self.calculate_silence_score(waveform)
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
def get_average_score(self, scores: SignalScores) -> float:
|
| 270 |
+
"""Calculate weighted average of all scores.
|
| 271 |
+
|
| 272 |
+
Args:
|
| 273 |
+
scores: Signal scores
|
| 274 |
+
|
| 275 |
+
Returns:
|
| 276 |
+
Weighted average score
|
| 277 |
+
"""
|
| 278 |
+
weights = {
|
| 279 |
+
"prosody": 0.15,
|
| 280 |
+
"breath": 0.25,
|
| 281 |
+
"spectral": 0.30,
|
| 282 |
+
"formant": 0.15,
|
| 283 |
+
"silence": 0.15
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
weighted = (
|
| 287 |
+
scores.prosody_score * weights["prosody"] +
|
| 288 |
+
scores.breath_score * weights["breath"] +
|
| 289 |
+
scores.spectral_score * weights["spectral"] +
|
| 290 |
+
scores.formant_score * weights["formant"] +
|
| 291 |
+
scores.silence_score * weights["silence"]
|
| 292 |
+
)
|
| 293 |
+
|
| 294 |
+
return float(weighted)
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
# Create singleton instance
|
| 298 |
+
signal_analyzer = SignalAnalyzer()
|
app/main.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""VoiceGuard API - Main Application Entry Point.
|
| 2 |
+
|
| 3 |
+
AI-Generated Voice Detection API for Indian Languages.
|
| 4 |
+
Supports: Tamil, English, Hindi, Malayalam, Telugu
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from fastapi import FastAPI
|
| 8 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
+
from fastapi.staticfiles import StaticFiles
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
from app.api.routes import router
|
| 13 |
+
from app.config import settings
|
| 14 |
+
from app.core.detector import detector
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Create FastAPI app
|
| 18 |
+
app = FastAPI(
|
| 19 |
+
title="VoiceGuard API",
|
| 20 |
+
description="""
|
| 21 |
+
## AI-Generated Voice Detection for Indian Languages
|
| 22 |
+
|
| 23 |
+
VoiceGuard detects whether an audio sample is AI-generated (deepfake) or genuine human speech.
|
| 24 |
+
|
| 25 |
+
### Features
|
| 26 |
+
- 🎯 **Multi-Language Support**: Tamil, English, Hindi, Malayalam, Telugu
|
| 27 |
+
- 📊 **Signal Analysis**: 5 detection signals (prosody, breath, spectral, formant, silence)
|
| 28 |
+
- 🔥 **Visual Heatmaps**: Spectrogram visualization showing artifact locations
|
| 29 |
+
- 📝 **Explanations**: Human-readable reasoning for each detection
|
| 30 |
+
|
| 31 |
+
### How It Works
|
| 32 |
+
1. Upload Base64-encoded MP3/WAV audio
|
| 33 |
+
2. AI analyzes the audio for synthetic patterns
|
| 34 |
+
3. Get classification, confidence, and visual explanation
|
| 35 |
+
|
| 36 |
+
---
|
| 37 |
+
|
| 38 |
+
**India AI Impact Summit Buildathon 2026**
|
| 39 |
+
""",
|
| 40 |
+
version="1.0.0",
|
| 41 |
+
docs_url="/docs",
|
| 42 |
+
redoc_url="/redoc"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Add CORS middleware (allow frontend to connect)
|
| 46 |
+
app.add_middleware(
|
| 47 |
+
CORSMiddleware,
|
| 48 |
+
allow_origins=["*"], # Allow all origins for demo
|
| 49 |
+
allow_credentials=True,
|
| 50 |
+
allow_methods=["*"],
|
| 51 |
+
allow_headers=["*"],
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Mount static files for heatmaps
|
| 55 |
+
os.makedirs(settings.HEATMAP_DIR, exist_ok=True)
|
| 56 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 57 |
+
|
| 58 |
+
# Mount frontend files
|
| 59 |
+
frontend_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "frontend")
|
| 60 |
+
if os.path.exists(frontend_dir):
|
| 61 |
+
app.mount("/frontend", StaticFiles(directory=frontend_dir, html=True), name="frontend")
|
| 62 |
+
|
| 63 |
+
# Include API routes (endpoint: /api/voice-detection)
|
| 64 |
+
app.include_router(router, prefix="/api", tags=["Detection"])
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# ============== ROOT ENDPOINT ==============
|
| 68 |
+
|
| 69 |
+
@app.get("/", tags=["Info"])
|
| 70 |
+
async def root():
|
| 71 |
+
"""Root endpoint with API information."""
|
| 72 |
+
return {
|
| 73 |
+
"name": "VoiceGuard API",
|
| 74 |
+
"description": "AI-Generated Voice Detection for Indian Languages",
|
| 75 |
+
"version": "1.0.0",
|
| 76 |
+
"docs": "/docs",
|
| 77 |
+
"frontend": "/frontend/",
|
| 78 |
+
"health": "/api/health",
|
| 79 |
+
"languages": ["tamil", "english", "hindi", "malayalam", "telugu"],
|
| 80 |
+
"status": "online"
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
@app.get("/favicon.ico", include_in_schema=False)
|
| 85 |
+
async def favicon():
|
| 86 |
+
"""Return empty response for favicon (browser default request)."""
|
| 87 |
+
from fastapi.responses import Response
|
| 88 |
+
return Response(content="", media_type="image/x-icon")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# ============== STARTUP EVENT ==============
|
| 92 |
+
|
| 93 |
+
@app.on_event("startup")
|
| 94 |
+
async def startup_event():
|
| 95 |
+
"""Initialize resources on startup."""
|
| 96 |
+
print("=" * 50)
|
| 97 |
+
print("🛡️ VoiceGuard API Starting...")
|
| 98 |
+
print("=" * 50)
|
| 99 |
+
|
| 100 |
+
# Single multilingual model for all languages
|
| 101 |
+
print("📌 Single-model architecture:")
|
| 102 |
+
print(" Model: abhishtagatya/hubert-base-960h-itw-deepfake")
|
| 103 |
+
print(" Accuracy: Human=99.83%, AI=99.79%")
|
| 104 |
+
print(" Languages: Tamil, English, Hindi, Malayalam, Telugu")
|
| 105 |
+
|
| 106 |
+
print("=" * 50)
|
| 107 |
+
print(f"🚀 API ready at http://{settings.HOST}:{settings.PORT}")
|
| 108 |
+
print(f"📚 Docs: http://{settings.HOST}:{settings.PORT}/docs")
|
| 109 |
+
print(f"🎨 Frontend: http://{settings.HOST}:{settings.PORT}/frontend/")
|
| 110 |
+
print("=" * 50)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# ============== SHUTDOWN EVENT ==============
|
| 114 |
+
|
| 115 |
+
@app.on_event("shutdown")
|
| 116 |
+
async def shutdown_event():
|
| 117 |
+
"""Cleanup on shutdown."""
|
| 118 |
+
print("VoiceGuard API shutting down...")
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# ============== MAIN ==============
|
| 122 |
+
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
+
import uvicorn
|
| 125 |
+
uvicorn.run(
|
| 126 |
+
"app.main:app",
|
| 127 |
+
host=settings.HOST,
|
| 128 |
+
port=settings.PORT,
|
| 129 |
+
reload=settings.DEBUG
|
| 130 |
+
)
|
app/utils/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Heatmap Visualization Module.
|
| 2 |
+
|
| 3 |
+
Generates spectrogram heatmaps with artifact highlighting.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import uuid
|
| 8 |
+
import numpy as np
|
| 9 |
+
import matplotlib
|
| 10 |
+
matplotlib.use('Agg') # Non-GUI backend
|
| 11 |
+
import matplotlib.pyplot as plt
|
| 12 |
+
import librosa
|
| 13 |
+
import librosa.display
|
| 14 |
+
from typing import Optional, Tuple
|
| 15 |
+
|
| 16 |
+
from app.config import settings
|
| 17 |
+
from app.core.signal_analyzer import SignalScores
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class HeatmapGenerator:
|
| 21 |
+
"""Generates spectrogram heatmaps with attention overlays."""
|
| 22 |
+
|
| 23 |
+
def __init__(self, output_dir: str = None):
|
| 24 |
+
"""Initialize generator.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
output_dir: Directory to save heatmap images
|
| 28 |
+
"""
|
| 29 |
+
self.output_dir = output_dir or settings.HEATMAP_DIR
|
| 30 |
+
os.makedirs(self.output_dir, exist_ok=True)
|
| 31 |
+
|
| 32 |
+
def generate_spectrogram(
|
| 33 |
+
self,
|
| 34 |
+
waveform: np.ndarray,
|
| 35 |
+
sr: int = 16000
|
| 36 |
+
) -> np.ndarray:
|
| 37 |
+
"""Generate Mel spectrogram from waveform.
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
waveform: Audio waveform
|
| 41 |
+
sr: Sample rate
|
| 42 |
+
|
| 43 |
+
Returns:
|
| 44 |
+
Mel spectrogram in dB scale
|
| 45 |
+
"""
|
| 46 |
+
mel_spec = librosa.feature.melspectrogram(
|
| 47 |
+
y=waveform,
|
| 48 |
+
sr=sr,
|
| 49 |
+
n_mels=128,
|
| 50 |
+
fmax=8000
|
| 51 |
+
)
|
| 52 |
+
mel_db = librosa.power_to_db(mel_spec, ref=np.max)
|
| 53 |
+
return mel_db
|
| 54 |
+
|
| 55 |
+
def create_attention_overlay(
|
| 56 |
+
self,
|
| 57 |
+
spectrogram: np.ndarray,
|
| 58 |
+
scores: SignalScores,
|
| 59 |
+
duration: float
|
| 60 |
+
) -> np.ndarray:
|
| 61 |
+
"""Create attention overlay showing artifact regions.
|
| 62 |
+
|
| 63 |
+
This creates a heatmap based on signal scores to highlight
|
| 64 |
+
suspicious regions in the spectrogram.
|
| 65 |
+
|
| 66 |
+
Args:
|
| 67 |
+
spectrogram: Mel spectrogram
|
| 68 |
+
scores: Signal analysis scores
|
| 69 |
+
duration: Audio duration in seconds
|
| 70 |
+
|
| 71 |
+
Returns:
|
| 72 |
+
Attention weights array (same shape as spectrogram)
|
| 73 |
+
"""
|
| 74 |
+
n_mels, n_frames = spectrogram.shape
|
| 75 |
+
attention = np.zeros((n_mels, n_frames))
|
| 76 |
+
|
| 77 |
+
# Average score determines overall intensity
|
| 78 |
+
avg_score = (
|
| 79 |
+
scores.prosody_score +
|
| 80 |
+
scores.breath_score +
|
| 81 |
+
scores.spectral_score +
|
| 82 |
+
scores.formant_score +
|
| 83 |
+
scores.silence_score
|
| 84 |
+
) / 5
|
| 85 |
+
|
| 86 |
+
# Create regions based on individual scores
|
| 87 |
+
frame_third = n_frames // 3
|
| 88 |
+
|
| 89 |
+
# Early region (prosody/rhythm issues)
|
| 90 |
+
if scores.prosody_score > 0.5:
|
| 91 |
+
attention[:, :frame_third] += scores.prosody_score * 0.6
|
| 92 |
+
|
| 93 |
+
# Mid region (breath/silence issues)
|
| 94 |
+
if scores.breath_score > 0.5:
|
| 95 |
+
attention[:, frame_third:2*frame_third] += scores.breath_score * 0.8
|
| 96 |
+
|
| 97 |
+
if scores.silence_score > 0.5:
|
| 98 |
+
attention[64:, frame_third:2*frame_third] += scores.silence_score * 0.5
|
| 99 |
+
|
| 100 |
+
# Late region (formant issues)
|
| 101 |
+
if scores.formant_score > 0.5:
|
| 102 |
+
attention[:64, 2*frame_third:] += scores.formant_score * 0.6
|
| 103 |
+
|
| 104 |
+
# Spectral artifacts throughout
|
| 105 |
+
if scores.spectral_score > 0.5:
|
| 106 |
+
# Focus on middle frequencies where vocoder artifacts appear
|
| 107 |
+
attention[32:96, :] += scores.spectral_score * 0.4
|
| 108 |
+
|
| 109 |
+
# Normalize to 0-1
|
| 110 |
+
if np.max(attention) > 0:
|
| 111 |
+
attention = attention / np.max(attention)
|
| 112 |
+
|
| 113 |
+
# Apply some smoothing
|
| 114 |
+
from scipy.ndimage import gaussian_filter
|
| 115 |
+
attention = gaussian_filter(attention, sigma=3)
|
| 116 |
+
|
| 117 |
+
return attention
|
| 118 |
+
|
| 119 |
+
def create_heatmap(
|
| 120 |
+
self,
|
| 121 |
+
waveform: np.ndarray,
|
| 122 |
+
scores: SignalScores,
|
| 123 |
+
duration: float,
|
| 124 |
+
classification: str,
|
| 125 |
+
sr: int = 16000
|
| 126 |
+
) -> str:
|
| 127 |
+
"""Create and save heatmap visualization.
|
| 128 |
+
|
| 129 |
+
Args:
|
| 130 |
+
waveform: Audio waveform
|
| 131 |
+
scores: Signal analysis scores
|
| 132 |
+
duration: Audio duration
|
| 133 |
+
classification: Detection result
|
| 134 |
+
sr: Sample rate
|
| 135 |
+
|
| 136 |
+
Returns:
|
| 137 |
+
Path to saved heatmap image
|
| 138 |
+
"""
|
| 139 |
+
# Generate spectrogram
|
| 140 |
+
mel_db = self.generate_spectrogram(waveform, sr)
|
| 141 |
+
|
| 142 |
+
# Create attention overlay
|
| 143 |
+
attention = self.create_attention_overlay(mel_db, scores, duration)
|
| 144 |
+
|
| 145 |
+
# Create figure
|
| 146 |
+
fig, ax = plt.subplots(figsize=(12, 4))
|
| 147 |
+
|
| 148 |
+
# Plot spectrogram
|
| 149 |
+
img = librosa.display.specshow(
|
| 150 |
+
mel_db,
|
| 151 |
+
sr=sr,
|
| 152 |
+
x_axis='time',
|
| 153 |
+
y_axis='mel',
|
| 154 |
+
ax=ax,
|
| 155 |
+
cmap='viridis'
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# Overlay attention heatmap
|
| 159 |
+
# Red for AI, green for human
|
| 160 |
+
cmap = 'Reds' if classification == "AI_GENERATED" else 'Greens'
|
| 161 |
+
ax.imshow(
|
| 162 |
+
attention,
|
| 163 |
+
aspect='auto',
|
| 164 |
+
alpha=0.5,
|
| 165 |
+
cmap=cmap,
|
| 166 |
+
extent=[0, duration, 0, mel_db.shape[0]],
|
| 167 |
+
origin='lower'
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
# Styling
|
| 171 |
+
ax.set_title(
|
| 172 |
+
f'Detection Heatmap - {classification}',
|
| 173 |
+
fontsize=14,
|
| 174 |
+
fontweight='bold',
|
| 175 |
+
color='white' if classification == "AI_GENERATED" else 'darkgreen'
|
| 176 |
+
)
|
| 177 |
+
ax.set_xlabel('Time (seconds)')
|
| 178 |
+
ax.set_ylabel('Mel Frequency')
|
| 179 |
+
|
| 180 |
+
# Colorbar
|
| 181 |
+
plt.colorbar(img, ax=ax, format='%+2.0f dB')
|
| 182 |
+
|
| 183 |
+
# Background color
|
| 184 |
+
fig.patch.set_facecolor('#1e293b')
|
| 185 |
+
ax.set_facecolor('#1e293b')
|
| 186 |
+
ax.tick_params(colors='#94a3b8')
|
| 187 |
+
ax.xaxis.label.set_color('#f1f5f9')
|
| 188 |
+
ax.yaxis.label.set_color('#f1f5f9')
|
| 189 |
+
|
| 190 |
+
plt.tight_layout()
|
| 191 |
+
|
| 192 |
+
# Save
|
| 193 |
+
filename = f"{uuid.uuid4().hex[:12]}.png"
|
| 194 |
+
filepath = os.path.join(self.output_dir, filename)
|
| 195 |
+
|
| 196 |
+
fig.savefig(
|
| 197 |
+
filepath,
|
| 198 |
+
dpi=150,
|
| 199 |
+
facecolor='#1e293b',
|
| 200 |
+
edgecolor='none',
|
| 201 |
+
bbox_inches='tight'
|
| 202 |
+
)
|
| 203 |
+
plt.close(fig)
|
| 204 |
+
|
| 205 |
+
return filename
|
| 206 |
+
|
| 207 |
+
def get_heatmap_url(self, filename: str) -> str:
|
| 208 |
+
"""Get API URL for heatmap.
|
| 209 |
+
|
| 210 |
+
Args:
|
| 211 |
+
filename: Heatmap filename
|
| 212 |
+
|
| 213 |
+
Returns:
|
| 214 |
+
API URL path
|
| 215 |
+
"""
|
| 216 |
+
return f"/api/v1/heatmap/{filename}"
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
# Create singleton instance
|
| 220 |
+
heatmap_generator = HeatmapGenerator()
|
frontend/css/animations.css
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
| 14 |
+
/* Slide In Up */
|
| 15 |
+
@keyframes slideInUp {
|
| 16 |
+
from {
|
| 17 |
+
transform: translateY(20px);
|
| 18 |
+
opacity: 0;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
to {
|
| 22 |
+
transform: translateY(0);
|
| 23 |
+
opacity: 1;
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
/* Slide In Down */
|
| 28 |
+
@keyframes slideInDown {
|
| 29 |
+
from {
|
| 30 |
+
transform: translateY(-20px);
|
| 31 |
+
opacity: 0;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
to {
|
| 35 |
+
transform: translateY(0);
|
| 36 |
+
opacity: 1;
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
/* Pulse */
|
| 41 |
+
@keyframes pulse {
|
| 42 |
+
|
| 43 |
+
0%,
|
| 44 |
+
100% {
|
| 45 |
+
opacity: 1;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
50% {
|
| 49 |
+
opacity: 0.4;
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
/* Spin */
|
| 54 |
+
@keyframes spin {
|
| 55 |
+
from {
|
| 56 |
+
transform: rotate(0deg);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
to {
|
| 60 |
+
transform: rotate(360deg);
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
/* Shake (for errors) */
|
| 65 |
+
@keyframes shake {
|
| 66 |
+
|
| 67 |
+
0%,
|
| 68 |
+
100% {
|
| 69 |
+
transform: translateX(0);
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
10%,
|
| 73 |
+
30%,
|
| 74 |
+
50%,
|
| 75 |
+
70%,
|
| 76 |
+
90% {
|
| 77 |
+
transform: translateX(-5px);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
20%,
|
| 81 |
+
40%,
|
| 82 |
+
60%,
|
| 83 |
+
80% {
|
| 84 |
+
transform: translateX(5px);
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
/* Scale In */
|
| 89 |
+
@keyframes scaleIn {
|
| 90 |
+
from {
|
| 91 |
+
transform: scale(0.9);
|
| 92 |
+
opacity: 0;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
to {
|
| 96 |
+
transform: scale(1);
|
| 97 |
+
opacity: 1;
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
/* Bounce In */
|
| 102 |
+
@keyframes bounceIn {
|
| 103 |
+
0% {
|
| 104 |
+
transform: scale(0.3);
|
| 105 |
+
opacity: 0;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
50% {
|
| 109 |
+
transform: scale(1.05);
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
70% {
|
| 113 |
+
transform: scale(0.9);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
100% {
|
| 117 |
+
transform: scale(1);
|
| 118 |
+
opacity: 1;
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
/* Glow Pulse */
|
| 123 |
+
@keyframes glowPulse {
|
| 124 |
+
|
| 125 |
+
0%,
|
| 126 |
+
100% {
|
| 127 |
+
box-shadow: 0 0 5px var(--glow-color, rgba(99, 102, 241, 0.3));
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
50% {
|
| 131 |
+
box-shadow: 0 0 20px var(--glow-color, rgba(99, 102, 241, 0.6));
|
| 132 |
+
}
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/* Progress Fill */
|
| 136 |
+
@keyframes fillProgress {
|
| 137 |
+
from {
|
| 138 |
+
width: 0%;
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
to {
|
| 142 |
+
width: var(--progress-width, 100%);
|
| 143 |
+
}
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
/* ==================== UTILITY CLASSES ==================== */
|
| 147 |
+
|
| 148 |
+
.animate-fadeIn {
|
| 149 |
+
animation: fadeIn var(--duration-normal) var(--ease-out);
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
.animate-slideInUp {
|
| 153 |
+
animation: slideInUp var(--duration-normal) var(--ease-out);
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
.animate-pulse {
|
| 157 |
+
animation: pulse 2s infinite;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
.animate-spin {
|
| 161 |
+
animation: spin 1s linear infinite;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
.animate-shake {
|
| 165 |
+
animation: shake 0.5s ease-in-out;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
.animate-bounceIn {
|
| 169 |
+
animation: bounceIn 0.5s var(--ease-out);
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
/* Spinner SVG Animation */
|
| 173 |
+
.spinner {
|
| 174 |
+
animation: spin 1s linear infinite;
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
/* Delay Classes */
|
| 178 |
+
.animation-delay-100 {
|
| 179 |
+
animation-delay: 100ms;
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
.animation-delay-200 {
|
| 183 |
+
animation-delay: 200ms;
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
.animation-delay-300 {
|
| 187 |
+
animation-delay: 300ms;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
.animation-delay-500 {
|
| 191 |
+
animation-delay: 500ms;
|
| 192 |
+
}
|
frontend/css/base.css
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* ==================== BASE STYLES ==================== */
|
| 2 |
+
/* Reset, Typography, and Global Styles */
|
| 3 |
+
|
| 4 |
+
/* Box Sizing Reset */
|
| 5 |
+
*,
|
| 6 |
+
*::before,
|
| 7 |
+
*::after {
|
| 8 |
+
box-sizing: border-box;
|
| 9 |
+
margin: 0;
|
| 10 |
+
padding: 0;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
/* Root Element */
|
| 14 |
+
html {
|
| 15 |
+
font-size: 16px;
|
| 16 |
+
-webkit-font-smoothing: antialiased;
|
| 17 |
+
-moz-osx-font-smoothing: grayscale;
|
| 18 |
+
text-rendering: optimizeLegibility;
|
| 19 |
+
scroll-behavior: smooth;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
/* Body */
|
| 23 |
+
body {
|
| 24 |
+
font-family: var(--font-sans);
|
| 25 |
+
font-size: var(--text-base);
|
| 26 |
+
font-weight: var(--font-normal);
|
| 27 |
+
line-height: var(--leading-normal);
|
| 28 |
+
color: var(--color-text);
|
| 29 |
+
background: var(--color-bg);
|
| 30 |
+
min-height: 100vh;
|
| 31 |
+
overflow-x: hidden;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
/* Background Gradient */
|
| 35 |
+
body::before {
|
| 36 |
+
content: '';
|
| 37 |
+
position: fixed;
|
| 38 |
+
inset: 0;
|
| 39 |
+
background:
|
| 40 |
+
radial-gradient(ellipse 80% 50% at 50% -20%, rgba(99, 102, 241, 0.15), transparent),
|
| 41 |
+
radial-gradient(ellipse 60% 40% at 100% 100%, rgba(139, 92, 246, 0.1), transparent);
|
| 42 |
+
pointer-events: none;
|
| 43 |
+
z-index: -1;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
/* ==================== TYPOGRAPHY ==================== */
|
| 47 |
+
|
| 48 |
+
h1,
|
| 49 |
+
h2,
|
| 50 |
+
h3,
|
| 51 |
+
h4,
|
| 52 |
+
h5,
|
| 53 |
+
h6 {
|
| 54 |
+
color: var(--color-text);
|
| 55 |
+
font-weight: var(--font-bold);
|
| 56 |
+
line-height: var(--leading-tight);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
h1 {
|
| 60 |
+
font-size: var(--text-4xl);
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
h2 {
|
| 64 |
+
font-size: var(--text-2xl);
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
h3 {
|
| 68 |
+
font-size: var(--text-xl);
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
h4 {
|
| 72 |
+
font-size: var(--text-lg);
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
p {
|
| 76 |
+
color: var(--color-text-muted);
|
| 77 |
+
line-height: var(--leading-relaxed);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
strong {
|
| 81 |
+
font-weight: var(--font-semibold);
|
| 82 |
+
color: var(--color-text);
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
/* ==================== LINKS ==================== */
|
| 86 |
+
|
| 87 |
+
a {
|
| 88 |
+
color: var(--color-primary-400);
|
| 89 |
+
text-decoration: none;
|
| 90 |
+
transition: color var(--duration-fast) var(--ease-out);
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
a:hover {
|
| 94 |
+
color: var(--color-primary-300);
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
/* ==================== FOCUS STATES ==================== */
|
| 98 |
+
|
| 99 |
+
:focus {
|
| 100 |
+
outline: none;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
:focus-visible {
|
| 104 |
+
outline: 2px solid var(--color-primary-500);
|
| 105 |
+
outline-offset: 2px;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
/* ==================== SELECTION ==================== */
|
| 109 |
+
|
| 110 |
+
::selection {
|
| 111 |
+
background-color: var(--color-primary-600);
|
| 112 |
+
color: white;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
/* ==================== BUTTONS (Reset) ==================== */
|
| 116 |
+
|
| 117 |
+
button {
|
| 118 |
+
font-family: inherit;
|
| 119 |
+
font-size: inherit;
|
| 120 |
+
cursor: pointer;
|
| 121 |
+
border: none;
|
| 122 |
+
background: none;
|
| 123 |
+
color: inherit;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
button:disabled {
|
| 127 |
+
cursor: not-allowed;
|
| 128 |
+
opacity: 0.5;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
/* ==================== INPUTS (Reset) ==================== */
|
| 132 |
+
|
| 133 |
+
input,
|
| 134 |
+
textarea,
|
| 135 |
+
select {
|
| 136 |
+
font-family: inherit;
|
| 137 |
+
font-size: inherit;
|
| 138 |
+
color: inherit;
|
| 139 |
+
background: transparent;
|
| 140 |
+
border: none;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
input[type="file"] {
|
| 144 |
+
cursor: pointer;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
input[type="radio"] {
|
| 148 |
+
appearance: none;
|
| 149 |
+
-webkit-appearance: none;
|
| 150 |
+
position: absolute;
|
| 151 |
+
opacity: 0;
|
| 152 |
+
width: 0;
|
| 153 |
+
height: 0;
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
/* ==================== LISTS ==================== */
|
| 157 |
+
|
| 158 |
+
ul,
|
| 159 |
+
ol {
|
| 160 |
+
list-style: none;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
/* ==================== IMAGES ==================== */
|
| 164 |
+
|
| 165 |
+
img,
|
| 166 |
+
svg {
|
| 167 |
+
display: block;
|
| 168 |
+
max-width: 100%;
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
/* ==================== SCROLLBAR ==================== */
|
| 172 |
+
|
| 173 |
+
::-webkit-scrollbar {
|
| 174 |
+
width: 8px;
|
| 175 |
+
height: 8px;
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
::-webkit-scrollbar-track {
|
| 179 |
+
background: var(--color-neutral-900);
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
::-webkit-scrollbar-thumb {
|
| 183 |
+
background: var(--color-neutral-700);
|
| 184 |
+
border-radius: var(--radius-full);
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
::-webkit-scrollbar-thumb:hover {
|
| 188 |
+
background: var(--color-neutral-600);
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
/* ==================== UTILITIES ==================== */
|
| 192 |
+
|
| 193 |
+
.sr-only {
|
| 194 |
+
position: absolute;
|
| 195 |
+
width: 1px;
|
| 196 |
+
height: 1px;
|
| 197 |
+
padding: 0;
|
| 198 |
+
margin: -1px;
|
| 199 |
+
overflow: hidden;
|
| 200 |
+
clip: rect(0, 0, 0, 0);
|
| 201 |
+
white-space: nowrap;
|
| 202 |
+
border: 0;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
[hidden] {
|
| 206 |
+
display: none !important;
|
| 207 |
+
}
|
frontend/css/components.css
ADDED
|
@@ -0,0 +1,555 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* ==================== COMPONENT STYLES ==================== */
|
| 2 |
+
|
| 3 |
+
/* ==================== APP LAYOUT ==================== */
|
| 4 |
+
|
| 5 |
+
.app {
|
| 6 |
+
display: flex;
|
| 7 |
+
flex-direction: column;
|
| 8 |
+
min-height: 100vh;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
.main {
|
| 12 |
+
flex: 1;
|
| 13 |
+
width: 100%;
|
| 14 |
+
max-width: 1200px;
|
| 15 |
+
margin: 0 auto;
|
| 16 |
+
padding: var(--space-6);
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
/* ==================== HEADER ==================== */
|
| 20 |
+
|
| 21 |
+
.header {
|
| 22 |
+
display: flex;
|
| 23 |
+
align-items: center;
|
| 24 |
+
justify-content: space-between;
|
| 25 |
+
padding: var(--space-4) var(--space-6);
|
| 26 |
+
background: rgba(15, 23, 42, 0.8);
|
| 27 |
+
backdrop-filter: blur(12px);
|
| 28 |
+
border-bottom: 1px solid var(--color-border);
|
| 29 |
+
position: sticky;
|
| 30 |
+
top: 0;
|
| 31 |
+
z-index: var(--z-sticky);
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
.header__brand {
|
| 35 |
+
display: flex;
|
| 36 |
+
align-items: center;
|
| 37 |
+
gap: var(--space-3);
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
.header__logo {
|
| 41 |
+
font-size: var(--text-2xl);
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
.header__title {
|
| 45 |
+
font-size: var(--text-xl);
|
| 46 |
+
font-weight: var(--font-bold);
|
| 47 |
+
background: linear-gradient(135deg, var(--color-primary-400), var(--color-primary-200));
|
| 48 |
+
-webkit-background-clip: text;
|
| 49 |
+
-webkit-text-fill-color: transparent;
|
| 50 |
+
background-clip: text;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
/* Status Indicator */
|
| 54 |
+
.status-indicator {
|
| 55 |
+
display: flex;
|
| 56 |
+
align-items: center;
|
| 57 |
+
gap: var(--space-2);
|
| 58 |
+
padding: var(--space-2) var(--space-3);
|
| 59 |
+
background: var(--color-bg-elevated);
|
| 60 |
+
border-radius: var(--radius-full);
|
| 61 |
+
font-size: var(--text-sm);
|
| 62 |
+
color: var(--color-text-muted);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
.status-indicator__dot {
|
| 66 |
+
width: 8px;
|
| 67 |
+
height: 8px;
|
| 68 |
+
border-radius: 50%;
|
| 69 |
+
background: var(--color-warning-500);
|
| 70 |
+
animation: pulse 2s infinite;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
.status-indicator.is-online .status-indicator__dot {
|
| 74 |
+
background: var(--color-success-500);
|
| 75 |
+
animation: none;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
.status-indicator.is-offline .status-indicator__dot {
|
| 79 |
+
background: var(--color-danger-500);
|
| 80 |
+
animation: none;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
/* ==================== HERO SECTION ==================== */
|
| 84 |
+
|
| 85 |
+
.hero {
|
| 86 |
+
text-align: center;
|
| 87 |
+
padding: var(--space-10) var(--space-4);
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
.hero__title {
|
| 91 |
+
font-size: var(--text-4xl);
|
| 92 |
+
font-weight: var(--font-bold);
|
| 93 |
+
margin-bottom: var(--space-4);
|
| 94 |
+
background: linear-gradient(135deg, #fff 0%, var(--color-neutral-300) 100%);
|
| 95 |
+
-webkit-background-clip: text;
|
| 96 |
+
-webkit-text-fill-color: transparent;
|
| 97 |
+
background-clip: text;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
.hero__subtitle {
|
| 101 |
+
font-size: var(--text-lg);
|
| 102 |
+
color: var(--color-text-muted);
|
| 103 |
+
max-width: 600px;
|
| 104 |
+
margin: 0 auto;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
/* ==================== CONTENT GRID ==================== */
|
| 108 |
+
|
| 109 |
+
.content-grid {
|
| 110 |
+
display: grid;
|
| 111 |
+
grid-template-columns: 1fr;
|
| 112 |
+
gap: var(--space-6);
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
@media (min-width: 1024px) {
|
| 116 |
+
.content-grid {
|
| 117 |
+
grid-template-columns: 1fr 1fr;
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
/* ==================== PANELS ==================== */
|
| 122 |
+
|
| 123 |
+
.panel {
|
| 124 |
+
background: var(--color-bg-elevated);
|
| 125 |
+
border: 1px solid var(--color-border);
|
| 126 |
+
border-radius: var(--radius-2xl);
|
| 127 |
+
padding: var(--space-6);
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
.panel__title {
|
| 131 |
+
font-size: var(--text-lg);
|
| 132 |
+
font-weight: var(--font-semibold);
|
| 133 |
+
margin-bottom: var(--space-6);
|
| 134 |
+
color: var(--color-text);
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
/* ==================== FILE UPLOAD ==================== */
|
| 138 |
+
|
| 139 |
+
.file-upload {
|
| 140 |
+
margin-bottom: var(--space-6);
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
.file-upload__dropzone {
|
| 144 |
+
display: flex;
|
| 145 |
+
flex-direction: column;
|
| 146 |
+
align-items: center;
|
| 147 |
+
justify-content: center;
|
| 148 |
+
padding: var(--space-10) var(--space-6);
|
| 149 |
+
border: 2px dashed var(--color-border);
|
| 150 |
+
border-radius: var(--radius-xl);
|
| 151 |
+
background: var(--color-bg-card);
|
| 152 |
+
transition: all var(--duration-normal) var(--ease-out);
|
| 153 |
+
cursor: pointer;
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
.file-upload__dropzone:hover,
|
| 157 |
+
.file-upload__dropzone.is-dragging {
|
| 158 |
+
border-color: var(--color-primary-500);
|
| 159 |
+
background: rgba(99, 102, 241, 0.05);
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
.file-upload__dropzone.is-dragging {
|
| 163 |
+
transform: scale(1.02);
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
.file-upload__dropzone.has-error {
|
| 167 |
+
border-color: var(--color-danger-500);
|
| 168 |
+
animation: shake 0.5s ease-in-out;
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
.file-upload__icon {
|
| 172 |
+
color: var(--color-text-muted);
|
| 173 |
+
margin-bottom: var(--space-4);
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
.file-upload__text {
|
| 177 |
+
font-size: var(--text-lg);
|
| 178 |
+
font-weight: var(--font-medium);
|
| 179 |
+
color: var(--color-text);
|
| 180 |
+
margin-bottom: var(--space-2);
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
.file-upload__or {
|
| 184 |
+
color: var(--color-text-subtle);
|
| 185 |
+
font-size: var(--text-sm);
|
| 186 |
+
margin-bottom: var(--space-3);
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
.file-upload__browse {
|
| 190 |
+
padding: var(--space-2) var(--space-4);
|
| 191 |
+
background: var(--color-primary-600);
|
| 192 |
+
color: white;
|
| 193 |
+
border-radius: var(--radius-lg);
|
| 194 |
+
font-weight: var(--font-medium);
|
| 195 |
+
transition: all var(--duration-fast) var(--ease-out);
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
.file-upload__browse:hover {
|
| 199 |
+
background: var(--color-primary-500);
|
| 200 |
+
transform: translateY(-1px);
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
.file-upload__hint {
|
| 204 |
+
margin-top: var(--space-4);
|
| 205 |
+
font-size: var(--text-xs);
|
| 206 |
+
color: var(--color-text-subtle);
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
/* File Preview */
|
| 210 |
+
.file-preview {
|
| 211 |
+
display: flex;
|
| 212 |
+
align-items: center;
|
| 213 |
+
justify-content: space-between;
|
| 214 |
+
padding: var(--space-4);
|
| 215 |
+
background: var(--color-bg-card);
|
| 216 |
+
border: 1px solid var(--color-border);
|
| 217 |
+
border-radius: var(--radius-xl);
|
| 218 |
+
animation: slideInUp var(--duration-normal) var(--ease-out);
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
.file-preview__info {
|
| 222 |
+
display: flex;
|
| 223 |
+
align-items: center;
|
| 224 |
+
gap: var(--space-3);
|
| 225 |
+
color: var(--color-primary-400);
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
.file-preview__name {
|
| 229 |
+
font-weight: var(--font-medium);
|
| 230 |
+
color: var(--color-text);
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
.file-preview__meta {
|
| 234 |
+
font-size: var(--text-sm);
|
| 235 |
+
color: var(--color-text-muted);
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
.file-preview__remove {
|
| 239 |
+
padding: var(--space-2);
|
| 240 |
+
color: var(--color-text-muted);
|
| 241 |
+
border-radius: var(--radius-lg);
|
| 242 |
+
transition: all var(--duration-fast) var(--ease-out);
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
.file-preview__remove:hover {
|
| 246 |
+
background: var(--color-danger-500);
|
| 247 |
+
color: white;
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
/* ==================== LANGUAGE SELECTOR ==================== */
|
| 251 |
+
|
| 252 |
+
.language-selector {
|
| 253 |
+
margin-bottom: var(--space-6);
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
.language-selector__label {
|
| 257 |
+
display: block;
|
| 258 |
+
font-size: var(--text-sm);
|
| 259 |
+
font-weight: var(--font-medium);
|
| 260 |
+
color: var(--color-text-muted);
|
| 261 |
+
margin-bottom: var(--space-2);
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
.language-selector__select {
|
| 265 |
+
width: 100%;
|
| 266 |
+
appearance: none;
|
| 267 |
+
-webkit-appearance: none;
|
| 268 |
+
padding: var(--space-3) var(--space-4);
|
| 269 |
+
padding-right: var(--space-10);
|
| 270 |
+
background: var(--color-bg-card);
|
| 271 |
+
border: 1px solid var(--color-border);
|
| 272 |
+
border-radius: var(--radius-lg);
|
| 273 |
+
font-size: var(--text-base);
|
| 274 |
+
color: var(--color-text);
|
| 275 |
+
cursor: pointer;
|
| 276 |
+
transition: all var(--duration-fast) var(--ease-out);
|
| 277 |
+
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
|
| 278 |
+
background-repeat: no-repeat;
|
| 279 |
+
background-position: right 12px center;
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
.language-selector__select:hover {
|
| 283 |
+
border-color: var(--color-border-hover);
|
| 284 |
+
background-color: rgba(99, 102, 241, 0.05);
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
.language-selector__select:focus {
|
| 288 |
+
border-color: var(--color-primary-500);
|
| 289 |
+
box-shadow: 0 0 0 1px var(--color-primary-500);
|
| 290 |
+
outline: none;
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
.language-selector__select option {
|
| 294 |
+
background: var(--color-bg-elevated);
|
| 295 |
+
color: var(--color-text);
|
| 296 |
+
padding: var(--space-2);
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
/* ==================== ANALYZE BUTTON ==================== */
|
| 300 |
+
|
| 301 |
+
.analyze-btn {
|
| 302 |
+
width: 100%;
|
| 303 |
+
display: flex;
|
| 304 |
+
align-items: center;
|
| 305 |
+
justify-content: center;
|
| 306 |
+
gap: var(--space-2);
|
| 307 |
+
padding: var(--space-4);
|
| 308 |
+
background: linear-gradient(135deg, var(--color-primary-600), var(--color-primary-700));
|
| 309 |
+
color: white;
|
| 310 |
+
font-size: var(--text-lg);
|
| 311 |
+
font-weight: var(--font-semibold);
|
| 312 |
+
border-radius: var(--radius-xl);
|
| 313 |
+
transition: all var(--duration-normal) var(--ease-out);
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
.analyze-btn:hover:not(:disabled) {
|
| 317 |
+
background: linear-gradient(135deg, var(--color-primary-500), var(--color-primary-600));
|
| 318 |
+
transform: translateY(-2px);
|
| 319 |
+
box-shadow: var(--shadow-glow-primary);
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
.analyze-btn:disabled {
|
| 323 |
+
background: var(--color-neutral-700);
|
| 324 |
+
cursor: not-allowed;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
.analyze-btn.is-loading {
|
| 328 |
+
pointer-events: none;
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
.analyze-btn__idle,
|
| 332 |
+
.analyze-btn__loading {
|
| 333 |
+
display: flex;
|
| 334 |
+
align-items: center;
|
| 335 |
+
gap: var(--space-2);
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
/* ==================== RESULTS PANEL ==================== */
|
| 339 |
+
|
| 340 |
+
.results-empty {
|
| 341 |
+
display: flex;
|
| 342 |
+
flex-direction: column;
|
| 343 |
+
align-items: center;
|
| 344 |
+
justify-content: center;
|
| 345 |
+
padding: var(--space-12) var(--space-6);
|
| 346 |
+
text-align: center;
|
| 347 |
+
color: var(--color-text-subtle);
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
.results-empty svg {
|
| 351 |
+
margin-bottom: var(--space-4);
|
| 352 |
+
opacity: 0.5;
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
.results-content {
|
| 356 |
+
animation: fadeIn var(--duration-normal) var(--ease-out);
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
/* Verdict Card */
|
| 360 |
+
.verdict-card {
|
| 361 |
+
display: flex;
|
| 362 |
+
flex-direction: column;
|
| 363 |
+
align-items: center;
|
| 364 |
+
padding: var(--space-8);
|
| 365 |
+
border-radius: var(--radius-xl);
|
| 366 |
+
margin-bottom: var(--space-6);
|
| 367 |
+
animation: slideInUp var(--duration-slow) var(--ease-out);
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
.verdict-card.is-ai {
|
| 371 |
+
background: rgba(239, 68, 68, 0.1);
|
| 372 |
+
border: 1px solid var(--color-danger-500);
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
.verdict-card.is-human {
|
| 376 |
+
background: rgba(16, 185, 129, 0.1);
|
| 377 |
+
border: 1px solid var(--color-success-500);
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
.verdict-card__icon {
|
| 381 |
+
font-size: 48px;
|
| 382 |
+
margin-bottom: var(--space-3);
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
.verdict-card__label {
|
| 386 |
+
font-size: var(--text-2xl);
|
| 387 |
+
font-weight: var(--font-bold);
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
.verdict-card.is-ai .verdict-card__label {
|
| 391 |
+
color: var(--color-danger-400);
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
.verdict-card.is-human .verdict-card__label {
|
| 395 |
+
color: var(--color-success-400);
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
/* Confidence Meter */
|
| 399 |
+
.confidence-meter {
|
| 400 |
+
margin-bottom: var(--space-4);
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
.confidence-meter__header {
|
| 404 |
+
display: flex;
|
| 405 |
+
justify-content: space-between;
|
| 406 |
+
margin-bottom: var(--space-2);
|
| 407 |
+
font-size: var(--text-sm);
|
| 408 |
+
color: var(--color-text-muted);
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
+
.confidence-meter__value {
|
| 412 |
+
font-weight: var(--font-bold);
|
| 413 |
+
color: var(--color-text);
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
.confidence-meter__track {
|
| 417 |
+
height: 8px;
|
| 418 |
+
background: var(--color-bg-card);
|
| 419 |
+
border-radius: var(--radius-full);
|
| 420 |
+
overflow: hidden;
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
.confidence-meter__fill {
|
| 424 |
+
height: 100%;
|
| 425 |
+
background: linear-gradient(90deg, var(--color-primary-500), var(--color-primary-400));
|
| 426 |
+
border-radius: var(--radius-full);
|
| 427 |
+
width: 0%;
|
| 428 |
+
transition: width 1s var(--ease-out);
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
/* Info Row */
|
| 432 |
+
.info-row {
|
| 433 |
+
display: flex;
|
| 434 |
+
justify-content: space-between;
|
| 435 |
+
padding: var(--space-3) 0;
|
| 436 |
+
border-bottom: 1px solid var(--color-border);
|
| 437 |
+
font-size: var(--text-sm);
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
.info-row__label {
|
| 441 |
+
color: var(--color-text-muted);
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
.info-row__value {
|
| 445 |
+
font-weight: var(--font-medium);
|
| 446 |
+
color: var(--color-text);
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
/* Explanation Card */
|
| 450 |
+
.explanation-card {
|
| 451 |
+
margin-top: var(--space-4);
|
| 452 |
+
padding: var(--space-4);
|
| 453 |
+
background: var(--color-bg-card);
|
| 454 |
+
border-radius: var(--radius-lg);
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
.explanation-card__header {
|
| 458 |
+
display: flex;
|
| 459 |
+
align-items: center;
|
| 460 |
+
gap: var(--space-2);
|
| 461 |
+
margin-bottom: var(--space-3);
|
| 462 |
+
color: var(--color-primary-400);
|
| 463 |
+
font-size: var(--text-sm);
|
| 464 |
+
font-weight: var(--font-medium);
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
.explanation-card__text {
|
| 468 |
+
font-size: var(--text-sm);
|
| 469 |
+
color: var(--color-text-muted);
|
| 470 |
+
line-height: var(--leading-relaxed);
|
| 471 |
+
}
|
| 472 |
+
|
| 473 |
+
/* ==================== FOOTER ==================== */
|
| 474 |
+
|
| 475 |
+
.footer {
|
| 476 |
+
padding: var(--space-6);
|
| 477 |
+
text-align: center;
|
| 478 |
+
border-top: 1px solid var(--color-border);
|
| 479 |
+
color: var(--color-text-subtle);
|
| 480 |
+
font-size: var(--text-sm);
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
/* ==================== TOAST ==================== */
|
| 484 |
+
|
| 485 |
+
.toast-container {
|
| 486 |
+
position: fixed;
|
| 487 |
+
bottom: var(--space-6);
|
| 488 |
+
right: var(--space-6);
|
| 489 |
+
z-index: var(--z-toast);
|
| 490 |
+
display: flex;
|
| 491 |
+
flex-direction: column;
|
| 492 |
+
gap: var(--space-3);
|
| 493 |
+
}
|
| 494 |
+
|
| 495 |
+
.toast {
|
| 496 |
+
display: flex;
|
| 497 |
+
align-items: flex-start;
|
| 498 |
+
gap: var(--space-3);
|
| 499 |
+
padding: var(--space-4);
|
| 500 |
+
background: var(--color-bg-elevated);
|
| 501 |
+
border: 1px solid var(--color-border);
|
| 502 |
+
border-radius: var(--radius-lg);
|
| 503 |
+
box-shadow: var(--shadow-lg);
|
| 504 |
+
min-width: 300px;
|
| 505 |
+
max-width: 400px;
|
| 506 |
+
animation: slideInUp var(--duration-normal) var(--ease-out);
|
| 507 |
+
}
|
| 508 |
+
|
| 509 |
+
.toast--error {
|
| 510 |
+
border-color: var(--color-danger-500);
|
| 511 |
+
}
|
| 512 |
+
|
| 513 |
+
.toast--success {
|
| 514 |
+
border-color: var(--color-success-500);
|
| 515 |
+
}
|
| 516 |
+
|
| 517 |
+
.toast__icon {
|
| 518 |
+
flex-shrink: 0;
|
| 519 |
+
}
|
| 520 |
+
|
| 521 |
+
.toast--error .toast__icon {
|
| 522 |
+
color: var(--color-danger-400);
|
| 523 |
+
}
|
| 524 |
+
|
| 525 |
+
.toast--success .toast__icon {
|
| 526 |
+
color: var(--color-success-400);
|
| 527 |
+
}
|
| 528 |
+
|
| 529 |
+
.toast__content {
|
| 530 |
+
flex: 1;
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
.toast__title {
|
| 534 |
+
font-weight: var(--font-semibold);
|
| 535 |
+
color: var(--color-text);
|
| 536 |
+
margin-bottom: var(--space-1);
|
| 537 |
+
}
|
| 538 |
+
|
| 539 |
+
.toast__message {
|
| 540 |
+
font-size: var(--text-sm);
|
| 541 |
+
color: var(--color-text-muted);
|
| 542 |
+
}
|
| 543 |
+
|
| 544 |
+
.toast__close {
|
| 545 |
+
flex-shrink: 0;
|
| 546 |
+
padding: var(--space-1);
|
| 547 |
+
color: var(--color-text-muted);
|
| 548 |
+
border-radius: var(--radius-md);
|
| 549 |
+
transition: all var(--duration-fast) var(--ease-out);
|
| 550 |
+
}
|
| 551 |
+
|
| 552 |
+
.toast__close:hover {
|
| 553 |
+
background: var(--color-bg-card);
|
| 554 |
+
color: var(--color-text);
|
| 555 |
+
}
|
frontend/css/variables.css
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* ==================== CSS CUSTOM PROPERTIES ==================== */
|
| 2 |
+
/* Design Tokens for VoiceGuard UI */
|
| 3 |
+
|
| 4 |
+
:root {
|
| 5 |
+
/* ==================== COLORS ==================== */
|
| 6 |
+
|
| 7 |
+
/* Primary Palette (Indigo) */
|
| 8 |
+
--color-primary-50: #EEF2FF;
|
| 9 |
+
--color-primary-100: #E0E7FF;
|
| 10 |
+
--color-primary-200: #C7D2FE;
|
| 11 |
+
--color-primary-300: #A5B4FC;
|
| 12 |
+
--color-primary-400: #818CF8;
|
| 13 |
+
--color-primary-500: #6366F1;
|
| 14 |
+
--color-primary-600: #4F46E5;
|
| 15 |
+
--color-primary-700: #4338CA;
|
| 16 |
+
--color-primary-800: #3730A3;
|
| 17 |
+
--color-primary-900: #312E81;
|
| 18 |
+
|
| 19 |
+
/* Success (Human - Green) */
|
| 20 |
+
--color-success-50: #ECFDF5;
|
| 21 |
+
--color-success-100: #D1FAE5;
|
| 22 |
+
--color-success-200: #A7F3D0;
|
| 23 |
+
--color-success-300: #6EE7B7;
|
| 24 |
+
--color-success-400: #34D399;
|
| 25 |
+
--color-success-500: #10B981;
|
| 26 |
+
--color-success-600: #059669;
|
| 27 |
+
--color-success-700: #047857;
|
| 28 |
+
|
| 29 |
+
/* Danger (AI - Red) */
|
| 30 |
+
--color-danger-50: #FEF2F2;
|
| 31 |
+
--color-danger-100: #FEE2E2;
|
| 32 |
+
--color-danger-200: #FECACA;
|
| 33 |
+
--color-danger-300: #FCA5A5;
|
| 34 |
+
--color-danger-400: #F87171;
|
| 35 |
+
--color-danger-500: #EF4444;
|
| 36 |
+
--color-danger-600: #DC2626;
|
| 37 |
+
--color-danger-700: #B91C1C;
|
| 38 |
+
|
| 39 |
+
/* Warning (Yellow) */
|
| 40 |
+
--color-warning-500: #F59E0B;
|
| 41 |
+
--color-warning-600: #D97706;
|
| 42 |
+
|
| 43 |
+
/* Neutrals (Slate) */
|
| 44 |
+
--color-neutral-50: #F8FAFC;
|
| 45 |
+
--color-neutral-100: #F1F5F9;
|
| 46 |
+
--color-neutral-200: #E2E8F0;
|
| 47 |
+
--color-neutral-300: #CBD5E1;
|
| 48 |
+
--color-neutral-400: #94A3B8;
|
| 49 |
+
--color-neutral-500: #64748B;
|
| 50 |
+
--color-neutral-600: #475569;
|
| 51 |
+
--color-neutral-700: #334155;
|
| 52 |
+
--color-neutral-800: #1E293B;
|
| 53 |
+
--color-neutral-900: #0F172A;
|
| 54 |
+
--color-neutral-950: #020617;
|
| 55 |
+
|
| 56 |
+
/* Semantic Colors */
|
| 57 |
+
--color-bg: var(--color-neutral-950);
|
| 58 |
+
--color-bg-elevated: var(--color-neutral-900);
|
| 59 |
+
--color-bg-card: var(--color-neutral-800);
|
| 60 |
+
--color-text: var(--color-neutral-50);
|
| 61 |
+
--color-text-muted: var(--color-neutral-400);
|
| 62 |
+
--color-text-subtle: var(--color-neutral-500);
|
| 63 |
+
--color-border: var(--color-neutral-700);
|
| 64 |
+
--color-border-hover: var(--color-neutral-600);
|
| 65 |
+
|
| 66 |
+
/* ==================== TYPOGRAPHY ==================== */
|
| 67 |
+
|
| 68 |
+
--font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 69 |
+
--font-mono: 'Fira Code', 'Consolas', 'Monaco', monospace;
|
| 70 |
+
|
| 71 |
+
/* Font Sizes */
|
| 72 |
+
--text-xs: 0.75rem; /* 12px */
|
| 73 |
+
--text-sm: 0.875rem; /* 14px */
|
| 74 |
+
--text-base: 1rem; /* 16px */
|
| 75 |
+
--text-lg: 1.125rem; /* 18px */
|
| 76 |
+
--text-xl: 1.25rem; /* 20px */
|
| 77 |
+
--text-2xl: 1.5rem; /* 24px */
|
| 78 |
+
--text-3xl: 1.875rem; /* 30px */
|
| 79 |
+
--text-4xl: 2.25rem; /* 36px */
|
| 80 |
+
--text-5xl: 3rem; /* 48px */
|
| 81 |
+
|
| 82 |
+
/* Font Weights */
|
| 83 |
+
--font-normal: 400;
|
| 84 |
+
--font-medium: 500;
|
| 85 |
+
--font-semibold: 600;
|
| 86 |
+
--font-bold: 700;
|
| 87 |
+
|
| 88 |
+
/* Line Heights */
|
| 89 |
+
--leading-tight: 1.25;
|
| 90 |
+
--leading-snug: 1.375;
|
| 91 |
+
--leading-normal: 1.5;
|
| 92 |
+
--leading-relaxed: 1.625;
|
| 93 |
+
|
| 94 |
+
/* ==================== SPACING ==================== */
|
| 95 |
+
|
| 96 |
+
--space-0: 0;
|
| 97 |
+
--space-1: 0.25rem; /* 4px */
|
| 98 |
+
--space-2: 0.5rem; /* 8px */
|
| 99 |
+
--space-3: 0.75rem; /* 12px */
|
| 100 |
+
--space-4: 1rem; /* 16px */
|
| 101 |
+
--space-5: 1.25rem; /* 20px */
|
| 102 |
+
--space-6: 1.5rem; /* 24px */
|
| 103 |
+
--space-8: 2rem; /* 32px */
|
| 104 |
+
--space-10: 2.5rem; /* 40px */
|
| 105 |
+
--space-12: 3rem; /* 48px */
|
| 106 |
+
--space-16: 4rem; /* 64px */
|
| 107 |
+
--space-20: 5rem; /* 80px */
|
| 108 |
+
|
| 109 |
+
/* ==================== BORDERS ==================== */
|
| 110 |
+
|
| 111 |
+
--radius-sm: 0.25rem; /* 4px */
|
| 112 |
+
--radius-md: 0.375rem; /* 6px */
|
| 113 |
+
--radius-lg: 0.5rem; /* 8px */
|
| 114 |
+
--radius-xl: 0.75rem; /* 12px */
|
| 115 |
+
--radius-2xl: 1rem; /* 16px */
|
| 116 |
+
--radius-3xl: 1.5rem; /* 24px */
|
| 117 |
+
--radius-full: 9999px;
|
| 118 |
+
|
| 119 |
+
--border-width: 1px;
|
| 120 |
+
--border-width-2: 2px;
|
| 121 |
+
|
| 122 |
+
/* ==================== SHADOWS ==================== */
|
| 123 |
+
|
| 124 |
+
--shadow-xs: 0 1px 2px 0 rgb(0 0 0 / 0.05);
|
| 125 |
+
--shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
|
| 126 |
+
--shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
|
| 127 |
+
--shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
|
| 128 |
+
--shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
|
| 129 |
+
--shadow-2xl: 0 25px 50px -12px rgb(0 0 0 / 0.25);
|
| 130 |
+
|
| 131 |
+
--shadow-glow-primary: 0 0 20px rgba(99, 102, 241, 0.4);
|
| 132 |
+
--shadow-glow-success: 0 0 20px rgba(16, 185, 129, 0.4);
|
| 133 |
+
--shadow-glow-danger: 0 0 20px rgba(239, 68, 68, 0.4);
|
| 134 |
+
|
| 135 |
+
/* ==================== TRANSITIONS ==================== */
|
| 136 |
+
|
| 137 |
+
--duration-instant: 50ms;
|
| 138 |
+
--duration-fast: 150ms;
|
| 139 |
+
--duration-normal: 200ms;
|
| 140 |
+
--duration-slow: 300ms;
|
| 141 |
+
--duration-slower: 500ms;
|
| 142 |
+
|
| 143 |
+
--ease-linear: linear;
|
| 144 |
+
--ease-in: cubic-bezier(0.4, 0, 1, 1);
|
| 145 |
+
--ease-out: cubic-bezier(0, 0, 0.2, 1);
|
| 146 |
+
--ease-in-out: cubic-bezier(0.4, 0, 0.2, 1);
|
| 147 |
+
--ease-bounce: cubic-bezier(0.68, -0.55, 0.265, 1.55);
|
| 148 |
+
|
| 149 |
+
/* ==================== Z-INDEX ==================== */
|
| 150 |
+
|
| 151 |
+
--z-base: 0;
|
| 152 |
+
--z-dropdown: 100;
|
| 153 |
+
--z-sticky: 200;
|
| 154 |
+
--z-modal: 300;
|
| 155 |
+
--z-toast: 400;
|
| 156 |
+
--z-tooltip: 500;
|
| 157 |
+
|
| 158 |
+
/* ==================== BREAKPOINTS (for reference) ==================== */
|
| 159 |
+
/* Use in media queries: @media (min-width: 640px) */
|
| 160 |
+
/* --breakpoint-sm: 640px; */
|
| 161 |
+
/* --breakpoint-md: 768px; */
|
| 162 |
+
/* --breakpoint-lg: 1024px; */
|
| 163 |
+
/* --breakpoint-xl: 1280px; */
|
| 164 |
+
}
|
frontend/index.html
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
|
| 4 |
+
<head>
|
| 5 |
+
<meta charset="UTF-8">
|
| 6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 7 |
+
<meta name="description" content="VoiceGuard - AI-Generated Voice Detection for Indian Languages">
|
| 8 |
+
|
| 9 |
+
<title>VoiceGuard | AI Voice Detection</title>
|
| 10 |
+
|
| 11 |
+
<!-- Fonts -->
|
| 12 |
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
| 13 |
+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
| 14 |
+
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
|
| 15 |
+
|
| 16 |
+
<!-- Styles -->
|
| 17 |
+
<link rel="stylesheet" href="css/variables.css">
|
| 18 |
+
<link rel="stylesheet" href="css/base.css">
|
| 19 |
+
<link rel="stylesheet" href="css/components.css">
|
| 20 |
+
<link rel="stylesheet" href="css/animations.css">
|
| 21 |
+
|
| 22 |
+
<!-- Favicon -->
|
| 23 |
+
<link rel="icon" type="image/svg+xml"
|
| 24 |
+
href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>🛡️</text></svg>">
|
| 25 |
+
</head>
|
| 26 |
+
|
| 27 |
+
<body>
|
| 28 |
+
<div class="app">
|
| 29 |
+
<!-- ==================== HEADER ==================== -->
|
| 30 |
+
<header class="header">
|
| 31 |
+
<div class="header__brand">
|
| 32 |
+
<span class="header__logo">🛡️</span>
|
| 33 |
+
<span class="header__title">VoiceGuard</span>
|
| 34 |
+
</div>
|
| 35 |
+
<div class="header__status">
|
| 36 |
+
<span class="status-indicator" id="apiStatus">
|
| 37 |
+
<span class="status-indicator__dot"></span>
|
| 38 |
+
<span class="status-indicator__text">Checking...</span>
|
| 39 |
+
</span>
|
| 40 |
+
</div>
|
| 41 |
+
</header>
|
| 42 |
+
|
| 43 |
+
<!-- ==================== MAIN CONTENT ==================== -->
|
| 44 |
+
<main class="main">
|
| 45 |
+
<!-- Hero Section -->
|
| 46 |
+
<section class="hero">
|
| 47 |
+
<h1 class="hero__title">Detect AI-Generated Voice</h1>
|
| 48 |
+
<p class="hero__subtitle">
|
| 49 |
+
Analyze audio samples in <strong>Tamil</strong>, <strong>English</strong>,
|
| 50 |
+
<strong>Hindi</strong>, <strong>Malayalam</strong> & <strong>Telugu</strong>
|
| 51 |
+
</p>
|
| 52 |
+
</section>
|
| 53 |
+
|
| 54 |
+
<!-- Content Grid -->
|
| 55 |
+
<div class="content-grid">
|
| 56 |
+
<!-- Input Panel -->
|
| 57 |
+
<div class="panel panel--input">
|
| 58 |
+
<h2 class="panel__title">Upload Audio</h2>
|
| 59 |
+
|
| 60 |
+
<!-- File Upload Zone -->
|
| 61 |
+
<div class="file-upload" id="fileUpload">
|
| 62 |
+
<div class="file-upload__dropzone" id="dropzone">
|
| 63 |
+
<div class="file-upload__icon">
|
| 64 |
+
<svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
| 65 |
+
stroke-width="1.5">
|
| 66 |
+
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
|
| 67 |
+
<polyline points="17 8 12 3 7 8" />
|
| 68 |
+
<line x1="12" y1="3" x2="12" y2="15" />
|
| 69 |
+
</svg>
|
| 70 |
+
</div>
|
| 71 |
+
<p class="file-upload__text">Drop your MP3 file here</p>
|
| 72 |
+
<p class="file-upload__or">or</p>
|
| 73 |
+
<button type="button" class="file-upload__browse" id="browseBtn">
|
| 74 |
+
Browse Files
|
| 75 |
+
</button>
|
| 76 |
+
<p class="file-upload__hint">MP3 only • 1-60 seconds • Max 10MB</p>
|
| 77 |
+
</div>
|
| 78 |
+
|
| 79 |
+
<input type="file" id="fileInput" accept=".mp3,audio/mpeg" hidden>
|
| 80 |
+
|
| 81 |
+
<!-- File Preview -->
|
| 82 |
+
<div class="file-preview" id="filePreview" hidden>
|
| 83 |
+
<div class="file-preview__info">
|
| 84 |
+
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
| 85 |
+
stroke-width="2">
|
| 86 |
+
<path d="M9 18V5l12-2v13" />
|
| 87 |
+
<circle cx="6" cy="18" r="3" />
|
| 88 |
+
<circle cx="18" cy="16" r="3" />
|
| 89 |
+
</svg>
|
| 90 |
+
<div class="file-preview__details">
|
| 91 |
+
<p class="file-preview__name" id="fileName">audio.mp3</p>
|
| 92 |
+
<p class="file-preview__meta" id="fileMeta">5.2s • 1.2 MB</p>
|
| 93 |
+
</div>
|
| 94 |
+
</div>
|
| 95 |
+
<button type="button" class="file-preview__remove" id="removeBtn" aria-label="Remove file">
|
| 96 |
+
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
| 97 |
+
stroke-width="2">
|
| 98 |
+
<line x1="18" y1="6" x2="6" y2="18" />
|
| 99 |
+
<line x1="6" y1="6" x2="18" y2="18" />
|
| 100 |
+
</svg>
|
| 101 |
+
</button>
|
| 102 |
+
</div>
|
| 103 |
+
</div>
|
| 104 |
+
|
| 105 |
+
<!-- Language Selector -->
|
| 106 |
+
<div class="language-selector">
|
| 107 |
+
<label class="language-selector__label" for="languageSelect">Audio Language:</label>
|
| 108 |
+
<select class="language-selector__select" id="languageSelect">
|
| 109 |
+
<option value="Tamil">Tamil</option>
|
| 110 |
+
<option value="English" selected>English</option>
|
| 111 |
+
<option value="Hindi">Hindi</option>
|
| 112 |
+
<option value="Malayalam">Malayalam</option>
|
| 113 |
+
<option value="Telugu">Telugu</option>
|
| 114 |
+
</select>
|
| 115 |
+
</div>
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
<!-- Analyze Button -->
|
| 119 |
+
<button type="button" class="analyze-btn" id="analyzeBtn" disabled>
|
| 120 |
+
<span class="analyze-btn__idle">
|
| 121 |
+
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
| 122 |
+
stroke-width="2">
|
| 123 |
+
<circle cx="11" cy="11" r="8" />
|
| 124 |
+
<line x1="21" y1="21" x2="16.65" y2="16.65" />
|
| 125 |
+
</svg>
|
| 126 |
+
<span>Analyze Voice Sample</span>
|
| 127 |
+
</span>
|
| 128 |
+
<span class="analyze-btn__loading" hidden>
|
| 129 |
+
<svg class="spinner" width="20" height="20" viewBox="0 0 24 24" fill="none"
|
| 130 |
+
stroke="currentColor" stroke-width="2">
|
| 131 |
+
<circle cx="12" cy="12" r="10" stroke-dasharray="32" stroke-dashoffset="32" />
|
| 132 |
+
</svg>
|
| 133 |
+
<span>Analyzing...</span>
|
| 134 |
+
</span>
|
| 135 |
+
</button>
|
| 136 |
+
</div>
|
| 137 |
+
|
| 138 |
+
<!-- Results Panel -->
|
| 139 |
+
<div class="panel panel--results">
|
| 140 |
+
<h2 class="panel__title">Detection Results</h2>
|
| 141 |
+
|
| 142 |
+
<!-- Empty State -->
|
| 143 |
+
<div class="results-empty" id="resultsEmpty">
|
| 144 |
+
<svg width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
| 145 |
+
stroke-width="1">
|
| 146 |
+
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
|
| 147 |
+
<polyline points="14 2 14 8 20 8" />
|
| 148 |
+
<line x1="16" y1="13" x2="8" y2="13" />
|
| 149 |
+
<line x1="16" y1="17" x2="8" y2="17" />
|
| 150 |
+
<polyline points="10 9 9 9 8 9" />
|
| 151 |
+
</svg>
|
| 152 |
+
<p>Upload an audio file to see analysis results</p>
|
| 153 |
+
</div>
|
| 154 |
+
|
| 155 |
+
<!-- Results Content -->
|
| 156 |
+
<div class="results-content" id="resultsContent" hidden>
|
| 157 |
+
<!-- Verdict Card -->
|
| 158 |
+
<div class="verdict-card" id="verdictCard">
|
| 159 |
+
<div class="verdict-card__icon" id="verdictIcon">
|
| 160 |
+
<!-- Dynamic icon -->
|
| 161 |
+
</div>
|
| 162 |
+
<div class="verdict-card__label" id="verdictLabel">
|
| 163 |
+
<!-- AI_GENERATED or HUMAN -->
|
| 164 |
+
</div>
|
| 165 |
+
</div>
|
| 166 |
+
|
| 167 |
+
<!-- Confidence Meter -->
|
| 168 |
+
<div class="confidence-meter">
|
| 169 |
+
<div class="confidence-meter__header">
|
| 170 |
+
<span>Confidence Score</span>
|
| 171 |
+
<span class="confidence-meter__value" id="confidenceValue">--</span>
|
| 172 |
+
</div>
|
| 173 |
+
<div class="confidence-meter__track">
|
| 174 |
+
<div class="confidence-meter__fill" id="confidenceFill"></div>
|
| 175 |
+
</div>
|
| 176 |
+
</div>
|
| 177 |
+
|
| 178 |
+
<!-- Language -->
|
| 179 |
+
<div class="info-row">
|
| 180 |
+
<span class="info-row__label">Language:</span>
|
| 181 |
+
<span class="info-row__value" id="resultLanguage">--</span>
|
| 182 |
+
</div>
|
| 183 |
+
|
| 184 |
+
<!-- Explanation -->
|
| 185 |
+
<div class="explanation-card">
|
| 186 |
+
<div class="explanation-card__header">
|
| 187 |
+
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
| 188 |
+
stroke-width="2">
|
| 189 |
+
<circle cx="12" cy="12" r="10" />
|
| 190 |
+
<line x1="12" y1="16" x2="12" y2="12" />
|
| 191 |
+
<line x1="12" y1="8" x2="12.01" y2="8" />
|
| 192 |
+
</svg>
|
| 193 |
+
<span>Analysis Explanation</span>
|
| 194 |
+
</div>
|
| 195 |
+
<p class="explanation-card__text" id="explanationText">
|
| 196 |
+
<!-- Dynamic explanation -->
|
| 197 |
+
</p>
|
| 198 |
+
</div>
|
| 199 |
+
</div>
|
| 200 |
+
</div>
|
| 201 |
+
</div>
|
| 202 |
+
</main>
|
| 203 |
+
|
| 204 |
+
<!-- ==================== FOOTER ==================== -->
|
| 205 |
+
<footer class="footer">
|
| 206 |
+
<p>🏆 India AI Impact Summit Buildathon 2026 • VoiceGuard Team</p>
|
| 207 |
+
</footer>
|
| 208 |
+
|
| 209 |
+
<!-- ==================== TOAST CONTAINER ==================== -->
|
| 210 |
+
<div class="toast-container" id="toastContainer"></div>
|
| 211 |
+
</div>
|
| 212 |
+
|
| 213 |
+
<!-- Scripts -->
|
| 214 |
+
<script type="module" src="js/app.js"></script>
|
| 215 |
+
</body>
|
| 216 |
+
|
| 217 |
+
</html>
|
frontend/js/api.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// ==================== API CLIENT ====================
|
| 2 |
+
// Handles all communication with the VoiceGuard backend API
|
| 3 |
+
|
| 4 |
+
import { CONFIG } from './config.js';
|
| 5 |
+
|
| 6 |
+
/**
|
| 7 |
+
* Custom error class for API errors
|
| 8 |
+
*/
|
| 9 |
+
export class APIError extends Error {
|
| 10 |
+
constructor(message, status) {
|
| 11 |
+
super(message);
|
| 12 |
+
this.name = 'APIError';
|
| 13 |
+
this.status = status;
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
/**
|
| 18 |
+
* VoiceGuard API Client
|
| 19 |
+
*/
|
| 20 |
+
class APIClient {
|
| 21 |
+
constructor() {
|
| 22 |
+
this.baseUrl = CONFIG.API_BASE_URL;
|
| 23 |
+
this.apiKey = CONFIG.API_KEY;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
/**
|
| 27 |
+
* Check API health status
|
| 28 |
+
* @returns {Promise<{status: string, message?: string}>}
|
| 29 |
+
*/
|
| 30 |
+
async checkHealth() {
|
| 31 |
+
try {
|
| 32 |
+
const controller = new AbortController();
|
| 33 |
+
const timeoutId = setTimeout(() => controller.abort(), CONFIG.TIMEOUTS.HEALTH_CHECK);
|
| 34 |
+
|
| 35 |
+
const response = await fetch(`${this.baseUrl}${CONFIG.ENDPOINTS.HEALTH}`, {
|
| 36 |
+
method: 'GET',
|
| 37 |
+
signal: controller.signal
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
clearTimeout(timeoutId);
|
| 41 |
+
|
| 42 |
+
if (!response.ok) {
|
| 43 |
+
return { status: 'offline', message: `HTTP ${response.status}` };
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
const data = await response.json();
|
| 47 |
+
return { status: 'online', ...data };
|
| 48 |
+
|
| 49 |
+
} catch (error) {
|
| 50 |
+
if (error.name === 'AbortError') {
|
| 51 |
+
return { status: 'offline', message: 'Request timeout' };
|
| 52 |
+
}
|
| 53 |
+
return { status: 'offline', message: error.message };
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
/**
|
| 58 |
+
* Detect AI-generated voice in audio file
|
| 59 |
+
* @param {File} audioFile - MP3 audio file
|
| 60 |
+
* @param {string} language - Language of the audio
|
| 61 |
+
* @returns {Promise<Object>} - Detection result
|
| 62 |
+
*/
|
| 63 |
+
async detectVoice(audioFile, language) {
|
| 64 |
+
// 1. Convert file to Base64
|
| 65 |
+
console.log('📤 Converting file to Base64...');
|
| 66 |
+
const audioBase64 = await this._fileToBase64(audioFile);
|
| 67 |
+
console.log(`📦 Base64 length: ${audioBase64.length} characters`);
|
| 68 |
+
|
| 69 |
+
// 2. Build request body
|
| 70 |
+
const requestBody = {
|
| 71 |
+
language: language,
|
| 72 |
+
audioFormat: 'mp3',
|
| 73 |
+
audioBase64: audioBase64
|
| 74 |
+
};
|
| 75 |
+
|
| 76 |
+
// 3. Make API request
|
| 77 |
+
console.log('📡 Sending request to API...');
|
| 78 |
+
const controller = new AbortController();
|
| 79 |
+
const timeoutId = setTimeout(() => controller.abort(), CONFIG.TIMEOUTS.DETECTION);
|
| 80 |
+
|
| 81 |
+
try {
|
| 82 |
+
const response = await fetch(`${this.baseUrl}${CONFIG.ENDPOINTS.DETECT}`, {
|
| 83 |
+
method: 'POST',
|
| 84 |
+
headers: {
|
| 85 |
+
'Content-Type': 'application/json',
|
| 86 |
+
'x-api-key': this.apiKey
|
| 87 |
+
},
|
| 88 |
+
body: JSON.stringify(requestBody),
|
| 89 |
+
signal: controller.signal
|
| 90 |
+
});
|
| 91 |
+
|
| 92 |
+
clearTimeout(timeoutId);
|
| 93 |
+
|
| 94 |
+
// 4. Parse response
|
| 95 |
+
const data = await response.json();
|
| 96 |
+
console.log('📊 Response received:', data);
|
| 97 |
+
|
| 98 |
+
// 5. Handle errors
|
| 99 |
+
if (!response.ok) {
|
| 100 |
+
const errorMessage = data.message ||
|
| 101 |
+
data.detail?.message ||
|
| 102 |
+
data.detail ||
|
| 103 |
+
'Detection failed';
|
| 104 |
+
throw new APIError(errorMessage, response.status);
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
return data;
|
| 108 |
+
|
| 109 |
+
} catch (error) {
|
| 110 |
+
clearTimeout(timeoutId);
|
| 111 |
+
|
| 112 |
+
if (error.name === 'AbortError') {
|
| 113 |
+
throw new APIError('Request timed out. The server may be downloading the model.', 0);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
if (error instanceof APIError) {
|
| 117 |
+
throw error;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
throw new APIError(`Network error: ${error.message}`, 0);
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
/**
|
| 125 |
+
* Convert File to Base64 string
|
| 126 |
+
* @private
|
| 127 |
+
* @param {File} file - File to convert
|
| 128 |
+
* @returns {Promise<string>} - Base64 encoded string
|
| 129 |
+
*/
|
| 130 |
+
_fileToBase64(file) {
|
| 131 |
+
return new Promise((resolve, reject) => {
|
| 132 |
+
const reader = new FileReader();
|
| 133 |
+
|
| 134 |
+
reader.onload = () => {
|
| 135 |
+
// Remove data URL prefix (e.g., "data:audio/mpeg;base64,")
|
| 136 |
+
const base64 = reader.result.split(',')[1];
|
| 137 |
+
resolve(base64);
|
| 138 |
+
};
|
| 139 |
+
|
| 140 |
+
reader.onerror = () => {
|
| 141 |
+
reject(new Error('Failed to read file'));
|
| 142 |
+
};
|
| 143 |
+
|
| 144 |
+
reader.readAsDataURL(file);
|
| 145 |
+
});
|
| 146 |
+
}
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
// Export singleton instance
|
| 150 |
+
export const api = new APIClient();
|
frontend/js/app.js
ADDED
|
@@ -0,0 +1,452 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// ==================== VOICEGUARD APPLICATION ====================
|
| 2 |
+
// Main application controller
|
| 3 |
+
|
| 4 |
+
import { api, APIError } from './api.js';
|
| 5 |
+
import { CONFIG } from './config.js';
|
| 6 |
+
|
| 7 |
+
// ==================== STATE ====================
|
| 8 |
+
|
| 9 |
+
const state = {
|
| 10 |
+
// UI State
|
| 11 |
+
isLoading: false,
|
| 12 |
+
|
| 13 |
+
// File State
|
| 14 |
+
selectedFile: null,
|
| 15 |
+
fileDuration: null,
|
| 16 |
+
|
| 17 |
+
// Language State
|
| 18 |
+
selectedLanguage: 'English',
|
| 19 |
+
|
| 20 |
+
// Results State
|
| 21 |
+
hasResults: false,
|
| 22 |
+
results: null,
|
| 23 |
+
|
| 24 |
+
// API State
|
| 25 |
+
apiStatus: 'checking'
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
// ==================== DOM ELEMENTS ====================
|
| 29 |
+
|
| 30 |
+
const elements = {
|
| 31 |
+
// API Status
|
| 32 |
+
apiStatus: document.getElementById('apiStatus'),
|
| 33 |
+
|
| 34 |
+
// File Upload
|
| 35 |
+
dropzone: document.getElementById('dropzone'),
|
| 36 |
+
fileInput: document.getElementById('fileInput'),
|
| 37 |
+
browseBtn: document.getElementById('browseBtn'),
|
| 38 |
+
filePreview: document.getElementById('filePreview'),
|
| 39 |
+
fileName: document.getElementById('fileName'),
|
| 40 |
+
fileMeta: document.getElementById('fileMeta'),
|
| 41 |
+
removeBtn: document.getElementById('removeBtn'),
|
| 42 |
+
|
| 43 |
+
// Language
|
| 44 |
+
languageSelect: document.getElementById('languageSelect'),
|
| 45 |
+
|
| 46 |
+
// Analyze Button
|
| 47 |
+
analyzeBtn: document.getElementById('analyzeBtn'),
|
| 48 |
+
|
| 49 |
+
// Results
|
| 50 |
+
resultsEmpty: document.getElementById('resultsEmpty'),
|
| 51 |
+
resultsContent: document.getElementById('resultsContent'),
|
| 52 |
+
verdictCard: document.getElementById('verdictCard'),
|
| 53 |
+
verdictIcon: document.getElementById('verdictIcon'),
|
| 54 |
+
verdictLabel: document.getElementById('verdictLabel'),
|
| 55 |
+
confidenceValue: document.getElementById('confidenceValue'),
|
| 56 |
+
confidenceFill: document.getElementById('confidenceFill'),
|
| 57 |
+
resultLanguage: document.getElementById('resultLanguage'),
|
| 58 |
+
explanationText: document.getElementById('explanationText'),
|
| 59 |
+
|
| 60 |
+
// Toast
|
| 61 |
+
toastContainer: document.getElementById('toastContainer')
|
| 62 |
+
};
|
| 63 |
+
|
| 64 |
+
// ==================== INITIALIZATION ====================
|
| 65 |
+
|
| 66 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 67 |
+
console.log('🛡️ VoiceGuard UI initialized');
|
| 68 |
+
|
| 69 |
+
initFileUpload();
|
| 70 |
+
initLanguageSelector();
|
| 71 |
+
initAnalyzeButton();
|
| 72 |
+
checkAPIHealth();
|
| 73 |
+
|
| 74 |
+
// Periodic health check
|
| 75 |
+
setInterval(checkAPIHealth, 30000);
|
| 76 |
+
});
|
| 77 |
+
|
| 78 |
+
// ==================== API HEALTH ====================
|
| 79 |
+
|
| 80 |
+
async function checkAPIHealth() {
|
| 81 |
+
const result = await api.checkHealth();
|
| 82 |
+
state.apiStatus = result.status;
|
| 83 |
+
updateAPIStatus();
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
function updateAPIStatus() {
|
| 87 |
+
const statusEl = elements.apiStatus;
|
| 88 |
+
const textEl = statusEl.querySelector('.status-indicator__text');
|
| 89 |
+
|
| 90 |
+
statusEl.classList.remove('is-online', 'is-offline');
|
| 91 |
+
|
| 92 |
+
// Backend returns 'healthy' which matches our 'online' requirement
|
| 93 |
+
if (state.apiStatus === 'online' || state.apiStatus === 'healthy') {
|
| 94 |
+
statusEl.classList.add('is-online');
|
| 95 |
+
textEl.textContent = 'System Online';
|
| 96 |
+
} else if (state.apiStatus === 'offline') {
|
| 97 |
+
statusEl.classList.add('is-offline');
|
| 98 |
+
textEl.textContent = 'System Offline';
|
| 99 |
+
} else {
|
| 100 |
+
textEl.textContent = 'Checking...';
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
// ==================== FILE UPLOAD ====================
|
| 105 |
+
|
| 106 |
+
function initFileUpload() {
|
| 107 |
+
const { dropzone, fileInput, browseBtn, removeBtn } = elements;
|
| 108 |
+
|
| 109 |
+
// Browse button click
|
| 110 |
+
browseBtn.addEventListener('click', () => fileInput.click());
|
| 111 |
+
|
| 112 |
+
// Dropzone click
|
| 113 |
+
dropzone.addEventListener('click', (e) => {
|
| 114 |
+
if (e.target === dropzone || e.target.closest('.file-upload__icon')) {
|
| 115 |
+
fileInput.click();
|
| 116 |
+
}
|
| 117 |
+
});
|
| 118 |
+
|
| 119 |
+
// File input change
|
| 120 |
+
fileInput.addEventListener('change', (e) => {
|
| 121 |
+
const file = e.target.files[0];
|
| 122 |
+
if (file) handleFileSelect(file);
|
| 123 |
+
});
|
| 124 |
+
|
| 125 |
+
// Drag and drop
|
| 126 |
+
dropzone.addEventListener('dragover', (e) => {
|
| 127 |
+
e.preventDefault();
|
| 128 |
+
dropzone.classList.add('is-dragging');
|
| 129 |
+
});
|
| 130 |
+
|
| 131 |
+
dropzone.addEventListener('dragleave', (e) => {
|
| 132 |
+
e.preventDefault();
|
| 133 |
+
dropzone.classList.remove('is-dragging');
|
| 134 |
+
});
|
| 135 |
+
|
| 136 |
+
dropzone.addEventListener('drop', (e) => {
|
| 137 |
+
e.preventDefault();
|
| 138 |
+
dropzone.classList.remove('is-dragging');
|
| 139 |
+
|
| 140 |
+
const file = e.dataTransfer.files[0];
|
| 141 |
+
if (file) handleFileSelect(file);
|
| 142 |
+
});
|
| 143 |
+
|
| 144 |
+
// Remove button
|
| 145 |
+
removeBtn.addEventListener('click', handleFileRemove);
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
async function handleFileSelect(file) {
|
| 149 |
+
console.log('📁 File selected:', file.name, file.type, file.size);
|
| 150 |
+
|
| 151 |
+
// Validate file type
|
| 152 |
+
if (!CONFIG.FILE.ALLOWED_TYPES.includes(file.type)) {
|
| 153 |
+
showToast('error', 'Invalid File', 'Only MP3 files are supported');
|
| 154 |
+
shakeDropzone();
|
| 155 |
+
return;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
// Validate file size
|
| 159 |
+
if (file.size > CONFIG.FILE.MAX_SIZE) {
|
| 160 |
+
showToast('error', 'File Too Large', 'Maximum file size is 10MB');
|
| 161 |
+
shakeDropzone();
|
| 162 |
+
return;
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
// Get audio duration
|
| 166 |
+
try {
|
| 167 |
+
const duration = await getAudioDuration(file);
|
| 168 |
+
console.log('⏱️ Duration:', duration, 'seconds');
|
| 169 |
+
|
| 170 |
+
// Validate duration
|
| 171 |
+
if (duration < CONFIG.FILE.MIN_DURATION) {
|
| 172 |
+
showToast('error', 'Audio Too Short', 'Minimum duration is 1 second');
|
| 173 |
+
shakeDropzone();
|
| 174 |
+
return;
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
if (duration > CONFIG.FILE.MAX_DURATION) {
|
| 178 |
+
showToast('error', 'Audio Too Long', 'Maximum duration is 60 seconds');
|
| 179 |
+
shakeDropzone();
|
| 180 |
+
return;
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
// Store file
|
| 184 |
+
state.selectedFile = file;
|
| 185 |
+
state.fileDuration = duration;
|
| 186 |
+
|
| 187 |
+
// Update UI
|
| 188 |
+
updateFilePreview();
|
| 189 |
+
updateAnalyzeButton();
|
| 190 |
+
|
| 191 |
+
} catch (error) {
|
| 192 |
+
console.error('Error reading file:', error);
|
| 193 |
+
showToast('error', 'Invalid File', 'Could not read audio file');
|
| 194 |
+
shakeDropzone();
|
| 195 |
+
}
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
function handleFileRemove() {
|
| 199 |
+
state.selectedFile = null;
|
| 200 |
+
state.fileDuration = null;
|
| 201 |
+
elements.fileInput.value = '';
|
| 202 |
+
|
| 203 |
+
updateFilePreview();
|
| 204 |
+
updateAnalyzeButton();
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
function updateFilePreview() {
|
| 208 |
+
const { dropzone, filePreview, fileName, fileMeta } = elements;
|
| 209 |
+
|
| 210 |
+
if (state.selectedFile) {
|
| 211 |
+
dropzone.hidden = true;
|
| 212 |
+
filePreview.hidden = false;
|
| 213 |
+
|
| 214 |
+
fileName.textContent = state.selectedFile.name;
|
| 215 |
+
fileMeta.textContent = `${state.fileDuration.toFixed(1)}s • ${formatFileSize(state.selectedFile.size)}`;
|
| 216 |
+
} else {
|
| 217 |
+
dropzone.hidden = false;
|
| 218 |
+
filePreview.hidden = true;
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
function shakeDropzone() {
|
| 223 |
+
elements.dropzone.classList.add('has-error');
|
| 224 |
+
setTimeout(() => {
|
| 225 |
+
elements.dropzone.classList.remove('has-error');
|
| 226 |
+
}, 500);
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
// ==================== LANGUAGE SELECTOR ====================
|
| 230 |
+
|
| 231 |
+
function initLanguageSelector() {
|
| 232 |
+
const { languageSelect } = elements;
|
| 233 |
+
if (languageSelect) {
|
| 234 |
+
// Set initial state from dropdown
|
| 235 |
+
state.selectedLanguage = languageSelect.value;
|
| 236 |
+
|
| 237 |
+
// Listen for changes
|
| 238 |
+
languageSelect.addEventListener('change', (e) => {
|
| 239 |
+
state.selectedLanguage = e.target.value;
|
| 240 |
+
console.log('🌐 Language changed to:', state.selectedLanguage);
|
| 241 |
+
});
|
| 242 |
+
}
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
// ==================== ANALYZE BUTTON ====================
|
| 246 |
+
|
| 247 |
+
function initAnalyzeButton() {
|
| 248 |
+
elements.analyzeBtn.addEventListener('click', handleAnalyze);
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
function updateAnalyzeButton() {
|
| 252 |
+
elements.analyzeBtn.disabled = !state.selectedFile || state.isLoading;
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
async function handleAnalyze() {
|
| 256 |
+
if (!state.selectedFile || state.isLoading) return;
|
| 257 |
+
|
| 258 |
+
console.log('🔍 Starting analysis...');
|
| 259 |
+
|
| 260 |
+
// Set loading state
|
| 261 |
+
state.isLoading = true;
|
| 262 |
+
setButtonLoading(true);
|
| 263 |
+
updateAnalyzeButton();
|
| 264 |
+
|
| 265 |
+
try {
|
| 266 |
+
// Make API request
|
| 267 |
+
const result = await api.detectVoice(state.selectedFile, state.selectedLanguage);
|
| 268 |
+
|
| 269 |
+
// Store results
|
| 270 |
+
state.hasResults = true;
|
| 271 |
+
state.results = result;
|
| 272 |
+
|
| 273 |
+
// Show results
|
| 274 |
+
showResults(result);
|
| 275 |
+
|
| 276 |
+
console.log('✅ Analysis complete:', result);
|
| 277 |
+
|
| 278 |
+
} catch (error) {
|
| 279 |
+
console.error('❌ Analysis failed:', error);
|
| 280 |
+
|
| 281 |
+
const message = error instanceof APIError
|
| 282 |
+
? error.message
|
| 283 |
+
: 'An unexpected error occurred';
|
| 284 |
+
|
| 285 |
+
showToast('error', 'Analysis Failed', message);
|
| 286 |
+
|
| 287 |
+
} finally {
|
| 288 |
+
// Reset loading state
|
| 289 |
+
state.isLoading = false;
|
| 290 |
+
setButtonLoading(false);
|
| 291 |
+
updateAnalyzeButton();
|
| 292 |
+
}
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
function setButtonLoading(isLoading) {
|
| 296 |
+
const btn = elements.analyzeBtn;
|
| 297 |
+
const idle = btn.querySelector('.analyze-btn__idle');
|
| 298 |
+
const loading = btn.querySelector('.analyze-btn__loading');
|
| 299 |
+
|
| 300 |
+
if (isLoading) {
|
| 301 |
+
btn.classList.add('is-loading');
|
| 302 |
+
idle.hidden = true;
|
| 303 |
+
loading.hidden = false;
|
| 304 |
+
} else {
|
| 305 |
+
btn.classList.remove('is-loading');
|
| 306 |
+
idle.hidden = false;
|
| 307 |
+
loading.hidden = true;
|
| 308 |
+
}
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
// ==================== RESULTS ====================
|
| 312 |
+
|
| 313 |
+
function showResults(result) {
|
| 314 |
+
const {
|
| 315 |
+
resultsEmpty, resultsContent, verdictCard, verdictIcon, verdictLabel,
|
| 316 |
+
confidenceValue, confidenceFill, resultLanguage, explanationText
|
| 317 |
+
} = elements;
|
| 318 |
+
|
| 319 |
+
// Hide empty state, show content
|
| 320 |
+
resultsEmpty.hidden = true;
|
| 321 |
+
resultsContent.hidden = false;
|
| 322 |
+
|
| 323 |
+
// Determine if AI or Human
|
| 324 |
+
const isAI = result.classification === 'AI_GENERATED';
|
| 325 |
+
|
| 326 |
+
// Update verdict card
|
| 327 |
+
verdictCard.classList.remove('is-ai', 'is-human');
|
| 328 |
+
verdictCard.classList.add(isAI ? 'is-ai' : 'is-human');
|
| 329 |
+
|
| 330 |
+
verdictIcon.textContent = isAI ? '🤖' : '👤';
|
| 331 |
+
verdictLabel.textContent = isAI ? 'AI GENERATED' : 'HUMAN';
|
| 332 |
+
|
| 333 |
+
// Update confidence
|
| 334 |
+
const confidencePercent = Math.round(result.confidenceScore * 100);
|
| 335 |
+
confidenceValue.textContent = `${confidencePercent}%`;
|
| 336 |
+
|
| 337 |
+
// Animate confidence bar
|
| 338 |
+
setTimeout(() => {
|
| 339 |
+
confidenceFill.style.width = `${confidencePercent}%`;
|
| 340 |
+
}, 100);
|
| 341 |
+
|
| 342 |
+
// Update language
|
| 343 |
+
resultLanguage.textContent = result.language;
|
| 344 |
+
|
| 345 |
+
// Update explanation
|
| 346 |
+
explanationText.textContent = result.explanation || 'No detailed explanation available.';
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
// ==================== TOAST NOTIFICATIONS ====================
|
| 350 |
+
|
| 351 |
+
function showToast(type, title, message, duration = 5000) {
|
| 352 |
+
const toast = document.createElement('div');
|
| 353 |
+
toast.className = `toast toast--${type}`;
|
| 354 |
+
toast.innerHTML = `
|
| 355 |
+
<div class="toast__icon">
|
| 356 |
+
${type === 'error' ? `
|
| 357 |
+
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
| 358 |
+
<circle cx="12" cy="12" r="10"/>
|
| 359 |
+
<line x1="15" y1="9" x2="9" y2="15"/>
|
| 360 |
+
<line x1="9" y1="9" x2="15" y2="15"/>
|
| 361 |
+
</svg>
|
| 362 |
+
` : `
|
| 363 |
+
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
| 364 |
+
<path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/>
|
| 365 |
+
<polyline points="22 4 12 14.01 9 11.01"/>
|
| 366 |
+
</svg>
|
| 367 |
+
`}
|
| 368 |
+
</div>
|
| 369 |
+
<div class="toast__content">
|
| 370 |
+
<p class="toast__title">${title}</p>
|
| 371 |
+
<p class="toast__message">${message}</p>
|
| 372 |
+
</div>
|
| 373 |
+
<button class="toast__close" aria-label="Dismiss">
|
| 374 |
+
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
| 375 |
+
<line x1="18" y1="6" x2="6" y2="18"/>
|
| 376 |
+
<line x1="6" y1="6" x2="18" y2="18"/>
|
| 377 |
+
</svg>
|
| 378 |
+
</button>
|
| 379 |
+
`;
|
| 380 |
+
|
| 381 |
+
// Add close handler
|
| 382 |
+
const closeBtn = toast.querySelector('.toast__close');
|
| 383 |
+
closeBtn.addEventListener('click', () => removeToast(toast));
|
| 384 |
+
|
| 385 |
+
// Add to container
|
| 386 |
+
elements.toastContainer.appendChild(toast);
|
| 387 |
+
|
| 388 |
+
// Auto remove after duration
|
| 389 |
+
setTimeout(() => removeToast(toast), duration);
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
function removeToast(toast) {
|
| 393 |
+
toast.style.animation = 'fadeOut 0.2s ease-out forwards';
|
| 394 |
+
setTimeout(() => toast.remove(), 200);
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
// ==================== UTILITIES ====================
|
| 398 |
+
|
| 399 |
+
function getAudioDuration(file) {
|
| 400 |
+
return new Promise((resolve, reject) => {
|
| 401 |
+
const audio = new Audio();
|
| 402 |
+
const url = URL.createObjectURL(file);
|
| 403 |
+
let resolved = false;
|
| 404 |
+
|
| 405 |
+
// Timeout fallback: if browser can't read metadata in 2 seconds, assume it's valid
|
| 406 |
+
const timeout = setTimeout(() => {
|
| 407 |
+
if (!resolved) {
|
| 408 |
+
URL.revokeObjectURL(url);
|
| 409 |
+
console.warn('⚠️ Could not read audio metadata, assuming valid duration');
|
| 410 |
+
resolve(10); // Assume 10 seconds as fallback
|
| 411 |
+
resolved = true;
|
| 412 |
+
}
|
| 413 |
+
}, 2000);
|
| 414 |
+
|
| 415 |
+
audio.addEventListener('loadedmetadata', () => {
|
| 416 |
+
if (!resolved) {
|
| 417 |
+
clearTimeout(timeout);
|
| 418 |
+
URL.revokeObjectURL(url);
|
| 419 |
+
resolve(audio.duration);
|
| 420 |
+
resolved = true;
|
| 421 |
+
}
|
| 422 |
+
});
|
| 423 |
+
|
| 424 |
+
audio.addEventListener('error', (e) => {
|
| 425 |
+
if (!resolved) {
|
| 426 |
+
clearTimeout(timeout);
|
| 427 |
+
URL.revokeObjectURL(url);
|
| 428 |
+
console.warn('⚠️ Could not load audio metadata, assuming valid duration');
|
| 429 |
+
resolve(10); // Assume 10 seconds as fallback instead of rejecting
|
| 430 |
+
resolved = true;
|
| 431 |
+
}
|
| 432 |
+
});
|
| 433 |
+
|
| 434 |
+
audio.src = url;
|
| 435 |
+
});
|
| 436 |
+
}
|
| 437 |
+
|
| 438 |
+
function formatFileSize(bytes) {
|
| 439 |
+
if (bytes < 1024) return `${bytes} B`;
|
| 440 |
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
| 441 |
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
// Add fadeOut animation
|
| 445 |
+
const style = document.createElement('style');
|
| 446 |
+
style.textContent = `
|
| 447 |
+
@keyframes fadeOut {
|
| 448 |
+
from { opacity: 1; transform: translateY(0); }
|
| 449 |
+
to { opacity: 0; transform: translateY(10px); }
|
| 450 |
+
}
|
| 451 |
+
`;
|
| 452 |
+
document.head.appendChild(style);
|
frontend/js/config.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// ==================== CONFIGURATION ====================
|
| 2 |
+
// API and app configuration for VoiceGuard
|
| 3 |
+
|
| 4 |
+
export const CONFIG = {
|
| 5 |
+
// API Settings
|
| 6 |
+
API_BASE_URL: 'http://localhost:8000',
|
| 7 |
+
API_KEY: 'sk_voiceguard_2026_secret',
|
| 8 |
+
|
| 9 |
+
// Endpoints
|
| 10 |
+
ENDPOINTS: {
|
| 11 |
+
HEALTH: '/api/health',
|
| 12 |
+
DETECT: '/api/voice-detection'
|
| 13 |
+
},
|
| 14 |
+
|
| 15 |
+
// Timeouts (in milliseconds)
|
| 16 |
+
TIMEOUTS: {
|
| 17 |
+
HEALTH_CHECK: 5000, // 5 seconds
|
| 18 |
+
DETECTION: 180000 // 3 minutes (model loading can take time)
|
| 19 |
+
},
|
| 20 |
+
|
| 21 |
+
// File Validation
|
| 22 |
+
FILE: {
|
| 23 |
+
MAX_SIZE: 10 * 1024 * 1024, // 10MB
|
| 24 |
+
MIN_DURATION: 1, // 1 second
|
| 25 |
+
MAX_DURATION: 60, // 60 seconds
|
| 26 |
+
ALLOWED_TYPES: ['audio/mpeg', 'audio/mp3'],
|
| 27 |
+
ALLOWED_EXTENSIONS: ['.mp3']
|
| 28 |
+
},
|
| 29 |
+
|
| 30 |
+
// Supported Languages
|
| 31 |
+
LANGUAGES: [
|
| 32 |
+
{ value: 'Tamil', script: 'தமிழ்', name: 'Tamil' },
|
| 33 |
+
{ value: 'English', script: 'Eng', name: 'English' },
|
| 34 |
+
{ value: 'Hindi', script: 'हिंदी', name: 'Hindi' },
|
| 35 |
+
{ value: 'Malayalam', script: 'മലയാളം', name: 'Malayalam' },
|
| 36 |
+
{ value: 'Telugu', script: 'తెలుగు', name: 'Telugu' }
|
| 37 |
+
]
|
| 38 |
+
};
|
render.yaml
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
services:
|
| 2 |
+
- type: web
|
| 3 |
+
name: voiceguard-api
|
| 4 |
+
env: docker
|
| 5 |
+
plan: free
|
| 6 |
+
envVars:
|
| 7 |
+
- key: API_KEY
|
| 8 |
+
sync: false
|
| 9 |
+
- key: HOST
|
| 10 |
+
value: 0.0.0.0
|
| 11 |
+
- key: PORT
|
| 12 |
+
value: 8000
|
requirements.txt
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# VoiceGuard Backend Requirements
|
| 2 |
+
# ================================
|
| 3 |
+
|
| 4 |
+
# Gradio UI
|
| 5 |
+
gradio==4.44.0
|
| 6 |
+
|
| 7 |
+
# Audio Processing
|
| 8 |
+
librosa==0.10.1
|
| 9 |
+
soundfile==0.12.1
|
| 10 |
+
numpy==1.26.3
|
| 11 |
+
scipy==1.11.4
|
| 12 |
+
|
| 13 |
+
# Machine Learning
|
| 14 |
+
transformers==4.36.2
|
| 15 |
+
|
| 16 |
+
# FastAPI (for main app)
|
| 17 |
+
fastapi==0.109.0
|
| 18 |
+
uvicorn[standard]==0.27.0
|
| 19 |
+
python-multipart>=0.0.9
|
| 20 |
+
pydantic==2.5.3
|
| 21 |
+
pydantic-settings==2.1.0
|
| 22 |
+
python-dotenv==1.0.0
|
| 23 |
+
|
| 24 |
+
# Visualization
|
| 25 |
+
matplotlib==3.8.2
|