rahafahmad89 commited on
Commit
14e95ac
Β·
verified Β·
1 Parent(s): 804f187

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -11
app.py CHANGED
@@ -1,20 +1,49 @@
1
- # app.py β€” inline SHAP via gr.Plot (no PIL/files)
2
- import os, json, joblib, traceback
3
- import numpy as np, pandas as pd, gradio as gr
4
- import matplotlib
5
- matplotlib.use("Agg")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import matplotlib.pyplot as plt
 
7
  import shap
 
8
 
9
-
10
- MODEL_PATH = "model_extratrees_ds4.joblib"
11
  FEATS_PATH = "feature_names.json"
12
 
13
-
 
 
14
  model = joblib.load(MODEL_PATH)
 
15
  with open(FEATS_PATH) as f:
16
  FEATURES = json.load(f)
17
 
 
18
  def _clean_headers(df):
19
  df.columns = (df.columns.astype(str)
20
  .str.replace(r'[\u200b\ufeff]', '', regex=True)
@@ -32,6 +61,7 @@ def _read_table(file):
32
  on_bad_lines="skip", low_memory=False,
33
  encoding_errors="ignore")
34
  except Exception:
 
35
  for sep in [",", "\t", ";", "|"]:
36
  try:
37
  df = pd.read_csv(name, engine="python", sep=sep, dtype=str,
@@ -41,6 +71,8 @@ def _read_table(file):
41
  break
42
  except Exception:
43
  pass
 
 
44
  return _clean_headers(df)
45
 
46
  def _coerce_numeric(df):
@@ -60,6 +92,7 @@ def _validate_columns(df):
60
  extra = [c for c in df.columns if c not in FEATURES]
61
  return missing, extra
62
 
 
63
  def predict_csv(file):
64
  try:
65
  if file is None: return None, "No file."
@@ -69,7 +102,9 @@ def predict_csv(file):
69
  X = _prepare(df)
70
  proba = model.predict_proba(X)[:, 1]
71
  pred = (proba > 0.5).astype(int)
72
- out = pd.DataFrame({"row_index": df.index, "pred_prob_pathogenic": proba, "pred_label": pred})
 
 
73
  return out, "OK"
74
  except Exception:
75
  return None, "ERROR:\n" + traceback.format_exc()
@@ -82,7 +117,6 @@ def explain_first_row(file):
82
  if missing: return None, f"Missing features for SHAP. First 10: {missing[:10]}"
83
  X = _prepare(df)
84
  if X.shape[0] == 0: return None, "No rows."
85
-
86
  X_imp = pd.DataFrame(model.named_steps["imputer"].transform(X), columns=FEATURES)
87
  clf = model.named_steps["clf"]
88
 
@@ -102,6 +136,7 @@ def explain_first_row(file):
102
  except Exception:
103
  return None, "ERROR:\n" + traceback.format_exc()
104
 
 
105
  with gr.Blocks(title="Variant Pathogenicity Demo β€” ExtraTrees (Dataset-4)") as demo:
106
  gr.Markdown("### Variant Pathogenicity Demo β€” ExtraTrees (Dataset-4)")
107
  inp = gr.File(label="Upload CSV/XLSX containing the trained feature columns")
@@ -109,7 +144,7 @@ with gr.Blocks(title="Variant Pathogenicity Demo β€” ExtraTrees (Dataset-4)") as
109
  btn_pred = gr.Button("Predict")
110
  btn_expl = gr.Button("Explain first row")
111
  out_df = gr.Dataframe(label="Predictions")
112
- out_plot = gr.Plot(label="SHAP (row #1)") # <- plot component
113
  out_txt = gr.Markdown()
114
  btn_pred.click(fn=predict_csv, inputs=inp, outputs=[out_df, out_txt])
115
  btn_expl.click(fn=explain_first_row, inputs=inp, outputs=[out_plot, out_txt])
 
1
+ # --- bootstrap: force-install deps if Hugging Face didn't ---
2
+ import sys, subprocess
3
+ def _need(modname):
4
+ try: __import__(modname); return False
5
+ except ImportError: return True
6
+ def _pip(pkg_spec):
7
+ subprocess.check_call([sys.executable, "-m", "pip", "install", pkg_spec])
8
+
9
+ PKGS = [
10
+ ("joblib", "1.4.2", "joblib"),
11
+ ("numpy", "1.26.4","numpy"),
12
+ ("pandas", "2.2.2", "pandas"),
13
+ ("scikit-learn", "1.4.2", "sklearn"),
14
+ ("scipy", "1.11.4","scipy"),
15
+ ("shap", "0.46.0","shap"),
16
+ ("matplotlib", "3.8.4", "matplotlib"),
17
+ ("openpyxl", "3.1.5", "openpyxl"),
18
+ ("pillow", None, "PIL"),
19
+ ("gradio", "4.44.1","gradio"),
20
+ ]
21
+ for pkg, ver, mod in PKGS:
22
+ if _need(mod):
23
+ _pip(f"{pkg}=={ver}" if ver else pkg)
24
+
25
+ # now safe to import everything
26
+ import os, json, io, gzip, joblib, traceback
27
+ import numpy as np, pandas as pd
28
+ import matplotlib; matplotlib.use("Agg") # headless rendering
29
  import matplotlib.pyplot as plt
30
+ from PIL import Image
31
  import shap
32
+ import gradio as gr
33
 
34
+ # ---- artifacts in repo root ----
35
+ MODEL_PATH = "model_extratrees_ds4.joblib" # or "model_extratrees_ds4.gz"
36
  FEATS_PATH = "feature_names.json"
37
 
38
+ # if you uploaded a .gz model, uncomment these two lines and delete the next joblib.load:
39
+ # with gzip.open(MODEL_PATH, "rb") as f:
40
+ # model = joblib.load(f)
41
  model = joblib.load(MODEL_PATH)
42
+
43
  with open(FEATS_PATH) as f:
44
  FEATURES = json.load(f)
45
 
46
+ # ---------- helpers ----------
47
  def _clean_headers(df):
48
  df.columns = (df.columns.astype(str)
49
  .str.replace(r'[\u200b\ufeff]', '', regex=True)
 
61
  on_bad_lines="skip", low_memory=False,
62
  encoding_errors="ignore")
63
  except Exception:
64
+ df = None
65
  for sep in [",", "\t", ";", "|"]:
66
  try:
67
  df = pd.read_csv(name, engine="python", sep=sep, dtype=str,
 
71
  break
72
  except Exception:
73
  pass
74
+ if df is None:
75
+ raise
76
  return _clean_headers(df)
77
 
78
  def _coerce_numeric(df):
 
92
  extra = [c for c in df.columns if c not in FEATURES]
93
  return missing, extra
94
 
95
+ # ---------- app fns ----------
96
  def predict_csv(file):
97
  try:
98
  if file is None: return None, "No file."
 
102
  X = _prepare(df)
103
  proba = model.predict_proba(X)[:, 1]
104
  pred = (proba > 0.5).astype(int)
105
+ out = pd.DataFrame({"row_index": df.index,
106
+ "pred_prob_pathogenic": proba,
107
+ "pred_label": pred})
108
  return out, "OK"
109
  except Exception:
110
  return None, "ERROR:\n" + traceback.format_exc()
 
117
  if missing: return None, f"Missing features for SHAP. First 10: {missing[:10]}"
118
  X = _prepare(df)
119
  if X.shape[0] == 0: return None, "No rows."
 
120
  X_imp = pd.DataFrame(model.named_steps["imputer"].transform(X), columns=FEATURES)
121
  clf = model.named_steps["clf"]
122
 
 
136
  except Exception:
137
  return None, "ERROR:\n" + traceback.format_exc()
138
 
139
+ # ---------- UI ----------
140
  with gr.Blocks(title="Variant Pathogenicity Demo β€” ExtraTrees (Dataset-4)") as demo:
141
  gr.Markdown("### Variant Pathogenicity Demo β€” ExtraTrees (Dataset-4)")
142
  inp = gr.File(label="Upload CSV/XLSX containing the trained feature columns")
 
144
  btn_pred = gr.Button("Predict")
145
  btn_expl = gr.Button("Explain first row")
146
  out_df = gr.Dataframe(label="Predictions")
147
+ out_plot = gr.Plot(label="SHAP (row #1)")
148
  out_txt = gr.Markdown()
149
  btn_pred.click(fn=predict_csv, inputs=inp, outputs=[out_df, out_txt])
150
  btn_expl.click(fn=explain_first_row, inputs=inp, outputs=[out_plot, out_txt])