Spaces:
Sleeping
Sleeping
Your (user)name commited on
Commit ·
d426b80
1
Parent(s): 5bf0240
Detect Fraud
Browse files- app.py +94 -0
- model/categories_ohe_without_fraudulent.pickle +3 -0
- model/modelo_proyecto_final2.pkl +3 -0
- model/saved_bins_order.pickle +3 -0
- model/saved_bins_transaction.pickle +3 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import pickle
|
| 4 |
+
from typing import List, Dict, Any
|
| 5 |
+
|
| 6 |
+
# CSS styles (unchanged)
|
| 7 |
+
css = """
|
| 8 |
+
body, html {
|
| 9 |
+
height: 100%;
|
| 10 |
+
margin: 0;
|
| 11 |
+
color: white;
|
| 12 |
+
background-color: black;
|
| 13 |
+
}
|
| 14 |
+
header {
|
| 15 |
+
background: url('fraude.png') no-repeat top left;
|
| 16 |
+
background-size: 120px; /* Adjust this value to the desired size of your image */
|
| 17 |
+
padding-top: 130px; /* Adjust this value to provide space for the image */
|
| 18 |
+
}
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
# Load model and configurations
|
| 22 |
+
def load_pickle(filename: str) -> Any:
|
| 23 |
+
with open(filename, 'rb') as f:
|
| 24 |
+
return pickle.load(f)
|
| 25 |
+
|
| 26 |
+
model = load_pickle('model/modelo_proyecto_final2.pkl')
|
| 27 |
+
ohe_columns = load_pickle('model/categories_ohe_without_fraudulent.pickle')
|
| 28 |
+
bins_order = load_pickle('model/saved_bins_order.pickle')
|
| 29 |
+
bins_transaction = load_pickle('model/saved_bins_transaction.pickle')
|
| 30 |
+
|
| 31 |
+
def predict(order_amount: float, order_state: str, payment_method_registration_failure: bool,
|
| 32 |
+
payment_method_type: str, payment_method_provider: str, payment_method_issuer: str,
|
| 33 |
+
transaction_amount: float, transaction_failed: bool, email_domain: str,
|
| 34 |
+
email_provider: str, customer_ip_address_simplified: str, same_city: str) -> str:
|
| 35 |
+
# Create input DataFrame
|
| 36 |
+
data = {
|
| 37 |
+
"orderAmount": [order_amount],
|
| 38 |
+
"orderState": [order_state],
|
| 39 |
+
"paymentMethodRegistrationFailure": [payment_method_registration_failure],
|
| 40 |
+
"paymentMethodType": [payment_method_type],
|
| 41 |
+
"paymentMethodProvider": [payment_method_provider],
|
| 42 |
+
"paymentMethodIssuer": [payment_method_issuer],
|
| 43 |
+
"transactionAmount": [transaction_amount],
|
| 44 |
+
"transactionFailed": [transaction_failed],
|
| 45 |
+
"emailDomain": [email_domain],
|
| 46 |
+
"emailProvider": [email_provider],
|
| 47 |
+
"customerIPAddressSimplified": [customer_ip_address_simplified],
|
| 48 |
+
"sameCity": [same_city]
|
| 49 |
+
}
|
| 50 |
+
df = pd.DataFrame(data)
|
| 51 |
+
|
| 52 |
+
# Fill null values with modes or medians
|
| 53 |
+
for column in df.columns:
|
| 54 |
+
if pd.api.types.is_numeric_dtype(df[column]):
|
| 55 |
+
df[column].fillna(df[column].median(), inplace=True)
|
| 56 |
+
else:
|
| 57 |
+
df[column].fillna(df[column].mode().iloc[0], inplace=True)
|
| 58 |
+
|
| 59 |
+
# Apply binning and one-hot encoding
|
| 60 |
+
df["orderAmount"] = pd.cut(df["orderAmount"].astype(float), bins=bins_order, include_lowest=True)
|
| 61 |
+
df["transactionAmount"] = pd.cut(df["transactionAmount"].astype(int), bins=bins_transaction, include_lowest=True)
|
| 62 |
+
df_encoded = pd.get_dummies(df).reindex(columns=ohe_columns, fill_value=0)
|
| 63 |
+
|
| 64 |
+
# Prediction
|
| 65 |
+
prediction = model.predict(df_encoded)
|
| 66 |
+
type_of_fraud = int(prediction[0])
|
| 67 |
+
responses = ["OK", "FRAUD DETECTED", "Warning"]
|
| 68 |
+
return responses[type_of_fraud] if type_of_fraud in [0, 1, 2] else "Error parsing value"
|
| 69 |
+
|
| 70 |
+
# Configure Gradio interface
|
| 71 |
+
interface = gr.Interface(
|
| 72 |
+
fn=predict,
|
| 73 |
+
inputs=[
|
| 74 |
+
gr.Number(label="Order Amount"),
|
| 75 |
+
gr.Dropdown(choices=["pending", "fulfilled", "failed"], label="Order State"),
|
| 76 |
+
gr.Checkbox(label="Payment Method Registration Failure"),
|
| 77 |
+
gr.Dropdown(choices=["card", "bitcoin", "paypal"], label="Payment Method Type"),
|
| 78 |
+
gr.Dropdown(choices=['JCB 16 digit', 'VISA 16 digit', 'Diners Club / Carte Blanche', 'Mastercard', 'American Express', 'Maestro', 'Discover', 'Voyager', 'VISA 13 digit', 'JCB 15 digit'], label="Payment Method Provider"),
|
| 79 |
+
gr.Dropdown(choices=['Citizens First Banks', 'Solace Banks', 'Vertex Bancorp', 'His Majesty Bank Corp.', 'Bastion Banks', 'Her Majesty Trust', 'Fountain Financial Inc.', 'Grand Credit Corporation', 'weird', 'Bulwark Trust Corp.', 'Rose Bancshares'], label="Payment Method Issuer"),
|
| 80 |
+
gr.Number(label="Transaction Amount"),
|
| 81 |
+
gr.Checkbox(label="Transaction Failed"),
|
| 82 |
+
gr.Dropdown(choices=["info","com","biz","org"], label="Email Domain"),
|
| 83 |
+
gr.Dropdown(choices=["yahoo","gmail","hotmail","other"], label="Email Provider"),
|
| 84 |
+
gr.Dropdown(choices=["only_letters", "digits_and_letters"], label="Customer IP Address Simplified"),
|
| 85 |
+
gr.Dropdown(choices=["yes", "no"], label="Same City")
|
| 86 |
+
],
|
| 87 |
+
outputs="text",
|
| 88 |
+
title="API for Fraud Detection",
|
| 89 |
+
description="APP to predict if a transaction is fraudulent.",
|
| 90 |
+
css=css
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
interface.launch()
|
model/categories_ohe_without_fraudulent.pickle
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5dbeb4b297ccfe265a6cceb5283089d2276a835ec3735a39e17b698952983fa8
|
| 3 |
+
size 1972
|
model/modelo_proyecto_final2.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e90f9a99d271fa2abfe3e52fe8c26efb7741652be995b53da3503f5510ada398
|
| 3 |
+
size 3996578
|
model/saved_bins_order.pickle
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:552fdcdb92034ea7aebdb6d837f1dcef67f643d8a9c409074d425789a5cd0317
|
| 3 |
+
size 174
|
model/saved_bins_transaction.pickle
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:23fd3e29980e60fa786b5819d59e4803e6817a2cd8c16fbe610db359ba413fe3
|
| 3 |
+
size 166
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Cython>=0.29
|