Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanDomino committed Nov 18, 2024
1 parent e0ac030 commit 90a2d1c
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .Trash-12574/info/Untitled Folder.trashinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[Trash Info]
Path=Untitled%20Folder
DeletionDate=2024-11-18T16:45:58
35 changes: 15 additions & 20 deletions .ipynb_checkpoints/app-checkpoint.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
#!/usr/bin/env bash

# This is a bash script for Domino's App publishing feature
# Learn more at http://support.dominodatalab.com/hc/en-us/articles/209150326

## R/Shiny Example
## This is an example of the code you would need in this bash script for a R/Shiny app
R -e 'shiny::runApp("./apps/wine.R", port=8888, host="0.0.0.0")'

## Flask example
## This is an example of the code you would need in this bash script for a Python/Flask app
#export LC_ALL=C.UTF-8
#export LANG=C.UTF-8
#export FLASK_APP=app-flask.py
#export FLASK_DEBUG=1
#python -m flask run --host=0.0.0.0 --port=8888

## Dash Example
## This is an example of the code you would need in this bash script for a Dash app
#python app-dash.py
mkdir ~/.streamlit
echo "[browser]" > ~/.streamlit/config.toml
echo "gatherUsageStats = true" >> ~/.streamlit/config.toml
echo "serverAddress = \"0.0.0.0\"" >> ~/.streamlit/config.toml
echo "serverPort = 8501" >> ~/.streamlit/config.toml
echo "[server]" >> ~/.streamlit/config.toml
echo "port = 8501" >> ~/.streamlit/config.toml
echo "enableCORS = false" >> ~/.streamlit/config.toml
echo "enableXsrfProtection = false" >> ~/.streamlit/config.toml

streamlit run apps/streamlit_app.py

# python app/rai.py

# R -e 'shiny::runApp("/mnt/scripts/shiny_app.R", port=8888, host="0.0.0.0")'
35 changes: 15 additions & 20 deletions app.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
#!/usr/bin/env bash

# This is a bash script for Domino's App publishing feature
# Learn more at http://support.dominodatalab.com/hc/en-us/articles/209150326

## R/Shiny Example
## This is an example of the code you would need in this bash script for a R/Shiny app
R -e 'shiny::runApp("./apps/lead_gen.R", port=8888, host="0.0.0.0")'

## Flask example
## This is an example of the code you would need in this bash script for a Python/Flask app
#export LC_ALL=C.UTF-8
#export LANG=C.UTF-8
#export FLASK_APP=app-flask.py
#export FLASK_DEBUG=1
#python -m flask run --host=0.0.0.0 --port=8888

## Dash Example
## This is an example of the code you would need in this bash script for a Dash app
#python app-dash.py
mkdir ~/.streamlit
echo "[browser]" > ~/.streamlit/config.toml
echo "gatherUsageStats = true" >> ~/.streamlit/config.toml
echo "serverAddress = \"0.0.0.0\"" >> ~/.streamlit/config.toml
echo "serverPort = 8501" >> ~/.streamlit/config.toml
echo "[server]" >> ~/.streamlit/config.toml
echo "port = 8501" >> ~/.streamlit/config.toml
echo "enableCORS = false" >> ~/.streamlit/config.toml
echo "enableXsrfProtection = false" >> ~/.streamlit/config.toml

streamlit run apps/streamlit_app.py

# python app/rai.py

# R -e 'shiny::runApp("/mnt/scripts/shiny_app.R", port=8888, host="0.0.0.0")'
157 changes: 157 additions & 0 deletions apps/.ipynb_checkpoints/streamlit_app-checkpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
###########################################################################################################
###########################################################################################################
###########################################################################################################
###########################################################################################################
###########################################################################################################
###########################################################################################################

import streamlit as st
from streamlit import components
import numpy as np
import pandas as pd
import pickle
import time
from matplotlib import pyplot as plt
from matplotlib.ticker import FuncFormatter
import seaborn as sns
import requests
import eli5
import xgboost as xgb
import seaborn as sns
import os

