Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -102,45 +102,59 @@ def predict(*feature_values):
|
|
| 102 |
proba_dict: dict(label -> probability), sorted desc, top-N shown by gr.Label
|
| 103 |
message: Markdown summary with predicted label + confidence
|
| 104 |
"""
|
| 105 |
-
# Map UI inputs to a dict matching the model's feature columns
|
| 106 |
-
input_data = {}
|
| 107 |
-
for col, val in zip(FEATURE_COLS, feature_values[:len(FEATURE_COLS)]):
|
| 108 |
-
try:
|
| 109 |
-
# Try numeric first (keeps sliders/numbers numeric)
|
| 110 |
-
input_data[col] = float(val) if val != "" else 0.0
|
| 111 |
-
except:
|
| 112 |
-
# Otherwise leave as string (for categorical columns)
|
| 113 |
-
input_data[col] = val
|
| 114 |
-
|
| 115 |
-
# Build a DataFrame row for inference
|
| 116 |
-
X = pd.DataFrame([input_data])
|
| 117 |
-
|
| 118 |
-
# Predicted label (or regression value)
|
| 119 |
-
pred = PREDICTOR.predict(X)
|
| 120 |
-
pred_value = pred.iloc[0]
|
| 121 |
-
|
| 122 |
-
# Class probabilities (if classifier). If regression, synthesize 100% on prediction.
|
| 123 |
try:
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
-
for col in proba_df.columns:
|
| 131 |
-
proba_dict[str(col)] = float(proba_df[col].iloc[0])
|
| 132 |
|
| 133 |
-
#
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
# ============================================================================
|
|
|
|
| 102 |
proba_dict: dict(label -> probability), sorted desc, top-N shown by gr.Label
|
| 103 |
message: Markdown summary with predicted label + confidence
|
| 104 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
try:
|
| 106 |
+
# Map UI inputs to a dict matching the model's feature columns
|
| 107 |
+
input_data = {}
|
| 108 |
+
for col, val in zip(FEATURE_COLS, feature_values[:len(FEATURE_COLS)]):
|
| 109 |
+
try:
|
| 110 |
+
# Try numeric first (keeps sliders/numbers numeric)
|
| 111 |
+
input_data[col] = float(val) if val != "" else 0.0
|
| 112 |
+
except:
|
| 113 |
+
# Otherwise leave as string (for categorical columns)
|
| 114 |
+
input_data[col] = val
|
| 115 |
|
| 116 |
+
print(f"Input data: {input_data}")
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
# Build a DataFrame row for inference
|
| 119 |
+
X = pd.DataFrame([input_data])
|
| 120 |
+
print(f"DataFrame shape: {X.shape}")
|
| 121 |
+
print(f"DataFrame columns: {X.columns.tolist()}")
|
| 122 |
+
|
| 123 |
+
# Predicted label (or regression value)
|
| 124 |
+
pred = PREDICTOR.predict(X)
|
| 125 |
+
pred_value = pred.iloc[0]
|
| 126 |
+
print(f"Prediction: {pred_value}")
|
| 127 |
+
|
| 128 |
+
# Class probabilities (if classifier). If regression, synthesize 100% on prediction.
|
| 129 |
+
try:
|
| 130 |
+
proba_df = PREDICTOR.predict_proba(X)
|
| 131 |
+
if isinstance(proba_df, pd.Series):
|
| 132 |
+
# Normalize to DataFrame shape if AG returns a Series
|
| 133 |
+
proba_df = proba_df.to_frame().T
|
| 134 |
+
|
| 135 |
+
proba_dict = {}
|
| 136 |
+
for col in proba_df.columns:
|
| 137 |
+
proba_dict[str(col)] = float(proba_df[col].iloc[0])
|
| 138 |
+
|
| 139 |
+
# Sort highest to lowest
|
| 140 |
+
proba_dict = dict(sorted(proba_dict.items(), key=lambda x: x[1], reverse=True))
|
| 141 |
+
except Exception as e:
|
| 142 |
+
print(f"Error getting probabilities: {e}")
|
| 143 |
+
# Regression or unsupported proba: show pseudo-confidence
|
| 144 |
+
proba_dict = {str(pred_value): 1.0}
|
| 145 |
+
|
| 146 |
+
# Human-readable summary (confidence = max probability * 100)
|
| 147 |
+
confidence = max(proba_dict.values()) * 100 if proba_dict else 100
|
| 148 |
+
message = f"**Prediction:** {pred_value}\n**Confidence:** {confidence:.2f}%"
|
| 149 |
+
|
| 150 |
+
return proba_dict, message
|
| 151 |
+
|
| 152 |
+
except Exception as e:
|
| 153 |
+
error_msg = f"**Error:** {str(e)}\n\nPlease check the logs for details."
|
| 154 |
+
print(f"Prediction error: {e}")
|
| 155 |
+
import traceback
|
| 156 |
+
traceback.print_exc()
|
| 157 |
+
return {}, error_msg
|
| 158 |
|
| 159 |
|
| 160 |
# ============================================================================
|