Spaces:
Build error
Build error
Upload 3 files
Browse files- XGB_softprob.pkl +3 -0
- app.py +110 -0
- requirements.txt +10 -0
XGB_softprob.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fe9d69ec9a220b0e64f12c0e9cd55663b0e92158069bca40a65547506106df81
|
3 |
+
size 626245
|
app.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import shap
|
2 |
+
from shap.plots._force_matplotlib import draw_additive_plot
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# load the model from disk
|
6 |
+
loaded_model = pickle.load(open("XGB_softprob.pkl", 'rb'))
|
7 |
+
|
8 |
+
# Setup SHAP
|
9 |
+
explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.
|
10 |
+
|
11 |
+
# Create the main function for server
|
12 |
+
# Create the main function for server
|
13 |
+
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):
|
14 |
+
|
15 |
+
new_row = pd.DataFrame.from_dict({'EAL_SCORE':EAL_SCORE,'SOVI_SCORE':SOVI_SCORE, 'RESL_SCORE':RESL_SCORE,
|
16 |
+
'DROUGHT_RISKS':DROUGHT_RISKS,'EARTHQUAKE_RISKS':EARTHQUAKE_RISKS,'HURICANE_RISKS':HURICANE_RISKS,
|
17 |
+
'TORNADO_RISKS':TORNADO_RISKS,'WILDFIRE_RISKS':WILDFIRE_RISKS,'WINTERWEATHER_RISKS':WINTERWEATHER_RISKS,
|
18 |
+
'SOCIAL':SOCIAL,'ECONOMY':ECONOMY,'HOUSING_INFRASTRUCTURE':HOUSING_INFRASTRUCTURE,
|
19 |
+
'COMMUNNITY_CAPITAL':COMMUNNITY_CAPITAL,'INSTITUTIONAL':INSTITUTIONAL,'ENVIRONMENT':ENVIRONMENT}, orient = 'index').transpose()
|
20 |
+
|
21 |
+
prob = loaded_model.predict_proba(new_row)
|
22 |
+
|
23 |
+
# compute SHAP values
|
24 |
+
explainer = shap.TreeExplainer(xgb_clf)
|
25 |
+
shap_values = explainer.shap_values(new_row,check_additivity=False)
|
26 |
+
# plot = shap.summary_plot(shap_values, new_row.values, plot_type="bar", class_names= class_names, feature_names = new_row.columns)
|
27 |
+
plot = shap.summary_plot(shap_values[1], new_row.values, feature_names = new_row.columns)
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
# plot = shap.waterfall_plot(shap.Explanation(values=shap_values[0][0],
|
32 |
+
# base_values=explainer.expected_value[0], data=X_test.iloc[0],
|
33 |
+
# feature_names=X_test.columns.tolist()))
|
34 |
+
|
35 |
+
# shap_values = explainer(new_row, check_additivity=False)
|
36 |
+
# plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
|
37 |
+
# plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
|
38 |
+
# plot = shap.waterfall_plot(explainer.base_values[0], shap_values[0][0], X[0])
|
39 |
+
# plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
|
40 |
+
|
41 |
+
plt.tight_layout()
|
42 |
+
local_plot = plt.gcf()
|
43 |
+
plt.close()
|
44 |
+
|
45 |
+
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
|
46 |
+
|
47 |
+
# Create the UI
|
48 |
+
title = "**Climate Risk Model** 🌍"
|
49 |
+
description1 = """
|
50 |
+
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.
|
51 |
+
"""
|
52 |
+
|
53 |
+
description2 = """
|
54 |
+
To use the app, click on one of the examples, or adjust the values of the six factors, and click on Analyze. ✨
|
55 |
+
"""
|
56 |
+
|
57 |
+
with gr.Blocks(title=title) as demo:
|
58 |
+
gr.Markdown(f"## {title}")
|
59 |
+
# gr.Markdown("""![marketing](types-of-employee-turnover.jpg)""")
|
60 |
+
gr.Markdown(description1)
|
61 |
+
gr.Markdown("""---""")
|
62 |
+
gr.Markdown(description2)
|
63 |
+
gr.Markdown("""---""")
|
64 |
+
with gr.Row():
|
65 |
+
with gr.Column():
|
66 |
+
EAL_SCORE = gr.Slider(label="EAL Score", minimum=0, maximum=100, value=20, step=5)
|
67 |
+
SOVI_SCORE = gr.Slider(label="SOVI Score", minimum=0, maximum=100, value=20, step=5)
|
68 |
+
RESL_SCORE = gr.Slider(label="RESILIENCE Score", minimum=0, maximum=100, value=20, step=5)
|
69 |
+
DROUGHT_RISKS = gr.Slider(label="DROUGHT Score", minimum=0, maximum=100, value=20, step=5)
|
70 |
+
EARTHQUAKE_RISKS = gr.Slider(label="EARTHQUAKE Score", minimum=0, maximum=100, value=20, step=5)
|
71 |
+
HURICANE_RISKS = gr.Slider(label="HURICANE Score", minimum=0, maximum=100, value=20, step=5)
|
72 |
+
TORNADO_RISKS = gr.Slider(label="TORNADO Score", minimum=0, maximum=100, value=20, step=5)
|
73 |
+
WILDFIRE_RISKS = gr.Slider(label="WILDFIRE Score", minimum=0, maximum=100, value=20, step=5)
|
74 |
+
WINTERWEATHER_RISKS = gr.Slider(label="WINTER WEATHER Score", minimum=0, maximum=100, value=20, step=5)
|
75 |
+
SOCIAL = gr.Slider(label="Social Score", minimum=0, maximum=1, value=.5, step=.1)
|
76 |
+
ECONOMY = gr.Slider(label="Economic Score", minimum=0, maximum=1, value=.5, step=.1)
|
77 |
+
HOUSING_INFRASTRUCTURE = gr.Slider(label="Housing Score", minimum=0, maximum=1, value=.5, step=.1)
|
78 |
+
COMMUNNITY_CAPITAL = gr.Slider(label="Community Capital Score", minimum=0, maximum=1, value=.5, step=.1)
|
79 |
+
INSTITUTIONAL = gr.Slider(label="Institutional Score", minimum=0, maximum=1, value=.5, step=.1)
|
80 |
+
ENVIRONMENT = gr.Slider(label="Environment Score", minimum=0, maximum=1, value=.5, step=.1)
|
81 |
+
submit_btn = gr.Button("Analyze")
|
82 |
+
with gr.Column(visible=True) as output_col:
|
83 |
+
label = gr.Label(label = "RISK RATING")
|
84 |
+
local_plot = gr.Plot(label = 'Shap:')
|
85 |
+
|
86 |
+
submit_btn.click(
|
87 |
+
main_func,
|
88 |
+
[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],
|
89 |
+
[label,local_plot], api_name="Climate Risk Model"
|
90 |
+
)
|
91 |
+
|
92 |
+
gr.Markdown("### Click on any of the examples below to see how it works:")
|
93 |
+
gr.Textbox(label="Kings County, New York")
|
94 |
+
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]],
|
95 |
+
[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],
|
96 |
+
[label,local_plot], main_func, cache_examples=True)
|
97 |
+
gr.Textbox(label="Miami-Dade County, Florida")
|
98 |
+
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]],
|
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="Washington County, Minnesota")
|
102 |
+
gr.Examples([[21.06, 15.37, 58.77, 2.32, .5035, 0, 11.98, 4.957, 6.808, .7231, .5359, .2884, .328, .407, .5015]],
|
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="Falls Church, Virginia")
|
106 |
+
gr.Examples([[6.947, 4.178, 56.86, 0, .1587, .5027, 1.071, 0, 0, .8181, .5221, .3878, .2463, .389, .3921]],
|
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 |
+
|
110 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.1.3
|
2 |
+
Pillow
|
3 |
+
yake
|
4 |
+
pandas
|
5 |
+
sklearn
|
6 |
+
shap
|
7 |
+
xgboost
|
8 |
+
matplotlib
|
9 |
+
numpy
|
10 |
+
streamlit
|