Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import numpy as np
|
| 5 |
+
import joblib
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
+
|
| 8 |
+
# -----------------------------
|
| 9 |
+
# Page config (premium look)
|
| 10 |
+
# -----------------------------
|
| 11 |
+
st.set_page_config(
|
| 12 |
+
page_title="Cyber Threat Detection Dashboard",
|
| 13 |
+
page_icon="🛡️",
|
| 14 |
+
layout="wide"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# -----------------------------
|
| 18 |
+
# Title Section
|
| 19 |
+
# -----------------------------
|
| 20 |
+
st.markdown("""
|
| 21 |
+
# 🛡️ Cyber Threat Detection Dashboard
|
| 22 |
+
**Deep Learning–Based Suspicious Activity Detection**
|
| 23 |
+
""")
|
| 24 |
+
|
| 25 |
+
st.markdown("---")
|
| 26 |
+
|
| 27 |
+
# -----------------------------
|
| 28 |
+
# Model definition
|
| 29 |
+
# -----------------------------
|
| 30 |
+
class SuspiciousModel(nn.Module):
|
| 31 |
+
def __init__(self, input_dim):
|
| 32 |
+
super().__init__()
|
| 33 |
+
self.net = nn.Sequential(
|
| 34 |
+
nn.Linear(input_dim, 128),
|
| 35 |
+
nn.BatchNorm1d(128),
|
| 36 |
+
nn.ReLU(),
|
| 37 |
+
nn.Dropout(0.3),
|
| 38 |
+
|
| 39 |
+
nn.Linear(128, 64),
|
| 40 |
+
nn.BatchNorm1d(64),
|
| 41 |
+
nn.ReLU(),
|
| 42 |
+
nn.Dropout(0.3),
|
| 43 |
+
|
| 44 |
+
nn.Linear(64, 1)
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
def forward(self, x):
|
| 48 |
+
return self.net(x)
|
| 49 |
+
|
| 50 |
+
# -----------------------------
|
| 51 |
+
# Load model & scaler
|
| 52 |
+
# -----------------------------
|
| 53 |
+
scaler = joblib.load("scaler.pkl")
|
| 54 |
+
INPUT_DIM = scaler.mean_.shape[0]
|
| 55 |
+
|
| 56 |
+
model = SuspiciousModel(INPUT_DIM)
|
| 57 |
+
model.load_state_dict(torch.load("model.pth", map_location="cpu"))
|
| 58 |
+
model.eval()
|
| 59 |
+
|
| 60 |
+
# -----------------------------
|
| 61 |
+
# Sidebar – Log Input
|
| 62 |
+
# -----------------------------
|
| 63 |
+
st.sidebar.header("🔍 Log Feature Input")
|
| 64 |
+
|
| 65 |
+
features = []
|
| 66 |
+
for i in range(INPUT_DIM):
|
| 67 |
+
val = st.sidebar.number_input(
|
| 68 |
+
f"Feature {i+1}",
|
| 69 |
+
value=0.0,
|
| 70 |
+
step=0.01
|
| 71 |
+
)
|
| 72 |
+
features.append(val)
|
| 73 |
+
|
| 74 |
+
# -----------------------------
|
| 75 |
+
# Prediction
|
| 76 |
+
# -----------------------------
|
| 77 |
+
if st.sidebar.button("🚨 Analyze Event"):
|
| 78 |
+
x = np.array(features).reshape(1, -1)
|
| 79 |
+
x_scaled = scaler.transform(x)
|
| 80 |
+
x_tensor = torch.tensor(x_scaled, dtype=torch.float32)
|
| 81 |
+
|
| 82 |
+
with torch.no_grad():
|
| 83 |
+
prob = torch.sigmoid(model(x_tensor)).item()
|
| 84 |
+
|
| 85 |
+
# -------------------------
|
| 86 |
+
# Risk level logic
|
| 87 |
+
# -------------------------
|
| 88 |
+
if prob > 0.7:
|
| 89 |
+
risk = "HIGH"
|
| 90 |
+
color = "red"
|
| 91 |
+
action = "Immediate investigation required. Isolate affected system."
|
| 92 |
+
elif prob > 0.4:
|
| 93 |
+
risk = "MEDIUM"
|
| 94 |
+
color = "orange"
|
| 95 |
+
action = "Monitor closely. Correlate with other logs."
|
| 96 |
+
else:
|
| 97 |
+
risk = "LOW"
|
| 98 |
+
color = "green"
|
| 99 |
+
action = "No action required. Log for auditing."
|
| 100 |
+
|
| 101 |
+
# -------------------------
|
| 102 |
+
# Main dashboard layout
|
| 103 |
+
# -------------------------
|
| 104 |
+
col1, col2, col3 = st.columns(3)
|
| 105 |
+
|
| 106 |
+
col1.metric("Risk Level", risk)
|
| 107 |
+
col2.metric("Suspicion Probability", f"{prob:.2f}")
|
| 108 |
+
col3.metric("Recommended Action", action)
|
| 109 |
+
|
| 110 |
+
# -------------------------
|
| 111 |
+
# Gauge chart (VERY impressive)
|
| 112 |
+
# -------------------------
|
| 113 |
+
fig = go.Figure(go.Indicator(
|
| 114 |
+
mode="gauge+number",
|
| 115 |
+
value=prob * 100,
|
| 116 |
+
title={'text': "Threat Confidence (%)"},
|
| 117 |
+
gauge={
|
| 118 |
+
'axis': {'range': [0, 100]},
|
| 119 |
+
'bar': {'color': color},
|
| 120 |
+
'steps': [
|
| 121 |
+
{'range': [0, 40], 'color': "lightgreen"},
|
| 122 |
+
{'range': [40, 70], 'color': "orange"},
|
| 123 |
+
{'range': [70, 100], 'color': "red"}
|
| 124 |
+
],
|
| 125 |
+
}
|
| 126 |
+
))
|
| 127 |
+
|
| 128 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 129 |
+
|
| 130 |
+
# -------------------------
|
| 131 |
+
# Explanation block
|
| 132 |
+
# -------------------------
|
| 133 |
+
st.markdown("### 🧠 Model Interpretation")
|
| 134 |
+
st.info(
|
| 135 |
+
"The model analyzes behavioral patterns in system logs and assigns a risk score "
|
| 136 |
+
"based on learned representations of malicious activity from the BETH dataset."
|
| 137 |
+
)
|
| 138 |
+
|