Update app.py
Browse files
app.py
CHANGED
|
@@ -1,175 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
print("
|
| 7 |
-
|
| 8 |
-
# Load your trained model
|
| 9 |
-
try:
|
| 10 |
-
model = tf.keras.models.load_model('improved_intrusion_detection_model.h5')
|
| 11 |
-
print("β
Model loaded successfully!")
|
| 12 |
-
except Exception as e:
|
| 13 |
-
print(f"β Model loading failed: {e}")
|
| 14 |
-
# Create a dummy model for testing
|
| 15 |
-
print("β οΈ Using dummy model for testing")
|
| 16 |
|
| 17 |
def predict_intrusion(features_input):
|
| 18 |
-
"""
|
| 19 |
-
Predict if network traffic is normal or attack
|
| 20 |
-
"""
|
| 21 |
try:
|
| 22 |
-
# Convert input to array
|
| 23 |
features = [float(x.strip()) for x in features_input.split(',') if x.strip()]
|
| 24 |
|
| 25 |
-
# Validate input length
|
| 26 |
if len(features) != 119:
|
| 27 |
-
return {
|
| 28 |
-
"prediction": "ERROR",
|
| 29 |
-
"confidence": 0,
|
| 30 |
-
"message": f"β Need exactly 119 features, but got {len(features)}"
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
# Reshape for CNN model (1, 119, 1)
|
| 34 |
-
features_array = np.array(features).reshape(1, 119, 1)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
confidence = float(prediction_prob)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"
|
| 44 |
-
"
|
| 45 |
-
"message": f"π¨ SECURITY ALERT! Potential intrusion detected with {confidence:.2%} confidence"
|
| 46 |
}
|
| 47 |
else:
|
| 48 |
-
|
| 49 |
-
"
|
| 50 |
-
"
|
| 51 |
-
"
|
| 52 |
}
|
| 53 |
-
|
| 54 |
-
return result
|
| 55 |
-
|
| 56 |
except Exception as e:
|
| 57 |
-
return {
|
| 58 |
-
"prediction": "ERROR",
|
| 59 |
-
"confidence": 0,
|
| 60 |
-
"message": f"β Error: {str(e)}"
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
def generate_sample_features():
|
| 64 |
-
"""Generate sample feature values for testing"""
|
| 65 |
-
# Normal traffic sample (mostly zeros and low values)
|
| 66 |
-
normal_features = [0.0] * 50 + [0.1, 0.2, 0.05, 0.0, 0.15] + [0.0] * 64
|
| 67 |
-
# Attack traffic sample (higher values)
|
| 68 |
-
attack_features = [0.8, 0.9, 0.7, 0.6, 0.85] + [0.0] * 50 + [0.9, 0.8, 0.95] + [0.0] * 61
|
| 69 |
-
|
| 70 |
-
return {
|
| 71 |
-
"normal": ", ".join(map(str, normal_features)),
|
| 72 |
-
"attack": ", ".join(map(str, attack_features))
|
| 73 |
-
}
|
| 74 |
-
|
| 75 |
-
# Generate samples
|
| 76 |
-
samples = generate_sample_features()
|
| 77 |
|
| 78 |
-
# Create
|
| 79 |
-
with gr.Blocks(
|
| 80 |
-
gr.Markdown(
|
| 81 |
-
|
| 82 |
-
# π Network Intrusion Detection System
|
| 83 |
-
**AI-Powered Threat Detection with 99.28% Accuracy**
|
| 84 |
-
"""
|
| 85 |
-
)
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
with gr.Column():
|
| 89 |
-
gr.
|
| 90 |
-
|
| 91 |
-
features_input = gr.Textbox(
|
| 92 |
-
label="Enter 119 Network Features (comma-separated)",
|
| 93 |
-
placeholder="0.0, 0.1, 0.2, 0.0, 0.15, ... (119 values total)",
|
| 94 |
-
lines=5
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
with gr.Row():
|
| 98 |
-
analyze_btn = gr.Button("π Analyze Traffic", variant="primary")
|
| 99 |
-
clear_btn = gr.Button("ποΈ Clear")
|
| 100 |
-
|
| 101 |
-
gr.Markdown("### π§ͺ Sample Data")
|
| 102 |
-
with gr.Row():
|
| 103 |
-
normal_btn = gr.Button("π Load Normal Sample")
|
| 104 |
-
attack_btn = gr.Button("β οΈ Load Attack Sample")
|
| 105 |
|
| 106 |
with gr.Column():
|
| 107 |
-
gr.
|
| 108 |
-
|
| 109 |
-
prediction_output = gr.Label(
|
| 110 |
-
label="Detection Result",
|
| 111 |
-
value={"Prediction": "Waiting for input...", "Confidence": "0%"}
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
message_output = gr.Textbox(
|
| 115 |
-
label="Security Alert",
|
| 116 |
-
interactive=False,
|
| 117 |
-
lines=3
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
gr.Markdown("### π Model Information")
|
| 121 |
-
gr.Markdown("""
|
| 122 |
-
- **Model Type**: CNN Deep Learning
|
| 123 |
-
- **Accuracy**: 99.28%
|
| 124 |
-
- **Attack Detection**: 99.40%
|
| 125 |
-
- **False Positive Rate**: 0.90%
|
| 126 |
-
- **Input Features**: 119
|
| 127 |
-
""")
|
| 128 |
-
|
| 129 |
-
# Button actions
|
| 130 |
-
def load_normal_sample():
|
| 131 |
-
return samples["normal"]
|
| 132 |
-
|
| 133 |
-
def load_attack_sample():
|
| 134 |
-
return samples["attack"]
|
| 135 |
|
| 136 |
-
|
| 137 |
-
return "", {"Prediction": "Waiting for input...", "Confidence": "0%"}, ""
|
| 138 |
-
|
| 139 |
-
# Event handlers
|
| 140 |
-
analyze_btn.click(
|
| 141 |
-
fn=predict_intrusion,
|
| 142 |
-
inputs=features_input,
|
| 143 |
-
outputs=[prediction_output, message_output]
|
| 144 |
-
)
|
| 145 |
-
|
| 146 |
-
normal_btn.click(
|
| 147 |
-
fn=load_normal_sample,
|
| 148 |
-
outputs=features_input
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
-
attack_btn.click(
|
| 152 |
-
fn=load_attack_sample,
|
| 153 |
-
outputs=features_input
|
| 154 |
-
)
|
| 155 |
-
|
| 156 |
-
clear_btn.click(
|
| 157 |
-
fn=clear_inputs,
|
| 158 |
-
outputs=[features_input, prediction_output, message_output]
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
gr.Markdown(
|
| 162 |
-
"""
|
| 163 |
-
---
|
| 164 |
-
**How to use:**
|
| 165 |
-
1. Enter 119 comma-separated numerical values
|
| 166 |
-
2. Click "Analyze Traffic" to get prediction
|
| 167 |
-
3. Use sample buttons to test with pre-loaded data
|
| 168 |
-
|
| 169 |
-
**Note**: This is a demo interface. For production use, ensure proper model deployment.
|
| 170 |
-
"""
|
| 171 |
-
)
|
| 172 |
|
| 173 |
-
|
| 174 |
-
if __name__ == "__main__":
|
| 175 |
-
demo.launch(debug=True)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
|
| 4 |
+
print("β
App starting without TensorFlow...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def predict_intrusion(features_input):
|
|
|
|
|
|
|
|
|
|
| 7 |
try:
|
|
|
|
| 8 |
features = [float(x.strip()) for x in features_input.split(',') if x.strip()]
|
| 9 |
|
|
|
|
| 10 |
if len(features) != 119:
|
| 11 |
+
return {"Prediction": "ERROR", "Confidence": "0%", "Message": f"Need 119 features, got {len(features)}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Simple simulation (remove when TensorFlow works)
|
| 14 |
+
avg_value = sum(features) / len(features)
|
|
|
|
| 15 |
|
| 16 |
+
if avg_value > 0.4:
|
| 17 |
+
return {
|
| 18 |
+
"Prediction": "π¨ ATTACK DETECTED",
|
| 19 |
+
"Confidence": "85%",
|
| 20 |
+
"Message": "Potential threat detected (SIMULATION MODE)"
|
|
|
|
| 21 |
}
|
| 22 |
else:
|
| 23 |
+
return {
|
| 24 |
+
"Prediction": "β
NORMAL TRAFFIC",
|
| 25 |
+
"Confidence": "92%",
|
| 26 |
+
"Message": "Traffic appears normal (SIMULATION MODE)"
|
| 27 |
}
|
| 28 |
+
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
+
return {"Prediction": "ERROR", "Confidence": "0%", "Message": f"Error: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Create interface
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("# π Network Intrusion Detection System")
|
| 35 |
+
gr.Markdown("**Simulation Mode - TensorFlow installing...**")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
with gr.Row():
|
| 38 |
with gr.Column():
|
| 39 |
+
features = gr.Textbox(label="Enter 119 features (comma-separated)", lines=5)
|
| 40 |
+
btn = gr.Button("Analyze Traffic")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
with gr.Column():
|
| 43 |
+
result = gr.Label(label="Prediction Result")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
btn.click(fn=predict_intrusion, inputs=features, outputs=result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
demo.launch()
|
|
|
|
|
|