CoolFace
Apppublic

alex42t/CreditScore

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
app.py59 linesDownload Raw Back to root
1from joblib import load2import gradio as gr3import pandas as pd4import numpy as np5import shap6import matplotlib.pyplot as plt7from features import extract_basic_aggregations8from os import listdir9from os.path import join, isfile10 11examples_path = './csv_examples/'12examples = [[join(examples_path, f), 'A'] for f in listdir(examples_path)]13model = load('xgb_cpu.joblib')14 15explainer = shap.TreeExplainer(model)16products = {'A': 0,17            'B': 1,18            'C': 2,19            'D': 3,20            'E': 421           }22def score_client(card_transactions_file, product: str):23    df = pd.read_csv(card_transactions_file)24    assert product in products25 26    features = extract_basic_aggregations(df, cat_columns=['mcc_category', 'day_of_week', 'operation_type'])27    features = features.reindex(columns=model.feature_names_in_, fill_value=0)28    features['product'] = products[product]29    default_proba = model.predict_proba(features)[0][0]30    shap_values = explainer.shap_values(features)31    shap.plots.waterfall(explainer(features)[0], max_display=14, show=False)32    plt.tight_layout()33    shap_fig = plt.gcf()34 35    plt.close()36    return default_proba, shap_fig37 38 39title = "Credit score demo"40description = "This demo allows to evaluate credit score solely based on card transaction history. \41    You can upload your own transaction history .csv file or use transactions from the examples. \42    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."43 44inputs = [gr.File(), gr.Dropdown(choices=list(products.keys()), value=list(products.keys())[0])]45outputs =  [gr.Textbox(label='Your credit score (the more, the better)', interactive=False),46            gr.Plot(label='SHAP')47           ]48 49demo = gr.Interface(50    fn=score_client,51    inputs=inputs,52    outputs=outputs,53    allow_flagging='never',54    examples=examples,55    title=title,56    description=description,57)58demo.launch()59