MrUtakata commited on
Commit
dc13330
Β·
verified Β·
1 Parent(s): 015c6bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -18
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-order to exact feature list
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
- "Use the sidebar to enter one row of raw keystroke features, then click **Predict**. "
58
- "The model will return the predicted subject ID plus per-class probabilities."
 
 
59
  )
60
 
61
- # Load the feature‐list so we can build inputs
62
- preprocessor = load_preprocessor()
63
- feature_cols = preprocessor.transformers_[0][2]
64
 
65
- st.sidebar.header("Enter keystroke features")
66
- user_vals = {}
67
- # one number_input per feature
68
- for col in feature_cols:
69
- # you can tweak min/max/default as appropriate
70
- user_vals[col] = st.sidebar.number_input(col, value=0.0, format="%.4f")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
- if st.sidebar.button("Predict"):
73
- # pack into single-row DataFrame
74
- df_input = pd.DataFrame([user_vals], columns=feature_cols)
 
75
 
76
- # Show what we're about to send
77
- st.write("### Your input")
78
  st.dataframe(df_input, use_container_width=True)
79
 
80
- # Do prediction
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")