{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "X3jW0N2vQe7p" }, "source": [ "# \ud83d\udd75\ufe0f\u200d\u2642\ufe0f Multimodal Deception Analysis - Google Colab\n", "\n", "**Run this entire deception detection system in the cloud.**\n", "\n", "This notebook provides a complete environment for detecting deception using:\n", "- **Facial micro-expressions** (Mediapipe Face Mesh)\n", "- **Audio speech patterns** (Librosa)\n", "- **SHAP-based explainability**\n", "\n", "## \ud83d\ude80 Quick Start\n", "1. **Runtime \u2192 Run all** (Ctrl+F9).\n", "2. If the session restarts, just click **Run all** again.\n", "3. **Scroll to the bottom** of the last cell to find the UI." ] }, { "cell_type": "markdown", "metadata": { "id": "8h2L5U7nQe7q" }, "source": [ "## 1. Environment Setup" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "7hQ2nVv-Qe7r" }, "outputs": [], "source": [ "#@title Install Dependencies\n", "print(\"\ud83d\udee0\ufe0f Installing project dependencies...\")\n", "\n", "!pip install -q mediapipe==0.10.11\n", "!pip install -q opencv-python librosa gradio shap joblib tqdm pandas numpy scikit-learn\n", "\n", "print(\"\u2705 Installation complete!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "q2Q2nVv-Qe7s" }, "outputs": [], "source": [ "#@title Clone & Setup Path\n", "import os\n", "import sys\n", "import shutil\n", "\n", "if os.path.exists('/content/deception-analysis'):\n", " shutil.rmtree('/content/deception-analysis')\n", "\n", "!git clone -q https://github.com/apexprit/deception-analysis.git /content/deception-analysis\n", "sys.path.insert(0, '/content/deception-analysis')\n", "\n", "print(\"\u2705 Project ready!\")" ] }, { "cell_type": "markdown", "metadata": { "id": "MhQ2nVv-Qe7t" }, "source": [ "## 2. Train & Launch App" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "NhQ2nVv-Qe7u" }, "outputs": [], "source": [ "#@title Generate Model & Start Web App\n", "import sys\n", "import os\n", "import pandas as pd\n", "import numpy as np\n", "sys.path.insert(0, '/content/deception-analysis')\n", "\n", "from src.utils.synthetic_data import SyntheticDataGenerator\n", "from src.utils.config import get_default_config\n", "from src.model.classifier import DeceptionClassifier\n", "import gradio as gr\n", "from src.pipeline import DeceptionPipeline\n", "\n", "print(\"\ud83d\udd27 Loading configuration...\")\n", "config = get_default_config()\n", "\n", "print(\"\ud83d\udd27 Generating synthetic dataset for model training...\")\n", "generator = SyntheticDataGenerator(config, seed=42)\n", "features_df, labels, _ = generator.generate_dataset(n_truthful=300, n_deceptive=300)\n", "\n", "print(\"\ud83d\udcc8 Training classifier...\")\n", "classifier = DeceptionClassifier(config.model)\n", "X, feature_names = classifier.prepare_features(features_df)\n", "metrics = classifier.train(X, labels, feature_names)\n", "print(f\"\u2705 Training complete! Accuracy: {metrics.get('train_accuracy', 0):.3f}\")\n", "\n", "model_dir = '/content/deception-analysis/models'\n", "os.makedirs(model_dir, exist_ok=True)\n", "model_path = os.path.join(model_dir, 'deception_model.pkl')\n", "classifier.save(model_path)\n", "\n", "print(\"\\n\ud83d\ude80 Initializing Analysis Pipeline...\")\n", "pipeline = DeceptionPipeline(config)\n", "pipeline.load_model(model_path)\n", "\n", "def analyze(video, subject_id):\n", " if not video: return \"Please upload a video.\", None, None\n", " \n", " output_results = '/content/results'\n", " os.makedirs(output_results, exist_ok=True)\n", " \n", " res = pipeline.analyze_video(\n", " video, \n", " subject_id=subject_id or \"S01\", \n", " generate_visualizations=True, \n", " output_dir=output_results\n", " )\n", " \n", " if \"error\" in res:\n", " return f\"### Error during analysis:\\n{res['error']}\", None, None\n", " \n", " summary = f\"### Result: {res['prediction'].upper()}\\n- Prob: {res['deception_probability']:.3f}\\n- Conf: {res['confidence']:.3f}\"\n", " viz = res.get('visualizations', {})\n", " \n", " return summary, viz.get('temporal_trajectory'), viz.get('shap_summary')\n", "\n", "print(\"\ud83d\udce1 Building Interface...\")\n", "with gr.Blocks(theme=gr.themes.Soft()) as demo:\n", " gr.Markdown(\"# \ud83d\udd75\ufe0f\u200d\u2642\ufe0f Multimodal Deception Analysis\")\n", " gr.Markdown(\"Analyze facial micro-expressions and audio patterns for deception detection.\")\n", " \n", " with gr.Row():\n", " with gr.Column():\n", " v_in = gr.Video(label=\"Interview Video\")\n", " s_in = gr.Textbox(label=\"Subject ID\", placeholder=\"e.g. S01\")\n", " btn = gr.Button(\"Analyze Video\", variant=\"primary\")\n", " with gr.Column():\n", " sum_out = gr.Markdown(label=\"Assessment Summary\")\n", " with gr.Tabs():\n", " with gr.TabItem(\"Temporal\"): \n", " traj_out = gr.Image(label=\"Probability Trajectory\")\n", " with gr.TabItem(\"Explainability\"): \n", " shap_out = gr.Image(label=\"SHAP Feature Importance\")\n", " \n", " btn.click(analyze, [v_in, s_in], [sum_out, traj_out, shap_out])\n", "\n", "print(\"\\n\u2728 APP IS READY! Scroll down to see the interface below.\")\n", "demo.launch(share=True, debug=True)" ] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }