# abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis architecture

> Architecture diagram of the GitHub repository abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis
- Repository: https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis

## Overview

This is a local-first Gmail threat-analysis web app. A user signs in, selects a scan scope, and starts a scan; Flask fetches mail, classifies it, adds risk signals, stores results, and presents them for review. The app also supports single-text analysis, analytics, threat views, and consented, reversible mailbox actions. Optional local Ollama summaries and model-training modules are distinct branches. Relationships are limited to README statements and source-index references; unsampled route and template wiring is not inferred.

Main entry points: [`app.py`](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/app.py) (Flask application), [`models/predictor.py`](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/predictor.py) (Mail predictor), [`utils/gmail_client.py`](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/gmail_client.py) (Gmail client), [`static/js/main.js`](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/static/js/main.js) (Web interface)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web experience"]
  node_browser["Web interface<br/>[main.js]"]
  node_flask["Flask application<br/>[app.py]"]
end

subgraph group_analysis["Threat analysis"]
  node_predictor["Mail predictor<br/>[predictor.py]"]
  node_preprocess["Text preprocessing<br/>[preprocessing.py]"]
  node_roberta["RoBERTa inference<br/>[roberta_model.py]"]
  node_domain["Domain intelligence<br/>[domain_intel.py]"]
  node_attachments["Attachment inspection"]
  node_risk["Risk scoring<br/>[risk.py]"]
  node_helpers["Urgency and reputation<br/>[helpers.py]"]
  node_explanation["AI explanation and summary<br/>[ai_explanation.py]"]
  node_ensembletrain["Ensemble training<br/>[ensemble_train.py]"]
  node_robertatrain["RoBERTa training<br/>[roberta_train.py]"]
  node_evaluation["Model evaluation<br/>[evaluate.py]"]
end

subgraph group_mailbox["Mailbox operations"]
  node_auth["Google sign-in<br/>[auth.py]"]
  node_gmailclient["Gmail client<br/>[gmail_client.py]"]
  node_actions["Reviewed mail actions<br/>[mail_actions.py]"]
end

subgraph group_state["Local state and models"]
  node_database[("Email and scan records<br/>[email_model.py]")]
end

node_user(("User"))
node_gmail["Gmail API"]
node_ollama["Local Ollama"]

node_user -->|"uses"| node_browser
node_browser -->|"requests pages"| node_flask
node_flask -->|"starts sign-in"| node_auth
node_auth -->|"OAuth consent"| node_gmail
node_flask -->|"fetches mail"| node_gmailclient
node_gmailclient -->|"reads messages"| node_gmail
node_flask -->|"classifies mail"| node_predictor
node_predictor -->|"prepares text"| node_preprocess
node_predictor -->|"runs inference"| node_roberta
node_predictor -->|"uses signals"| node_domain
node_predictor -->|"uses signals"| node_attachments
node_predictor -->|"uses scoring"| node_risk
node_flask -->|"inspects files"| node_attachments
node_flask -->|"adds risk details"| node_helpers
node_helpers -->|"reads and updates"| node_database
node_flask -->|"stores results"| node_database
node_flask -->|"routes reviewed actions"| node_actions
node_flask -->|"requests summaries"| node_explanation
node_explanation -.->|"requests generation"| node_ollama
node_ensembletrain -->|"uses features"| node_preprocess
node_robertatrain -->|"uses mail data"| node_gmailclient
node_evaluation -->|"evaluates predictions"| node_predictor

