Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import shap
|
| 4 |
+
from shap.plots._force_matplotlib import draw_additive_plot
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
# load the model from disk
|
| 10 |
+
loaded_model = pickle.load(open("glioma_xgb.pkl", 'rb'))
|
| 11 |
+
|
| 12 |
+
# Setup SHAP
|
| 13 |
+
explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.
|
| 14 |
+
|
| 15 |
+
# Create the main function for server
|
| 16 |
+
def main_func(Gender, Age_at_diagnosis, IDH1, TP53, ATRX, PTEN, EGFR, CIC, MUC16, PIK3CA, NF1, PIK3R1, FUBP1, RB1, NOTCH1, BCOR, CSMD3, SMARCA4, GRIN2A, IDH2, FAT4, PDGFRA):
|
| 17 |
+
new_row = pd.DataFrame.from_dict({'Gender':Gender,
|
| 18 |
+
'Age_at_diagnosis':Age_at_diagnosis,'IDH1':IDH1,'TP53':TP53,
|
| 19 |
+
'ATRX':ATRX, 'PTEN':PTEN,'EGFR':EGFR,'CIC':CIC,
|
| 20 |
+
'MUC16':MUC16,'PIK3CA':PIK3CA,'NF1':NF1,'PIK3R1':PIK3R1, 'FUBP1': FUBP1, 'RB1': RB1, 'NOTCH1': NOTCH1,
|
| 21 |
+
'BCOR': BCOR, 'CSMD3': CSMD3, 'SMARCA4': SMARCA4, 'GRIN2A': GRIN2A, 'IDH2': IDH2, 'FAT4': FAT4, 'PDGFRA': PDGFRA},
|
| 22 |
+
orient = 'index').transpose()
|
| 23 |
+
|
| 24 |
+
prob = loaded_model.predict_proba(new_row)
|
| 25 |
+
|
| 26 |
+
shap_values = explainer(new_row)
|
| 27 |
+
# plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
|
| 28 |
+
# plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
|
| 29 |
+
plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
|
| 30 |
+
|
| 31 |
+
plt.tight_layout()
|
| 32 |
+
local_plot = plt.gcf()
|
| 33 |
+
plt.close()
|
| 34 |
+
|
| 35 |
+
return {"Chance of Having GBM Tumor": 1-float(prob[0][0]), "Chance of Having LGG Tumor": float(prob[0][0])}, local_plot
|
| 36 |
+
|
| 37 |
+
# Create the UI
|
| 38 |
+
title = "**Glioma Predictor & Interpreter** 🪐"
|
| 39 |
+
description1 = """This app takes info from subjects and predicts the severity of their brain tumor (LGG or GBM). Do not use for medical diagnosis."""
|
| 40 |
+
|
| 41 |
+
description2 = """
|
| 42 |
+
To use the app, click on one of the examples, or adjust the values of the factors, and click on Analyze. 🤞
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
with gr.Blocks(title=title) as demo:
|
| 46 |
+
gr.Markdown(f"## {title}")
|
| 47 |
+
gr.Markdown(description1)
|
| 48 |
+
gr.Markdown("""---""")
|
| 49 |
+
gr.Markdown(description2)
|
| 50 |
+
gr.Markdown("""---""")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
Gender = gr.Radio(["Female", "Male"], label="Gender", type = "index")
|
| 54 |
+
Age_at_diagnosis = gr.Number(label="Age at Diagnosis")
|
| 55 |
+
with gr.Row():
|
| 56 |
+
IDH1 = gr.Radio(["No", "Yes"], label="IDH1 Mutation", type="index")
|
| 57 |
+
TP53 = gr.Radio(["No", "Yes"], label="TP53 Mutation", type="index")
|
| 58 |
+
ATRX = gr.Radio(["No", "Yes"], label="ATRX Mutation", type="index")
|
| 59 |
+
with gr.Row():
|
| 60 |
+
PTEN = gr.Radio(["No", "Yes"], label="PTEN Mutation", type="index")
|
| 61 |
+
EGFR = gr.Radio(["No", "Yes"], label="EGFR Mutation", type="index")
|
| 62 |
+
CIC = gr.Radio(["No", "Yes"], label="CIC Mutation", type="index")
|
| 63 |
+
with gr.Row():
|
| 64 |
+
MUC16 = gr.Radio(["No", "Yes"], label="MUC16 Mutation", type="index")
|
| 65 |
+
PIK3CA = gr.Radio(["No", "Yes"], label="PIK3CA Mutation", type="index")
|
| 66 |
+
NF1 = gr.Radio(["No", "Yes"], label="NF1 Mutation", type="index")
|
| 67 |
+
with gr.Row():
|
| 68 |
+
PIK3R1 = gr.Radio(["No", "Yes"], label="PIK3R1 Mutation", type="index")
|
| 69 |
+
FUBP1 = gr.Radio(["No", "Yes"], label="FUBP1 Mutation", type="index")
|
| 70 |
+
RB1 = gr.Radio(["No", "Yes"], label="RB1 Mutation", type="index")
|
| 71 |
+
with gr.Row():
|
| 72 |
+
NOTCH1 = gr.Radio(["No", "Yes"], label="NOTCH1 Mutation", type="index")
|
| 73 |
+
BCOR = gr.Radio(["No", "Yes"], label="BCOR Mutation", type="index")
|
| 74 |
+
CSMD3 = gr.Radio(["No", "Yes"], label="CSMD3 Mutation", type="index")
|
| 75 |
+
with gr.Row():
|
| 76 |
+
SMARCA4 = gr.Radio(["No", "Yes"], label="SMAECA4 Mutation", type="index")
|
| 77 |
+
GRIN2A = gr.Radio(["No", "Yes"], label="GRIN2A Mutation", type="index")
|
| 78 |
+
IDH2 = gr.Radio(["No", "Yes"], label="IDH2 Mutation", type="index")
|
| 79 |
+
FAT4 = gr.Radio(["No", "Yes"], label="FAT4 Mutation", type="index")
|
| 80 |
+
PDGFRA = gr.Radio(["No", "Yes"], label="PDGFRA Mutation", type="index")
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
submit_btn = gr.Button("Analyze")
|
| 89 |
+
|
| 90 |
+
with gr.Column(visible=True) as output_col:
|
| 91 |
+
label = gr.Label(label = "Predicted Label")
|
| 92 |
+
local_plot = gr.Plot(label = 'Grade:')
|
| 93 |
+
|
| 94 |
+
submit_btn.click(
|
| 95 |
+
main_func,
|
| 96 |
+
[Gender, Age_at_diagnosis, IDH1, TP53, ATRX, PTEN, EGFR, CIC, MUC16, PIK3CA, NF1, PIK3R1, FUBP1, RB1, NOTCH1, BCOR, CSMD3, SMARCA4, GRIN2A, IDH2, FAT4, PDGFRA],
|
| 97 |
+
[label,local_plot], api_name="Glioma_Predictor"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
gr.Markdown("### Click on any of the examples below to see how it works:")
|
| 101 |
+
gr.Examples([["Male",24,"Yes","No","Yes","Yes","Yes","No","Yes","Yes","Yes","Yes","Yes","No","No","No","No","Yes","No","Yes","No","Yes"], ["Male",70,"No","No","No","No","No","No","No","No","No","Yes","No","Yes","No","No","No","No","No","No","No", "No"]], [Gender, Age_at_diagnosis, IDH1, TP53, ATRX, PTEN, EGFR, CIC, MUC16, PIK3CA, NF1, PIK3R1, FUBP1, RB1, NOTCH1, BCOR, CSMD3, SMARCA4, GRIN2A, IDH2, FAT4, PDGFRA], [label,local_plot], main_func, cache_examples=True)
|
| 102 |
+
|
| 103 |
+
demo.launch()
|