Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,147 +2,136 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
|
| 5 |
-
# =========================
|
| 6 |
-
# DATABASE (temporary)
|
| 7 |
-
# =========================
|
| 8 |
users = {}
|
| 9 |
-
|
| 10 |
-
# Store OTP temporarily
|
| 11 |
otp_store = {}
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# =========================
|
| 14 |
# SIGNUP
|
| 15 |
# =========================
|
| 16 |
def signup(username, password):
|
| 17 |
if username in users:
|
| 18 |
-
return "β User already exists"
|
| 19 |
-
|
| 20 |
users[username] = password
|
| 21 |
-
return "β
Signup successful! Please login."
|
| 22 |
-
|
| 23 |
|
| 24 |
# =========================
|
| 25 |
-
# LOGIN
|
| 26 |
# =========================
|
| 27 |
def login(username, password):
|
| 28 |
if username in users and users[username] == password:
|
| 29 |
otp = random.randint(1000, 9999)
|
| 30 |
otp_store[username] = otp
|
| 31 |
-
return f"
|
| 32 |
-
|
| 33 |
-
return "β Invalid credentials"
|
| 34 |
-
|
| 35 |
|
| 36 |
# =========================
|
| 37 |
-
# OTP VERIFY
|
| 38 |
# =========================
|
| 39 |
def verify_otp(username, otp_input):
|
| 40 |
if username in otp_store and str(otp_store[username]) == str(otp_input):
|
| 41 |
-
return "β
Login Successful", True
|
| 42 |
-
|
| 43 |
-
return "β Invalid OTP", False
|
| 44 |
-
|
| 45 |
|
| 46 |
# =========================
|
| 47 |
-
# PREDICT
|
| 48 |
# =========================
|
| 49 |
-
def predict(logged_in,
|
| 50 |
-
count, error_rate, same_srv_rate, diff_srv_rate,
|
| 51 |
-
host_count, login_attempts):
|
| 52 |
-
|
| 53 |
if not logged_in:
|
| 54 |
return "π« Please login first"
|
| 55 |
|
| 56 |
-
data = np.array(
|
| 57 |
-
duration, src_bytes, dst_bytes, failed_logins,
|
| 58 |
-
count, error_rate, same_srv_rate, diff_srv_rate,
|
| 59 |
-
host_count, login_attempts
|
| 60 |
-
])
|
| 61 |
-
|
| 62 |
score = np.sum(data)
|
| 63 |
|
| 64 |
if score < 30:
|
| 65 |
-
|
| 66 |
-
explanation = "Low activity detected."
|
| 67 |
elif score < 60:
|
| 68 |
-
|
| 69 |
-
explanation = "Moderate anomaly detected."
|
| 70 |
else:
|
| 71 |
-
|
| 72 |
-
explanation = "High anomaly detected."
|
| 73 |
-
|
| 74 |
-
return f"""
|
| 75 |
-
{status}
|
| 76 |
-
|
| 77 |
-
Risk Score: {score:.2f}
|
| 78 |
-
|
| 79 |
-
0β30 β Normal
|
| 80 |
-
30β60 β Suspicious
|
| 81 |
-
60+ β Attack
|
| 82 |
-
|
| 83 |
-
Analysis: {explanation}
|
| 84 |
-
"""
|
| 85 |
-
|
| 86 |
|
| 87 |
# =========================
|
| 88 |
# UI
|
| 89 |
# =========================
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
|
| 92 |
-
gr.
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
gr.
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
#
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
demo.launch()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
users = {}
|
|
|
|
|
|
|
| 6 |
otp_store = {}
|
| 7 |
|
| 8 |
+
# =========================
|
| 9 |
+
# STATE NAVIGATION
|
| 10 |
+
# =========================
|
| 11 |
+
def go_to(page):
|
| 12 |
+
return page
|
| 13 |
+
|
| 14 |
# =========================
|
| 15 |
# SIGNUP
|
| 16 |
# =========================
|
| 17 |
def signup(username, password):
|
| 18 |
if username in users:
|
| 19 |
+
return "β User already exists", "signup"
|
|
|
|
| 20 |
users[username] = password
|
| 21 |
+
return "β
Signup successful! Please login.", "login"
|
|
|
|
| 22 |
|
| 23 |
# =========================
|
| 24 |
+
# LOGIN
|
| 25 |
# =========================
|
| 26 |
def login(username, password):
|
| 27 |
if username in users and users[username] == password:
|
| 28 |
otp = random.randint(1000, 9999)
|
| 29 |
otp_store[username] = otp
|
| 30 |
+
return f"OTP: {otp} (demo)", "otp", username
|
| 31 |
+
return "β Invalid credentials", "login", ""
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# =========================
|
| 34 |
+
# OTP VERIFY
|
| 35 |
# =========================
|
| 36 |
def verify_otp(username, otp_input):
|
| 37 |
if username in otp_store and str(otp_store[username]) == str(otp_input):
|
| 38 |
+
return "β
Login Successful", "dashboard", True
|
| 39 |
+
return "β Invalid OTP", "otp", False
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# =========================
|
| 42 |
+
# PREDICT
|
| 43 |
# =========================
|
| 44 |
+
def predict(logged_in, *inputs):
|
|
|
|
|
|
|
|
|
|
| 45 |
if not logged_in:
|
| 46 |
return "π« Please login first"
|
| 47 |
|
| 48 |
+
data = np.array(inputs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
score = np.sum(data)
|
| 50 |
|
| 51 |
if score < 30:
|
| 52 |
+
return f"β
Normal Traffic\n\nScore: {score:.2f}"
|
|
|
|
| 53 |
elif score < 60:
|
| 54 |
+
return f"β οΈ Suspicious Activity\n\nScore: {score:.2f}"
|
|
|
|
| 55 |
else:
|
| 56 |
+
return f"π¨ Attack Detected!\n\nScore: {score:.2f}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# =========================
|
| 59 |
# UI
|
| 60 |
# =========================
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
|
| 63 |
+
page = gr.State("home")
|
| 64 |
+
current_user = gr.State("")
|
| 65 |
+
logged_in = gr.State(False)
|
| 66 |
+
|
| 67 |
+
# ================= HOME =================
|
| 68 |
+
with gr.Column(visible=True) as home_page:
|
| 69 |
+
gr.Markdown("# π Secure IDS System")
|
| 70 |
+
login_btn = gr.Button("Login")
|
| 71 |
+
signup_btn = gr.Button("Signup")
|
| 72 |
+
|
| 73 |
+
# ================= SIGNUP =================
|
| 74 |
+
with gr.Column(visible=False) as signup_page:
|
| 75 |
+
gr.Markdown("## Signup")
|
| 76 |
+
su_user = gr.Textbox(label="Username")
|
| 77 |
+
su_pass = gr.Textbox(label="Password", type="password")
|
| 78 |
+
su_btn = gr.Button("Create Account")
|
| 79 |
+
su_out = gr.Textbox()
|
| 80 |
+
|
| 81 |
+
# ================= LOGIN =================
|
| 82 |
+
with gr.Column(visible=False) as login_page:
|
| 83 |
+
gr.Markdown("## Login")
|
| 84 |
+
li_user = gr.Textbox(label="Username")
|
| 85 |
+
li_pass = gr.Textbox(label="Password", type="password")
|
| 86 |
+
li_btn = gr.Button("Login")
|
| 87 |
+
li_out = gr.Textbox()
|
| 88 |
+
|
| 89 |
+
# ================= OTP =================
|
| 90 |
+
with gr.Column(visible=False) as otp_page:
|
| 91 |
+
gr.Markdown("## Enter OTP")
|
| 92 |
+
otp_input = gr.Textbox()
|
| 93 |
+
otp_btn = gr.Button("Verify")
|
| 94 |
+
otp_out = gr.Textbox()
|
| 95 |
+
|
| 96 |
+
# ================= DASHBOARD =================
|
| 97 |
+
with gr.Column(visible=False) as dashboard_page:
|
| 98 |
+
gr.Markdown("## IDS Dashboard")
|
| 99 |
+
|
| 100 |
+
inputs = [gr.Number(label=f"Feature {i+1}") for i in range(10)]
|
| 101 |
+
pred_btn = gr.Button("Analyze")
|
| 102 |
+
output = gr.Markdown()
|
| 103 |
+
|
| 104 |
+
# ================= NAVIGATION LOGIC =================
|
| 105 |
+
def render(page_name):
|
| 106 |
+
return (
|
| 107 |
+
gr.update(visible=page_name=="home"),
|
| 108 |
+
gr.update(visible=page_name=="signup"),
|
| 109 |
+
gr.update(visible=page_name=="login"),
|
| 110 |
+
gr.update(visible=page_name=="otp"),
|
| 111 |
+
gr.update(visible=page_name=="dashboard")
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Home buttons
|
| 115 |
+
login_btn.click(lambda: "login", None, page).then(render, page,
|
| 116 |
+
[home_page, signup_page, login_page, otp_page, dashboard_page])
|
| 117 |
+
|
| 118 |
+
signup_btn.click(lambda: "signup", None, page).then(render, page,
|
| 119 |
+
[home_page, signup_page, login_page, otp_page, dashboard_page])
|
| 120 |
+
|
| 121 |
+
# Signup action
|
| 122 |
+
su_btn.click(signup, [su_user, su_pass], [su_out, page]).then(render, page,
|
| 123 |
+
[home_page, signup_page, login_page, otp_page, dashboard_page])
|
| 124 |
+
|
| 125 |
+
# Login action
|
| 126 |
+
li_btn.click(login, [li_user, li_pass], [li_out, page, current_user]).then(render, page,
|
| 127 |
+
[home_page, signup_page, login_page, otp_page, dashboard_page])
|
| 128 |
+
|
| 129 |
+
# OTP verify
|
| 130 |
+
otp_btn.click(verify_otp, [current_user, otp_input],
|
| 131 |
+
[otp_out, page, logged_in]).then(render, page,
|
| 132 |
+
[home_page, signup_page, login_page, otp_page, dashboard_page])
|
| 133 |
+
|
| 134 |
+
# Prediction
|
| 135 |
+
pred_btn.click(predict, [logged_in] + inputs, output)
|
| 136 |
|
| 137 |
demo.launch()
|