Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,7 @@ def predict_subjects(df_raw):
|
|
| 35 |
if c in df_raw.columns:
|
| 36 |
df_raw = df_raw.drop(columns=[c])
|
| 37 |
|
| 38 |
-
# Re
|
| 39 |
feature_cols = preprocessor.transformers_[0][2]
|
| 40 |
df_features = df_raw[feature_cols]
|
| 41 |
|
|
@@ -52,54 +52,65 @@ def predict_subjects(df_raw):
|
|
| 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
|
| 61 |
-
|
| 62 |
"""
|
| 63 |
)
|
| 64 |
|
| 65 |
# Load features list for display and parsing
|
| 66 |
-
preprocessor
|
| 67 |
-
feature_cols
|
| 68 |
|
| 69 |
-
st.write("**
|
| 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.
|
| 75 |
height=120
|
| 76 |
)
|
| 77 |
|
| 78 |
if st.button("Predict"):
|
| 79 |
if not input_text.strip():
|
| 80 |
-
st.warning("Please paste one row of
|
| 81 |
return
|
| 82 |
|
| 83 |
-
# Attempt to parse
|
| 84 |
try:
|
|
|
|
|
|
|
|
|
|
| 85 |
df_input = pd.read_csv(
|
| 86 |
-
StringIO(input_text.strip()),
|
| 87 |
-
header=None,
|
| 88 |
-
|
| 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
|
| 97 |
return
|
| 98 |
|
| 99 |
-
st.write("### Parsed
|
| 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")
|
|
|
|
| 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 |
|
|
|
|
| 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 feature values (tab or commaβseparated, no header).
|
| 61 |
+
The system will ignore `subject`, `sessionIndex`, or `rep` if present.
|
| 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("**Expected 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. from Excel or CSV, including subject if present):",
|
| 75 |
height=120
|
| 76 |
)
|
| 77 |
|
| 78 |
if st.button("Predict"):
|
| 79 |
if not input_text.strip():
|
| 80 |
+
st.warning("Please paste one row of values.")
|
| 81 |
return
|
| 82 |
|
|
|
|
| 83 |
try:
|
| 84 |
+
# Try tab-delimited first, fallback to comma
|
| 85 |
+
delimiter = '\t' if '\t' in input_text else ','
|
| 86 |
+
|
| 87 |
df_input = pd.read_csv(
|
| 88 |
+
StringIO(input_text.strip()),
|
| 89 |
+
header=None,
|
| 90 |
+
sep=delimiter
|
| 91 |
)
|
| 92 |
+
|
| 93 |
+
# Remove leading columns until number matches feature_cols
|
| 94 |
+
while df_input.shape[1] > len(feature_cols):
|
| 95 |
+
df_input = df_input.drop(columns=[df_input.columns[0]])
|
| 96 |
+
|
| 97 |
+
if df_input.shape[1] != len(feature_cols):
|
| 98 |
+
st.error(f"Expected {len(feature_cols)} features, got {df_input.shape[1]}.")
|
| 99 |
+
return
|
| 100 |
+
|
| 101 |
+
df_input.columns = feature_cols
|
| 102 |
+
|
| 103 |
except Exception as e:
|
| 104 |
st.error(f"Could not parse input: {e}")
|
| 105 |
return
|
| 106 |
|
|
|
|
| 107 |
if df_input.shape[0] != 1:
|
| 108 |
+
st.error(f"Expected exactly 1 row, but got {df_input.shape[0]}.")
|
| 109 |
return
|
| 110 |
|
| 111 |
+
st.write("### Parsed Input")
|
| 112 |
st.dataframe(df_input, use_container_width=True)
|
| 113 |
|
|
|
|
| 114 |
try:
|
| 115 |
df_pred = predict_subjects(df_input)
|
| 116 |
st.write("### Prediction")
|