File size: 2,359 Bytes
678772b
ef9c91b
 
 
 
 
 
 
 
2616304
 
678772b
ef9c91b
aff7fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef9c91b
aff7fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef9c91b
 
 
aff7fb9
 
 
 
 
ef9c91b
aff7fb9
 
 
 
 
 
 
 
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
---
title: Network Intrusion Detection System
colorFrom: blue
colorTo: red
sdk: gradio
sdk_version: 4.0.0
app_file: app.py
pinned: false
license: mit
tags:
- ml-intern
---

# πŸ›‘οΈ Network Intrusion Detection System (NIDS)

Real-time network intrusion detection using ML & Deep Learning models trained on the NSL-KDD dataset.

## Models
- **XGBoost** (best) β€” Accuracy 76.18%, AUC-ROC 95.75%
- **Random Forest** β€” Accuracy 73.10%, AUC-ROC 95.34%
- **MLP** β€” Deep learning baseline
- **Autoencoder** β€” Unsupervised anomaly detection
- **LSTM** β€” Sequential feature modeling
- **Transformer** β€” FlowTransformer-style architecture

## Dataset
[NSL-KDD](https://huggingface.co/datasets/Mireu-Lab/NSL-KDD) β€” 185K records, 41 features, binary classification (normal/anomaly)

## Usage

### REST API (FastAPI)
```bash
pip install fastapi uvicorn
uvicorn api:app --host 0.0.0.0 --port 8000
```

Endpoints:
- `GET /health` β€” Health check
- `GET /models` β€” List models
- `POST /predict` β€” Single flow detection
- `POST /predict/batch` β€” Batch detection
- `GET /stats` β€” Usage statistics

### Gradio UI
```bash
python app.py
```

## API Example
```bash
curl -X POST http://localhost:8000/predict \
  -H "Content-Type: application/json" \
  -d '{"features":[0,1,45,0,491,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,2,2,0,0,0,0,1,0,0,0,0.17], "model":"XGBoost"}'
```

## Results
| Model | Accuracy | Macro F1 | AUC-ROC | Type |
|-------|----------|----------|---------|------|
| XGBoost | 76.18% | 76.04% | 95.75% | Supervised |
| RandomForest | 73.10% | 73.05% | 95.34% | Supervised |
| MLP | 73.28% | 73.21% | 89.33% | Supervised |
| Autoencoder | 71.84% | 71.34% | 73.60% | Unsupervised |
| LSTM | 70.65% | 70.58% | 87.80% | Unsupervised |
| Transformer | 57.94% | 57.26% | 80.29% | Supervised |
| IsolationForest | 56.55% | 55.96% | 65.24% | Unsupervised |

## Project Structure
```
nids_project/
β”œβ”€β”€ api.py           # FastAPI REST API
β”œβ”€β”€ app.py           # Gradio web UI (this Space)
β”œβ”€β”€ data_loader.py   # Dataset loading
β”œβ”€β”€ ml_models.py     # ML implementations
β”œβ”€β”€ dl_models.py     # DL implementations
β”œβ”€β”€ realtime.py      # Real-time pipeline
β”œβ”€β”€ realtime_batch.py # Batch throughput benchmark
β”œβ”€β”€ Dockerfile       # Container image
└── requirements.txt # Dependencies
```