Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,31 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AlbertTokenizer, AlbertForSequenceClassification, BertTokenizer, BertForSequenceClassification
|
|
|
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
-
import joblib
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
# Load ALBERT model
|
| 9 |
albert_model = AlbertForSequenceClassification.from_pretrained("Deepaksai1/albert-fraud-detector")
|
| 10 |
albert_tokenizer = AlbertTokenizer.from_pretrained("Deepaksai1/albert-fraud-detector")
|
| 11 |
albert_model.eval()
|
| 12 |
|
| 13 |
-
# Load FinBERT model
|
| 14 |
finbert_model = BertForSequenceClassification.from_pretrained("Deepaksai1/finbert-fraud-detector")
|
| 15 |
finbert_tokenizer = BertTokenizer.from_pretrained("Deepaksai1/finbert-fraud-detector")
|
| 16 |
finbert_model.eval()
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
catboost_model_path = "catboost_fraud_model.cbm"
|
| 21 |
catboost_model = CatBoostClassifier()
|
| 22 |
catboost_model.load_model(catboost_model_path)
|
| 23 |
|
| 24 |
-
# CatBoost prediction (
|
| 25 |
def predict_with_catboost(text):
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
prediction = catboost_model.predict([[amount]])[0]
|
| 29 |
proba = catboost_model.predict_proba([[amount]])[0][1]
|
| 30 |
return ("Fraud" if prediction == 1 else "Not Fraud"), float(proba)
|
|
@@ -49,7 +50,7 @@ def predict_with_finbert(text):
|
|
| 49 |
pred_prob = probs[0][1].item()
|
| 50 |
return ("Fraud" if pred_class == 1 else "Not Fraud"), float(pred_prob)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
def predict(text, model_name):
|
| 54 |
if model_name == "ALBERT":
|
| 55 |
return predict_with_albert(text)
|
|
@@ -60,7 +61,7 @@ def predict(text, model_name):
|
|
| 60 |
else:
|
| 61 |
return "Unknown Model", 0.0
|
| 62 |
|
| 63 |
-
#
|
| 64 |
examples = [
|
| 65 |
"Step: 305, Type: CASH_OUT, Amount: 2321633.57, Origin Balance: 2321633.57, Dest Balance: 0.0",
|
| 66 |
"Step: 6, Type: CASH_OUT, Amount: 13704.0, Origin Balance: 13704.0, Dest Balance: 3382.84",
|
|
@@ -69,7 +70,7 @@ examples = [
|
|
| 69 |
"Step: 372, Type: CASH_IN, Amount: 187503.32, Origin Balance: 76827.0, Dest Balance: 0.0"
|
| 70 |
]
|
| 71 |
|
| 72 |
-
# Gradio
|
| 73 |
gui = gr.Interface(
|
| 74 |
fn=predict,
|
| 75 |
inputs=[
|
|
@@ -81,10 +82,10 @@ gui = gr.Interface(
|
|
| 81 |
gr.Number(label="Fraud Probability")
|
| 82 |
],
|
| 83 |
examples=[[ex, "ALBERT"] for ex in examples],
|
| 84 |
-
title="💸 Fraud Detection Assistant
|
| 85 |
-
description="Analyze transaction text
|
| 86 |
)
|
| 87 |
|
| 88 |
-
# Launch
|
| 89 |
if __name__ == "__main__":
|
| 90 |
gui.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AlbertTokenizer, AlbertForSequenceClassification, BertTokenizer, BertForSequenceClassification
|
| 4 |
+
from catboost import CatBoostClassifier
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
import numpy as np
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Load ALBERT model and tokenizer from Hugging Face Hub
|
| 9 |
albert_model = AlbertForSequenceClassification.from_pretrained("Deepaksai1/albert-fraud-detector")
|
| 10 |
albert_tokenizer = AlbertTokenizer.from_pretrained("Deepaksai1/albert-fraud-detector")
|
| 11 |
albert_model.eval()
|
| 12 |
|
| 13 |
+
# Load FinBERT model and tokenizer from Hugging Face Hub
|
| 14 |
finbert_model = BertForSequenceClassification.from_pretrained("Deepaksai1/finbert-fraud-detector")
|
| 15 |
finbert_tokenizer = BertTokenizer.from_pretrained("Deepaksai1/finbert-fraud-detector")
|
| 16 |
finbert_model.eval()
|
| 17 |
|
| 18 |
+
# Download and load CatBoost model from Hugging Face Hub
|
| 19 |
+
catboost_model_path = hf_hub_download(repo_id="Deepaksai1/catboost-fraud-detector", filename="catboost_fraud_model.cbm")
|
|
|
|
| 20 |
catboost_model = CatBoostClassifier()
|
| 21 |
catboost_model.load_model(catboost_model_path)
|
| 22 |
|
| 23 |
+
# CatBoost prediction (simple numeric extraction)
|
| 24 |
def predict_with_catboost(text):
|
| 25 |
+
try:
|
| 26 |
+
amount = float([s for s in text.split(',') if 'Amount' in s][0].split(':')[1].strip())
|
| 27 |
+
except:
|
| 28 |
+
return "Invalid input", 0.0
|
| 29 |
prediction = catboost_model.predict([[amount]])[0]
|
| 30 |
proba = catboost_model.predict_proba([[amount]])[0][1]
|
| 31 |
return ("Fraud" if prediction == 1 else "Not Fraud"), float(proba)
|
|
|
|
| 50 |
pred_prob = probs[0][1].item()
|
| 51 |
return ("Fraud" if pred_class == 1 else "Not Fraud"), float(pred_prob)
|
| 52 |
|
| 53 |
+
# Model selector
|
| 54 |
def predict(text, model_name):
|
| 55 |
if model_name == "ALBERT":
|
| 56 |
return predict_with_albert(text)
|
|
|
|
| 61 |
else:
|
| 62 |
return "Unknown Model", 0.0
|
| 63 |
|
| 64 |
+
# Examples from dataset
|
| 65 |
examples = [
|
| 66 |
"Step: 305, Type: CASH_OUT, Amount: 2321633.57, Origin Balance: 2321633.57, Dest Balance: 0.0",
|
| 67 |
"Step: 6, Type: CASH_OUT, Amount: 13704.0, Origin Balance: 13704.0, Dest Balance: 3382.84",
|
|
|
|
| 70 |
"Step: 372, Type: CASH_IN, Amount: 187503.32, Origin Balance: 76827.0, Dest Balance: 0.0"
|
| 71 |
]
|
| 72 |
|
| 73 |
+
# Gradio Interface
|
| 74 |
gui = gr.Interface(
|
| 75 |
fn=predict,
|
| 76 |
inputs=[
|
|
|
|
| 82 |
gr.Number(label="Fraud Probability")
|
| 83 |
],
|
| 84 |
examples=[[ex, "ALBERT"] for ex in examples],
|
| 85 |
+
title="💸 Fraud Detection Assistant",
|
| 86 |
+
description="Analyze transaction text using ALBERT, FinBERT, or CatBoost models."
|
| 87 |
)
|
| 88 |
|
| 89 |
+
# Launch the app
|
| 90 |
if __name__ == "__main__":
|
| 91 |
gui.launch()
|