mdurst12 commited on
Commit
7fbeebd
·
1 Parent(s): c776bfe

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("XGB_softprob.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
+ # Create the main function for server
17
+ def main_func(EAL_SCORE,SOVI_SCORE,RESL_SCORE,DROUGHT_RISKS,EARTHQUAKE_RISKS,HURICANE_RISKS,TORNADO_RISKS,WILDFIRE_RISKS,WINTERWEATHER_RISKS,SOCIAL,ECONOMY,HOUSING_INFRASTRUCTURE,COMMUNNITY_CAPITAL,INSTITUTIONAL,ENVIRONMENT):
18
+
19
+ new_row = pd.DataFrame.from_dict({'EAL_SCORE':EAL_SCORE,'SOVI_SCORE':SOVI_SCORE, 'RESL_SCORE':RESL_SCORE,
20
+ 'DROUGHT_RISKS':DROUGHT_RISKS,'EARTHQUAKE_RISKS':EARTHQUAKE_RISKS,'HURICANE_RISKS':HURICANE_RISKS,
21
+ 'TORNADO_RISKS':TORNADO_RISKS,'WILDFIRE_RISKS':WILDFIRE_RISKS,'WINTERWEATHER_RISKS':WINTERWEATHER_RISKS,
22
+ 'SOCIAL':SOCIAL,'ECONOMY':ECONOMY,'HOUSING_INFRASTRUCTURE':HOUSING_INFRASTRUCTURE,
23
+ 'COMMUNNITY_CAPITAL':COMMUNNITY_CAPITAL,'INSTITUTIONAL':INSTITUTIONAL,'ENVIRONMENT':ENVIRONMENT}, orient = 'index').transpose()
24
+
25
+ prob = loaded_model.predict_proba(new_row)
26
+
27
+ # compute SHAP values
28
+ explainer = shap.TreeExplainer(xgb_clf)
29
+ shap_values = explainer.shap_values(new_row,check_additivity=False)
30
+ # plot = shap.summary_plot(shap_values, new_row.values, plot_type="bar", class_names= class_names, feature_names = new_row.columns)
31
+ plot = shap.summary_plot(shap_values[1], new_row.values, feature_names = new_row.columns)
32
+
33
+
34
+
35
+ # plot = shap.waterfall_plot(shap.Explanation(values=shap_values[0][0],
36
+ # base_values=explainer.expected_value[0], data=X_test.iloc[0],
37
+ # feature_names=X_test.columns.tolist()))
38
+
39
+ # shap_values = explainer(new_row, check_additivity=False)
40
+ # plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
41
+ # plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
42
+ # plot = shap.waterfall_plot(explainer.base_values[0], shap_values[0][0], X[0])
43
+ # plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
44
+
45
+ plt.tight_layout()
46
+ local_plot = plt.gcf()
47
+ plt.close()
48
+
49
+ return {"Very Low Risk": float(prob[0][0]), "Moderately Low Risk": float(prob[0][1]), "Moderate Risk": float(prob[0][2]), "Moderately High Risk": float(prob[0][3]), "Very High Risk": float(prob[0][4])}, local_plot
50
+
51
+ # Create the UI
52
+ title = "**Climate Risk Model** 🌍"
53
+ description1 = """
54
+ The BRIC index considers six broad categories of community disaster resilience: social, economic, community capital, institutional, infrastructural, and environmental at the county level. Used as an initial baseline for monitoring existing attributes of resilience to natural hazards, BRIC can be used to compare places to one another, to determine the specific drivers of resilience for counties, and to monitor improvements in resilience over time.
55
+ """
56
+
57
+ description2 = """
58
+ To use the app, click on one of the examples, or adjust the values of the six factors, and click on Analyze. ✨
59
+ """
60
+
61
+ with gr.Blocks(title=title) as demo:
62
+ gr.Markdown(f"## {title}")
63
+ # gr.Markdown("""![marketing](types-of-employee-turnover.jpg)""")
64
+ gr.Markdown(description1)
65
+ gr.Markdown("""---""")
66
+ gr.Markdown(description2)
67
+ gr.Markdown("""---""")
68
+ with gr.Row():
69
+ with gr.Column():
70
+ EAL_SCORE = gr.Slider(label="EAL Score", minimum=0, maximum=100, value=20, step=5)
71
+ SOVI_SCORE = gr.Slider(label="SOVI Score", minimum=0, maximum=100, value=20, step=5)
72
+ RESL_SCORE = gr.Slider(label="RESILIENCE Score", minimum=0, maximum=100, value=20, step=5)
73
+ DROUGHT_RISKS = gr.Slider(label="DROUGHT Score", minimum=0, maximum=100, value=20, step=5)
74
+ EARTHQUAKE_RISKS = gr.Slider(label="EARTHQUAKE Score", minimum=0, maximum=100, value=20, step=5)
75
+ HURICANE_RISKS = gr.Slider(label="HURICANE Score", minimum=0, maximum=100, value=20, step=5)
76
+ TORNADO_RISKS = gr.Slider(label="TORNADO Score", minimum=0, maximum=100, value=20, step=5)
77
+ WILDFIRE_RISKS = gr.Slider(label="WILDFIRE Score", minimum=0, maximum=100, value=20, step=5)
78
+ WINTERWEATHER_RISKS = gr.Slider(label="WINTER WEATHER Score", minimum=0, maximum=100, value=20, step=5)
79
+ SOCIAL = gr.Slider(label="Social Score", minimum=0, maximum=1, value=.5, step=.1)
80
+ ECONOMY = gr.Slider(label="Economic Score", minimum=0, maximum=1, value=.5, step=.1)
81
+ HOUSING_INFRASTRUCTURE = gr.Slider(label="Housing Score", minimum=0, maximum=1, value=.5, step=.1)
82
+ COMMUNNITY_CAPITAL = gr.Slider(label="Community Capital Score", minimum=0, maximum=1, value=.5, step=.1)
83
+ INSTITUTIONAL = gr.Slider(label="Institutional Score", minimum=0, maximum=1, value=.5, step=.1)
84
+ ENVIRONMENT = gr.Slider(label="Environment Score", minimum=0, maximum=1, value=.5, step=.1)
85
+ submit_btn = gr.Button("Analyze")
86
+ with gr.Column(visible=True) as output_col:
87
+ label = gr.Label(label = "RISK RATING")
88
+ local_plot = gr.Plot(label = 'Shap:')
89
+
90
+ submit_btn.click(
91
+ main_func,
92
+ [EAL_SCORE,SOVI_SCORE,RESL_SCORE,DROUGHT_RISKS,EARTHQUAKE_RISKS,HURICANE_RISKS,TORNADO_RISKS,WILDFIRE_RISKS,WINTERWEATHER_RISKS,SOCIAL,ECONOMY,HOUSING_INFRASTRUCTURE,COMMUNNITY_CAPITAL,INSTITUTIONAL,ENVIRONMENT],
93
+ [label,local_plot], api_name="Climate Risk Model"
94
+ )
95
+
96
+ gr.Markdown("### Click on any of the examples below to see how it works:")
97
+ gr.Textbox(label="Kings County, New York")
98
+ gr.Examples([[32.03, 68.86, 50.5, 50.5, .1, 30, 19.41, 57.47, 1.024, 45.16, .5441, .4393, .4095, .2630, .3799, .3773]],
99
+ [EAL_SCORE,SOVI_SCORE,RESL_SCORE,DROUGHT_RISKS,EARTHQUAKE_RISKS,HURICANE_RISKS,TORNADO_RISKS,WILDFIRE_RISKS,WINTERWEATHER_RISKS,SOCIAL,ECONOMY,HOUSING_INFRASTRUCTURE,COMMUNNITY_CAPITAL,INSTITUTIONAL,ENVIRONMENT],
100
+ [label,local_plot], main_func, cache_examples=True)
101
+ gr.Textbox(label="Miami-Dade County, Florida")
102
+ gr.Examples([[46.24, 63.85, 53.39, 31.27, 6.771, 58.93, 76.76, 54.54, 0, .564, .4703, .3068, .2161, .3623, .6264]],
103
+ [EAL_SCORE,SOVI_SCORE,RESL_SCORE,DROUGHT_RISKS,EARTHQUAKE_RISKS,HURICANE_RISKS,TORNADO_RISKS,WILDFIRE_RISKS,WINTERWEATHER_RISKS,SOCIAL,ECONOMY,HOUSING_INFRASTRUCTURE,COMMUNNITY_CAPITAL,INSTITUTIONAL,ENVIRONMENT],
104
+ [label,local_plot], main_func, cache_examples=True)
105
+ gr.Textbox(label="Washington County, Minnesota")
106
+ gr.Examples([[21.06, 15.37, 58.77, 2.32, .5035, 0, 11.98, 4.957, 6.808, .7231, .5359, .2884, .328, .407, .5015]],
107
+ [EAL_SCORE,SOVI_SCORE,RESL_SCORE,DROUGHT_RISKS,EARTHQUAKE_RISKS,HURICANE_RISKS,TORNADO_RISKS,WILDFIRE_RISKS,WINTERWEATHER_RISKS,SOCIAL,ECONOMY,HOUSING_INFRASTRUCTURE,COMMUNNITY_CAPITAL,INSTITUTIONAL,ENVIRONMENT],
108
+ [label,local_plot], main_func, cache_examples=True)
109
+ gr.Textbox(label="Falls Church, Virginia")
110
+ gr.Examples([[6.947, 4.178, 56.86, 0, .1587, .5027, 1.071, 0, 0, .8181, .5221, .3878, .2463, .389, .3921]],
111
+ [EAL_SCORE,SOVI_SCORE,RESL_SCORE,DROUGHT_RISKS,EARTHQUAKE_RISKS,HURICANE_RISKS,TORNADO_RISKS,WILDFIRE_RISKS,WINTERWEATHER_RISKS,SOCIAL,ECONOMY,HOUSING_INFRASTRUCTURE,COMMUNNITY_CAPITAL,INSTITUTIONAL,ENVIRONMENT],
112
+ [label,local_plot], main_func, cache_examples=True)
113
+
114
+ demo.launch(share=True)