Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,15 @@ from sklearn.metrics import precision_score, recall_score
|
|
12 |
from datetime import datetime
|
13 |
|
14 |
def generate_report(data, predictions, model_names, metrics):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
report = f"""
|
16 |
Network Anomaly Detection Report
|
17 |
Generated: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
|
@@ -24,7 +33,7 @@ def generate_report(data, predictions, model_names, metrics):
|
|
24 |
- Anomaly Percentage: {sum(predictions)/len(data):.2%}
|
25 |
|
26 |
Model Performance:
|
27 |
-
{
|
28 |
|
29 |
Conclusion:
|
30 |
The system detected {sum(predictions)} potential anomalies using ensemble of {len(model_names)} models.
|
@@ -65,12 +74,16 @@ def main():
|
|
65 |
contamination = st.sidebar.slider("Expected Anomaly Ratio", 0.01, 0.5, 0.1)
|
66 |
ensemble_method = st.sidebar.selectbox("Ensemble Method", ["Average", "MOA", "AOM"])
|
67 |
|
68 |
-
# Data
|
69 |
uploaded_file = st.file_uploader("Upload network data (CSV)", type=["csv"])
|
70 |
|
71 |
if uploaded_file:
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
74 |
else:
|
75 |
# Generate synthetic network data
|
76 |
np.random.seed(42)
|
@@ -90,6 +103,10 @@ def main():
|
|
90 |
|
91 |
# Data preprocessing
|
92 |
numeric_cols = data.select_dtypes(include=np.number).columns.tolist()
|
|
|
|
|
|
|
|
|
93 |
X = data[numeric_cols].values
|
94 |
X_norm = standardizer(X)
|
95 |
|
@@ -100,20 +117,24 @@ def main():
|
|
100 |
"One-Class SVM": OCSVM(contamination=contamination)
|
101 |
}
|
102 |
|
103 |
-
selected_models = [model_dict[m] for m in models]
|
104 |
if not selected_models:
|
105 |
st.error("Please select at least one detection model!")
|
106 |
return
|
107 |
|
108 |
-
#
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
model
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
117 |
|
118 |
# Ensemble prediction
|
119 |
try:
|
@@ -157,8 +178,8 @@ def main():
|
|
157 |
|
158 |
# Visualization
|
159 |
st.subheader("Data Visualization")
|
160 |
-
|
161 |
tab1, tab2 = st.tabs(["2D Projection", "3D Projection"])
|
|
|
162 |
with tab1:
|
163 |
fig, ax = plt.subplots(figsize=(10, 6))
|
164 |
pca = PCA(n_components=2)
|
@@ -176,12 +197,12 @@ def main():
|
|
176 |
with tab2:
|
177 |
st.pyplot(plot_3d_projections(X_norm, predictions))
|
178 |
|
179 |
-
#
|
180 |
st.subheader("Analysis Report")
|
181 |
report = generate_report(data[numeric_cols], predictions, models, metrics_df)
|
182 |
st.code(report, language='text')
|
183 |
|
184 |
-
#
|
185 |
st.download_button(
|
186 |
label="Download Full Report",
|
187 |
data=report,
|
|
|
12 |
from datetime import datetime
|
13 |
|
14 |
def generate_report(data, predictions, model_names, metrics):
|
15 |
+
# Create markdown table manually
|
16 |
+
metrics_table = "\n".join([
|
17 |
+
"| Model | Precision | Recall |",
|
18 |
+
"|-------|-----------|--------|"
|
19 |
+
] + [
|
20 |
+
f"| {row['Model']} | {row['Precision']} | {row['Recall']} |"
|
21 |
+
for _, row in metrics.iterrows()
|
22 |
+
])
|
23 |
+
|
24 |
report = f"""
|
25 |
Network Anomaly Detection Report
|
26 |
Generated: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
|
|
|
33 |
- Anomaly Percentage: {sum(predictions)/len(data):.2%}
|
34 |
|
35 |
Model Performance:
|
36 |
+
{metrics_table}
|
37 |
|
38 |
Conclusion:
|
39 |
The system detected {sum(predictions)} potential anomalies using ensemble of {len(model_names)} models.
|
|
|
74 |
contamination = st.sidebar.slider("Expected Anomaly Ratio", 0.01, 0.5, 0.1)
|
75 |
ensemble_method = st.sidebar.selectbox("Ensemble Method", ["Average", "MOA", "AOM"])
|
76 |
|
77 |
+
# Data handling
|
78 |
uploaded_file = st.file_uploader("Upload network data (CSV)", type=["csv"])
|
79 |
|
80 |
if uploaded_file:
|
81 |
+
try:
|
82 |
+
data = pd.read_csv(uploaded_file)
|
83 |
+
st.success("Uploaded data loaded successfully!")
|
84 |
+
except Exception as e:
|
85 |
+
st.error(f"Error reading file: {str(e)}")
|
86 |
+
return
|
87 |
else:
|
88 |
# Generate synthetic network data
|
89 |
np.random.seed(42)
|
|
|
103 |
|
104 |
# Data preprocessing
|
105 |
numeric_cols = data.select_dtypes(include=np.number).columns.tolist()
|
106 |
+
if not numeric_cols:
|
107 |
+
st.error("No numeric columns found for analysis!")
|
108 |
+
return
|
109 |
+
|
110 |
X = data[numeric_cols].values
|
111 |
X_norm = standardizer(X)
|
112 |
|
|
|
117 |
"One-Class SVM": OCSVM(contamination=contamination)
|
118 |
}
|
119 |
|
120 |
+
selected_models = [model_dict[m] for m in models if m in model_dict]
|
121 |
if not selected_models:
|
122 |
st.error("Please select at least one detection model!")
|
123 |
return
|
124 |
|
125 |
+
# Model training
|
126 |
+
try:
|
127 |
+
st.subheader("Model Training Progress")
|
128 |
+
progress_bar = st.progress(0)
|
129 |
+
train_scores = np.zeros([len(X), len(selected_models)])
|
130 |
+
|
131 |
+
for i, model in enumerate(selected_models):
|
132 |
+
model.fit(X_norm)
|
133 |
+
train_scores[:, i] = model.decision_function(X_norm)
|
134 |
+
progress_bar.progress((i+1)/len(selected_models))
|
135 |
+
except Exception as e:
|
136 |
+
st.error(f"Model training failed: {str(e)}")
|
137 |
+
return
|
138 |
|
139 |
# Ensemble prediction
|
140 |
try:
|
|
|
178 |
|
179 |
# Visualization
|
180 |
st.subheader("Data Visualization")
|
|
|
181 |
tab1, tab2 = st.tabs(["2D Projection", "3D Projection"])
|
182 |
+
|
183 |
with tab1:
|
184 |
fig, ax = plt.subplots(figsize=(10, 6))
|
185 |
pca = PCA(n_components=2)
|
|
|
197 |
with tab2:
|
198 |
st.pyplot(plot_3d_projections(X_norm, predictions))
|
199 |
|
200 |
+
# Report generation
|
201 |
st.subheader("Analysis Report")
|
202 |
report = generate_report(data[numeric_cols], predictions, models, metrics_df)
|
203 |
st.code(report, language='text')
|
204 |
|
205 |
+
# Download report
|
206 |
st.download_button(
|
207 |
label="Download Full Report",
|
208 |
data=report,
|