xgc = xgb.Booster(model_file="/mnt/code/models/xgb_clf.xgb")


st.set_page_config(layout="wide")



####################
### INTRODUCTION ###
####################

row0_spacer1, row0_1, row0_spacer2, row0_2, row0_spacer3 = st.columns((.1, 2.3, .1, 1.3, .1))
with row0_1:
st.title('Credit Card Default Dashboard')
with row0_2:
st.text("")
row3_spacer1, row3_1, row3_spacer2 = st.columns((.1, 3.2, .1))
with row3_1:
st.markdown("")

#################
### SELECTION ###
#################


st.sidebar.text('')
st.sidebar.text('')
st.sidebar.text('')

### SEASON RANGE ###
st.sidebar.subheader("**Enter the application inputs to view the default scores.**")
st.sidebar.subheader("")
with st.sidebar.form("my_form"):
PAY_0 = st.number_input('Repayment status last month', min_value = -1, max_value = 9)
PAY_2 = st.number_input('Repayment status 2 months ago', min_value = -1, max_value = 9)
PAY_3 = st.number_input('Repayment status 3 months ago', min_value = -1, max_value = 9)
PAY_4 = st.number_input('Repayment status 4 months ago', min_value = -1, max_value = 9)
LIMIT_BAL = st.number_input('Credit Limit', min_value=0)
BILL_AMT1 = st.number_input('Current Balance', min_value=0)
AGE = st.number_input('Insert applicant age', min_value = 20, max_value = 115)
scored = st.form_submit_button("Score")

# baseline = '/domino/datasets/local/CreditCard-Approval/data/train_data_0.csv'
# df_cr = pd.read_csv(baseline)

# age_min, age_max = 21, 115
# age_std = (age - age_min) / (age_max - age_min)

column_names = ["PAY_0", "PAY_2", "PAY_3", "PAY_4", "LIMIT_BAL", "BILL_AMT1"]

# column_names_all = ['PAY_0', 'PAY_2', 'PAY_4', 'LIMIT_BAL', 'PAY_3', 'BILL_AMT1']

# Set some default values
sample_data = [[-1, 2, 0, 12000, 0, 2682]]

df_all = pd.DataFrame(sample_data, columns=column_names)

# Override the values with what was passed to the scoring function
df_all[["PAY_0"]] = PAY_0
df_all[["PAY_2"]] = PAY_2
df_all[["PAY_3"]] = PAY_3
df_all[["PAY_4"]] = PAY_4
df_all[["LIMIT_BAL"]] = LIMIT_BAL
df_all[["BILL_AMT1"]] = BILL_AMT1

for col in ["PAY_0", "PAY_2", "PAY_3", "PAY_4", "LIMIT_BAL", "BILL_AMT1"]:
df_all[col] = df_all[col].astype('int')

df = pd.DataFrame(columns=column_names,
data=[[PAY_0, PAY_2, PAY_3, PAY_4, LIMIT_BAL, BILL_AMT1]])

setup_dict = {}
scoring_request = {}
results = list()

for n in range(df.shape[0]):
for i in list(df.columns):
setup_dict.update({i :list(df[n:n+1].to_dict().get(i).values())[0]})
scoring_request = {'data' : setup_dict}

response = requests.post(os.environ['API_URL'],
auth=(
os.environ['API_PASSWORD'],
os.environ['API_PASSWORD']
),
json=scoring_request
)
results.append(response.json().get('result'))


### Results ###

probability = results[0][0]

if probability == 1:
result_text = ":green[REPAYMENT LIKELY]"
else:
result_text = ":red[HIGH RISK OF DEFAULT]"

try:
try_df = eli5.explain_prediction_df(xgc, df_all.iloc[0],
feature_names=list(df_all.columns))
except:
print("An exception occurred")