click node_browser "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/static/js/main.js"
click node_flask "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/app.py"
click node_auth "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/auth.py"
click node_gmailclient "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/gmail_client.py"
click node_predictor "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/predictor.py"
click node_preprocess "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/utils/preprocessing.py"
click node_roberta "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/roberta_model.py"
click node_domain "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/domain_intel.py"
click node_attachments "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/attachment_intel.py"
click node_risk "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/risk.py"
click node_helpers "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/helpers.py"
click node_database "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/email_model.py"
click node_actions "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/mail_actions.py"
click node_explanation "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/ai_explanation.py"
click node_ensembletrain "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/ensemble_train.py"
click node_robertatrain "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/roberta_train.py"
click node_evaluation "https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/evaluate.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_browser,node_flask,node_user toneBlue
class node_predictor,node_preprocess,node_roberta,node_domain,node_attachments,node_risk,node_helpers,node_explanation,node_ensembletrain,node_robertatrain,node_evaluation toneAmber
class node_auth,node_gmailclient,node_actions,node_gmail toneMint
class node_database toneRose
class node_ollama toneIndigo
```

## Components

### Web experience

- [Web interface](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/static/js/main.js): `static/js/main.js`
- [Flask application](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/app.py): `app.py`

### Threat analysis

- [Mail predictor](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/predictor.py): `models/predictor.py`
- [Text preprocessing](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/utils/preprocessing.py): `models/utils/preprocessing.py`
- [RoBERTa inference](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/roberta_model.py): `models/roberta_model.py`
- [Domain intelligence](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/domain_intel.py): `utils/domain_intel.py`
- [Attachment inspection](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/attachment_intel.py): `utils/attachment_intel.py`
- [Risk scoring](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/risk.py): `utils/risk.py`
- [Urgency and reputation](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/helpers.py): `utils/helpers.py`
- [AI explanation and summary](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/ai_explanation.py): `utils/ai_explanation.py`
- [Ensemble training](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/ensemble_train.py): `models/ensemble_train.py`
- [RoBERTa training](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/roberta_train.py): `models/roberta_train.py`
- [Model evaluation](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/evaluate.py): `models/evaluate.py`

### Mailbox operations

- [Google sign-in](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/auth.py): `utils/auth.py`
- [Gmail client](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/gmail_client.py): `utils/gmail_client.py`
- [Reviewed mail actions](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/utils/mail_actions.py): `utils/mail_actions.py`

### Local state and models

- [Email and scan records](https://github.com/abhisheksharma611/ai-powered-email-spam-detection-and-threat-analysis/blob/main/models/email_model.py): `models/email_model.py`

### Other components

- **User**
- **Gmail API**
- **Local Ollama**

## Connections

- User → Web interface: uses (evidence: `README.md`)
- Web interface → Flask application: requests pages (evidence: `README.md`)
- Flask application → Google sign-in: starts sign-in (evidence: `README.md`)
- Google sign-in → Gmail API: OAuth consent (evidence: `README.md`)
- Flask application → Gmail client: fetches mail (evidence: `README.md`)
- Gmail client → Gmail API: reads messages (evidence: `utils/gmail_client.py`)
- Flask application → Mail predictor: classifies mail (evidence: `app.py`)
- Mail predictor → Text preprocessing: prepares text (evidence: `models/predictor.py`)
- Mail predictor → RoBERTa inference: runs inference (evidence: `models/predictor.py`)
- Mail predictor → Domain intelligence: uses signals (evidence: `models/predictor.py`)
- Mail predictor → Attachment inspection: uses signals (evidence: `models/predictor.py`)
- Mail predictor → Risk scoring: uses scoring (evidence: `models/predictor.py`)
- Flask application → Attachment inspection: inspects files (evidence: `app.py`)
- Flask application → Urgency and reputation: adds risk details (evidence: `app.py`)
- Urgency and reputation → Email and scan records: reads and updates (evidence: `utils/helpers.py`)
- Flask application → Email and scan records: stores results (evidence: `app.py`)
- Flask application → Reviewed mail actions: routes reviewed actions (evidence: `README.md`)
- Flask application → AI explanation and summary: requests summaries (evidence: `README.md`)
- AI explanation and summary → Local Ollama: requests generation
- Ensemble training → Text preprocessing: uses features (evidence: `models/ensemble_train.py`)
- RoBERTa training → Gmail client: uses mail data (evidence: `models/roberta_train.py`)
- Model evaluation → Mail predictor: evaluates predictions (evidence: `models/evaluate.py`)
