Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,75 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AlbertTokenizer, AlbertForSequenceClassification, BertTokenizer, BertForSequenceClassification
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
albert_model = AlbertForSequenceClassification.from_pretrained("Deepaksai1/albert-fraud-detector-v2")
|
| 8 |
albert_tokenizer = AlbertTokenizer.from_pretrained("Deepaksai1/albert-fraud-detector-v2")
|
| 9 |
-
albert_model.eval()
|
| 10 |
|
| 11 |
-
|
| 12 |
-
finbert_model = BertForSequenceClassification.from_pretrained("Deepaksai1/finbert-fraud-detector-v2")
|
| 13 |
finbert_tokenizer = BertTokenizer.from_pretrained("Deepaksai1/finbert-fraud-detector-v2")
|
| 14 |
-
finbert_model.eval()
|
| 15 |
|
| 16 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
inputs = albert_tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 20 |
with torch.no_grad():
|
| 21 |
-
outputs =
|
| 22 |
-
probs =
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
return ("Fraud" if pred_class == 1 else "Not Fraud"), float(pred_prob)
|
| 26 |
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
-
inputs = finbert_tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 31 |
-
with torch.no_grad():
|
| 32 |
-
outputs = finbert_model(**inputs)
|
| 33 |
-
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 34 |
-
pred_class = torch.argmax(probs).item()
|
| 35 |
-
pred_prob = probs[0][1].item()
|
| 36 |
-
return ("Fraud" if pred_class == 1 else "Not Fraud"), float(pred_prob)
|
| 37 |
-
|
| 38 |
-
# Model selector
|
| 39 |
-
|
| 40 |
-
def predict(text, model_name):
|
| 41 |
-
if model_name == "ALBERT":
|
| 42 |
-
return predict_with_albert(text)
|
| 43 |
-
elif model_name == "FinBERT":
|
| 44 |
-
return predict_with_finbert(text)
|
| 45 |
-
else:
|
| 46 |
-
return "Unknown Model", 0.0
|
| 47 |
-
|
| 48 |
-
# Updated examples: 3 fraud + 3 non-fraud, using training-format features
|
| 49 |
examples = [
|
| 50 |
-
[
|
| 51 |
-
[
|
| 52 |
-
[
|
| 53 |
-
[
|
| 54 |
-
[
|
| 55 |
-
[
|
| 56 |
]
|
| 57 |
|
| 58 |
# Gradio Interface
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
gr.
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
# Launch
|
| 75 |
if __name__ == "__main__":
|
| 76 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AlbertTokenizer, AlbertForSequenceClassification, BertTokenizer, BertForSequenceClassification
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
|
| 6 |
+
# Load models
|
| 7 |
+
albert_model = AlbertForSequenceClassification.from_pretrained("Deepaksai1/albert-fraud-detector-v2").eval()
|
| 8 |
albert_tokenizer = AlbertTokenizer.from_pretrained("Deepaksai1/albert-fraud-detector-v2")
|
|
|
|
| 9 |
|
| 10 |
+
finbert_model = BertForSequenceClassification.from_pretrained("Deepaksai1/finbert-fraud-detector-v2").eval()
|
|
|
|
| 11 |
finbert_tokenizer = BertTokenizer.from_pretrained("Deepaksai1/finbert-fraud-detector-v2")
|
|
|
|
| 12 |
|
| 13 |
+
# Inference function
|
| 14 |
+
def predict_model(step, tx_type, amount, old_org, new_org, old_dest, new_dest, model_name):
|
| 15 |
+
text = f"Step: {step}, Type: {tx_type}, Amount: {amount}, " \
|
| 16 |
+
f"OldBalOrig: {old_org}, NewBalOrig: {new_org}, " \
|
| 17 |
+
f"OldBalDest: {old_dest}, NewBalDest: {new_dest}"
|
| 18 |
+
|
| 19 |
+
tokenizer = albert_tokenizer if model_name == "ALBERT" else finbert_tokenizer
|
| 20 |
+
model = albert_model if model_name == "ALBERT" else finbert_model
|
| 21 |
|
| 22 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
|
|
|
| 23 |
with torch.no_grad():
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
probs = F.softmax(outputs.logits, dim=1)
|
| 26 |
+
pred = torch.argmax(probs).item()
|
| 27 |
+
fraud_score = probs[0][1].item()
|
|
|
|
| 28 |
|
| 29 |
+
return "Fraud" if pred == 1 else "Not Fraud", round(fraud_score, 4)
|
| 30 |
|
| 31 |
+
# Example values
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
examples = [
|
| 33 |
+
[151, "CASH_OUT", 1633227.0, 1633227.0, 0.0, 2865353.22, 4498580.23, "ALBERT"],
|
| 34 |
+
[353, "CASH_OUT", 174566.53, 174566.53, 0.0, 1191715.74, 1366282.27, "FinBERT"],
|
| 35 |
+
[357, "TRANSFER", 484493.06, 484493.06, 0.0, 0.0, 0.0, "ALBERT"],
|
| 36 |
+
[43, "CASH_OUT", 81571.63, 0.0, 0.0, 176194.2, 257765.83, "FinBERT"],
|
| 37 |
+
[307, "DEBIT", 247.82, 11544.0, 11296.18, 3550535.53, 3550783.36, "ALBERT"],
|
| 38 |
+
[350, "DEBIT", 4330.57, 3766.0, 0.0, 239435.41, 243765.98, "FinBERT"]
|
| 39 |
]
|
| 40 |
|
| 41 |
# Gradio Interface
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
gr.Markdown("## 🔎 Fraud Detection with ALBERT and FinBERT")
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
step = gr.Number(label="Step", value=1)
|
| 47 |
+
tx_type = gr.Dropdown(choices=["CASH_OUT", "TRANSFER", "PAYMENT", "DEBIT", "CASH_IN"], label="Transaction Type")
|
| 48 |
+
amount = gr.Number(label="Amount", value=0.0)
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
old_org = gr.Number(label="Old Balance Orig", value=0.0)
|
| 52 |
+
new_org = gr.Number(label="New Balance Orig", value=0.0)
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
old_dest = gr.Number(label="Old Balance Dest", value=0.0)
|
| 56 |
+
new_dest = gr.Number(label="New Balance Dest", value=0.0)
|
| 57 |
+
|
| 58 |
+
model_selector = gr.Dropdown(choices=["ALBERT", "FinBERT"], value="ALBERT", label="Select Model")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
predict_btn = gr.Button("Predict")
|
| 62 |
+
pred_label = gr.Label(label="Prediction")
|
| 63 |
+
prob_score = gr.Number(label="Fraud Probability")
|
| 64 |
+
|
| 65 |
+
# Bind function
|
| 66 |
+
predict_btn.click(fn=predict_model,
|
| 67 |
+
inputs=[step, tx_type, amount, old_org, new_org, old_dest, new_dest, model_selector],
|
| 68 |
+
outputs=[pred_label, prob_score])
|
| 69 |
+
|
| 70 |
+
gr.Examples(examples=examples,
|
| 71 |
+
inputs=[step, tx_type, amount, old_org, new_org, old_dest, new_dest, model_selector])
|
| 72 |
|
| 73 |
+
# Launch app
|
| 74 |
if __name__ == "__main__":
|
| 75 |
+
demo.launch()
|