Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import pandas as pd
|
|
| 9 |
import numpy as np
|
| 10 |
import joblib
|
| 11 |
import tensorflow as tf
|
|
|
|
| 12 |
|
| 13 |
# βββ Caching loaders so they only run once per session βββββββββββββββββββββββ
|
| 14 |
@st.cache_resource
|
|
@@ -34,7 +35,7 @@ def predict_subjects(df_raw):
|
|
| 34 |
if c in df_raw.columns:
|
| 35 |
df_raw = df_raw.drop(columns=[c])
|
| 36 |
|
| 37 |
-
# Re
|
| 38 |
feature_cols = preprocessor.transformers_[0][2]
|
| 39 |
df_features = df_raw[feature_cols]
|
| 40 |
|
|
@@ -48,36 +49,57 @@ def predict_subjects(df_raw):
|
|
| 48 |
df_out = pd.DataFrame({"predicted_subject": labels})
|
| 49 |
for i, cls in enumerate(label_encoder.categories_[0]):
|
| 50 |
df_out[f"prob_{cls}"] = y_prob[:, i]
|
|
|
|
| 51 |
return df_out
|
| 52 |
|
| 53 |
# βββ Streamlit App ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 54 |
def main():
|
| 55 |
st.title("π Keystroke Dynamics Authentication")
|
| 56 |
st.markdown(
|
| 57 |
-
"
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
-
# Load
|
| 62 |
-
preprocessor
|
| 63 |
-
feature_cols
|
| 64 |
|
| 65 |
-
st.
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
for
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
st.write("### Your input")
|
| 78 |
st.dataframe(df_input, use_container_width=True)
|
| 79 |
|
| 80 |
-
#
|
| 81 |
try:
|
| 82 |
df_pred = predict_subjects(df_input)
|
| 83 |
st.write("### Prediction")
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
import joblib
|
| 11 |
import tensorflow as tf
|
| 12 |
+
from io import StringIO
|
| 13 |
|
| 14 |
# βββ Caching loaders so they only run once per session βββββββββββββββββββββββ
|
| 15 |
@st.cache_resource
|
|
|
|
| 35 |
if c in df_raw.columns:
|
| 36 |
df_raw = df_raw.drop(columns=[c])
|
| 37 |
|
| 38 |
+
# Reβorder to exact feature list
|
| 39 |
feature_cols = preprocessor.transformers_[0][2]
|
| 40 |
df_features = df_raw[feature_cols]
|
| 41 |
|
|
|
|
| 49 |
df_out = pd.DataFrame({"predicted_subject": labels})
|
| 50 |
for i, cls in enumerate(label_encoder.categories_[0]):
|
| 51 |
df_out[f"prob_{cls}"] = y_prob[:, i]
|
| 52 |
+
|
| 53 |
return df_out
|
| 54 |
|
| 55 |
# βββ Streamlit App ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 56 |
def main():
|
| 57 |
st.title("π Keystroke Dynamics Authentication")
|
| 58 |
st.markdown(
|
| 59 |
+
"""
|
| 60 |
+
Paste exactly **one** row of commaβseparated feature values (no header)
|
| 61 |
+
in the **order below**, then click **Predict**.
|
| 62 |
+
"""
|
| 63 |
)
|
| 64 |
|
| 65 |
+
# Load features list for display and parsing
|
| 66 |
+
preprocessor = load_preprocessor()
|
| 67 |
+
feature_cols = preprocessor.transformers_[0][2]
|
| 68 |
|
| 69 |
+
st.write("**Feature order:**")
|
| 70 |
+
st.code(", ".join(feature_cols), language="text")
|
| 71 |
+
|
| 72 |
+
# Textarea for single-row input
|
| 73 |
+
input_text = st.text_area(
|
| 74 |
+
"Paste your row here (e.g. `0.1491,0.3979,0.2488,...`)",
|
| 75 |
+
height=120
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
if st.button("Predict"):
|
| 79 |
+
if not input_text.strip():
|
| 80 |
+
st.warning("Please paste one row of commaβseparated values.")
|
| 81 |
+
return
|
| 82 |
+
|
| 83 |
+
# Attempt to parse
|
| 84 |
+
try:
|
| 85 |
+
df_input = pd.read_csv(
|
| 86 |
+
StringIO(input_text.strip()),
|
| 87 |
+
header=None,
|
| 88 |
+
names=feature_cols
|
| 89 |
+
)
|
| 90 |
+
except Exception as e:
|
| 91 |
+
st.error(f"Could not parse input: {e}")
|
| 92 |
+
return
|
| 93 |
|
| 94 |
+
# Ensure exactly one row
|
| 95 |
+
if df_input.shape[0] != 1:
|
| 96 |
+
st.error(f"Expected exactly 1 row, but parsed {df_input.shape[0]}.")
|
| 97 |
+
return
|
| 98 |
|
| 99 |
+
st.write("### Parsed input")
|
|
|
|
| 100 |
st.dataframe(df_input, use_container_width=True)
|
| 101 |
|
| 102 |
+
# Run prediction
|
| 103 |
try:
|
| 104 |
df_pred = predict_subjects(df_input)
|
| 105 |
st.write("### Prediction")
|