File size: 3,723 Bytes
ba94ba9
 
 
 
 
 
 
 
 
6ff7b35
ba94ba9
 
 
 
 
a5dc65c
4f67e80
 
50ea7ad
ba94ba9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f67e80
 
 
 
 
 
ca41539
ba94ba9
 
 
 
 
 
 
 
 
a5dc65c
4f67e80
ba94ba9
 
 
a5dc65c
ba94ba9
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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("classroom_xgb.pkl", 'rb'))

# Setup SHAP
explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.

# Create the main function for server
def main_func(Target, Admission_grade, Curricular_units_2nd_sem_grade, Previous_qualifications, Curricular_units_1st_sem_grade, Course, Curricular_units_2nd_sem_approved, Age_at_enrollment):
    new_row = pd.DataFrame.from_dict({'Target':Target, 'Admission grade':Admission_grade,'Curricular units 2nd sem (grade)':Curricular_units_2nd_sem_grade,
              'Previous qualifications':Previous_qualifications,'Curricular units 1st sem (grade)':Curricular_units_1st_sem_grade, 'Course':Course,'Curricular units 2nd sem (approved)':Curricular_units_2nd_sem_approved,
              'Age at enrollment':Age_at_enrollmentenrollment}).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 {"Low Chance": float(prob[0][0]), "High Chance": 1-float(prob[0][0])}, local_plot

# Create the UI
title = "**Heart Attack Predictor & Interpreter** πŸͺ"
description1 = """This app takes info from subjects and predicts their heart attack likelihood. 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("""---""")
    
    Curricular_units_2nd_sem_grade = gr.Number(label="Curricular units 2nd sem (grade)", value=40)
    Admission_grade = gr.Slider(label="Admission grade", minimum=0, maximum=1, value=1, step=1)
    Previous_qualifications = gr.Slider(label="Previous qualifications", minimum=1, maximum=5, value=4, step=1)
    Curricular_units_1st_sem_grade = gr.Slider(label="Curricular units 1st sem (grade)", minimum=1, maximum=5, value=4, step=1) 
    Course = gr.Slider(label="Course", minimum=1, maximum=5, value=4, step=1) 
    Curricular_units_2nd_sem_approved = gr.Slider(label="Curricular units 2nd sem (approved)", minimum=1, maximum=5, value=4, step=1)
    Age_at_enrollment = gr.Slider(label="Age_at_enrollment", minimum=1, maximum=5, value=4, step=1)
    
    submit_btn = gr.Button("Analyze")

    with gr.Column(visible=True) as output_col:
        label = gr.Label(label = "Predicted Label")
        local_plot = gr.Plot(label = 'Shap:')

    submit_btn.click(
        main_func,
        [Admission_grade, Curricular_units_2nd_sem_grade, Previous_qualifications, Curricular_units_1st_sem_grade, Course, Curricular_units_2nd_sem_approved, Age_at_enrollment],
        [label,local_plot], api_name="Dropout_Predictor"
    )
    
    gr.Markdown("### Click on any of the examples below to see how it works:")
    gr.Examples([[24,0,4,4,5,5,4,4,5,5,1,2,3], [24,0,4,4,5,3,3,2,1,1,1,2,3]], [Admission_grade, Curricular_units_2nd_sem_grade, Previous_qualifications, Curricular_units_1st_sem_grade, Course, Curricular_units_2nd_sem_approved, Age_at_enrollment], [label,local_plot], main_func, cache_examples=True)

demo.launch()