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()