Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -153,39 +153,56 @@ def explain(batch: Optional[BatchInputData] = None, limit: int = 100):
|
|
| 153 |
# =====================================================
|
| 154 |
|
| 155 |
@app.post("/metrics")
|
| 156 |
-
def metrics(batch: Optional[BatchInputData] = None,
|
| 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 |
else:
|
| 163 |
X = fetch_test_data(limit=limit)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
y = X["y"].astype(int).tolist()
|
| 169 |
-
X = X.drop(columns=["y"])
|
| 170 |
-
else:
|
| 171 |
-
return {"error": "ye values not provided or found in dataset"}
|
| 172 |
|
|
|
|
| 173 |
y_prob = model.predict_proba(X)[:, 1]
|
| 174 |
-
|
| 175 |
-
|
|
|
|
| 176 |
pr_auc = auc(recall, precision)
|
| 177 |
|
|
|
|
|
|
|
| 178 |
return {
|
| 179 |
"roc_auc": roc_auc,
|
| 180 |
"pr_auc": pr_auc,
|
| 181 |
-
"thresholds": thresholds.tolist()[:20],
|
| 182 |
"precision": precision.tolist()[:20],
|
| 183 |
"recall": recall.tolist()[:20]
|
| 184 |
}
|
| 185 |
|
| 186 |
except Exception as e:
|
| 187 |
import traceback
|
|
|
|
|
|
|
| 188 |
return {"error": str(e), "trace": traceback.format_exc()}
|
|
|
|
| 189 |
|
| 190 |
@app.get("/coefficients")
|
| 191 |
def coefficients():
|
|
|
|
| 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"
|
| 163 |
else:
|
| 164 |
X = fetch_test_data(limit=limit)
|
| 165 |
+
source = f"NoCoDB (limit={limit})"
|
| 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:
|
| 181 |
+
X = X.drop(columns=["Id"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 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)
|
| 189 |
|
| 190 |
+
print(f"[DEBUG] ROC AUC={roc_auc:.3f} | PR AUC={pr_auc:.3f}")
|
| 191 |
+
|
| 192 |
return {
|
| 193 |
"roc_auc": roc_auc,
|
| 194 |
"pr_auc": pr_auc,
|
| 195 |
+
"thresholds": thresholds.tolist()[:20],
|
| 196 |
"precision": precision.tolist()[:20],
|
| 197 |
"recall": recall.tolist()[:20]
|
| 198 |
}
|
| 199 |
|
| 200 |
except Exception as e:
|
| 201 |
import traceback
|
| 202 |
+
print("[ERROR] Metrics failed:", e)
|
| 203 |
+
print(traceback.format_exc())
|
| 204 |
return {"error": str(e), "trace": traceback.format_exc()}
|
| 205 |
+
|
| 206 |
|
| 207 |
@app.get("/coefficients")
|
| 208 |
def coefficients():
|