Beladiaamy commited on
Commit
e5ccee7
·
verified ·
1 Parent(s): cbecc6c

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -79
app.py DELETED
@@ -1,79 +0,0 @@
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("Disease_DAndRFTrees.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, BMI, Smoker, PhysActivity, Fruits, Veggies, HvyAlcoholConsump, GenHlth, PhysHlth, Sex, Age):
17
- new_row = pd.DataFrame.from_dict({'BP':highBP,'Chol':HighChol,
18
- 'BMI':BMI,'Smoker':Smoker,'PhysActivity':PhysActivity,
19
- 'Fruits':Fruits, 'Veggies':Veggies,'HvyAlcoholConsump': HvyAlcoholConsump,'GenHlth':GenHlth,
20
- 'PhysHlth':PhysHlth,'Sex':Sex,'Age':Age},
21
- orient = 'index').transpose()
22
-
23
- prob = loaded_model.predict_proba(new_row)
24
-
25
- shap_values = explainer(new_row)
26
- # plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
27
- # plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
28
- plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
29
-
30
- plt.tight_layout()
31
- local_plot = plt.gcf()
32
- plt.close()
33
-
34
- return {"Low Chance": float(prob[0][0]), "High Chance": 1-float(prob[0][0])}, local_plot
35
-
36
- # Create the UI
37
- title = "**Diabetes Predictor & Interpreter** 🪐"
38
- description1 = """This app takes info from subjects and predicts their diabetes likelihood. Do not use for medical diagnosis."""
39
-
40
- description2 = """
41
- To use the app, click on one of the examples, or adjust the values of the factors, and click on Analyze. 🤞
42
- """
43
-
44
- with gr.Blocks(title=title) as demo:
45
- gr.Markdown(f"## {title}")
46
- gr.Markdown(description1)
47
- gr.Markdown("""---""")
48
- gr.Markdown(description2)
49
- gr.Markdown("""---""")
50
-
51
- Age = gr.Number(label="age Score", value=40)
52
- BMI = gr.Number(label="BMI Score", value=98)
53
- Sex = gr.Slider(label="sex Score", minimum=0, maximum=1, value=1, step=1)
54
- Smoker = gr.Slider(label="Smoker Score", minimum=0, maximum=1, value=1, step=1)
55
- PhysActivity = gr.Slider(label="Physical Activity Score", minimum=0, maximum=1, value=1, step=1)
56
- Fruits = gr.Slider(label="Fruits Score", minimum=0, maximum=1, value=1, step=1)
57
- Veggies = gr.Slider(label="Veggies Score", minimum=0, maximum=1, value=1, step=1)
58
- HvyAlcoholConsump = gr.Slider(label="Alcohol Consumption Score", minimum=0, maximum=1, value=1, step=1)
59
- HighBP = gr.Slider(label="BP Score", minimum=1, maximum=1, value=1, step=1)
60
- HighChol = gr.Slider(label="Cholesterol Score", minimum=1, maximum=1, value=1, step=1)
61
- GenHlth = gr.Slider(label="GenHlth Score", minimum=1, maximum=5, value=4, step=1)
62
- PhysHealth = gr.Number(label="PhysHealth Score", value=40)
63
-
64
- submit_btn = gr.Button("Analyze")
65
-
66
- with gr.Column(visible=True) as output_col:
67
- label = gr.Label(label = "Predicted Label")
68
- local_plot = gr.Plot(label = 'Shap:')
69
-
70
- submit_btn.click(
71
- main_func,
72
- [HighBP, HighChol, BMI, Smoker, PhysActivity, Fruits, Veggies, HvyAlcoholConsump, GenHlth, PhysHlth, Sex, Age],
73
- [label,local_plot], api_name="Diabetes_Predictor"
74
- )
75
-
76
- gr.Markdown("### Click on any of the examples below to see how it works:")
77
- 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]], [HighBP, HighChol, BMI, Smoker, PhysActivity, Fruits, Veggies, HvyAlcoholConsump, GenHlth, PhysHlth, Sex, Age], [label,local_plot], main_func, cache_examples=True)
78
-
79
- demo.launch()