"""Gradio demo application for the Explainable Multimodal Deception Analysis System.""" import argparse import logging import os import tempfile import shutil from pathlib import Path from typing import Optional, Tuple, Dict, Any import json import gradio as gr import matplotlib.pyplot as plt import numpy as np import pandas as pd from matplotlib.figure import Figure from src.pipeline import DeceptionPipeline from src.utils.config import AppConfig, get_default_config logger = logging.getLogger(__name__) # Global state pipeline: Optional[DeceptionPipeline] = None config: Optional[AppConfig] = None current_model_path: Optional[str] = None # Custom CSS for styling custom_css = """ /* Deception assessment styling */ .deceptive-badge { background-color: #ffcccc; color: #b30000; padding: 8px 16px; border-radius: 20px; font-weight: bold; border: 2px solid #ff6666; display: inline-block; margin: 5px; } .truthful-badge { background-color: #ccffcc; color: #006600; padding: 8px 16px; border-radius: 20px; font-weight: bold; border: 2px solid #66cc66; display: inline-block; margin: 5px; } .error-badge { background-color: #ffebcc; color: #cc6600; padding: 8px 16px; border-radius: 20px; font-weight: bold; border: 2px solid #ff9933; display: inline-block; margin: 5px; } /* Professional font choices */ body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } /* Proper spacing */ .gr-container { padding: 20px; } .gr-tab { padding: 20px; } /* Highlighted suspicious intervals */ .suspicious-interval { background-color: #fff0f0; border-left: 4px solid #ff6666; padding: 8px 12px; margin: 5px 0; } /* Progress bar styling */ .gr-progress-bar { height: 8px; border-radius: 4px; } """ def initialize_pipeline(config_path: Optional[str] = None) -> DeceptionPipeline: """Initialize or reinitialize the pipeline.""" global pipeline, config try: if config_path and os.path.exists(config_path): # In a real implementation, you'd load config from file logger.warning(f"Config file loading not implemented, using default config") config = get_default_config() pipeline = DeceptionPipeline(config) logger.info("Pipeline initialized with default config.") return pipeline except Exception as e: logger.error(f"Failed to initialize pipeline: {e}") raise def get_pipeline() -> DeceptionPipeline: """Get or create the pipeline instance.""" global pipeline if pipeline is None: pipeline = initialize_pipeline() return pipeline def analyze_video( video_file: Optional[str], subject_id: Optional[str], threshold: float, progress=gr.Progress() ) -> Tuple[Dict[str, Any], Optional[Figure], Optional[Figure], Optional[pd.DataFrame], Optional[pd.DataFrame]]: """Run deception analysis on uploaded video.""" if video_file is None: return { "error": "No video file provided" }, None, None, None, None # Save uploaded video to a temporary file temp_dir = tempfile.mkdtemp(prefix="deception_video_") try: video_path = os.path.join(temp_dir, "uploaded_video.mp4") # Copy the uploaded file with open(video_file, 'rb') as src, open(video_path, 'wb') as dst: dst.write(src.read()) progress(0.1, desc="Initializing pipeline...") pipeline = get_pipeline() progress(0.2, desc="Analyzing video (this may take a minute)...") # Call the pipeline's analyze_video method result = pipeline.analyze_video( video_path=video_path, subject_id=subject_id if subject_id else None, generate_visualizations=True, output_dir=temp_dir ) progress(0.8, desc="Processing results...") # Check for error if "error" in result: return { "error": result["error"] }, None, None, None, None # Create temporal analysis plot temporal_fig = None if "temporal_analysis" in result and "timestamps" in result["temporal_analysis"]: try: temporal_fig = create_temporal_plot( result["temporal_analysis"]["timestamps"], result["temporal_analysis"]["probability_trajectory"], result["temporal_analysis"].get("suspicious_intervals", []) ) except Exception as e: logger.warning(f"Failed to create temporal plot: {e}") # Create feature importance plot feature_fig = None if "explanation" in result and "feature_importance" in result["explanation"]: try: feature_fig = create_feature_importance_plot( result["explanation"]["feature_importance"] ) except Exception as e: logger.warning(f"Failed to create feature importance plot: {e}") # Prepare behavioral indicators dataframe behavioral_df = None if "explanation" in result and "behavioral_explanations" in result["explanation"]: try: behavioral_df = create_behavioral_dataframe( result["explanation"]["behavioral_explanations"] ) except Exception as e: logger.warning(f"Failed to create behavioral dataframe: {e}") # Prepare suspicious intervals dataframe suspicious_df = None if "temporal_analysis" in result and "suspicious_intervals" in result["temporal_analysis"]: try: suspicious_df = create_suspicious_intervals_dataframe( result["temporal_analysis"]["suspicious_intervals"] ) except Exception as e: logger.warning(f"Failed to create suspicious intervals dataframe: {e}") # Format overall assessment assessment = { "prediction": result.get("prediction", "unknown"), "probability": result.get("deception_probability", 0.5), "confidence": result.get("confidence", 0.0), "calibrated": result.get("calibrated", False), "threshold": threshold, "is_deceptive": result.get("prediction") == "deceptive" if "prediction" in result else False } progress(1.0, desc="Analysis complete!") return assessment, temporal_fig, feature_fig, behavioral_df, suspicious_df except Exception as e: logger.error(f"Analysis failed: {e}") return { "error": f"Analysis failed: {str(e)}" }, None, None, None, None finally: # Clean up temp directory after a short delay (or keep for debugging) # shutil.rmtree(temp_dir, ignore_errors=True) pass def create_temporal_plot(timestamps, probabilities, suspicious_intervals) -> Figure: """Create matplotlib figure for temporal analysis.""" fig, ax = plt.subplots(figsize=(10, 5)) ax.plot(timestamps, probabilities, 'b-', linewidth=2, label='Deception Probability') ax.axhline(y=0.5, color='gray', linestyle='--', alpha=0.5, label='Neutral (0.5)') ax.set_xlabel('Time (seconds)', fontsize=12) ax.set_ylabel('Deception Probability', fontsize=12) ax.set_title('Deception Probability Over Time', fontsize=14, fontweight='bold') ax.grid(True, alpha=0.3) ax.set_ylim(0, 1) # Highlight suspicious intervals for interval in suspicious_intervals: if len(interval) >= 2: start, end = interval[0], interval[1] ax.axvspan(start, end, alpha=0.2, color='red', label='Suspicious Interval' if 'Suspicious Interval' not in ax.get_legend_handles_labels()[1] else '') ax.legend(loc='upper right') fig.tight_layout() return fig def create_feature_importance_plot(feature_importance: Dict[str, float]) -> Figure: """Create matplotlib figure for top feature importance.""" # Sort features by absolute importance sorted_features = sorted(feature_importance.items(), key=lambda x: abs(x[1]), reverse=True) top_features = sorted_features[:10] # Top 10 features if not top_features: # Create empty plot fig, ax = plt.subplots(figsize=(8, 5)) ax.text(0.5, 0.5, 'No feature importance data available', ha='center', va='center', fontsize=12) ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.axis('off') return fig features, importances = zip(*top_features) # Shorten long feature names for display display_features = [f[:30] + '...' if len(f) > 30 else f for f in features] fig, ax = plt.subplots(figsize=(10, 6)) colors = ['red' if imp < 0 else 'green' for imp in importances] y_pos = np.arange(len(display_features)) ax.barh(y_pos, importances, color=colors, alpha=0.7) ax.set_yticks(y_pos) ax.set_yticklabels(display_features, fontsize=10) ax.set_xlabel('SHAP Value (Impact on Prediction)', fontsize=12) ax.set_title('Top 10 Feature Importance for Deception Prediction', fontsize=14, fontweight='bold') ax.grid(True, alpha=0.3, axis='x') fig.tight_layout() return fig def create_behavioral_dataframe(behavioral_explanations: Dict[str, Any]) -> pd.DataFrame: """Create dataframe for behavioral indicators.""" rows = [] for category, indicators in behavioral_explanations.items(): if isinstance(indicators, dict): for indicator, value in indicators.items(): rows.append({ 'Category': category, 'Indicator': indicator, 'Value': value, 'Interpretation': interpret_behavioral_indicator(category, indicator, value) }) else: rows.append({ 'Category': category, 'Indicator': 'Overall', 'Value': indicators, 'Interpretation': interpret_behavioral_indicator(category, 'Overall', indicators) }) if rows: df = pd.DataFrame(rows) return df return pd.DataFrame(columns=['Category', 'Indicator', 'Value', 'Interpretation']) def interpret_behavioral_indicator(category: str, indicator: str, value: Any) -> str: """Provide human-readable interpretation of behavioral indicators.""" # Simplified interpretation logic try: val = float(value) if category == 'facial': if 'eye_contact' in indicator.lower(): return "Low eye contact may indicate deception" if val < 0.5 else "Normal eye contact" elif 'blink_rate' in indicator.lower(): return "Elevated blink rate may indicate stress" if val > 0.7 else "Normal blink rate" elif category == 'audio': if 'pitch_variability' in indicator.lower(): return "Reduced pitch variability may indicate rehearsed speech" if val < 0.3 else "Normal pitch variability" elif 'speech_rate' in indicator.lower(): return "Increased speech rate may indicate anxiety" if val > 1.2 else "Normal speech rate" elif category == 'cross_modal': return "Incongruence between facial and audio cues detected" if val > 0.6 else "Consistent multimodal behavior" except: pass return "No specific interpretation available" def create_suspicious_intervals_dataframe(suspicious_intervals: list) -> pd.DataFrame: """Create dataframe for suspicious intervals.""" if not suspicious_intervals: return pd.DataFrame(columns=['Start Time (s)', 'End Time (s)', 'Duration (s)', 'Max Probability', 'Severity']) rows = [] for i, interval in enumerate(suspicious_intervals): if len(interval) >= 4: start, end, max_prob, severity = interval[0], interval[1], interval[2], interval[3] elif len(interval) >= 2: start, end = interval[0], interval[1] max_prob = 0.8 # default severity = 'medium' else: continue rows.append({ 'Start Time (s)': f"{start:.2f}", 'End Time (s)': f"{end:.2f}", 'Duration (s)': f"{end - start:.2f}", 'Max Probability': f"{max_prob:.3f}", 'Severity': severity }) df = pd.DataFrame(rows) return df def load_model(model_path: str, progress=gr.Progress()) -> Dict[str, Any]: """Load a pre-trained model.""" global pipeline, current_model_path try: progress(0.3, desc="Checking model file...") if not os.path.exists(model_path): return {"error": f"Model file not found: {model_path}"} pipeline = get_pipeline() progress(0.6, desc="Loading model...") # Assuming pipeline has a load_model method if hasattr(pipeline, 'load_model'): pipeline.load_model(model_path) else: # Try to load via classifier if hasattr(pipeline.classifier, 'load'): pipeline.classifier.load(model_path) else: return {"error": "Model loading not implemented in pipeline"} current_model_path = model_path progress(1.0, desc="Model loaded successfully!") return { "status": "success", "model_path": model_path, "feature_count": getattr(pipeline.classifier, 'n_features_', 'unknown'), "is_fitted": getattr(pipeline.classifier, 'is_fitted_', False) } except Exception as e: logger.error(f"Failed to load model: {e}") return {"error": f"Failed to load model: {str(e)}"} def get_model_info() -> Dict[str, Any]: """Return current model information.""" global pipeline, current_model_path if pipeline is None: return {"status": "No pipeline initialized"} classifier = pipeline.classifier return { "model_path": current_model_path or "Not loaded", "is_fitted": getattr(classifier, 'is_fitted_', False), "feature_count": getattr(classifier, 'n_features_', 'unknown'), "model_type": type(classifier).__name__, "config": { "decision_threshold": pipeline.config.model.decision_threshold, "n_estimators": pipeline.config.model.n_estimators, "calibration_method": pipeline.config.model.calibration_method } } def update_settings( fps: int, audio_segment_length: int, generate_visualizations: bool, output_dir: str ) -> Dict[str, Any]: """Update pipeline settings.""" global pipeline, config try: # Note: AppConfig is frozen (immutable), so we cannot modify it directly. # In a real implementation, we would create a new config with updated values. # For this demo, we'll just store the settings and apply them when pipeline is reinitialized. settings = { "fps": fps, "audio_segment_length": audio_segment_length, "generate_visualizations": generate_visualizations, "output_dir": output_dir if output_dir.strip() else "Not specified (will use temp directory)", "note": "Settings will be applied on next pipeline initialization" } logger.info(f"Settings updated: {settings}") return {"status": "Settings updated (note: config is immutable, requires pipeline reinitialization)", "settings": settings} except Exception as e: logger.error(f"Failed to update settings: {e}") return {"error": str(e)} def format_assessment_html(assessment: Dict[str, Any]) -> str: """Format assessment results as HTML with styling.""" if "error" in assessment: return f"""
{assessment['error']}
Deception Probability: {probability:.3f}
Confidence: {confidence:.3f}
Decision Threshold: {threshold:.2f}
Calibrated: {calibrated}