File size: 13,705 Bytes
50776af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
---
title: SentinelAI
emoji: πŸ›‘οΈ
colorFrom: blue
colorTo: red
sdk: docker
pinned: false
---

<div align="center">

# πŸ›‘οΈ SentinelAI

### AI-Powered Digital Threat Intelligence Engine

*Detect scams. Quantify risk. Generate intelligence briefs.*

[![Python](https://img.shields.io/badge/Python-3.10+-3776AB?style=flat-square&logo=python&logoColor=white)](https://python.org)
[![Flask](https://img.shields.io/badge/Flask-3.0-000000?style=flat-square&logo=flask&logoColor=white)](https://flask.palletsprojects.com)
[![Transformers](https://img.shields.io/badge/HuggingFace-DistilBERT-FFD21E?style=flat-square&logo=huggingface&logoColor=black)](https://huggingface.co)
[![PyTorch](https://img.shields.io/badge/PyTorch-2.2-EE4C2C?style=flat-square&logo=pytorch&logoColor=white)](https://pytorch.org)
[![Accuracy](https://img.shields.io/badge/Accuracy-97.42%25-22C55E?style=flat-square)](/)
[![F1 Score](https://img.shields.io/badge/F1_Score-97.59%25-22C55E?style=flat-square)](/)
[![License: MIT](https://img.shields.io/badge/License-MIT-22D3EE?style=flat-square)](LICENSE)

</div>

---

## πŸ“‹ Overview

**SentinelAI** is a real-time digital threat analysis system that uses a **fine-tuned DistilBERT transformer** combined with **rule-based heuristic pre-filtering** to detect digital arrest scams, phishing attempts, and financial fraud in text messages.

The system features:
- A **hybrid inference pipeline** (rules + neural network) achieving **97.42% accuracy**
- A **tactical HUD-style frontend** with animated score visualizations and threat indicator chips
- A **downloadable PDF intelligence brief** formatted like a professional security document

---

## πŸ–₯️ UI Preview

> Dark-themed tactical HUD with animated SVG score arc, threat indicator chips, OCR image upload, and a downloadable intelligence brief.

---

## 🧠 Model Performance

The DistilBERT model was fine-tuned on a **curated digital arrest scam corpus** and evaluated on a held-out test set of **155 samples**.

### Final Evaluation Metrics

| Metric | Score |
|---|---|
| **Accuracy** | **97.42%** |
| **F1 Score** | **97.59%** |
| **Precision** | **97.59%** |
| **Recall** | **97.59%** |

### Confusion Matrix

| | Predicted SAFE | Predicted SCAM |
|---|---|---|
| **Actual SAFE** | 70 | 2 |
| **Actual SCAM** | 2 | 81 |

> Out of 155 test samples, the model produced only **4 misclassifications** (2 false positives + 2 false negatives), achieving a near-perfect detection rate.

### Key Takeaways
- **False Positive Rate**: 2.78% β€” only 2 out of 72 safe messages were incorrectly flagged
- **False Negative Rate**: 2.41% β€” only 2 out of 83 scam messages were missed
- **Balanced performance**: Equal precision and recall indicate the model doesn't bias toward either class

---

## ✨ Features

| Feature | Description |
|---|---|
| πŸ€– **Fine-Tuned DistilBERT** | Transformer model trained on curated digital scam corpus with 97.42% accuracy |
| ⚑ **Hybrid Inference Engine** | Rule-based pre-screening (10 signal patterns) + neural network fallback |
| 🎯 **Real-Time Risk Scoring** | Probabilistic scam/safe scoring with HIGH / MEDIUM / LOW classification |
| πŸ” **Signal Extraction** | Names specific scam tactics detected (authority threats, urgency pressure, etc.) |
| πŸ–₯️ **Tactical HUD Interface** | Dark-themed dashboard with animated SVG score arcs and threat indicator chips |
| πŸ“„ **PDF Intelligence Brief** | Professional SIB-format report with risk bands, signal tables, and action items |
| ⌨️ **Keyboard Shortcuts** | Ctrl+Enter to run analysis |

---

## πŸ—οΈ System Architecture

```mermaid
flowchart TD
    A["πŸ‘€ User\nPastes suspicious message"] --> B["🌐 Flask Web App\napp.py"]

    B --> C["πŸ” Hybrid Inference Engine\ninference.py"]

    C --> D{"Rule-Based\nPre-filter"}
    D -->|"β‰₯2 scam signals matched"| E["πŸ”΄ HIGH RISK\nReturn immediately"]
    D -->|"Safe keywords matched"| F["🟒 LOW RISK\nReturn immediately"]
    D -->|"Ambiguous"| G["πŸ€– DistilBERT Model\nsentinel_model/"]

    G --> H["Softmax Probabilities\nSCAM vs SAFE"]
    H --> I["Probability Adjustment\nvia rule scores"]
    I --> J["Final Classification\n+ risk_level + signals"]

    E --> K["πŸ“‘ JSON Response\nto Frontend"]
    F --> K
    J --> K

    K --> L["πŸ–₯️ Tactical HUD UI\nScore arc + threat chips"]
    L --> M{"User clicks\nDownload Brief?"}
    M -->|Yes| N["πŸ“„ pdf_generator.py\nStructured Intelligence Brief"]
    N --> O["⬇️ PDF Download\nSentinelAI_Report.pdf"]
```

---

## πŸ”¬ Inference Pipeline

```mermaid
sequenceDiagram
    participant U as User
    participant F as Flask API
    participant R as Rule Engine
    participant M as DistilBERT Model
    participant P as PDF Generator

    U->>F: POST /analyze { message }
    F->>R: Check 10 scam signal patterns
    alt β‰₯2 patterns matched
        R-->>F: HIGH RISK + matched signals
    else Safe keywords found
        R-->>F: LOW RISK + empty signals
    else Ambiguous
        R->>M: Tokenize + forward pass
        M-->>R: Softmax probabilities
        R-->>F: Adjusted label + risk_level + signals
    end
    F-->>U: JSON { label, scam_probability, risk_level, signals }

    U->>F: POST /download_report { analysis data }
    F->>P: generate_pdf(data, filepath)
    P-->>F: SentinelAI_Report.pdf
    F-->>U: PDF file download
```

---

## πŸ”¬ Technical Deep Dive

### Hybrid Inference Strategy

The inference engine uses a **two-stage approach** to maximize both speed and accuracy:

**Stage 1 β€” Rule-Based Pre-filter** (instant, zero-cost):
- Scans the input against **10 regex-based scam signal patterns**
- If β‰₯2 patterns match β†’ immediately returns `HIGH RISK` (no model inference needed)
- If safe keywords match and no scam signals β†’ immediately returns `LOW RISK`
- This handles clear-cut cases in **<1ms** without loading the model

**Stage 2 β€” Neural Network** (for ambiguous cases):
- Tokenizes the input using DistilBERT's WordPiece tokenizer
- Performs a forward pass through the fine-tuned model
- Applies softmax to get SCAM vs SAFE probabilities
- Adjusts probabilities using partial rule scores for better calibration

### Detected Scam Signal Categories

| # | Signal | Example Pattern |
|---|---|---|
| 1 | Arrest / legal authority threat | *"FBI warrant", "CBI enforcement"* |
| 2 | Urgency / time pressure | *"immediate", "within 2 hours"* |
| 3 | Payment demand with urgency | *"transfer funds now"* |
| 4 | Phishing link / click-bait | *"click here to verify"* |
| 5 | Account suspension threat | *"your account is frozen"* |
| 6 | Prize / lottery scam | *"you have won a prize"* |
| 7 | Credential / remote access request | *"share OTP", "install AnyDesk"* |
| 8 | Digital arrest pattern | *"stay on the line"* |
| 9 | Isolation / secrecy demand | *"do not tell anyone"* |
| 10 | Document / ID fraud | *"your Aadhaar is blocked"* |

---

## πŸ“ Project Structure

```
Sentinel/
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ app.py                  # Flask routes: /, /analyze, /ocr_analyze, /download_report
β”‚   β”œβ”€β”€ inference.py            # Hybrid prediction engine (rules + DistilBERT)
β”‚   β”œβ”€β”€ ocr.py                  # Image β†’ text extraction via Tesseract
β”‚   β”œβ”€β”€ pdf_generator.py        # Structured Intelligence Brief PDF generator
β”‚   └── templates/
β”‚       └── index.html          # Tactical HUD SPA (Tailwind CSS, dark theme)
β”‚
β”œβ”€β”€ models/
β”‚   └── train_model_v2.py       # Training script (for reference β€” model on HuggingFace)
β”‚
β”œβ”€β”€ data/
β”‚   └── sentinel_dataset_v3_final.csv   # Final training dataset (14,000 rows, 60:40 ratio)
β”‚
β”œβ”€β”€ Dockerfile                  # HuggingFace Spaces deployment (port 7860)
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ .gitignore
└── README.md
```

> ⚠️ The trained model weights are **not stored locally** β€” they are loaded directly from
> [`Shade63/sentinel-model`](https://huggingface.co/Shade63/sentinel-model) on HuggingFace Hub.

---

## πŸ› οΈ Tech Stack

| Layer | Technology | Purpose |
|---|---|---|
| **ML Model** | DistilBERT (HuggingFace Transformers) | Fine-tuned binary classifier for scam detection |
| **ML Framework** | PyTorch 2.2 | Tensor operations and model inference |
| **Backend** | Flask 3.0 | REST API serving `/analyze` and `/download_report` |
| **Frontend** | HTML + Tailwind CSS + Vanilla JS | Tactical HUD single-page application |
| **PDF Engine** | ReportLab 4.1 | Generates dark-themed Structured Intelligence Briefs |
| **Data Processing** | Pandas + scikit-learn | Dataset management and evaluation metrics |

---

## βš™οΈ Setup & Installation

### Prerequisites
- Python 3.10+
- pip

### 1. Clone the repository
```bash
git clone https://github.com/Shade-63/Sentinel.git
cd Sentinel
```

### 2. Create and activate a virtual environment
```bash
python -m venv env

# Windows
env\Scripts\activate

# macOS / Linux
source env/bin/activate
```

### 3. Install dependencies
```bash
pip install -r requirements.txt
```

### 4. Download the model weights

> ⚠️ `model.safetensors` (~255 MB) is **not included** in this repo due to GitHub's 100 MB file limit.

**Option A β€” From Releases** *(recommended)*
Download `model.safetensors` from the [Releases page](../../releases) and place it at:
```
models/sentinel_model/model.safetensors
```

**Option B β€” Train from scratch**
```bash
cd models
python train_model_v2.py
```

### 5. Run the application
```bash
cd app
python app.py
```

Open **http://127.0.0.1:7860** in your browser.

---

## πŸ”Œ API Reference

### `POST /analyze`

Analyzes a text message for scam indicators.

**Request:**
```json
{
  "message": "FBI warrant arrest immediate payment"
}
```

**Response:**
```json
{
  "label": "SCAM",
  "scam_probability": 0.99,
  "safe_probability": 0.01,
  "risk_level": "HIGH",
  "signals": [
    "Arrest or legal authority threat",
    "Urgency / time pressure",
    "Payment demand with urgency"
  ]
}
```

### `POST /download_report`

Generates and returns a PDF Structured Intelligence Brief.

**Request:**
```json
{
  "message": "...",
  "risk_score": "99.0",
  "risk_level": "HIGH",
  "signals": ["Arrest or legal authority threat"]
}
```

**Response:** Binary PDF file download

---

## πŸ“„ PDF Report β€” Structured Intelligence Brief

After analysis, click **DOWNLOAD INTELLIGENCE BRIEF** to get a formatted SIB containing:

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  SENTINELAI  Β·  STRUCTURED INTELLIGENCE BRIEF            β”‚
β”‚  Report ID: SIB-20260320-170900   Generated: 20 Mar 2026 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  ⬛ THREAT INTELLIGENCE REPORT β€” CONFIDENTIAL            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ CLASSIFICATIONβ”‚ REPORT TYPE β”‚ ENGINE       β”‚ TIMESTAMP   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                          β”‚
β”‚   πŸ”΄ HIGH RISK β€” SCAM DETECTED      99.0%                β”‚
β”‚   β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘ progress bar         β”‚
β”‚                                                          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  01 Β· INTERCEPTED COMMUNICATION                          β”‚
β”‚  02 Β· DETECTED RISK INDICATORS      (numbered table)     β”‚
β”‚  03 Β· AI MODEL INTERPRETATION       (key-value table)    β”‚
β”‚  04 Β· RECOMMENDED IMMEDIATE ACTIONS (CRITICAL/HIGH/MED)  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  CONFIDENTIAL β€” FOR AUTHORIZED USE ONLY    Page 1        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

---

## πŸš€ What Makes This Different

| Aspect | SentinelAI | Simple Keyword Matching |
|---|---|---|
| **Detection Method** | Fine-tuned transformer + rule engine | Static keyword lists |
| **Accuracy** | 97.42% | ~70% (high false-positive rate) |
| **Context Understanding** | Understands sentence-level semantics | Matches isolated words |
| **Signal Extraction** | Names specific tactics used | No explanation |
| **Risk Quantification** | Probabilistic score (0-100%) | Binary yes/no |
| **Output** | Professional PDF intelligence brief | Plain text alert |

---

## ⚠️ Disclaimer

SentinelAI provides AI-based probabilistic risk estimation and does **not** constitute legal advice. All findings are based on pattern recognition and should be verified through official law enforcement or financial authorities. This tool is intended for educational and informational purposes only.

---

<div align="center">

**Built with** Flask Β· HuggingFace Transformers Β· PyTorch Β· ReportLab

*Protecting innocents from digital threats through AI-powered intelligence*

</div>