Spaces:
Runtime error
Runtime error
asharma567
commited on
Commit
•
1c7e53a
1
Parent(s):
0a2155d
Add application file
Browse files- app.py +90 -2
- df_M_character.joblib +0 -0
- df_M_character.pickle +0 -0
- explainer.joblib +0 -0
- explainer.pickle +0 -0
- leaderboard.csv +0 -0
- requirements.txt +5 -0
- shap_values.joblib +0 -0
- shap_values.pickle +0 -0
app.py
CHANGED
@@ -1,4 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
x = st.slider('Select a value')
|
4 |
-
st.write(x, 'squared is', x * x)
|
|
|
1 |
+
#create the explainer and shap values
|
2 |
+
|
3 |
+
import pandas as pd
|
4 |
+
import streamlit as st
|
5 |
+
from st_aggrid import AgGrid, GridOptionsBuilder
|
6 |
+
from st_aggrid.shared import GridUpdateMode
|
7 |
+
import shap
|
8 |
import streamlit as st
|
9 |
+
import streamlit.components.v1 as components
|
10 |
+
import pickle
|
11 |
+
import xgboost
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
def st_shap(plot, height=None):
|
17 |
+
shap_html = f"<head>{shap.getjs()}</head><body>{plot.html()}</body>"
|
18 |
+
components.html(shap_html, height=height)
|
19 |
+
|
20 |
+
|
21 |
+
def scale_and_standardize(df):
|
22 |
+
from sklearn.preprocessing import StandardScaler
|
23 |
+
scaler = StandardScaler()
|
24 |
+
scaler.fit(df)
|
25 |
+
scaled_df = scaler.transform(df)
|
26 |
+
return scaled_df
|
27 |
+
|
28 |
+
def aggrid_interactive_table(df: pd.DataFrame):
|
29 |
+
"""Creates an st-aggrid interactive table based on a dataframe.
|
30 |
+
|
31 |
+
Args:
|
32 |
+
df (pd.DataFrame]): Source dataframe
|
33 |
+
|
34 |
+
Returns:
|
35 |
+
dict: The selected row
|
36 |
+
"""
|
37 |
+
options = GridOptionsBuilder.from_dataframe(
|
38 |
+
df, enableRowGroup=True, enableValue=True, enablePivot=True
|
39 |
+
)
|
40 |
+
|
41 |
+
options.configure_side_bar()
|
42 |
+
|
43 |
+
options.configure_selection("single")
|
44 |
+
selection = AgGrid(
|
45 |
+
df,
|
46 |
+
enable_enterprise_modules=True,
|
47 |
+
gridOptions=options.build(),
|
48 |
+
theme="light",
|
49 |
+
update_mode=GridUpdateMode.MODEL_CHANGED,
|
50 |
+
allow_unsafe_jscode=True,
|
51 |
+
)
|
52 |
+
|
53 |
+
return selection
|
54 |
+
|
55 |
+
st.title("Boba Leaderboard")
|
56 |
+
|
57 |
+
#load joblib
|
58 |
+
df_M_character = pickle.load(open('df_M_character.pickle', 'rb'))
|
59 |
+
explainer = pickle.load(open('explainer.pickle', 'rb'))
|
60 |
+
shap_values = pickle.load(open('shap_values.pickle', 'rb'))
|
61 |
+
df_M_character_scale = scale_and_standardize(df_M_character)
|
62 |
+
author_idx_lookup = dict([(name, idx) for idx, name in enumerate(df_M_character.index)])
|
63 |
+
|
64 |
+
X = pd.DataFrame(
|
65 |
+
df_M_character_scale,
|
66 |
+
columns=df_M_character.columns)
|
67 |
+
|
68 |
+
|
69 |
+
df_leaderboard = pd.read_csv("leaderboard.csv")
|
70 |
+
df_leaderboard.rename(columns = {'Unnamed: 0':'Rank', 'author':'member'}, inplace=True)
|
71 |
+
selection = aggrid_interactive_table(df=df_leaderboard)
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
if selection:
|
77 |
+
try:
|
78 |
+
auth = selection["selected_rows"][0]['member']
|
79 |
+
except:
|
80 |
+
auth = df_leaderboard['member'].iloc[0]
|
81 |
+
|
82 |
+
st.write(auth)
|
83 |
+
author_idx = author_idx_lookup.get(auth)
|
84 |
+
# visualize the first prediction's explaination with default colors
|
85 |
+
st_shap(shap.force_plot(explainer.expected_value, shap_values[author_idx,:], X.iloc[author_idx,:]))
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
|
|
|
|
df_M_character.joblib
ADDED
Binary file (519 kB). View file
|
|
df_M_character.pickle
ADDED
Binary file (486 kB). View file
|
|
explainer.joblib
ADDED
Binary file (304 kB). View file
|
|
explainer.pickle
ADDED
Binary file (297 kB). View file
|
|
leaderboard.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.12.2
|
2 |
+
streamlit-aggrid==0.3.2
|
3 |
+
shap==0.41.0
|
4 |
+
pandas==1.4.3
|
5 |
+
xgboost==1.6.1
|
shap_values.joblib
ADDED
Binary file (227 kB). View file
|
|
shap_values.pickle
ADDED
Binary file (227 kB). View file
|
|