Beladiaamy commited on
Commit
86508a7
·
verified ·
1 Parent(s): f30b579

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -1
app.py CHANGED
@@ -1,3 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  with gr.Blocks(title=title) as demo:
2
  gr.Markdown(f"## {title}")
3
  gr.Markdown(description1)
@@ -50,4 +90,4 @@ with gr.Blocks(title=title) as demo:
50
  gr.Markdown("### Click on any of the examples below to see how it works:")
51
  gr.Examples([[0,0,1,0,22,0,0,0,1,1,1,0,0,1,3,25,23,1,1,21,5,3], [1,1,1,1,30,1,1,1,0,0,0,1,1,0,2,20,23,0,0,21,3,2]], [HighBP, HighChol, CholCheck, BMI, Smoker, Stroke, HeartDiseaseorAttack, PhysActivity, Fruits, Veggies, HvyAlcoholConsump, AnyHealthcare, NoDocbcCost, GenHlth, MentHlth, PhysHlth, DiffWalk, Sex, Age, Education, Income], [label,local_plot], main_func, cache_examples=True)
52
 
53
- demo.launch()
 
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("db_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(HighBP, HighChol, CholCheck, BMI, Smoker, Stroke, HeartDiseaseorAttack, PhysActivity, Fruits, Veggies, HvyAlcoholConsump, AnyHealthcare, NoDocbcCost, GenHlth, MentHlth, PhysHlth, DiffWalk, Sex, Age, Education, Income):
17
+ new_row = pd.DataFrame.from_dict({'HighBP': HighBP, 'HighChol': HighChol, 'CholCheck': CholCheck, 'BMI': BMI, 'Smoker': Smoker, 'Stroke': Stroke, 'HeartDiseaseorAttack': HeartDiseaseorAttack, 'PhysActivity':PhysActivity, 'Fruits':Fruits, 'Veggies':Veggies, 'HvyAlcoholConsump': HvyAlcoholConsump, 'AnyHealthcare': AnyHealthcare, 'NoDocbcCost': NoDocbcCost, 'GenHlth': GenHlth, 'MentHlth': MentHlth, 'PhysHlth': PhysHlth, 'DiffWalk': DiffWalk, 'Sex': Sex, 'Age': Age, 'Education': Education, 'Income': Income},
18
+ orient = 'index').transpose()
19
+
20
+ prob = loaded_model.predict_proba(new_row)
21
+
22
+ shap_values = explainer(new_row)
23
+ # plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
24
+ # plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
25
+ plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
26
+
27
+ plt.tight_layout()
28
+ local_plot = plt.gcf()
29
+ plt.close()
30
+
31
+ return {"Low Chance": float(prob[0][0]), "High Chance": 1-float(prob[0][0])}, local_plot
32
+
33
+ # Create the UI
34
+ title = "**Diabetes Predictor & Interpreter** 🪐"
35
+ description1 = """This app takes info from subjects and predicts their diabetes likelihood. Do not use for medical diagnosis."""
36
+
37
+ description2 = """
38
+ To use the app, click on one of the examples, or adjust the values of the factors, and click on Analyze. 🤞
39
+ """
40
+
41
  with gr.Blocks(title=title) as demo:
42
  gr.Markdown(f"## {title}")
43
  gr.Markdown(description1)
 
90
  gr.Markdown("### Click on any of the examples below to see how it works:")
91
  gr.Examples([[0,0,1,0,22,0,0,0,1,1,1,0,0,1,3,25,23,1,1,21,5,3], [1,1,1,1,30,1,1,1,0,0,0,1,1,0,2,20,23,0,0,21,3,2]], [HighBP, HighChol, CholCheck, BMI, Smoker, Stroke, HeartDiseaseorAttack, PhysActivity, Fruits, Veggies, HvyAlcoholConsump, AnyHealthcare, NoDocbcCost, GenHlth, MentHlth, PhysHlth, DiffWalk, Sex, Age, Education, Income], [label,local_plot], main_func, cache_examples=True)
92
 
93
+ demo.launch()