Group_3 / app.py
jjp7um's picture
Update app.py
8411314 verified
raw
history blame
3.67 kB
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, 2nd_Sem_Grades, Previous_Qualification_Grade, 1st_Sem_Grades, Course, 2nd_Sem_Units_Approved, Age_at_Enrollment):
new_row = pd.DataFrame.from_dict({'Target':Target,'Admission_Grade':Admission_Grade,
'2nd_Sem_Grades':2nd_Sem_Grades,'Previous_Qualification_Grade':Previous_Qualification_Grade,'1st_Sem_Grades':1st_Sem_Grades,
'Course':Course,'2nd_Sem_Units_Approved':2nd_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 info 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, 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():
with gr.Column():
Target = gr.Number(label="Target Score", value=40)
Admission_Grade = gr.Slider(label="AdmissionGrade Score", minimum=0, maximum=1, value=1, step=1)
2nd_Sem_Grades = gr.Slider(label="PreviousQualificationGrade Score", minimum=1, maximum=5, value=4, step=1)
Previous_Qualification_Grade = gr.Slider(label="CurricularUnits1stSemGrade Score", minimum=1, maximum=5, value=4, step=1)
1st_Sem_Grades = gr.Slider(label="Course Score", minimum=1, maximum=5, value=4, step=1)
Course = gr.Slider(label="Course", minimum=1, maximum=5, value=4, step=1)
2nd_Sem_Units_Approved = gr.Slider(label="2nd_Sem_Units_Approved", minimum=1, maximum=5, value=4, step=1)
Age_at_Enrollment = gr.Slider(label="AgeAtEnrollment Score", 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,
[Target, Admission_Grade, 2nd_Sem_Grades, Previous_Qualification_Grade, 1st_Sem_Grades, Course, 2nd_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([['Graduate',119.6,13.000000,122.0,9773,5,18], [Target, Admission_Grade, 2nd_Sem_Grades, Previous_Qualification_Grade, 1st_Sem_Grades, Course, 2nd_Sem_Units_Approved, Age_at_Enrollment]
, [label,local_plot], main_func, cache_examples=True)
demo.launch()