File size: 8,938 Bytes
59f824e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
{
    "cells": [
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "view-in-github"
            },
            "source": [
                "<a href=\"https://colab.research.google.com/github/apexprit/deception-analysis/blob/main/colab_deception_analysis.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
            ]
        },
        {
            "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
}