import gradio as gr import torch import torchaudio import numpy as np import json import os from datetime import datetime import matplotlib.pyplot as plt import seaborn as sns from sklearn.preprocessing import LabelEncoder import warnings warnings.filterwarnings('ignore') # Import your existing classes and functions from torch import nn import torchvision class AudioPreprocessor: """Enhanced audio preprocessing for voice security.""" def __init__(self, sample_rate=16000, n_mels=128, n_fft=2048, hop_length=512): self.sample_rate = sample_rate self.n_mels = n_mels self.n_fft = n_fft self.hop_length = hop_length self.mel_spectrogram = torchaudio.transforms.MelSpectrogram( sample_rate=sample_rate, n_mels=n_mels, n_fft=n_fft, hop_length=hop_length ) self.amplitude_to_db = torchaudio.transforms.AmplitudeToDB() def audio_to_melspectrogram(self, audio_path): """Convert audio file to mel-spectrogram.""" try: # Load audio file waveform, sr = torchaudio.load(audio_path) # Resample if necessary if sr != self.sample_rate: resampler = torchaudio.transforms.Resample(sr, self.sample_rate) waveform = resampler(waveform) # Convert to mono if stereo if waveform.shape[0] > 1: waveform = torch.mean(waveform, dim=0, keepdim=True) # Pad or truncate to fixed length (3 seconds) target_length = self.sample_rate * 3 if waveform.shape[1] > target_length: waveform = waveform[:, :target_length] else: padding = target_length - waveform.shape[1] waveform = torch.nn.functional.pad(waveform, (0, padding)) # Convert to mel-spectrogram mel_spec = self.mel_spectrogram(waveform) mel_spec_db = self.amplitude_to_db(mel_spec) # Normalize mel_spec_db = (mel_spec_db - mel_spec_db.mean()) / (mel_spec_db.std() + 1e-8) # Convert to 3-channel image (RGB) for pretrained models mel_spec_rgb = mel_spec_db.repeat(3, 1, 1) return mel_spec_rgb, waveform.numpy() except Exception as e: print(f"Error processing audio: {e}") return None, None # Model Classes (same as your original code) class ResNet18Model(nn.Module): def __init__(self, num_classes): super(ResNet18Model, self).__init__() self.backbone = torchvision.models.resnet18(pretrained=False) self.backbone.fc = nn.Sequential( nn.Dropout(0.5), nn.Linear(self.backbone.fc.in_features, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, num_classes) ) def forward(self, x): return self.backbone(x) class ResNet50Model(nn.Module): def __init__(self, num_classes): super(ResNet50Model, self).__init__() self.backbone = torchvision.models.resnet50(pretrained=False) num_ftrs = self.backbone.fc.in_features self.backbone.fc = nn.Sequential( nn.BatchNorm1d(num_ftrs), nn.Dropout(0.4), nn.Linear(num_ftrs, 512), nn.ReLU(), nn.BatchNorm1d(512), nn.Dropout(0.3), nn.Linear(512, num_classes) ) def forward(self, x): return self.backbone(x) class EfficientNetB0Model(nn.Module): def __init__(self, num_classes): super(EfficientNetB0Model, self).__init__() self.backbone = torchvision.models.efficientnet_b0(pretrained=False) self.backbone.classifier = nn.Sequential( nn.Dropout(p=0.3, inplace=True), nn.Linear(in_features=1280, out_features=512), nn.ReLU(), nn.Dropout(0.4), nn.Linear(512, num_classes) ) def forward(self, x): return self.backbone(x) class MobileNetV2Model(nn.Module): def __init__(self, num_classes): super(MobileNetV2Model, self).__init__() self.backbone = torchvision.models.mobilenet_v2(pretrained=False) self.backbone.classifier = nn.Sequential( nn.Dropout(0.2), nn.Linear(self.backbone.last_channel, 512), nn.ReLU(), nn.Dropout(0.3), nn.Linear(512, num_classes) ) def forward(self, x): return self.backbone(x) class VoiceSecuritySystem: def __init__(self): self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.preprocessor = AudioPreprocessor() self.models = {} self.label_encoder = LabelEncoder() # Updated model info with actual training results self.model_info = { "resnet18": { "name": "ResNet-18 🏆 CHAMPION", "description": "🥇 BEST PERFORMING MODEL - Perfect 100% accuracy with 11.3M parameters (4.9M trainable). Exceptional security with 0.06% FAR and 0% FRR. Ideal for high-security applications requiring zero false rejections.", "accuracy": "100.00%", "far": "0.0006", "frr": "0.0000", "parameters": "11.3M total (4.9M trainable)", "status": "🏆 CHAMPION" }, "resnet50": { "name": "ResNet-50 🥈 HIGH PERFORMER", "description": "🥈 EXCELLENT ACCURACY - 99.94% accuracy with 24.6M parameters (16.0M trainable). Near-perfect performance with robust feature extraction. Best for applications requiring high accuracy with acceptable computational overhead.", "accuracy": "99.94%", "far": "0.0006", "frr": "0.0000", "parameters": "24.6M total (16.0M trainable)", "status": "🥈 RUNNER-UP" }, "efficientnet_b0": { "name": "EfficientNet-B0 ⚡ EFFICIENT", "description": "⚡ MOBILE OPTIMIZED - 99.76% accuracy with only 4.7M parameters (3.8M trainable). Excellent efficiency-accuracy trade-off. Perfect for mobile deployment with minimal computational requirements.", "accuracy": "99.76%", "far": "0.0030", "frr": "0.0000", "parameters": "4.7M total (3.8M trainable)", "status": "⚡ EFFICIENT" }, "mobilenet_v2": { "name": "MobileNet-V2 📱 LIGHTWEIGHT", "description": "📱 ULTRA-LIGHTWEIGHT - 99.76% accuracy with just 2.9M parameters (1.1M trainable). Smallest model with excellent performance. Ideal for edge devices and real-time applications with limited resources.", "accuracy": "99.76%", "far": "0.0012", "frr": "0.0000", "parameters": "2.9M total (1.1M trainable)", "status": "📱 COMPACT" } } self.load_models() def load_models(self): """Load all pre-trained models""" # This would load your actual trained models # For demo purposes, we'll create placeholder models num_classes = 26 # Based on your training output (26 users) # Initialize label encoder with dummy classes dummy_classes = [f"user_{i+1}" for i in range(num_classes)] self.label_encoder.fit(dummy_classes) model_classes = { "resnet18": ResNet18Model, "resnet50": ResNet50Model, "efficientnet_b0": EfficientNetB0Model, "mobilenet_v2": MobileNetV2Model } for model_name, model_class in model_classes.items(): try: model = model_class(num_classes).to(self.device) # In actual deployment, you would load the trained weights: # model.load_state_dict(torch.load(f"models/{model_name}.pth", map_location=self.device)) model.eval() self.models[model_name] = model print(f"✅ Loaded {model_name} successfully") except Exception as e: print(f"❌ Error loading {model_name}: {e}") def predict_voice(self, audio_file, model_name, confidence_threshold): """Predict voice access using selected model""" if audio_file is None: return "❌ Error", "No audio file provided", 0.0, self.create_empty_plot(), "Please upload an audio file" try: # Process audio features, waveform = self.preprocessor.audio_to_melspectrogram(audio_file) if features is None: return "❌ Error", "Failed to process audio", 0.0, self.create_empty_plot(), "Audio processing failed" # Get selected model model = self.models.get(model_name) if model is None: return "❌ Error", "Model not found", 0.0, self.create_empty_plot(), "Selected model is not available" # Make prediction features = features.unsqueeze(0).to(self.device) with torch.no_grad(): output = model(features) probabilities = torch.softmax(output, dim=1) confidence, predicted = torch.max(probabilities, 1) predicted_class = self.label_encoder.inverse_transform([predicted.item()])[0] confidence_score = confidence.item() # Create visualization viz_plot = self.create_prediction_visualization(probabilities.cpu().numpy()[0], predicted_class, confidence_score) # Determine access decision if confidence_score >= confidence_threshold: status = "🟢 ACCESS GRANTED" message = f"Welcome, {predicted_class}!" security_status = f"✅ AUTHORIZED USER DETECTED" else: status = "🔴 ACCESS DENIED" message = f"Access denied - Low confidence" security_status = f"⚠️ UNAUTHORIZED ACCESS ATTEMPT" model_stats = self.model_info[model_name] detailed_info = f""" ## 🤖 Model Performance **Model Used:** {model_stats['name']} **Training Accuracy:** {model_stats['accuracy']} **Model Size:** {model_stats['parameters']} **Status:** {model_stats['status']} ## 🔍 Prediction Results **Predicted User:** {predicted_class} **Confidence Score:** {confidence_score:.3f} **Security Threshold:** {confidence_threshold} **Decision:** {'✅ GRANT ACCESS' if confidence_score >= confidence_threshold else '❌ DENY ACCESS'} ## 🛡️ Security Metrics **False Accept Rate (FAR):** {model_stats['far']} **False Reject Rate (FRR):** {model_stats['frr']} **Security Level:** {'🔒 HIGH' if confidence_score >= 0.8 else '🔓 MEDIUM' if confidence_score >= 0.5 else '⚠️ LOW'} """ return status, message, confidence_score, viz_plot, detailed_info except Exception as e: return "❌ Error", f"Prediction failed: {str(e)}", 0.0, self.create_empty_plot(), "An error occurred during prediction" def create_prediction_visualization(self, probabilities, predicted_class, confidence): """Create visualization of prediction results""" fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6)) # Enhanced color scheme colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#F7DC6F', '#BB8FCE', '#85C1E9', '#F8C471', '#82E0AA', '#F1948A'] # Plot 1: Top 5 predictions with enhanced styling top_5_indices = np.argsort(probabilities)[-5:][::-1] top_5_probs = probabilities[top_5_indices] top_5_labels = [self.label_encoder.inverse_transform([i])[0] for i in top_5_indices] bars = ax1.barh(range(len(top_5_labels)), top_5_probs, color=colors[:len(top_5_labels)]) ax1.set_yticks(range(len(top_5_labels))) ax1.set_yticklabels(top_5_labels) ax1.set_xlabel('Confidence Score', fontweight='bold') ax1.set_title('🎯 Top 5 User Predictions', fontweight='bold', fontsize=12) ax1.set_xlim(0, 1) ax1.grid(axis='x', alpha=0.3) # Highlight the top prediction with gold color bars[0].set_color('#FFD700') bars[0].set_edgecolor('#FF8C00') bars[0].set_linewidth(3) # Add value labels with better formatting for i, (bar, prob) in enumerate(zip(bars, top_5_probs)): ax1.text(prob + 0.02, bar.get_y() + bar.get_height()/2, f'{prob:.3f}', va='center', fontweight='bold', fontsize=10) # Plot 2: Enhanced confidence gauge theta = np.linspace(0, np.pi, 100) r = np.ones_like(theta) ax2 = plt.subplot(122, projection='polar') ax2.set_theta_zero_location('S') ax2.set_theta_direction(1) ax2.set_ylim(0, 1) # Enhanced color segments based on confidence levels if confidence < 0.3: color = '#FF4757' # Red status_text = '⚠️ LOW' risk_level = 'HIGH RISK' elif confidence < 0.7: color = '#FFA726' # Orange status_text = '🟡 MEDIUM' risk_level = 'MODERATE RISK' else: color = '#66BB6A' # Green status_text = '✅ HIGH' risk_level = 'LOW RISK' # Draw enhanced gauge ax2.fill_between(theta, 0, r, alpha=0.2, color='lightgray') confidence_theta = theta[int(confidence * len(theta))] ax2.plot([confidence_theta, confidence_theta], [0, 1], color=color, linewidth=10) ax2.fill_between(theta[:int(confidence * len(theta))], 0, r[:int(confidence * len(theta))], alpha=0.8, color=color) ax2.set_title(f'🎚️ Confidence Level\n{confidence:.3f} - {status_text}\n{risk_level}', pad=30, fontweight='bold') ax2.set_ylim(0, 1) ax2.set_yticklabels([]) ax2.set_xticklabels(['🔴 Low', '', '🟡 Med', '', '🟢 High'], fontweight='bold') plt.tight_layout() return fig def create_empty_plot(self): """Create empty plot for error cases""" fig, ax = plt.subplots(figsize=(10, 6)) ax.text(0.5, 0.5, '📊 No Data Available\nPlease upload an audio file', ha='center', va='center', fontsize=18, color='gray', fontweight='bold') ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.axis('off') return fig def get_model_comparison(self): """Return model comparison information with actual training results""" comparison_data = [] for model_key, info in self.model_info.items(): comparison_data.append([ info['name'], info['accuracy'], info['far'], info['frr'], info['parameters'], info['status'] ]) return comparison_data # Initialize the system voice_system = VoiceSecuritySystem() def process_voice(audio_file, model_name, confidence_threshold): """Main processing function for Gradio interface""" return voice_system.predict_voice(audio_file, model_name, confidence_threshold) def get_model_info(model_name): """Get information about selected model""" if model_name in voice_system.model_info: info = voice_system.model_info[model_name] return f"## {info['name']}\n\n{info['description']}\n\n**📊 Key Stats:**\n- Accuracy: {info['accuracy']}\n- Parameters: {info['parameters']}\n- FAR: {info['far']} | FRR: {info['frr']}" return "Model information not available" # Enhanced custom CSS custom_css = """ .gradio-container { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important; } .gr-button-primary { background: linear-gradient(45deg, #FF6B6B, #FF8E53) !important; border: none !important; font-weight: bold !important; text-transform: uppercase !important; letter-spacing: 1px !important; } .gr-button-secondary { background: linear-gradient(45deg, #4ECDC4, #44A08D) !important; border: none !important; } .gr-panel { background: rgba(255, 255, 255, 0.95) !important; backdrop-filter: blur(15px) !important; border-radius: 20px !important; border: 2px solid rgba(255, 255, 255, 0.3) !important; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1) !important; } .gr-form { background: transparent !important; } .gr-box { border-radius: 15px !important; border: 1px solid #E0E0E0 !important; box-shadow: 0 4px 16px rgba(0, 0, 0, 0.05) !important; } h1, h2, h3 { color: #2C3E50 !important; text-shadow: 2px 2px 4px rgba(0,0,0,0.1) !important; } .champion-badge { background: linear-gradient(45deg, #FFD700, #FFA500); padding: 5px 10px; border-radius: 20px; color: #333; font-weight: bold; display: inline-block; margin: 5px; } """ # Create enhanced Gradio interface with gr.Blocks(css=custom_css, title="🔊 Voice Recognition Security System - Trained Results") as app: gr.HTML("""
Advanced AI-powered voice authentication with 4 deep learning models
🏆 Training Complete: 26 Users | 1,693 Samples | Best Accuracy: 100%
© 2025 - Voice Security System. All rights reserved.