# dhanush342/url-phishing-detection architecture

> Architecture diagram of the GitHub repository dhanush342/url-phishing-detection, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dhanush342/url-phishing-detection
- Repository: https://github.com/dhanush342/url-phishing-detection

## Overview

1. **What this repo is**
   - A phishing URL detection system with two main parts: a **browser-based UI** for manual testing and a **backend inference pipeline** that classifies URLs and explains why.

2. **Top-level repository shape**
   - The visible root files are:
     - `index.html`, `script.js`, `style.css` for the frontend
     - `dataset_phishing (1).csv` for training data
     - `run_all.ps1` for one-command setup/run
   - The README also describes a `backend/` directory containing the FastAPI app, feature extraction, model training, and saved models.

3. **Frontend subsystem**
   - `index.html` is the app shell.
   - `style.css` defines the presentation layer.
   - `script.js` is the client logic that likely captures a URL, sends it to the backend, and renders prediction/explanation results.
   - This is a **static browser UI**, not a heavy frontend framework.

4. **Backend API boundary**
   - The backend is described as a **FastAPI service** exposing `POST /predict`.
   - That endpoint is the main integration boundary: the UI submits a raw URL string, and the backend returns the label plus diagnostic data.

5. **Core inference pipeline**
   - The backend architecture starts with **feature extraction** from a raw URL.
   - The README says there are **29 features** derived from URL structure, lexical patterns, entropy/NLP-like signals, and heuristic indicators.

6. **Hybrid reasoning stack**
   - Inference is not just a single classifier. It combines:
     - an **ensemble ML model**
     - a **Bayesian reasoning layer**
     - a **rule engine**
     - **CSP-style consistency checks / graph-style reasoning**
   - The response is therefore both a prediction and an explanation trace.

7. **Model subsystem**
   - The model zoo includes:
     - Random Forest
     - Gaussian Naive Bayes
     - Gradient Boosting
     - Decision Tree
     - Soft-voting ensemble as the production model
   - Saved model artifacts live under `backend/model/` and are loaded during prediction.

8. **Explainability outputs**
   - The API response includes not just `label` and probability, but also:
     - extracted feature values
     - fired rules and rule score
     - CSP violations and satisfaction score
     - Bayesian score
     - human-readable explanations
   - This makes explainability a first-class subsystem rather than an afterthought.

9. **Training and data flow**
   - `dataset_phishing (1).csv` is the training/evaluation source.
   - `train_model.py` uses that dataset to build the individual models and the ensemble, then stores the artifacts for inference.
   - Training is separate from runtime prediction, which is a clean ML lifecycle boundary.

10. **Orchestration and startup**
   - `run_all.ps1` is the operational entry point.
   - It coordinates environment setup, model training if needed, backend startup, and opening the browser UI.
   - That script is the main “glue” between the data/model pipeline and the interactive demo.

11. **Primary runtime/data path**
   - User enters a URL in the browser UI.
   - The frontend sends the URL to the FastAPI backend.
   - The backend extracts features, runs ML + reasoning checks, aggregates the result, and returns a structured JSON response.
   - The frontend displays the verdict and explanation details.

12. **Important architectural boundaries**
   - **Presentation boundary:** static browser files vs. server-side inference.
   - **Model boundary:** trained artifacts vs. runtime scoring.
   - **Reasoning boundary:** numeric classification vs. human-readable explanation generation.
   - **Data boundary:** raw URL input vs. engineered features vs. prediction output.

13. **What an architecture diagram should emphasize**
   - A left-to-right flow from **URL input → feature extraction → hybrid inference stack → JSON response → UI rendering**.
   - Show the backend as multiple cooperating modules, not a single black box.
   - Show the dataset and training pipeline as a separate offline path feeding the saved model artifacts used by online inference.

