Upload 9 files
Browse filesFiles changed (10)
- .gitattributes+2
- Decision Tree_model.pklAdded+3
- Logistic Regression_model.pklAdded+3
- Random Forest_model.pklAdded+3
- app.pyAdded+150
- fraudTest.csvAdded+3
- fraudTrain.csvAdded+3
- label_encoders.pklAdded+3
- scaler.pklAdded+3
- xgboost_model.pklAdded+3
32 unmodified lines333435363732 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textfraudTest.csv filter=lfs diff=lfs merge=lfs -textfraudTrain.csv filter=lfs diff=lfs merge=lfs -text123version https://git-lfs.github.com/spec/v1oid sha256:39151dacb5de173cdb369f1c86c833f2e1a5e8ea00899bcdce9190f8e5c4aefdsize 840441123version https://git-lfs.github.com/spec/v1oid sha256:7b8f873548925952a84b7b752aaa0e1414b2e1557bf5e39b340e1f189750b9fcsize 1007123version https://git-lfs.github.com/spec/v1oid sha256:d48b3b6e190d0e84dbe11f44d35f8c6049635f1794708d532a79b0bafd60d7f5size 70075545123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150import streamlit as stimport joblibimport pandas as pdimport numpy as npfrom datetime import datetime
# ---------------- Page Configuration ----------------st.set_page_config( page_title="๐ก๏ธ Fraud Detection", page_icon="๐ก๏ธ", layout="wide", initial_sidebar_state="expanded")
# ---------------- Custom Styling ----------------st.markdown(""" <style> .title { text-align: center; font-size: 2.5rem; font-weight: 700; color: #222831; margin-bottom: 10px; } .subheader { text-align: center; font-size: 1.2rem; color: #393E46; margin-bottom: 30px; } .section { background-color: #f1f5f9; padding: 1.5rem; border-radius: 10px; margin-top: 1rem; } .prediction { padding: 1rem; margin: 1rem 0; # background-color: #e0f7fa; border-left: 8px solid #00796b; border-radius: 6px; } .fraud { border-left-color: #c62828; # background-color: #ffebee; } </style>""", unsafe_allow_html=True)
# ---------------- Load Models and Encoders ----------------@st.cache_resourcedef load_artifacts(): scaler = joblib.load('scaler.pkl') encoders = joblib.load('label_encoders.pkl') models = { 'Logistic Regression': joblib.load('Logistic Regression_model.pkl'), 'Decision Tree': joblib.load('Decision Tree_model.pkl'), 'Random Forest': joblib.load('Random Forest_model.pkl'), 'XGBoost': joblib.load('xgboost_model.pkl') } return scaler, encoders, models
scaler, encoders, models = load_artifacts()
feature_order = [ 'cc_num', 'merchant', 'category', 'amt', 'gender', 'city', 'state', 'zip', 'lat', 'long', 'city_pop', 'job', 'unix_time', 'merch_lat', 'merch_long', 'trans_hour', 'trans_day_of_week']
categorical_cols = ['merchant', 'category', 'gender', 'city', 'state', 'job']
# ---------------- Header ----------------st.markdown('<div class="title">๐ก๏ธ Credit Card Fraud Detection</div>', unsafe_allow_html=True)st.markdown('<div class="subheader">Predict whether a transaction is fraudulent using pre-trained ML models</div>', unsafe_allow_html=True)
# ---------------- Input Form ----------------with st.form("fraud_form"): st.markdown("### ๐ Transaction Details") col1, col2 = st.columns(2)
with col1: cc_num = st.number_input("๐ณ Credit Card Number", value=123456789.0) amt = st.number_input("๐ฐ Transaction Amount", value=100.0) zip_code = st.number_input("๐ฎ ZIP Code", value=10001.0) lat = st.number_input("๐ Latitude", value=40.0) long = st.number_input("๐ Longitude", value=-75.0) city_pop = st.number_input("๐๏ธ City Population", value=8500000.0)
with col2: unix_time = st.number_input("๐ Unix Timestamp", value=float(datetime.now().timestamp())) merch_lat = st.number_input("๐ช Merchant Latitude", value=40.0) merch_long = st.number_input("๐ช Merchant Longitude", value=-75.0) trans_hour = st.slider("โฐ Transaction Hour", 0, 23, 12) trans_day = st.slider("๐
Day of Week", 0, 6, 3)
st.markdown("### ๐ค Categorical Details") merchant = st.text_input("Merchant", value="fraud_Rippin, Kub and Mann") category = st.selectbox("Category", encoders['category'].classes_.tolist()) gender = st.selectbox("Gender", encoders['gender'].classes_.tolist()) city = st.text_input("City", value="New York") state = st.selectbox("State", encoders['state'].classes_.tolist()) job = st.selectbox("Job", encoders['job'].classes_.tolist())
submit = st.form_submit_button("๐ Predict")
# ---------------- Prediction Section ----------------if submit: try: sample_input = { 'cc_num': cc_num, 'merchant': encoders['merchant'].transform([merchant])[0] if merchant in encoders['merchant'].classes_ else 0, 'category': encoders['category'].transform([category])[0], 'amt': amt, 'gender': encoders['gender'].transform([gender])[0], 'city': encoders['city'].transform([city])[0] if city in encoders['city'].classes_ else 0, 'state': encoders['state'].transform([state])[0], 'zip': zip_code, 'lat': lat, 'long': long, 'city_pop': city_pop, 'job': encoders['job'].transform([job])[0], 'unix_time': unix_time, 'merch_lat': merch_lat, 'merch_long': merch_long, 'trans_hour': trans_hour, 'trans_day_of_week': trans_day }
input_df = pd.DataFrame([sample_input], columns=feature_order) input_scaled = scaler.transform(input_df)
st.markdown("### ๐ Prediction Results") for name, model in models.items(): pred = model.predict(input_scaled)[0] prob = model.predict_proba(input_scaled)[0][1] fraud = pred == 1
css_class = "fraud prediction" if fraud else "prediction" label = "๐จ Fraudulent" if fraud else "โ
Legitimate"
st.markdown(f""" <div class="{css_class}"> <strong>{name}</strong><br> {label} โ <b>{prob:.2%}</b> probability of fraud </div> """, unsafe_allow_html=True)
except Exception as e: st.error(f"Prediction failed: {e}")123version https://git-lfs.github.com/spec/v1oid sha256:12d553ab19440c752d2531ee1af44bb64f12cc3d3839f1649f19e81c230545f0size 150354339123version https://git-lfs.github.com/spec/v1oid sha256:fd7139200dbfcbed0b6742bbe05a4f1abce532c4fef20918228a651647a3e75dsize 351238196123version https://git-lfs.github.com/spec/v1oid sha256:2aa1d05501eac2028564588dd78d1593564e3a2a51c03b6a3b906ce220540fc1size 55337123version https://git-lfs.github.com/spec/v1oid sha256:4542ed03423e71fae989d4fa44ac4af68f79ca411e7ee348e029de0c197821b3size 1535123version https://git-lfs.github.com/spec/v1oid sha256:ddc2c0be41c93575bf148876ecb564f103a318632a66b5c356324622d1b63e7esize 359751.gitattributes+2
Decision Tree_model.pklAdded+3
Logistic Regression_model.pklAdded+3
Random Forest_model.pklAdded+3
app.pyAdded+150
fraudTest.csvAdded+3
fraudTrain.csvAdded+3
label_encoders.pklAdded+3
scaler.pklAdded+3
xgboost_model.pklAdded+3