sheltonmaharesh commited on
Commit
1a5ec58
·
verified ·
1 Parent(s): 8457df4

Deploy backend Flask app

Browse files
Files changed (1) hide show
  1. bot_detector_api.py +16 -4
bot_detector_api.py CHANGED
@@ -115,15 +115,27 @@ def predict():
115
  try:
116
  row = request.get_json()
117
  X = prepare_features(row)
 
 
118
  probs = model.predict_proba(X)[0]
119
- pred_label = model.classes_[int(np.argmax(probs))]
120
 
 
121
  shap_values = explainer.shap_values(X)
122
- explanation = generate_shap_explanation(0, shap_values, X)
 
 
 
 
 
 
 
123
 
124
  return jsonify({
125
- "Prediction": pred_label,
126
- "SHAP Explanation": explanation.strip()
 
 
127
  })
128
 
129
  except Exception as e:
 
115
  try:
116
  row = request.get_json()
117
  X = prepare_features(row)
118
+
119
+ # Get prediction probability and predicted class
120
  probs = model.predict_proba(X)[0]
121
+ pred_label = int(np.argmax(probs)) # 0 or 1
122
 
123
+ # Generate SHAP values
124
  shap_values = explainer.shap_values(X)
125
+
126
+ # Generate explanation for class 1
127
+ explanation, base_val, final_log_odds = generate_shap_explanation(
128
+ index=0,
129
+ shap_values=shap_values,
130
+ X=X,
131
+ class_index=1 # anomaly class
132
+ )
133
 
134
  return jsonify({
135
+ "Prediction": "Bot Attack" if pred_label == 1 else "Legitimate",
136
+ "SHAP Base Value": round(float(base_val), 4),
137
+ "SHAP Predicted Value": round(float(final_log_odds), 4),
138
+ "SHAP Explanation": explanation
139
  })
140
 
141
  except Exception as e: