Dun3Co commited on
Commit
259fc98
·
verified ·
1 Parent(s): 3fcc1a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -153,10 +153,20 @@ def explain(batch: Optional[BatchInputData] = None, limit: int = 100):
153
  # =====================================================
154
 
155
  @app.post("/metrics")
156
- def metrics(batch: Optional[BatchInputData] = None, y_true: Optional[List[int]] = None, limit: int = 100):
157
- """Compute ROC AUC and threshold analysis, using input or NoCoDB test data."""
 
 
 
 
 
 
 
 
 
 
158
  try:
159
- # Use provided data or fallback to test data from NoCoDB
160
  if batch:
161
  X = pd.DataFrame([item.dict() for item in batch.data])
162
  source = "client batch"
@@ -166,15 +176,13 @@ def metrics(batch: Optional[BatchInputData] = None, y_true: Optional[List[int]]
166
 
167
  print(f"[DEBUG] Metrics called using {source} | shape={X.shape}")
168
 
169
- # Identify and separate target variable
170
- possible_targets = ["y", "target", "label"]
171
- target_col = next((c for c in possible_targets if c in X.columns), None)
172
 
173
- if target_col is not None:
174
- y_true = X[target_col].astype(int).tolist()
175
- X = X.drop(columns=[target_col])
176
- elif y_true is None:
177
- return {"error": "No target (y_true) provided or found in dataset."}
178
 
179
  # Drop ID if exists
180
  if "Id" in X.columns:
@@ -183,6 +191,7 @@ def metrics(batch: Optional[BatchInputData] = None, y_true: Optional[List[int]]
183
  # Predict probabilities
184
  y_prob = model.predict_proba(X)[:, 1]
185
 
 
186
  roc_auc = roc_auc_score(y_true, y_prob)
187
  precision, recall, thresholds = precision_recall_curve(y_true, y_prob)
188
  pr_auc = auc(recall, precision)
@@ -203,6 +212,7 @@ def metrics(batch: Optional[BatchInputData] = None, y_true: Optional[List[int]]
203
  print(traceback.format_exc())
204
  return {"error": str(e), "trace": traceback.format_exc()}
205
 
 
206
 
207
  @app.get("/coefficients")
208
  def coefficients():
 
153
  # =====================================================
154
 
155
  @app.post("/metrics")
156
+ def metrics(batch: Optional[BatchInputData] = None, limit: int = 100):
157
+ """
158
+ Compute ROC AUC and threshold analysis using input or NoCoDB test data.
159
+ Assumes the target column 'y' is boolean (True/False).
160
+ """
161
+ # Defaults in case something fails
162
+ roc_auc = None
163
+ pr_auc = None
164
+ thresholds = []
165
+ precision = []
166
+ recall = []
167
+
168
  try:
169
+ # Fetch data from batch or NoCoDB
170
  if batch:
171
  X = pd.DataFrame([item.dict() for item in batch.data])
172
  source = "client batch"
 
176
 
177
  print(f"[DEBUG] Metrics called using {source} | shape={X.shape}")
178
 
179
+ # Ensure target 'y' exists
180
+ if "y" not in X.columns:
181
+ return {"error": "No target column 'y' found in dataset."}
182
 
183
+ # Convert boolean target to integer
184
+ y_true = X["y"].astype(int).tolist()
185
+ X = X.drop(columns=["y"])
 
 
186
 
187
  # Drop ID if exists
188
  if "Id" in X.columns:
 
191
  # Predict probabilities
192
  y_prob = model.predict_proba(X)[:, 1]
193
 
194
+ # Compute metrics
195
  roc_auc = roc_auc_score(y_true, y_prob)
196
  precision, recall, thresholds = precision_recall_curve(y_true, y_prob)
197
  pr_auc = auc(recall, precision)
 
212
  print(traceback.format_exc())
213
  return {"error": str(e), "trace": traceback.format_exc()}
214
 
215
+
216
 
217
  @app.get("/coefficients")
218
  def coefficients():