Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app.py +20 -23
- requirements.txt +2 -2
app.py
CHANGED
|
@@ -6,41 +6,38 @@ import numpy as np
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
|
| 8 |
# -----------------------------
|
| 9 |
-
# 1.
|
| 10 |
# -----------------------------
|
| 11 |
-
# Create fake training data
|
| 12 |
np.random.seed(0)
|
| 13 |
X = pd.DataFrame({
|
| 14 |
"age": np.random.randint(20, 80, 200),
|
| 15 |
"bmi": np.random.uniform(18, 40, 200),
|
| 16 |
"bp": np.random.uniform(80, 160, 200)
|
| 17 |
})
|
| 18 |
-
y = 0.3*X["age"] + 0.5*X["bmi"] + 0.2*X["bp"] + np.random.normal(0, 3, 200)
|
| 19 |
|
| 20 |
-
model = xgb.XGBRegressor(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
model.fit(X, y)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
explainer = shap.TreeExplainer(model)
|
| 25 |
|
| 26 |
# -----------------------------
|
| 27 |
-
# 2. Prediction + SHAP
|
| 28 |
# -----------------------------
|
| 29 |
def predict_and_explain(age, bmi, bp):
|
| 30 |
|
| 31 |
-
df = pd.DataFrame([{
|
| 32 |
-
|
| 33 |
-
"bmi": bmi,
|
| 34 |
-
"bp": bp
|
| 35 |
-
}])
|
| 36 |
-
|
| 37 |
-
# Prediction
|
| 38 |
-
pred = model.predict(df)[0]
|
| 39 |
|
| 40 |
-
# SHAP values
|
| 41 |
shap_values = explainer(df)
|
| 42 |
|
| 43 |
-
# SHAP waterfall plot
|
| 44 |
plt.figure(figsize=(8, 5))
|
| 45 |
shap.plots.waterfall(shap_values[0], show=False)
|
| 46 |
plt.tight_layout()
|
|
@@ -50,26 +47,26 @@ def predict_and_explain(age, bmi, bp):
|
|
| 50 |
return pred, "shap_plot.png"
|
| 51 |
|
| 52 |
# -----------------------------
|
| 53 |
-
# 3. Gradio
|
| 54 |
# -----------------------------
|
| 55 |
inputs = [
|
| 56 |
gr.Number(label="Age"),
|
| 57 |
gr.Number(label="BMI"),
|
| 58 |
-
gr.Number(label="Blood Pressure")
|
| 59 |
]
|
| 60 |
|
| 61 |
outputs = [
|
| 62 |
gr.Number(label="Prediction"),
|
| 63 |
-
gr.Image(label="SHAP Explanation")
|
| 64 |
]
|
| 65 |
|
| 66 |
-
|
| 67 |
fn=predict_and_explain,
|
| 68 |
inputs=inputs,
|
| 69 |
outputs=outputs,
|
| 70 |
-
title="XGBoost + SHAP
|
| 71 |
-
description="
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
-
|
|
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
|
| 8 |
# -----------------------------
|
| 9 |
+
# 1. Train a simple clean model
|
| 10 |
# -----------------------------
|
|
|
|
| 11 |
np.random.seed(0)
|
| 12 |
X = pd.DataFrame({
|
| 13 |
"age": np.random.randint(20, 80, 200),
|
| 14 |
"bmi": np.random.uniform(18, 40, 200),
|
| 15 |
"bp": np.random.uniform(80, 160, 200)
|
| 16 |
})
|
| 17 |
+
y = 0.3 * X["age"] + 0.5 * X["bmi"] + 0.2 * X["bp"] + np.random.normal(0, 3, 200)
|
| 18 |
|
| 19 |
+
model = xgb.XGBRegressor(
|
| 20 |
+
n_estimators=100,
|
| 21 |
+
max_depth=3,
|
| 22 |
+
learning_rate=0.1,
|
| 23 |
+
objective="reg:squarederror"
|
| 24 |
+
)
|
| 25 |
model.fit(X, y)
|
| 26 |
|
| 27 |
+
# Use TreeExplainer ONLY with XGBoost <= 1.7.6
|
| 28 |
explainer = shap.TreeExplainer(model)
|
| 29 |
|
| 30 |
# -----------------------------
|
| 31 |
+
# 2. Prediction + SHAP plot
|
| 32 |
# -----------------------------
|
| 33 |
def predict_and_explain(age, bmi, bp):
|
| 34 |
|
| 35 |
+
df = pd.DataFrame([{"age": age, "bmi": bmi, "bp": bp}])
|
| 36 |
+
pred = float(model.predict(df)[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
| 38 |
shap_values = explainer(df)
|
| 39 |
|
| 40 |
+
# Make SHAP waterfall plot
|
| 41 |
plt.figure(figsize=(8, 5))
|
| 42 |
shap.plots.waterfall(shap_values[0], show=False)
|
| 43 |
plt.tight_layout()
|
|
|
|
| 47 |
return pred, "shap_plot.png"
|
| 48 |
|
| 49 |
# -----------------------------
|
| 50 |
+
# 3. Gradio UI
|
| 51 |
# -----------------------------
|
| 52 |
inputs = [
|
| 53 |
gr.Number(label="Age"),
|
| 54 |
gr.Number(label="BMI"),
|
| 55 |
+
gr.Number(label="Blood Pressure"),
|
| 56 |
]
|
| 57 |
|
| 58 |
outputs = [
|
| 59 |
gr.Number(label="Prediction"),
|
| 60 |
+
gr.Image(label="SHAP Explanation"),
|
| 61 |
]
|
| 62 |
|
| 63 |
+
app = gr.Interface(
|
| 64 |
fn=predict_and_explain,
|
| 65 |
inputs=inputs,
|
| 66 |
outputs=outputs,
|
| 67 |
+
title="XGBoost + SHAP Deployment Fix",
|
| 68 |
+
description="Guaranteed working version for Hugging Face Spaces."
|
| 69 |
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
+
app.launch(share=True)
|
requirements.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
gradio==4.29.0
|
| 2 |
-
xgboost
|
| 3 |
-
shap
|
| 4 |
matplotlib
|
| 5 |
pandas
|
| 6 |
numpy
|
|
|
|
| 1 |
gradio==4.29.0
|
| 2 |
+
xgboost==1.7.6
|
| 3 |
+
shap==0.44.1
|
| 4 |
matplotlib
|
| 5 |
pandas
|
| 6 |
numpy
|