row4_spacer1, row4_1, row4_spacer2 = st.columns((.2, 7.1, .2))
with row4_1:
st.subheader('After scoring this application, the model suggests that the application be:')
st.subheader(' ')
st.subheader(result_text)
st.subheader(' ')
st.subheader("The following table provides insights into the model's prediction:")
st.subheader(' ')
col1, col2 = st.columns(2)
with col1:
st.subheader('Model Weights:')
df_weights = eli5.explain_weights_df(xgc,
feature_names=list(df_all.columns))
df_weights.columns = ['Feature', 'Weight']
st.dataframe(df_weights.style.background_gradient(axis=0,
gmap=df_weights['Weight'],
subset=['Feature', 'Weight'],
cmap = 'Greens').hide_index())


with col2:
st.subheader('Model Prediction:')
df_prediction = eli5.explain_prediction_df(xgc, df_all.iloc[0],
feature_names=list(df_all.columns))
df_prediction.drop("target", axis=1, inplace=True)
df_prediction.columns = ['Feature', 'Weight', 'value']
st.dataframe(df_prediction.style.background_gradient(axis=0,
gmap=df_prediction['Weight'],
subset=['Feature', 'Weight'],
cmap = 'RdYlGn').hide_index())
28 changes: 8 additions & 20 deletions apps/streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import eli5
import xgboost as xgb
import seaborn as sns
import os

#xgc = xgb.Booster(model_file="/mnt/models/xgb_clf.xgb")
xgc = xgb.Booster(model_file="/mnt/code/models/xgb_clf.xgb")


st.set_page_config(layout="wide")
Expand Down Expand Up @@ -99,10 +100,10 @@
setup_dict.update({i :list(df[n:n+1].to_dict().get(i).values())[0]})
scoring_request = {'data' : setup_dict}

response = requests.post("https://ykb.domino-eval.com:443/models/673b5499011f5130a7ff0036/latest/model",
response = requests.post(os.environ['API_URL'],
auth=(
"WCkJfQNr8MxoLcG277lCBCqWqZClns6GDMM6Kzs65WwnX9lIMDiA5Y76FKWh2lQJ",
"WCkJfQNr8MxoLcG277lCBCqWqZClns6GDMM6Kzs65WwnX9lIMDiA5Y76FKWh2lQJ"
os.environ['API_PASSWORD'],
os.environ['API_PASSWORD']
),
json=scoring_request
)
Expand All @@ -113,12 +114,10 @@

probability = results[0][0]

if probability >= 0.6:
if probability == 1:
result_text = ":green[REPAYMENT LIKELY]"
elif probability >= 0.4:
result_text = ":yellow[RISK OF DEFAULT - LEVEL 1 OUTREACH]"
else:
result_text = ":red[HIGH RISK OF DEFAULT - LEVEL 2 OUTREACH]"
result_text = ":red[HIGH RISK OF DEFAULT]"

try:
try_df = eli5.explain_prediction_df(xgc, df_all.iloc[0],
Expand All @@ -132,8 +131,6 @@
st.subheader(' ')
st.subheader(result_text)
st.subheader(' ')
st.plotly_chart(fig, use_container_width=True)
st.subheader(' ')
st.subheader("The following table provides insights into the model's prediction:")
st.subheader(' ')
col1, col2 = st.columns(2)
Expand All @@ -147,10 +144,6 @@
subset=['Feature', 'Weight'],
cmap = 'Greens').hide_index())

#html_object = eli5.show_weights(xgc,
# feature_names=list(df_all.columns))
#raw_html = html_object._repr_html_()
#components.v1.html(raw_html)

with col2:
st.subheader('Model Prediction:')
Expand All @@ -161,9 +154,4 @@
st.dataframe(df_prediction.style.background_gradient(axis=0,
gmap=df_prediction['Weight'],
subset=['Feature', 'Weight'],
cmap = 'RdYlGn').hide_index())
# html_object2 = eli5.show_prediction(xgc, df_all.iloc[0],
# feature_names=list(df_all.columns),
# show_feature_values=True)
# raw_html2 = html_object2._repr_html_()
# components.v1.html(raw_html2)
cmap = 'RdYlGn').hide_index())
Binary file added models/xgb_clf.xgb
Binary file not shown.

0 comments on commit 90a2d1c

Please sign in to comment.