import os import json import traceback import joblib import numpy as np import pandas as pd import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import shap import gradio as gr # ============================================================ # PATCH: Fix gradio_client boolean JSON-schema crash # ============================================================ try: import gradio_client.utils as _gcu if hasattr(_gcu, "_json_schema_to_python_type"): _orig = _gcu._json_schema_to_python_type def _patched(schema, defs=None): # JSON Schema allows boolean schemas (True / False) if isinstance(schema, bool): return "Any" return _orig(schema, defs) _gcu._json_schema_to_python_type = _patched except Exception: pass # ============================================================ # Artifacts # ============================================================ MODEL_PATH = "model_extratrees_ds4.joblib" FEATS_PATH = "feature_names.json" model = joblib.load(MODEL_PATH) with open(FEATS_PATH) as f: FEATURES = json.load(f) LABEL_NAME = {0: "Benign", 1: "Pathogenic"} THRESHOLD = 0.7 ID_CANDIDATES = [ "#Uploaded_Variation", "#Uploaded_variation", "uploaded_variation", "Uploaded_Variation", "Uploaded_variation", "variant_id", "Variant_ID", "variation_id", "Variant", "ID", "HGVSc", "HGVSp" ] # ============================================================ # Helpers # ============================================================ def _clean_headers(df): df.columns = ( df.columns.astype(str) .str.replace(r"[\u200b\ufeff]", "", regex=True) .str.strip() ) return df def _read_table(file): name = getattr(file, "name", str(file)) if name.lower().endswith((".xlsx", ".xls")): df = pd.read_excel(name, engine="openpyxl") else: df = pd.read_csv( name, engine="python", sep=None, dtype=str, na_values=["", "NA", "N/A"], quoting=3, on_bad_lines="skip", encoding_errors="ignore", ) return _clean_headers(df) def _coerce_numeric(df): for c in df.columns: df[c] = pd.to_numeric(df[c], errors="coerce") return df def _prepare(df): for c in FEATURES: if c not in df.columns: df[c] = np.nan return _coerce_numeric(df[FEATURES].copy()) def _validate_columns(df): return [c for c in FEATURES if c not in df.columns] def _pick_id_series(df): norm = {c.lower(): c for c in df.columns} for cand in ID_CANDIDATES: if cand.lower() in norm: col = norm[cand.lower()] return df[col].astype(str) return pd.Series(df.index, name="row_index") # ============================================================ # Prediction # ============================================================ def predict_csv(file): try: if file is None: return None, "❌ No file uploaded." df = _read_table(file) missing = _validate_columns(df) if missing: return None, f"❌ Missing {len(missing)} features. Example: {missing[:10]}" X = _prepare(df) proba = model.predict_proba(X)[:, 1] pred = (proba > THRESHOLD).astype(int) ids = _pick_id_series(df) out = pd.DataFrame({ "Variant ID": ids, "Predicted probability (pathogenic)": proba, "Predicted class": np.where( pred == 1, "🔴 Pathogenic", "🟢 Benign" ), }) summary = ( f"✅ Processed {len(out)} variants \n" f"🔴 Pathogenic: {(pred == 1).sum()} | " f"🟢 Benign: {(pred == 0).sum()} \n" f"Threshold = {THRESHOLD}" ) return out, summary except Exception: return None, "❌ ERROR:\n```" + traceback.format_exc() + "```" # ============================================================ # SHAP Explanation # ============================================================ def explain_first_row(file): try: if file is None: return None, "❌ No file uploaded." df = _read_table(file) missing = _validate_columns(df) if missing: return None, "❌ Missing required features for explanation." X = _prepare(df) if len(X) == 0: return None, "❌ No rows found." explainer = shap.Explainer(model.predict_proba, X) shap_values = explainer(X.iloc[[0]]) vals = shap_values.values[0, :, 1] s = pd.Series(vals, index=FEATURES).sort_values( key=np.abs, ascending=False ).head(15) fig = plt.figure(figsize=(7, 5)) s.iloc[::-1].plot(kind="barh") plt.title("Top feature contributions (Sample #1)") plt.tight_layout() return fig, ( "🔍 **Why this prediction?** \n" "Positive bars increase the probability of pathogenicity; " "negative bars decrease it. \n" "These signals are *supportive only* and must be interpreted " "within ACMG/AMP guidelines." ) except Exception: return None, "❌ ERROR:\n```" + traceback.format_exc() + "```" # ============================================================ # UI # ============================================================ with gr.Blocks( title="Pathogenicity Predictor", theme=gr.themes.Soft(primary_hue="rose", secondary_hue="slate"), ) as demo: gr.Markdown(""" # 🧬 Pathogenicity Predictor **Research prototype for variant pathogenicity assessment** Upload a CSV or Excel file containing the trained feature columns. This tool provides *supportive* predictions and explanations. """) with gr.Row(): with gr.Column(scale=1): gr.Markdown("### 📂 Input") inp = gr.File( label="Upload CSV / XLSX", file_types=[".csv", ".xlsx", ".xls"] ) gr.Markdown(""" **Requirements** - One row = one variant - Must include all trained feature columns - Results are **not** clinical diagnoses """) with gr.Column(scale=2): gr.Markdown("### 📊 Results") with gr.Row(): btn_pred = gr.Button("🔮 Predict", variant="primary") btn_expl = gr.Button("🔍 Explain first variant") out_df = gr.Dataframe( label="Predictions", interactive=False, wrap=True, ) out_txt = gr.Markdown() gr.Markdown("### 🔬 Why this prediction?") out_plot = gr.Plot(label="Feature contribution (SHAP)") btn_pred.click(predict_csv, inp, [out_df, out_txt], show_progress=True) btn_expl.click(explain_first_row, inp, [out_plot, out_txt]) # ======================================================== # Feedback section (email) # ======================================================== gr.Markdown(""" --- ### 💬 Feedback We would love to hear your thoughts, suggestions, or bug reports. 📧 **[Send feedback via email](mailto:dr.rahafahmad@gmail.com?subject=Pathogenicity%20Predictor%20Feedback)** Click on the link or email us directly at **dr.rahafahmad@gmail.com, 700039113@uaeu.ac.ae, bassam.ali@uaeu.ac.ae** """) gr.Markdown(""" --- ⚠️ **Research prototype** This tool is for research and educational use only. Not intended for clinical diagnosis or medical decision-making. © 2026 """) # ============================================================ # Launch (Spaces-safe) # ============================================================ if __name__ == "__main__": port = int(os.environ.get("PORT", 7860)) demo.queue() demo.launch(server_name="0.0.0.0", server_port=port, debug=True)