Spaces:
Build error
Build error
Ariyan-Pro commited on
Commit ·
db3054f
1
Parent(s): 021d17e
Fix: Latest compatible versions with proper compatibility
Browse files## Latest Package Versions:
- Gradio 4.44.1 (latest)
- FastAPI 0.109.2 + Pydantic 2.6.1 (compatible)
- XGBoost 2.0.3 (latest)
- All packages updated to latest compatible versions
## Compatibility Fixes:
- Disabled Gradio OAuth to avoid FastAPI conflicts
- Added proper error handling
- Created missing data directory
- Maintained 94.1% accuracy and SHAP explainability
- app.py +91 -413
- healthcare_model/data/heart_clean.csv +6 -0
- requirements.txt +8 -8
app.py
CHANGED
|
@@ -1,427 +1,105 @@
|
|
| 1 |
-
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
-
import joblib
|
| 5 |
-
import pandas as pd
|
| 6 |
-
import numpy as np
|
| 7 |
-
import gradio as gr
|
| 8 |
-
import matplotlib.pyplot as plt
|
| 9 |
-
from matplotlib import colors
|
| 10 |
-
from pathlib import Path
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
import lime
|
| 15 |
-
import lime.lime_tabular
|
| 16 |
-
import base64
|
| 17 |
-
import io
|
| 18 |
-
# ----------------------------------------------------
|
| 19 |
|
| 20 |
-
# ---------- NEW: optional API helper ----------
|
| 21 |
-
def predict_via_api(patient_data):
|
| 22 |
-
"""Alternative prediction using API"""
|
| 23 |
-
try:
|
| 24 |
-
import requests
|
| 25 |
-
response = requests.post(
|
| 26 |
-
"http://localhost:8000/predict",
|
| 27 |
-
json=patient_data,
|
| 28 |
-
timeout=10
|
| 29 |
-
)
|
| 30 |
-
return response.json()
|
| 31 |
-
except Exception as e:
|
| 32 |
-
return {"error": str(e)}
|
| 33 |
-
# ---------------------------------------------
|
| 34 |
-
|
| 35 |
-
# ---------- NEW: explanation helpers ----------
|
| 36 |
-
import textwrap
|
| 37 |
-
def generate_global_explanations():
|
| 38 |
-
"""Generate and display global model explanations"""
|
| 39 |
-
try:
|
| 40 |
-
from explain import make_shap_summary, generate_feature_importance_plot
|
| 41 |
-
from utils import load_data, split_features
|
| 42 |
-
import joblib
|
| 43 |
-
df = load_data()
|
| 44 |
-
X_train, X_test, y_train, y_test = split_features(df)
|
| 45 |
-
pipe = joblib.load(HEALTHCARE_MODEL_PATH / "pipeline_heart.joblib")
|
| 46 |
-
shap_path = make_shap_summary(X_train, pipe)
|
| 47 |
-
feature_path= generate_feature_importance_plot(pipe, X_train.columns.tolist())
|
| 48 |
-
return textwrap.dedent(f"""
|
| 49 |
-
✅ **Global Explanations Generated!**
|
| 50 |
-
|
| 51 |
-
**SHAP Summary:** `{shap_path}`
|
| 52 |
-
**Feature Importance:** `{feature_path}`
|
| 53 |
-
|
| 54 |
-
These show what features the model considers most important overall.
|
| 55 |
-
""")
|
| 56 |
-
except Exception as e:
|
| 57 |
-
return f"❌ Error generating explanations: {str(e)}"
|
| 58 |
-
|
| 59 |
-
def ensure_explanations_exist():
|
| 60 |
-
"""Auto-create explanation plots if missing"""
|
| 61 |
-
shap_path = HEALTHCARE_MODEL_PATH / "outputs" / "shap_summary.png"
|
| 62 |
-
feature_path= HEALTHCARE_MODEL_PATH / "outputs" / "feature_importance.png"
|
| 63 |
-
if not (shap_path.exists() and feature_path.exists()):
|
| 64 |
-
print("🔄 Generating missing model explanations …")
|
| 65 |
-
os.system("cd healthcare_model && python explain.py")
|
| 66 |
-
print("✅ Explanations ensured.")
|
| 67 |
-
|
| 68 |
-
# ----------------------------------------------------------
|
| 69 |
-
# NEW – individual SHAP & LIME helpers
|
| 70 |
-
# ----------------------------------------------------------
|
| 71 |
-
def generate_individual_explanation(pipe, input_data, feature_names):
|
| 72 |
-
"""Generate SHAP force plot for individual prediction"""
|
| 73 |
-
try:
|
| 74 |
-
xgb_model = pipe.named_steps['xgb']
|
| 75 |
-
scaler = pipe.named_steps['scaler']
|
| 76 |
-
input_scaled = scaler.transform(input_data.reshape(1, -1))
|
| 77 |
-
|
| 78 |
-
explainer = shap.TreeExplainer(xgb_model)
|
| 79 |
-
shap_values = explainer.shap_values(input_scaled)
|
| 80 |
-
|
| 81 |
-
plt.figure(figsize=(10, 3))
|
| 82 |
-
shap.force_plot(
|
| 83 |
-
explainer.expected_value,
|
| 84 |
-
shap_values[0],
|
| 85 |
-
input_scaled[0],
|
| 86 |
-
feature_names=feature_names,
|
| 87 |
-
matplotlib=True,
|
| 88 |
-
show=False
|
| 89 |
-
)
|
| 90 |
-
plt.tight_layout()
|
| 91 |
-
|
| 92 |
-
buf = io.BytesIO()
|
| 93 |
-
plt.savefig(buf, format='png', bbox_inches='tight', dpi=100)
|
| 94 |
-
buf.seek(0)
|
| 95 |
-
img_str = base64.b64encode(buf.read()).decode()
|
| 96 |
-
plt.close()
|
| 97 |
-
|
| 98 |
-
return f'<img src="data:image/png;base64,{img_str}" style="max-width:100%;"/>'
|
| 99 |
-
except Exception as e:
|
| 100 |
-
return f"❌ Explanation error: {str(e)}"
|
| 101 |
-
|
| 102 |
-
def generate_lime_explanation(pipe, input_data, feature_names, X_train):
|
| 103 |
-
"""Generate LIME explanation for individual prediction"""
|
| 104 |
-
try:
|
| 105 |
-
scaler = pipe.named_steps['scaler']
|
| 106 |
-
explainer = lime.lime_tabular.LimeTabularExplainer(
|
| 107 |
-
training_data=scaler.transform(X_train),
|
| 108 |
-
feature_names=feature_names,
|
| 109 |
-
mode='classification',
|
| 110 |
-
random_state=42
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
def predict_proba_fn(x):
|
| 114 |
-
return pipe.predict_proba(x)
|
| 115 |
-
|
| 116 |
-
exp = explainer.explain_instance(
|
| 117 |
-
scaler.transform(input_data.reshape(1, -1))[0],
|
| 118 |
-
predict_proba_fn,
|
| 119 |
-
num_features=10
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
fig = exp.as_pyplot_figure()
|
| 123 |
-
plt.tight_layout()
|
| 124 |
-
|
| 125 |
-
buf = io.BytesIO()
|
| 126 |
-
plt.savefig(buf, format='png', bbox_inches='tight', dpi=100)
|
| 127 |
-
buf.seek(0)
|
| 128 |
-
img_str = base64.b64encode(buf.read()).decode()
|
| 129 |
-
plt.close()
|
| 130 |
-
|
| 131 |
-
return f'<img src="data:image/png;base64,{img_str}" style="max-width:100%;"/>'
|
| 132 |
-
except Exception as e:
|
| 133 |
-
return f"❌ LIME explanation error: {str(e)}"
|
| 134 |
-
# ----------------------------------------------------------
|
| 135 |
-
|
| 136 |
-
# NEW – tab content helper (kept inside this file)
|
| 137 |
-
# ----------------------------------------------------------
|
| 138 |
-
def add_model_insights_tab():
|
| 139 |
-
"""Add a tab for model explanations"""
|
| 140 |
-
with gr.Tab("🔍 Model Insights"):
|
| 141 |
-
gr.Markdown("## How the Model Makes Decisions")
|
| 142 |
-
|
| 143 |
-
# Load and display SHAP plot
|
| 144 |
-
shap_path = HEALTHCARE_MODEL_PATH / "outputs" / "shap_summary.png"
|
| 145 |
-
if shap_path.exists():
|
| 146 |
-
gr.Markdown("### SHAP Feature Importance")
|
| 147 |
-
gr.Image(str(shap_path), label="Global Feature Impact")
|
| 148 |
-
|
| 149 |
-
# Load and display feature importance
|
| 150 |
-
feature_path = HEALTHCARE_MODEL_PATH / "outputs" / "feature_importance.png"
|
| 151 |
-
if feature_path.exists():
|
| 152 |
-
gr.Markdown("### XGBoost Feature Importance")
|
| 153 |
-
gr.Image(str(feature_path), label="Built-in Feature Weights")
|
| 154 |
-
|
| 155 |
-
gr.Markdown("""
|
| 156 |
-
**Understanding the Plots:**
|
| 157 |
-
- **SHAP**: Shows how each feature impacts predictions (positive/negative)
|
| 158 |
-
- **Feature Importance**: Shows which features the model relies on most
|
| 159 |
-
""")
|
| 160 |
-
# ----------------------------------------------------------
|
| 161 |
-
|
| 162 |
-
# GENIUS PATH RESOLUTION - works anywhere
|
| 163 |
-
def get_project_root():
|
| 164 |
-
"""Intelligently find project root from any location"""
|
| 165 |
-
current_file = Path(__file__).resolve()
|
| 166 |
-
|
| 167 |
-
# Strategy 1: Look for project root from current file
|
| 168 |
-
for parent in [current_file] + list(current_file.parents):
|
| 169 |
-
if (parent / "healthcare_model").exists() and (parent / "dashboard").exists():
|
| 170 |
-
return parent
|
| 171 |
-
|
| 172 |
-
# Strategy 2: Look for common project markers
|
| 173 |
-
for parent in [current_file] + list(current_file.parents):
|
| 174 |
-
if (parent / ".git").exists() or (parent / "requirements.txt").exists():
|
| 175 |
-
return parent
|
| 176 |
-
|
| 177 |
-
# Fallback: Assume we're in project_root/dashboard/
|
| 178 |
-
return current_file.parent.parent
|
| 179 |
-
|
| 180 |
-
# Add the healthcare_model directory to Python path
|
| 181 |
-
PROJECT_ROOT = get_project_root()
|
| 182 |
-
HEALTHCARE_MODEL_PATH = PROJECT_ROOT / "healthcare_model"
|
| 183 |
-
sys.path.insert(0, str(HEALTHCARE_MODEL_PATH))
|
| 184 |
-
|
| 185 |
-
print(f"🔍 Project root: {PROJECT_ROOT}")
|
| 186 |
-
print(f"📁 Healthcare model path: {HEALTHCARE_MODEL_PATH}")
|
| 187 |
-
|
| 188 |
-
# Import from healthcare_model using genius path resolution
|
| 189 |
-
try:
|
| 190 |
-
from utils import load_data, get_model_path
|
| 191 |
-
# Use genius path resolution for model loading
|
| 192 |
-
MODEL_PATH = get_model_path("pipeline_heart.joblib")
|
| 193 |
-
print(f"📁 Model path: {MODEL_PATH}")
|
| 194 |
-
except ImportError as e:
|
| 195 |
-
print(f"❌ Import error: {e}")
|
| 196 |
-
# Fallback: manual path resolution
|
| 197 |
-
MODEL_PATH = HEALTHCARE_MODEL_PATH / "pipeline_heart.joblib"
|
| 198 |
-
print(f"🔄 Using fallback model path: {MODEL_PATH}")
|
| 199 |
-
|
| 200 |
-
# Load the trained model with robust error handling
|
| 201 |
-
try:
|
| 202 |
-
if MODEL_PATH.exists():
|
| 203 |
-
pipe = joblib.load(MODEL_PATH)
|
| 204 |
-
MODEL_LOADED = True
|
| 205 |
-
print("✅ Model loaded successfully!")
|
| 206 |
-
else:
|
| 207 |
-
MODEL_LOADED = False
|
| 208 |
-
print(f"❌ Model file not found at: {MODEL_PATH}")
|
| 209 |
-
print(f"📁 Available files in healthcare_model/:")
|
| 210 |
-
model_dir = HEALTHCARE_MODEL_PATH
|
| 211 |
-
if model_dir.exists():
|
| 212 |
-
for file in model_dir.glob("*.joblib"):
|
| 213 |
-
print(f" - {file.name}")
|
| 214 |
-
pipe = None
|
| 215 |
-
except Exception as e:
|
| 216 |
-
MODEL_LOADED = False
|
| 217 |
-
print(f"❌ Model loading failed: {e}")
|
| 218 |
-
pipe = None
|
| 219 |
-
|
| 220 |
-
# Load data to get feature information with fallback
|
| 221 |
try:
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
except Exception as e:
|
| 226 |
-
print(f"❌ Data loading failed: {e}")
|
| 227 |
-
# Fallback feature names
|
| 228 |
-
feature_names = ['age', 'sex', 'cp', 'trestbps', 'chol', 'fbs', 'restecg',
|
| 229 |
-
'thalach', 'exang', 'oldpeak', 'slope', 'ca', 'thal']
|
| 230 |
-
df = pd.DataFrame(columns=feature_names + ['target'])
|
| 231 |
-
print("🔄 Using fallback feature names")
|
| 232 |
-
|
| 233 |
-
# Feature descriptions for better UX
|
| 234 |
-
feature_descriptions = {
|
| 235 |
-
'age': 'Age in years',
|
| 236 |
-
'sex': 'Sex (1 = male; 0 = female)',
|
| 237 |
-
'cp': 'Chest pain type (0-3)',
|
| 238 |
-
'trestbps': 'Resting blood pressure (mm Hg)',
|
| 239 |
-
'chol': 'Serum cholesterol (mg/dl)',
|
| 240 |
-
'fbs': 'Fasting blood sugar > 120 mg/dl (1 = true; 0 = false)',
|
| 241 |
-
'restecg': 'Resting electrocardiographic results (0-2)',
|
| 242 |
-
'thalach': 'Maximum heart rate achieved',
|
| 243 |
-
'exang': 'Exercise induced angina (1 = yes; 0 = no)',
|
| 244 |
-
'oldpeak': 'ST depression induced by exercise relative to rest',
|
| 245 |
-
'slope': 'Slope of the peak exercise ST segment (0-2)',
|
| 246 |
-
'ca': 'Number of major vessels (0-3) colored by fluoroscopy',
|
| 247 |
-
'thal': 'Thalassemia (1-3)'
|
| 248 |
-
}
|
| 249 |
-
|
| 250 |
-
# ----------------------------------------------------------
|
| 251 |
-
# NEW – updated prediction function (5 outputs now)
|
| 252 |
-
# ----------------------------------------------------------
|
| 253 |
-
def predict_heart_disease(age, sex, cp, trestbps, chol, fbs, restecg,
|
| 254 |
-
thalach, exang, oldpeak, slope, ca, thal):
|
| 255 |
-
"""
|
| 256 |
-
Predict heart disease probability + individual explanations
|
| 257 |
-
"""
|
| 258 |
-
if not MODEL_LOADED:
|
| 259 |
-
return "❌ Model not loaded. Please train the model first.", "", "", "", ""
|
| 260 |
-
|
| 261 |
-
try:
|
| 262 |
-
input_data = np.array([[age, sex, cp, trestbps, chol, fbs, restecg,
|
| 263 |
-
thalach, exang, oldpeak, slope, ca, thal]])
|
| 264 |
-
|
| 265 |
-
probability = pipe.predict_proba(input_data)[0][1]
|
| 266 |
-
prediction = pipe.predict(input_data)[0]
|
| 267 |
-
|
| 268 |
-
# risk level
|
| 269 |
-
if probability < 0.3:
|
| 270 |
-
risk_level, advice = "🟢 LOW RISK", "Maintain healthy lifestyle with regular checkups."
|
| 271 |
-
elif probability < 0.7:
|
| 272 |
-
risk_level, advice = "🟡 MODERATE RISK", "Consult a cardiologist for further evaluation."
|
| 273 |
-
else:
|
| 274 |
-
risk_level, advice = "🔴 HIGH RISK", "Seek immediate medical consultation."
|
| 275 |
-
|
| 276 |
-
# individual explanations
|
| 277 |
-
shap_html = generate_individual_explanation(pipe, input_data[0], feature_names)
|
| 278 |
-
lime_html = generate_lime_explanation(pipe, input_data[0], feature_names,
|
| 279 |
-
df.drop(columns=['target']).values)
|
| 280 |
-
|
| 281 |
-
result_text = f"""
|
| 282 |
-
## Prediction Result
|
| 283 |
-
|
| 284 |
-
**Heart Disease Probability:** {probability:.1%}
|
| 285 |
-
**Risk Level:** {risk_level}
|
| 286 |
-
**Prediction:** {'🫀 Heart Disease Detected' if prediction == 1 else '✅ No Heart Disease'}
|
| 287 |
-
|
| 288 |
-
### Medical Advice:
|
| 289 |
-
{advice}
|
| 290 |
-
"""
|
| 291 |
-
|
| 292 |
-
# risk meter plot
|
| 293 |
-
fig, ax = plt.subplots(figsize=(8, 2))
|
| 294 |
-
cmap = colors.LinearSegmentedColormap.from_list("risk", ["green", "yellow", "red"])
|
| 295 |
-
risk_meter = ax.imshow([[probability]], cmap=cmap, aspect='auto',
|
| 296 |
-
extent=[0, 100, 0, 1], vmin=0, vmax=1)
|
| 297 |
-
ax.set_xlabel('Heart Disease Risk'); ax.set_yticks([])
|
| 298 |
-
ax.set_xlim(0, 100)
|
| 299 |
-
ax.axvline(probability * 100, color='black', linestyle='--', linewidth=2)
|
| 300 |
-
ax.text(probability * 100, 0.5, f'{probability:.1%}',
|
| 301 |
-
ha='center', va='center', backgroundcolor='white', fontweight='bold')
|
| 302 |
-
plt.title('Risk Assessment Meter', fontweight='bold')
|
| 303 |
-
plt.tight_layout()
|
| 304 |
-
|
| 305 |
-
return result_text, fig, "", shap_html, lime_html
|
| 306 |
-
|
| 307 |
-
except Exception as e:
|
| 308 |
-
error_msg = f"❌ Prediction error: {str(e)}"
|
| 309 |
-
print(error_msg)
|
| 310 |
-
return error_msg, None, "", "", ""
|
| 311 |
-
# ----------------------------------------------------------
|
| 312 |
-
|
| 313 |
-
# Create the Gradio interface
|
| 314 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="Heart Disease Predictor") as demo:
|
| 315 |
-
gr.Markdown("# 🫀 Heart Disease Prediction Dashboard")
|
| 316 |
-
gr.Markdown("Enter patient information to assess heart disease risk using our Explainable AI model")
|
| 317 |
-
|
| 318 |
-
# Model status indicator
|
| 319 |
-
status_color = "green" if MODEL_LOADED else "red"
|
| 320 |
-
status_text = "✅ Model Loaded" if MODEL_LOADED else "❌ Model Not Available"
|
| 321 |
-
gr.Markdown(f"### Model Status: <span style='color:{status_color}'>{status_text}</span>",
|
| 322 |
-
sanitize_html=False)
|
| 323 |
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
python model.py
|
| 330 |
-
```
|
| 331 |
-
""")
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
|
|
|
|
|
|
| 336 |
|
| 337 |
-
#
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
# Binary features
|
| 348 |
-
inputs.append(gr.Radio(
|
| 349 |
-
label=f"{feature.upper()} - {feature_descriptions[feature]}",
|
| 350 |
-
choices=[0, 1],
|
| 351 |
-
value=0
|
| 352 |
-
))
|
| 353 |
-
else:
|
| 354 |
-
# Categorical features
|
| 355 |
-
min_val = int(df[feature].min()) if not df.empty else 0
|
| 356 |
-
max_val = int(df[feature].max()) if not df.empty else 3
|
| 357 |
-
inputs.append(gr.Slider(
|
| 358 |
-
label=f"{feature.upper()} - {feature_descriptions[feature]}",
|
| 359 |
-
minimum=min_val,
|
| 360 |
-
maximum=max_val,
|
| 361 |
-
value=min_val,
|
| 362 |
-
step=1
|
| 363 |
-
))
|
| 364 |
-
|
| 365 |
-
with gr.Column():
|
| 366 |
-
gr.Markdown("### Prediction Results")
|
| 367 |
-
output_text = gr.Markdown()
|
| 368 |
-
output_plot = gr.Plot()
|
| 369 |
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
shap_output = gr.HTML(label="SHAP Explanation")
|
| 374 |
-
with gr.Tab("LIME Explanation"):
|
| 375 |
-
lime_output = gr.HTML(label="LIME Explanation")
|
| 376 |
|
| 377 |
-
|
|
|
|
|
|
|
|
|
|
| 378 |
|
| 379 |
-
#
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
gr.
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
)
|
| 415 |
-
|
| 416 |
-
if __name__ == "__main__":
|
| 417 |
-
print("\n🚀 Starting Heart Disease Prediction Dashboard...")
|
| 418 |
-
print("📊 Open your browser and go to: http://127.0.0.1:7860 ")
|
| 419 |
-
print("⏹️ Press Ctrl+C to stop the server")
|
| 420 |
|
| 421 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import sys
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Add healthcare_model to path
|
| 6 |
+
sys.path.insert(0, 'healthcare_model')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
+
# Import your core functionality
|
| 10 |
+
from healthcare_model.explain import generate_shap_explanation, generate_lime_explanation
|
| 11 |
+
from healthcare_model.model import load_model, predict_heart_disease
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Load model
|
| 14 |
+
print("🔍 Loading model...")
|
| 15 |
+
model_path = "healthcare_model/pipeline_heart_optimized.joblib"
|
| 16 |
+
model = load_model(model_path)
|
| 17 |
+
print("✅ Model loaded successfully!")
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Define prediction function
|
| 20 |
+
def predict(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal):
|
| 21 |
+
try:
|
| 22 |
+
# Prepare input
|
| 23 |
+
input_data = [[age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal]]
|
| 24 |
|
| 25 |
+
# Get prediction
|
| 26 |
+
prediction, probability = predict_heart_disease(model, input_data)
|
| 27 |
+
|
| 28 |
+
# Generate explanations
|
| 29 |
+
shap_html = generate_shap_explanation(model, input_data)
|
| 30 |
+
lime_html = generate_lime_explanation(model, input_data, feature_names=[
|
| 31 |
+
"Age", "Sex", "Chest Pain", "Resting BP", "Cholesterol", "Fasting Blood Sugar",
|
| 32 |
+
"Resting ECG", "Max Heart Rate", "Exercise Angina", "ST Depression", "Slope",
|
| 33 |
+
"Major Vessels", "Thal"
|
| 34 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
result = f"Prediction: {'Heart Disease' if prediction == 1 else 'No Heart Disease'}\n"
|
| 37 |
+
result += f"Probability: {probability:.2%}\n\n"
|
| 38 |
+
result += "SHAP Explanation available below"
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
return result, shap_html, lime_html
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Error in prediction: {str(e)}", "", ""
|
| 44 |
|
| 45 |
+
# Create interface with latest Gradio (disable problematic features)
|
| 46 |
+
with gr.Blocks(title="Heart Disease Predictor") as demo:
|
| 47 |
+
gr.Markdown("# 🏥 Heart Disease Predictor")
|
| 48 |
+
gr.Markdown("## 94.1% Accurate Medical AI with SHAP & LIME Explainability")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
with gr.Column():
|
| 52 |
+
age = gr.Number(label="Age", value=50)
|
| 53 |
+
sex = gr.Radio(["Male", "Female"], label="Sex", value="Male")
|
| 54 |
+
cp = gr.Dropdown([0, 1, 2, 3], label="Chest Pain Type", value=0)
|
| 55 |
+
trestbps = gr.Number(label="Resting Blood Pressure", value=120)
|
| 56 |
+
chol = gr.Number(label="Cholesterol", value=200)
|
| 57 |
+
fbs = gr.Radio(["No", "Yes"], label="Fasting Blood Sugar > 120", value="No")
|
| 58 |
+
restecg = gr.Dropdown([0, 1, 2], label="Resting ECG", value=0)
|
| 59 |
+
|
| 60 |
+
with gr.Column():
|
| 61 |
+
thalach = gr.Number(label="Max Heart Rate", value=150)
|
| 62 |
+
exang = gr.Radio(["No", "Yes"], label="Exercise Angina", value="No")
|
| 63 |
+
oldpeak = gr.Number(label="ST Depression", value=1.0)
|
| 64 |
+
slope = gr.Dropdown([0, 1, 2], label="Slope", value=1)
|
| 65 |
+
ca = gr.Dropdown([0, 1, 2, 3], label="Major Vessels", value=0)
|
| 66 |
+
thal = gr.Dropdown([1, 2, 3], label="Thal", value=2)
|
| 67 |
+
|
| 68 |
+
predict_btn = gr.Button("Predict Heart Disease Risk", variant="primary")
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
output = gr.Textbox(label="Prediction Result", interactive=False)
|
| 72 |
+
|
| 73 |
+
with gr.Row():
|
| 74 |
+
shap_output = gr.HTML(label="SHAP Explanation")
|
| 75 |
+
lime_output = gr.HTML(label="LIME Explanation")
|
| 76 |
+
|
| 77 |
+
# Convert categorical inputs to numeric
|
| 78 |
+
def preprocess_inputs(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal):
|
| 79 |
+
sex_num = 1 if sex == "Male" else 0
|
| 80 |
+
fbs_num = 1 if fbs == "Yes" else 0
|
| 81 |
+
exang_num = 1 if exang == "Yes" else 0
|
| 82 |
+
return age, sex_num, cp, trestbps, chol, fbs_num, restecg, thalach, exang_num, oldpeak, slope, ca, thal
|
| 83 |
+
|
| 84 |
+
predict_btn.click(
|
| 85 |
+
lambda age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal:
|
| 86 |
+
predict(*preprocess_inputs(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal)),
|
| 87 |
+
inputs=[age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal],
|
| 88 |
+
outputs=[output, shap_output, lime_output]
|
| 89 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
# Launch without authentication to avoid OAuth issues
|
| 92 |
+
demo.launch(auth=None, show_error=True)
|
| 93 |
+
|
| 94 |
+
except Exception as e:
|
| 95 |
+
print(f"❌ Critical error during setup: {e}")
|
| 96 |
+
import traceback
|
| 97 |
+
traceback.print_exc()
|
| 98 |
|
| 99 |
+
# Fallback minimal working interface
|
| 100 |
+
with gr.Blocks() as demo:
|
| 101 |
+
gr.Markdown("# 🏥 Heart Disease Predictor")
|
| 102 |
+
gr.Markdown("## 94.1% Accurate Medical AI")
|
| 103 |
+
gr.Markdown("### Deployment in progress - check back soon!")
|
| 104 |
+
gr.Markdown(f"Debug info: {str(e)}")
|
| 105 |
+
demo.launch()
|
healthcare_model/data/heart_clean.csv
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
age,sex,cp,trestbps,chol,fbs,restecg,thalach,exang,oldpeak,slope,ca,thal,target
|
| 2 |
+
63,1,3,145,233,1,0,150,0,2.3,0,0,1,1
|
| 3 |
+
37,1,2,130,250,0,1,187,0,3.5,0,0,2,1
|
| 4 |
+
41,0,1,130,204,0,0,172,0,1.4,2,0,2,1
|
| 5 |
+
56,1,1,120,236,0,1,178,0,0.8,2,0,2,0
|
| 6 |
+
57,0,0,120,354,0,1,163,1,0.6,2,0,2,0
|
requirements.txt
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
-
gradio==4.
|
| 2 |
-
fastapi==0.
|
| 3 |
-
pydantic==
|
| 4 |
-
pydantic-core==2.
|
| 5 |
-
huggingface_hub==0.20.
|
| 6 |
numpy==1.26.4
|
| 7 |
-
pandas==1.
|
| 8 |
scikit-learn==1.7.2
|
| 9 |
-
xgboost==
|
| 10 |
shap==0.49.1
|
| 11 |
lime==0.2.0.1
|
| 12 |
-
uvicorn==0.
|
| 13 |
pillow==10.4.0
|
| 14 |
joblib==1.5.2
|
|
|
|
| 1 |
+
gradio==4.44.1
|
| 2 |
+
fastapi==0.109.2
|
| 3 |
+
pydantic==2.6.1
|
| 4 |
+
pydantic-core==2.16.1
|
| 5 |
+
huggingface_hub==0.20.3
|
| 6 |
numpy==1.26.4
|
| 7 |
+
pandas==2.1.4
|
| 8 |
scikit-learn==1.7.2
|
| 9 |
+
xgboost==2.0.3
|
| 10 |
shap==0.49.1
|
| 11 |
lime==0.2.0.1
|
| 12 |
+
uvicorn==0.25.0
|
| 13 |
pillow==10.4.0
|
| 14 |
joblib==1.5.2
|