Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.preprocessing import LabelEncoder, StandardScaler, OneHotEncoder
|
| 6 |
+
from sklearn.compose import ColumnTransformer
|
| 7 |
+
from tensorflow.keras.models import Sequential
|
| 8 |
+
from tensorflow.keras.layers import Input, Dense
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
|
| 11 |
+
@st.cache_data
|
| 12 |
+
def load_data():
|
| 13 |
+
return pd.read_csv("Global_Cybersecurity_Threats_2015-2024.csv")
|
| 14 |
+
|
| 15 |
+
df = load_data()
|
| 16 |
+
st.title("Cybersecurity Attack Type - ANN Summary & Metrics")
|
| 17 |
+
|
| 18 |
+
target = 'Attack Type'
|
| 19 |
+
|
| 20 |
+
cat_features = [
|
| 21 |
+
'Country',
|
| 22 |
+
'Target Industry',
|
| 23 |
+
'Attack Source',
|
| 24 |
+
'Security Vulnerability Type',
|
| 25 |
+
'Defense Mechanism Used'
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
num_features = [
|
| 29 |
+
'Year',
|
| 30 |
+
'Financial Loss (in Million $)',
|
| 31 |
+
'Number of Affected Users',
|
| 32 |
+
'Incident Resolution Time (in Hours)'
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
X = df.drop(columns=[target])
|
| 36 |
+
y = df[target]
|
| 37 |
+
|
| 38 |
+
preprocessor = ColumnTransformer(
|
| 39 |
+
transformers=[
|
| 40 |
+
('num', StandardScaler(), num_features),
|
| 41 |
+
('cat', OneHotEncoder(handle_unknown='ignore'), cat_features)
|
| 42 |
+
]
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=27)
|
| 46 |
+
X_train = preprocessor.fit_transform(X_train)
|
| 47 |
+
X_test = preprocessor.transform(X_test)
|
| 48 |
+
|
| 49 |
+
le = LabelEncoder()
|
| 50 |
+
y_train = le.fit_transform(y_train)
|
| 51 |
+
y_test = le.transform(y_test)
|
| 52 |
+
|
| 53 |
+
st.sidebar.header("Model Parameters")
|
| 54 |
+
epochs = st.sidebar.slider("Epochs", 5, 100, 30)
|
| 55 |
+
batch_size = st.sidebar.selectbox("Batch Size", [8, 16, 32, 64], index=1)
|
| 56 |
+
|
| 57 |
+
model = Sequential()
|
| 58 |
+
model.add(Input(shape=(X_train.shape[1],)))
|
| 59 |
+
model.add(Dense(16, activation='relu'))
|
| 60 |
+
model.add(Dense(32, activation='relu'))
|
| 61 |
+
model.add(Dense(len(np.unique(y)), activation='softmax'))
|
| 62 |
+
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
|
| 63 |
+
|
| 64 |
+
if st.button("Train Model"):
|
| 65 |
+
history = model.fit(X_train, y_train, validation_split=0.2, epochs=epochs, batch_size=batch_size, verbose=0)
|
| 66 |
+
st.success("Model training complete.")
|
| 67 |
+
|
| 68 |
+
st.subheader("Model Summary")
|
| 69 |
+
model_summary = []
|
| 70 |
+
model.summary(print_fn=lambda x: model_summary.append(x))
|
| 71 |
+
st.text("\\n".join(model_summary))
|
| 72 |
+
|
| 73 |
+
st.subheader("Training and Validation Metrics")
|
| 74 |
+
fig, ax = plt.subplots(2, 1, figsize=(8, 6))
|
| 75 |
+
ax[0].plot(history.history['loss'], label='Loss')
|
| 76 |
+
ax[0].plot(history.history['val_loss'], label='Val Loss')
|
| 77 |
+
ax[0].legend()
|
| 78 |
+
ax[0].set_title("Loss vs Val Loss")
|
| 79 |
+
|
| 80 |
+
ax[1].plot(history.history['accuracy'], label='Accuracy')
|
| 81 |
+
ax[1].plot(history.history['val_accuracy'], label='Val Accuracy')
|
| 82 |
+
ax[1].legend()
|
| 83 |
+
ax[1].set_title("Accuracy vs Val Accuracy")
|
| 84 |
+
|
| 85 |
+
st.pyplot(fig)
|
| 86 |
+
|
| 87 |
+
min_val_loss = min(history.history['val_loss'])
|
| 88 |
+
best_val_acc = max(history.history['val_accuracy'])
|
| 89 |
+
|
| 90 |
+
st.write(f"**Minimum Validation Loss:** {min_val_loss:.4f}")
|
| 91 |
+
st.write(f"**Best Validation Accuracy:** {best_val_acc:.4f}")
|