Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,85 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
from sklearn.
|
| 5 |
-
from sklearn.
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
st.
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
st.
|
| 66 |
-
st.
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
st.
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.metrics import accuracy_score, classification_report
|
| 7 |
+
from datasets import load_dataset
|
| 8 |
+
|
| 9 |
+
# Load dataset from Hugging Face
|
| 10 |
+
dataset = load_dataset("Nooha/cc_fraud_detection_dataset", split="train")
|
| 11 |
+
df = pd.DataFrame(dataset)
|
| 12 |
+
|
| 13 |
+
# Select relevant features and target variable
|
| 14 |
+
X = df[['Amount', 'Time', 'V1', 'V2', 'V3']]
|
| 15 |
+
y = df['Class']
|
| 16 |
+
|
| 17 |
+
# Split dataset into training and testing sets
|
| 18 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 19 |
+
|
| 20 |
+
# Train a RandomForestClassifier model
|
| 21 |
+
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 22 |
+
model.fit(X_train, y_train)
|
| 23 |
+
|
| 24 |
+
y_pred = model.predict(X_test)
|
| 25 |
+
|
| 26 |
+
# Model Performance Metrics
|
| 27 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 28 |
+
class_report_df = pd.DataFrame(classification_report(y_test, y_pred, output_dict=True)).transpose()
|
| 29 |
+
|
| 30 |
+
# Application Title
|
| 31 |
+
st.title('π³ Credit Card Fraud Detection System')
|
| 32 |
+
|
| 33 |
+
st.markdown(
|
| 34 |
+
"""
|
| 35 |
+
## π Introduction
|
| 36 |
+
Welcome to the **Credit Card Fraud Detection System**! This tool analyzes credit card transactions to detect fraudulent activity using a **Random Forest model**.
|
| 37 |
+
"""
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Tab Structure
|
| 41 |
+
tab1, tab2, tab3 = st.tabs(['π Dataset Preview', 'π Model Performance', 'π Fraud Prediction'])
|
| 42 |
+
|
| 43 |
+
# Dataset Preview
|
| 44 |
+
with tab1:
|
| 45 |
+
st.markdown(
|
| 46 |
+
"""
|
| 47 |
+
## π Dataset Preview
|
| 48 |
+
Below is a sample of the credit card transaction dataset used for fraud detection.
|
| 49 |
+
"""
|
| 50 |
+
)
|
| 51 |
+
st.dataframe(df.head())
|
| 52 |
+
|
| 53 |
+
# Model Performance
|
| 54 |
+
with tab2:
|
| 55 |
+
st.markdown(
|
| 56 |
+
"""
|
| 57 |
+
## π Model Performance
|
| 58 |
+
- **Accuracy:** Measures overall model performance.
|
| 59 |
+
- **Classification Report:** Precision, recall, and F1-score breakdown.
|
| 60 |
+
"""
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
st.write(f"**π Model Accuracy:** {accuracy:.2%}")
|
| 64 |
+
|
| 65 |
+
st.markdown("### π Classification Report")
|
| 66 |
+
st.dataframe(class_report_df)
|
| 67 |
+
|
| 68 |
+
# Fraud Prediction
|
| 69 |
+
with tab3:
|
| 70 |
+
st.markdown("""
|
| 71 |
+
## π Fraud Prediction
|
| 72 |
+
Enter transaction details below to predict if it's fraudulent.
|
| 73 |
+
""")
|
| 74 |
+
|
| 75 |
+
amount_input = st.number_input("π΅ Transaction Amount", min_value=0.0, value=100.0, step=1.0)
|
| 76 |
+
time_input = st.number_input("β³ Transaction Time", min_value=0.0, value=50000.0, step=1000.0)
|
| 77 |
+
v1_input = st.number_input("π’ Feature V1", value=0.0, step=0.1)
|
| 78 |
+
v2_input = st.number_input("π’ Feature V2", value=0.0, step=0.1)
|
| 79 |
+
v3_input = st.number_input("π’ Feature V3", value=0.0, step=0.1)
|
| 80 |
+
|
| 81 |
+
if st.button("π Predict Fraud"):
|
| 82 |
+
input_data = np.array([[amount_input, time_input, v1_input, v2_input, v3_input]])
|
| 83 |
+
prediction = model.predict(input_data)[0]
|
| 84 |
+
result = "π¨ Fraudulent" if prediction == 1 else "β
Legitimate"
|
| 85 |
+
st.success(f"### π― Prediction: **{result}**")
|