Spaces:
Runtime error
Runtime error
import gradio as gr | |
import pandas as pd | |
import joblib | |
model = joblib.load('decision_tree.pkl') | |
def onehot(df, column): | |
df = df.copy() | |
dummies = pd.get_dummies(df[column], prefix='type') | |
df = pd.concat([df,dummies], axis=1) | |
df = df.drop(column, axis=1) | |
return df | |
def dataframe(file_obj): | |
df = pd.read_csv(file_obj.name) | |
df = onehot(df, column='type') | |
#if the 'type' column doesn't have value 'CASH_OUT' then add a column 'type_CASH_OUT' with value 0 | |
if 'type_CASH_OUT' not in df.columns: | |
df['type_CASH_OUT'] = 0 | |
#if the 'type' column doesn't have value 'TRANSFER' then add a column 'type_TRANSFER' with value 0 | |
if 'type_TRANSFER' not in df.columns: | |
df['type_TRANSFER'] = 0 | |
#if the 'type' column doesn't have value 'PAYMENT' then add a column 'type_PAYMENT' with value 0 | |
if 'type_PAYMENT' not in df.columns: | |
df['type_PAYMENT'] = 0 | |
#if the 'type' column doesn't have value 'DEBIT' then add a column 'type_DEBIT' with value 0 | |
if 'type_DEBIT' not in df.columns: | |
df['type_DEBIT'] = 0 | |
#if the 'type' column doesn't have value 'PAYMENT' then add a column 'type_PAYMENT' with value 0 | |
if 'type_PAYMENT' not in df.columns: | |
df['type_PAYMENT'] = 0 | |
df = df.drop(['nameOrig','nameDest','isFraud'], axis=1) | |
print(df.shape) | |
y_pred = model.predict(df) | |
pred_df = pd.DataFrame(y_pred, columns = ['predictedFraud']) | |
#append the predictions to the original dataframe | |
df_original = pd.read_csv(file_obj.name) | |
pred_df = pd.concat([df_original, pred_df], axis=1) | |
print(type(pred_df)) | |
print(pred_df.shape) | |
# clr = classification_report(y_test, y_pred, target_names=['Not Fraud','Fraud']) | |
# return 'Classification Report:\n'+ clr | |
return pred_df | |
file = gr.components.File(file_count="single", type="file", label="Fisierul CSV cu tranzactii", optional=False) | |
y_pred_df = gr.components.Dataframe(max_rows=20, max_cols=None, overflow_row_behaviour="paginate", type="pandas", label="predictedFraud - Predictii bazate pe modelul de clasificare isFraud - Etichetele reale") | |
interface_csv = gr.Interface( | |
fn=dataframe, | |
inputs=file, | |
outputs=y_pred_df, | |
title="Fraud Detection - EXPERT SYSTEM", | |
description='<h2>Sistem expert bazat pe un model de clasificare pentru detectarea fraudelor in tranzactii bancare.<h2><h3>predictedFraud - Predictii bazate pe modelul de clasificare. isFraud - Etichetele reale<h3><blockquote class="imgur-embed-pub" lang="en" data-id="J1aOKXd"><a href="https://imgur.com/J1aOKXd">View post on imgur.com</a></blockquote>' | |
) | |
interface_csv.launch(inline=False) |