Spaces:
Running
Running
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
from sklearn.ensemble import RandomForestClassifier | |
from sklearn.model_selection import train_test_split | |
from sklearn.metrics import accuracy_score, classification_report | |
from datasets import load_dataset | |
# Load dataset from Hugging Face | |
dataset = load_dataset("Nooha/cc_fraud_detection_dataset", split="train") | |
df = pd.DataFrame(dataset) | |
# Select relevant features and target variable | |
X = df[['Amount', 'Time', 'V1', 'V2', 'V3']] | |
y = df['Class'] | |
# Split dataset into training and testing sets | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
# Train a RandomForestClassifier model | |
model = RandomForestClassifier(n_estimators=100, random_state=42) | |
model.fit(X_train, y_train) | |
y_pred = model.predict(X_test) | |
# Model Performance Metrics | |
accuracy = accuracy_score(y_test, y_pred) | |
class_report_df = pd.DataFrame(classification_report(y_test, y_pred, output_dict=True)).transpose() | |
# Application Title | |
st.title('π³ Credit Card Fraud Detection System') | |
st.markdown( | |
""" | |
## π Introduction | |
Welcome to the **Credit Card Fraud Detection System**! This tool analyzes credit card transactions to detect fraudulent activity using a **Random Forest model**. | |
""" | |
) | |
# Tab Structure | |
tab1, tab2, tab3 = st.tabs(['π Dataset Preview', 'π Model Performance', 'π Fraud Prediction']) | |
# Dataset Preview | |
with tab1: | |
st.markdown( | |
""" | |
## π Dataset Preview | |
Below is a sample of the credit card transaction dataset used for fraud detection. | |
""" | |
) | |
st.dataframe(df.head()) | |
# Model Performance | |
with tab2: | |
st.markdown( | |
""" | |
## π Model Performance | |
- **Accuracy:** Measures overall model performance. | |
- **Classification Report:** Precision, recall, and F1-score breakdown. | |
""" | |
) | |
st.write(f"**π Model Accuracy:** {accuracy:.2%}") | |
st.markdown("### π Classification Report") | |
st.dataframe(class_report_df) | |
# Fraud Prediction | |
with tab3: | |
st.markdown(""" | |
## π Fraud Prediction | |
Enter transaction details below to predict if it's fraudulent. | |
""") | |
amount_input = st.number_input("π΅ Transaction Amount", min_value=0.0, value=100.0, step=1.0) | |
time_input = st.number_input("β³ Transaction Time", min_value=0.0, value=50000.0, step=1000.0) | |
v1_input = st.number_input("π’ Feature V1", value=0.0, step=0.1) | |
v2_input = st.number_input("π’ Feature V2", value=0.0, step=0.1) | |
v3_input = st.number_input("π’ Feature V3", value=0.0, step=0.1) | |
if st.button("π Predict Fraud"): | |
input_data = np.array([[amount_input, time_input, v1_input, v2_input, v3_input]]) | |
prediction = model.predict(input_data)[0] | |
result = "π¨ Fraudulent" if prediction == 1 else "β Legitimate" | |
st.success(f"### π― Prediction: **{result}**") | |