Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import joblib
|
| 4 |
+
import shap
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
model = joblib.load("model_v6.pkl")
|
| 10 |
+
|
| 11 |
+
with open("feature_order.json") as f:
|
| 12 |
+
features = json.load(f)
|
| 13 |
+
|
| 14 |
+
explainer = shap.TreeExplainer(model)
|
| 15 |
+
|
| 16 |
+
def predict(log_AF, hydro_diff, protein_pos, charge_change, grantham):
|
| 17 |
+
|
| 18 |
+
X = pd.DataFrame([{
|
| 19 |
+
"log_AF": log_AF,
|
| 20 |
+
"Hydro_diff": hydro_diff,
|
| 21 |
+
"Protein_pos_norm": protein_pos,
|
| 22 |
+
"Charge_change": charge_change,
|
| 23 |
+
"Grantham": grantham
|
| 24 |
+
}])
|
| 25 |
+
|
| 26 |
+
prob = model.predict_proba(X)[0][1]
|
| 27 |
+
shap_vals = explainer.shap_values(X)[0]
|
| 28 |
+
|
| 29 |
+
explanation = ""
|
| 30 |
+
for i, feat in enumerate(features):
|
| 31 |
+
explanation += f"{feat}: value={X.iloc[0][feat]:.4f}, SHAP={shap_vals[i]:+.4f}\n"
|
| 32 |
+
|
| 33 |
+
return f"Pathogenic Probability: {prob:.4f}", explanation
|
| 34 |
+
|
| 35 |
+
demo = gr.Interface(
|
| 36 |
+
fn=predict,
|
| 37 |
+
inputs=[
|
| 38 |
+
gr.Number(label="log_AF"),
|
| 39 |
+
gr.Number(label="Hydrophobicity Difference"),
|
| 40 |
+
gr.Number(label="Protein Position Normalized (0–1)"),
|
| 41 |
+
gr.Number(label="Charge Change (0 or 1)"),
|
| 42 |
+
gr.Number(label="Grantham Score")
|
| 43 |
+
],
|
| 44 |
+
outputs=[
|
| 45 |
+
gr.Textbox(label="Prediction"),
|
| 46 |
+
gr.Textbox(label="Feature Contributions")
|
| 47 |
+
],
|
| 48 |
+
title="Explainable Mutation Pathogenicity Model v6",
|
| 49 |
+
description="XGBoost + SHAP Explainable Variant Predictor"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
demo.launch()
|