ritampatra commited on
Commit
53290b0
·
verified ·
1 Parent(s): 4ec59a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -17
app.py CHANGED
@@ -1,25 +1,47 @@
1
  import streamlit as st
2
- from transformers import pipeline
 
3
 
4
- # Load pretrained fraud detection model from Hugging Face
5
- @st.cache_resource
6
- def load_model():
7
- return pipeline("text-classification", model="d4data/bert-base-cased-finetuned-fraud-detection")
8
 
9
- model = load_model()
 
10
 
11
- # Streamlit UI
12
- st.title("💳 Financial Fraud Detection System (Hugging Face Model)")
 
 
 
 
13
 
14
- # User input text
15
- user_input = st.text_area("Enter transaction description:", "Payment of $500 for electronics purchase")
 
 
 
 
 
 
 
16
 
17
- if st.button("Check for Fraud"):
18
- prediction = model(user_input)
19
- label = prediction[0]["label"]
20
- score = prediction[0]["score"]
21
 
22
- if label == "LABEL_1": # Assuming LABEL_1 means fraud
23
- st.error(f"🚨 Fraudulent Transaction Detected! (Confidence: {score:.2f})")
 
 
 
 
 
 
 
 
24
  else:
25
- st.success(f"Legitimate Transaction (Confidence: {score:.2f})")
 
 
 
1
  import streamlit as st
2
+ import pandas as pd
3
+ import joblib
4
 
5
+ # Load the trained model and preprocessing objects
6
+ model = joblib.load("fraud_detection_model.joblib")
7
+ label_encoders = joblib.load("label_encoders.joblib")
8
+ scaler = joblib.load("scaler.joblib")
9
 
10
+ # Streamlit app
11
+ st.title("Credit Card Fraud Detection")
12
 
13
+ # Input fields for user
14
+ st.header("Enter Transaction Details")
15
+ amount = st.number_input("Amount", min_value=0.0)
16
+ merchant_id = st.text_input("Merchant ID")
17
+ transaction_type = st.selectbox("Transaction Type", ["purchase", "refund"])
18
+ location = st.text_input("Location")
19
 
20
+ # Preprocess input data
21
+ if st.button("Predict"):
22
+ # Create a DataFrame from the input
23
+ input_data = pd.DataFrame({
24
+ "Amount": [amount],
25
+ "MerchantID": [merchant_id],
26
+ "TransactionType": [transaction_type],
27
+ "Location": [location]
28
+ })
29
 
30
+ # Apply label encoding to categorical columns
31
+ for col, le in label_encoders.items():
32
+ input_data[col] = le.transform(input_data[col])
 
33
 
34
+ # Scale the "Amount" column
35
+ input_data["Amount"] = scaler.transform(input_data[["Amount"]])
36
+
37
+ # Make prediction
38
+ prediction = model.predict(input_data)
39
+ prediction_proba = model.predict_proba(input_data)
40
+
41
+ # Display the result
42
+ if prediction[0] == 1:
43
+ st.error("Fraudulent Transaction Detected!")
44
  else:
45
+ st.success("Legitimate Transaction")
46
+
47
+ st.write(f"Probability: {prediction_proba[0][1]:.2f}")