import gradio as gr import joblib import shap import numpy as np import pandas as pd import json model = joblib.load("model_v6.pkl") with open("feature_order.json") as f: features = json.load(f) explainer = shap.TreeExplainer(model) def predict(log_AF, hydro_diff, protein_pos, charge_change, grantham): X = pd.DataFrame([{ "log_AF": log_AF, "Hydro_diff": hydro_diff, "Protein_pos_norm": protein_pos, "Charge_change": charge_change, "Grantham": grantham }]) prob = model.predict_proba(X)[0][1] shap_vals = explainer.shap_values(X)[0] explanation = "" for i, feat in enumerate(features): explanation += f"{feat}: value={X.iloc[0][feat]:.4f}, SHAP={shap_vals[i]:+.4f}\n" return f"Pathogenic Probability: {prob:.4f}", explanation demo = gr.Interface( fn=predict, inputs=[ gr.Number(label="log_AF"), gr.Number(label="Hydrophobicity Difference"), gr.Number(label="Protein Position Normalized (0–1)"), gr.Number(label="Charge Change (0 or 1)"), gr.Number(label="Grantham Score") ], outputs=[ gr.Textbox(label="Prediction"), gr.Textbox(label="Feature Contributions") ], title="Explainable Mutation Pathogenicity Model v6", description="XGBoost + SHAP Explainable Variant Predictor" ) demo.launch()