import pickle import pandas as pd import shap from shap.plots._force_matplotlib import draw_additive_plot import gradio as gr import numpy as np import matplotlib.pyplot as plt # load the model from disk loaded_model = pickle.load(open("glioma_xgb.pkl", 'rb')) # Setup SHAP explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS. # Create the main function for server 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): new_row = pd.DataFrame.from_dict({'Gender':Gender, 'Age_at_diagnosis':Age_at_diagnosis,'IDH1':IDH1,'TP53':TP53, 'ATRX':ATRX, 'PTEN':PTEN,'EGFR':EGFR,'CIC':CIC, 'MUC16':MUC16,'PIK3CA':PIK3CA,'NF1':NF1,'PIK3R1':PIK3R1, 'FUBP1': FUBP1, 'RB1': RB1, 'NOTCH1': NOTCH1, 'BCOR': BCOR, 'CSMD3': CSMD3, 'SMARCA4': SMARCA4, 'GRIN2A': GRIN2A, 'IDH2': IDH2, 'FAT4': FAT4, 'PDGFRA': PDGFRA}, orient = 'index').transpose() prob = loaded_model.predict_proba(new_row) shap_values = explainer(new_row) # plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False) # plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False) plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False) plt.tight_layout() local_plot = plt.gcf() plt.close() return {"Chance of Having GBM Tumor": 1-float(prob[0][0]), "Chance of Having LGG Tumor": float(prob[0][0])}, local_plot # Create the UI title = "**Glioma Predictor & Interpreter** 🪐" description1 = """This app takes info from subjects and predicts the severity of their brain tumor (LGG or GBM). Do not use for medical diagnosis.""" description2 = """ To use the app, click on one of the examples, or adjust the values of the factors, and click on Analyze. 🤞 """ with gr.Blocks(title=title) as demo: gr.Markdown(f"## {title}") gr.Markdown(description1) gr.Markdown("""---""") gr.Markdown(description2) gr.Markdown("""---""") with gr.Row(): Gender = gr.Radio(["Female", "Male"], label="Gender", type = "index") Age_at_diagnosis = gr.Number(label="Age at Diagnosis") with gr.Row(): IDH1 = gr.Radio(["No", "Yes"], label="IDH1 Mutation", type="index") TP53 = gr.Radio(["No", "Yes"], label="TP53 Mutation", type="index") ATRX = gr.Radio(["No", "Yes"], label="ATRX Mutation", type="index") with gr.Row(): PTEN = gr.Radio(["No", "Yes"], label="PTEN Mutation", type="index") EGFR = gr.Radio(["No", "Yes"], label="EGFR Mutation", type="index") CIC = gr.Radio(["No", "Yes"], label="CIC Mutation", type="index") with gr.Row(): MUC16 = gr.Radio(["No", "Yes"], label="MUC16 Mutation", type="index") PIK3CA = gr.Radio(["No", "Yes"], label="PIK3CA Mutation", type="index") NF1 = gr.Radio(["No", "Yes"], label="NF1 Mutation", type="index") with gr.Row(): PIK3R1 = gr.Radio(["No", "Yes"], label="PIK3R1 Mutation", type="index") FUBP1 = gr.Radio(["No", "Yes"], label="FUBP1 Mutation", type="index") RB1 = gr.Radio(["No", "Yes"], label="RB1 Mutation", type="index") with gr.Row(): NOTCH1 = gr.Radio(["No", "Yes"], label="NOTCH1 Mutation", type="index") BCOR = gr.Radio(["No", "Yes"], label="BCOR Mutation", type="index") CSMD3 = gr.Radio(["No", "Yes"], label="CSMD3 Mutation", type="index") with gr.Row(): SMARCA4 = gr.Radio(["No", "Yes"], label="SMAECA4 Mutation", type="index") GRIN2A = gr.Radio(["No", "Yes"], label="GRIN2A Mutation", type="index") IDH2 = gr.Radio(["No", "Yes"], label="IDH2 Mutation", type="index") FAT4 = gr.Radio(["No", "Yes"], label="FAT4 Mutation", type="index") PDGFRA = gr.Radio(["No", "Yes"], label="PDGFRA Mutation", type="index") submit_btn = gr.Button("Analyze") with gr.Column(visible=True) as output_col: label = gr.Label(label = "Predicted Label") local_plot = gr.Plot(label = 'Grade:') submit_btn.click( 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], [label,local_plot], api_name="Glioma_Predictor" ) gr.Markdown("### Click on any of the examples below to see how it works:") 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) demo.launch()