Main entry points: [`script.js`](https://github.com/dhanush342/url-phishing-detection/blob/main/script.js) (Client logic), [`run_all.ps1`](https://github.com/dhanush342/url-phishing-detection/blob/main/run_all.ps1) (Run all), [`index.html`](https://github.com/dhanush342/url-phishing-detection/blob/main/index.html) (App shell), [`dataset_phishing (1).csv`](https://github.com/dhanush342/url-phishing-detection/blob/HEAD/dataset_phishing%20(1).csv) (Training data)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_index_html["App shell<br/>static HTML<br/>[index.html]"]
  node_style_css["Styles<br/>presentation CSS<br/>[style.css]"]
  node_script_js["Client logic<br/>browser JS<br/>[script.js]"]
  node_url_input["URL input<br/>user input"]
  node_json_result["Result view<br/>UI output"]
end

subgraph group_backend["Backend"]
  node_fastapi_api["Predict API<br/>FastAPI service"]
  node_feature_extract["Feature extractor<br/>29-feature pipeline"]
  node_ensemble_model[("Ensemble model<br/>soft-voting ML")]
  node_bayes_layer["Bayesian layer<br/>reasoning module"]
  node_rule_engine["Rule engine<br/>heuristic checks"]
  node_csp_checks["CSP checks<br/>consistency checks"]
  node_explanation_agg["Explanations<br/>response assembler"]
end

subgraph group_offline["Offline ML"]
  node_dataset_csv["Training data<br/>CSV dataset"]
  node_train_pipeline["Train pipeline<br/>model training"]
  node_model_store[("Saved models<br/>artifact store")]
end

subgraph group_ops["Orchestration"]
  node_run_all_ps1["Run all<br/>PowerShell orchestration<br/>[run_all.ps1]"]
end

node_url_input -->|"submit URL"| node_script_js
node_script_js -->|"POST /predict"| node_fastapi_api
node_fastapi_api -->|"extract features"| node_feature_extract
node_feature_extract -->|"score features"| node_ensemble_model
node_feature_extract -->|"evaluate rules"| node_rule_engine
node_feature_extract -->|"check constraints"| node_csp_checks
node_ensemble_model -->|"probability input"| node_bayes_layer
node_ensemble_model -->|"prediction"| node_explanation_agg
node_rule_engine -->|"rule trace"| node_explanation_agg
node_csp_checks -->|"violations"| node_explanation_agg
node_bayes_layer -->|"bayes score"| node_explanation_agg
node_explanation_agg -->|"JSON response"| node_script_js
node_script_js -->|"render result"| node_json_result
node_dataset_csv -->|"train on"| node_train_pipeline
node_train_pipeline -->|"persist artifacts"| node_model_store
node_model_store -->|"load at runtime"| node_ensemble_model
node_run_all_ps1 -->|"orchestrate training"| node_train_pipeline
node_run_all_ps1 -->|"start backend"| node_fastapi_api
node_run_all_ps1 -->|"open UI"| node_index_html
node_index_html -->|"uses styles"| node_style_css
node_index_html -->|"loads logic"| node_script_js

click node_index_html "https://github.com/dhanush342/url-phishing-detection/blob/main/index.html"
click node_style_css "https://github.com/dhanush342/url-phishing-detection/blob/main/style.css"
click node_script_js "https://github.com/dhanush342/url-phishing-detection/blob/main/script.js"
click node_dataset_csv "https://github.com/dhanush342/url-phishing-detection/blob/main/dataset_phishing (1).csv"
click node_run_all_ps1 "https://github.com/dhanush342/url-phishing-detection/blob/main/run_all.ps1"

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_index_html,node_style_css,node_script_js,node_url_input,node_json_result toneBlue
class node_fastapi_api,node_feature_extract,node_ensemble_model,node_bayes_layer,node_rule_engine,node_csp_checks,node_explanation_agg toneAmber
class node_dataset_csv,node_train_pipeline,node_model_store toneMint
class node_run_all_ps1 toneRose
```

## Components

### Frontend

Static browser UI for manual URL testing and result display.

- [App shell](https://github.com/dhanush342/url-phishing-detection/blob/main/index.html): static HTML · Browser page structure for the phishing checker UI. · `index.html`
- [Styles](https://github.com/dhanush342/url-phishing-detection/blob/main/style.css): presentation CSS · Visual styling for the static UI. · `style.css`
- [Client logic](https://github.com/dhanush342/url-phishing-detection/blob/main/script.js): browser JS · Captures URL input, calls the backend, and renders verdicts and explanations. · `script.js`
- **URL input**: user input · Manual URL entered by the user for inspection.
- **Result view**: UI output · Displays prediction, confidence, feature details, and explanation traces.

### Backend

Online inference service and explainability stack.

- **Predict API**: FastAPI service · Server boundary exposing POST /predict for URL classification.
- **Feature extractor**: 29-feature pipeline · Derives lexical, structural, entropy, and heuristic signals from a raw URL.
- **Ensemble model**: soft-voting ML · Primary classifier used at runtime to score phishing likelihood.
- **Bayesian layer**: reasoning module · Adds probabilistic reasoning on top of the classifier output.
- **Rule engine**: heuristic checks · Applies phishing rules and produces fired rules plus rule score.
- **CSP checks**: consistency checks · Validates consistency constraints and records violations or satisfaction.
- **Explanations**: response assembler · Combines model output, rules, Bayesian score, and checks into human-readable diagnostics.

### Offline ML

Training pipeline and persisted model artifacts.

- [Training data](https://github.com/dhanush342/url-phishing-detection/blob/HEAD/dataset_phishing%20(1).csv): CSV dataset · Labeled phishing dataset used for model training and evaluation. · `dataset_phishing (1).csv`
- **Train pipeline**: model training · Builds the individual classifiers and the production ensemble from the dataset.
- **Saved models**: artifact store · Persisted model artifacts loaded by the backend during inference.

### Orchestration

One-command setup and runtime entry point.

- [Run all](https://github.com/dhanush342/url-phishing-detection/blob/main/run_all.ps1): PowerShell orchestration · Sets up the environment, trains if needed, starts the backend, and opens the UI. · `run_all.ps1`

## Connections

- URL input → Client logic: submit URL · User enters a URL in the browser UI.
- Client logic → Predict API: POST /predict · Client sends the raw URL to the backend inference API.
- Predict API → Feature extractor: extract features · Backend converts the URL into engineered feature values.
- Feature extractor → Ensemble model: score features · Feature vector is passed to the production classifier.
- Feature extractor → Rule engine: evaluate rules · Heuristics inspect extracted URL signals.
- Feature extractor → CSP checks: check constraints · Consistency checks validate URL-derived conditions.
- Ensemble model → Bayesian layer: probability input · Model score informs Bayesian reasoning.
- Ensemble model → Explanations: prediction · Classifier output contributes to the final response.
- Rule engine → Explanations: rule trace · Fired rules and rule score are included in the explanation payload.
- CSP checks → Explanations: violations · Constraint results are summarized for the response.
- Bayesian layer → Explanations: bayes score · Bayesian reasoning is folded into the output.
- Explanations → Client logic: JSON response · Backend returns label, confidence, feature values, and explanations.
- Client logic → Result view: render result · Frontend displays the verdict and diagnostic details.
- Training data → Train pipeline: train on · Offline training consumes the labeled phishing dataset.
- Train pipeline → Saved models: persist artifacts · Trained models are saved for runtime scoring.
- Saved models → Ensemble model: load at runtime · Backend loads saved artifacts during prediction.
- Run all → Train pipeline: orchestrate training · Script triggers model training when required.
- Run all → Predict API: start backend · Script launches the FastAPI inference service.
- Run all → App shell: open UI · Script brings up the browser demo.
- App shell → Styles: uses styles · The app shell is styled by the CSS layer.
- App shell → Client logic: loads logic · The page loads the browser client code.
