File size: 1,991 Bytes
7c53b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7192794
 
 
 
7c53b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from joblib import load
import gradio as gr
import pandas as pd
import numpy as np
import shap
import matplotlib.pyplot as plt
from features import extract_basic_aggregations
from os import listdir
from os.path import join, isfile

examples_path = './csv_examples/'
examples = [[join(examples_path, f), 'A'] for f in listdir(examples_path)]
model = load('xgb_cpu.joblib')

explainer = shap.TreeExplainer(model)
products = {'A': 0,
            'B': 1,
            'C': 2,
            'D': 3,
            'E': 4
           }
def score_client(card_transactions_file, product: str):
    df = pd.read_csv(card_transactions_file)
    assert product in products

    features = extract_basic_aggregations(df, cat_columns=['mcc_category', 'day_of_week', 'operation_type'])
    features = features.reindex(columns=model.feature_names_in_, fill_value=0)
    features['product'] = products[product]
    default_proba = model.predict_proba(features)[0][0]
    shap_values = explainer.shap_values(features)
    shap.plots.waterfall(explainer(features)[0], max_display=14, show=False)
    plt.tight_layout()
    shap_fig = plt.gcf()

    plt.close()
    return default_proba, shap_fig


title = "Credit score demo"
description = "This demo allows to evaluate credit score solely based on card transaction history. \
    You can upload your own transaction history .csv file or use transactions from the examples. \
    After that, please specify a credit product of interest. When the evaluation is done, you can examine an importances plot that may explain the result."

inputs = [gr.File(), gr.Dropdown(choices=list(products.keys()), value=list(products.keys())[0])]
outputs =  [gr.Textbox(label='Your credit score (the more, the better)', interactive=False),
            gr.Plot(label='SHAP')
           ]

demo = gr.Interface(
    fn=score_client,
    inputs=inputs,
    outputs=outputs,
    allow_flagging='never',
    examples=examples,
    title=title,
    description=description,
)
demo.launch()