File size: 3,791 Bytes
ba94ba9
 
 
 
 
 
 
 
 
6ff7b35
ba94ba9
 
 
 
 
f53a546
 
5f0c0bc
 
f715f17
ba94ba9
 
 
 
 
 
 
 
 
 
 
 
f715f17
ba94ba9
 
f715f17
c57ee82
ba94ba9
 
c57ee82
ba94ba9
bf76ebb
85c9073
e1d9799
 
 
 
 
 
 
6ed3478
e1d9799
 
 
 
 
 
 
ba94ba9
e1d9799
 
 
6b72d7d
ba94ba9
 
f53a546
f715f17
ba94ba9
 
 
85c9073
8411314
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
73
74
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(Admission_Grade, Second_Sem_Grades, Previous_Qualification_Grade, First_Sem_Grades, Course, Second_Sem_Units_Approved, Age_at_Enrollment):
    new_row = pd.DataFrame.from_dict({'Admission_Grade':Admission_Grade,
              'Second_Sem_Grades':Second_Sem_Grades,'Previous_Qualification_Grade':Previous_Qualification_Grade,'First_Sem_Grades':First_Sem_Grades,
              'Course':Course,'Second_Sem_Units_Approved':Second_Sem_Units_Approved,'Age_at_Enrollment':Age_at_Enrollment},
                                      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 {"Dropout": float(prob[0][0]), "Graduate": 1-float(prob[0][0])}, local_plot

# Create the UI
title = "**Student Graduation Predictor & Interpreter** 🪐"
description1 = """This app takes information from subjects and predicts their graduation likelihood."""

description2 = """
To use the app, click on one of the examples or adjust the values of the factors, then click 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():
        with gr.Column():
            Admission_Grade = gr.Slider(label="Admission Grade", minimum=0, maximum=200, value=100, step=1)
            Age_at_Enrollment = gr.Slider(label="Age at Enrollment", minimum=10, maximum=80, value=40, step=1)
            Previous_Qualification_Grade = gr.Slider(label="Previous Qualification Grade", minimum=0, maximum=200, value=100, step=1)
            First_Sem_Grades = gr.Slider(label="First Semester Grade", minimum=0, maximum=20, value=10, step=1)
            Second_Sem_Grades = gr.Slider(label="Second Semester Grade", minimum=0, maximum=20, value=10, step=1)   
            Course = gr.Dropdown(label="Select a Course:", choices=[33,171,8014,9003,9070,9085,9119,9130,9147,9238,9254,9500,9556,9670,9773,9853,9991], value=33)
            Second_Sem_Units_Approved = gr.Slider(label="Second Semester Units Approved", minimum=0, maximum=20, value=10, 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, Second_Sem_Grades, Previous_Qualification_Grade, First_Sem_Grades, Course, Second_Sem_Units_Approved, Age_at_Enrollment],
        [label,local_plot], api_name="Graduation_Predictor"
    )
    
    gr.Markdown("### Click on any of the examples below to see how it works:")
    gr.Examples([[119,13,122,12,8014,0,18],[100,20,90,50,33,2,20], [150,15,102,46,171,8,25]], [Admission_Grade, Second_Sem_Grades, Previous_Qualification_Grade, First_Sem_Grades, Course, Second_Sem_Units_Approved, Age_at_Enrollment]
                 , [label,local_plot], main_func, cache_examples=True)

demo.launch()