mahmudunnabi commited on
Commit
6c2cd39
·
verified ·
1 Parent(s): f79514d

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +164 -0
  2. random_forest_model.pkl +3 -0
  3. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import joblib
3
+ import pandas as pd
4
+ from sklearn.preprocessing import LabelEncoder, StandardScaler
5
+
6
+ # Load the trained model
7
+ model = joblib.load('random_forest_model.pkl') # Replace with your actual model file
8
+
9
+ # Define encoders for categorical columns using the actual values
10
+ label_encoders = {
11
+ 'Day of Week': LabelEncoder().fit(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']),
12
+ 'Type of Card': LabelEncoder().fit(['Visa', 'MasterCard']),
13
+ 'Entry Mode': LabelEncoder().fit(['Tap', 'PIN', 'CVC']),
14
+ 'Type of Transaction': LabelEncoder().fit(['POS', 'Online', 'ATM']),
15
+ 'Merchant Group': LabelEncoder().fit(['Entertainment', 'Services', 'Restaurant', 'Electronics', 'Children',
16
+ 'Fashion', 'Food', 'Products', 'Subscription', 'Gaming']),
17
+ 'Country of Transaction': LabelEncoder().fit(['United Kingdom', 'USA', 'India', 'Russia', 'China']),
18
+ 'Shipping Address': LabelEncoder().fit(['United Kingdom', 'USA', 'India', 'Russia', 'China']),
19
+ 'Country of Residence': LabelEncoder().fit(['United Kingdom', 'USA', 'India', 'Russia', 'China']),
20
+ 'Gender': LabelEncoder().fit(['M', 'F'])
21
+ }
22
+
23
+ # Define the scaler for numerical columns (use the scaler from training if available)
24
+ scaler = StandardScaler()
25
+
26
+ # Define the function to make predictions
27
+ def predict_fraud(day_of_week, time, type_of_card, entry_mode, amount, type_of_transaction, merchant_group,
28
+ country_of_transaction, shipping_address, country_of_residence, gender, age):
29
+ # Create a DataFrame for the input
30
+ input_data = pd.DataFrame({
31
+ 'Day of Week': [day_of_week],
32
+ 'Time': [time],
33
+ 'Type of Card': [type_of_card],
34
+ 'Entry Mode': [entry_mode],
35
+ 'Amount': [amount],
36
+ 'Type of Transaction': [type_of_transaction],
37
+ 'Merchant Group': [merchant_group],
38
+ 'Country of Transaction': [country_of_transaction],
39
+ 'Shipping Address': [shipping_address],
40
+ 'Country of Residence': [country_of_residence],
41
+ 'Gender': [gender],
42
+ 'Age': [age],
43
+ })
44
+
45
+ # Encode categorical columns
46
+ for col in label_encoders:
47
+ input_data[col] = label_encoders[col].transform(input_data[col])
48
+
49
+ # Standardize numerical features
50
+ numerical_cols = ['Time', 'Amount', 'Age']
51
+ input_data[numerical_cols] = scaler.fit_transform(input_data[numerical_cols]) # Use the fitted scaler from training
52
+
53
+ # Make the prediction
54
+ prediction = model.predict(input_data)
55
+
56
+ # Convert the numeric prediction to a meaningful label
57
+ return "Fraud" if prediction[0] == 1 else "Not Fraud"
58
+
59
+ # Custom CSS for background, fonts, and boxes
60
+ st.markdown("""
61
+ <style>
62
+ /* Background color for the app */
63
+ .main {
64
+ background-color: #f0f2f6;
65
+ font-family: 'Helvetica', sans-serif;
66
+ }
67
+
68
+ /* Title and headers */
69
+ h1, h2, h3, h4, h5, h6 {
70
+ color: #3c3c3c;
71
+ font-family: 'Arial', sans-serif;
72
+ }
73
+
74
+ /* Customizing the input headers (labels) */
75
+ .stSelectbox label, .stNumberInput label {
76
+ font-size: 16px;
77
+ color: #333333;
78
+ font-family: 'Montserrat', sans-serif;
79
+ font-weight: 600;
80
+ text-transform: uppercase;
81
+ margin-bottom: 5px;
82
+ }
83
+
84
+ /* Input boxes */
85
+ .stSelectbox, .stNumberInput {
86
+ background-color: #e6eaf2;
87
+ border-radius: 10px;
88
+ color: #3c3c3c;
89
+ }
90
+
91
+ /* Adjust buttons */
92
+ button {
93
+ background-color: #4CAF50 !important;
94
+ color: white !important;
95
+ border-radius: 10px !important;
96
+ padding: 10px 20px !important;
97
+ }
98
+
99
+ /* Custom text for the prediction output */
100
+ .output-text {
101
+ font-size: 24px;
102
+ font-weight: bold;
103
+ }
104
+
105
+ /* Red text for fraud prediction */
106
+ .fraud {
107
+ color: red;
108
+ }
109
+
110
+ /* Green text for not fraud prediction */
111
+ .not-fraud {
112
+ color: green;
113
+ }
114
+
115
+ /* Custom styles for plus/minus buttons */
116
+ .stNumberInput button {
117
+ background-color: #d0d3da !important; /* Light color for the plus/minus buttons */
118
+ }
119
+
120
+ </style>
121
+ """, unsafe_allow_html=True)
122
+
123
+ # Streamlit app layout
124
+ st.markdown("<h1 style='text-align: center; font-family: Arial, sans-serif; color: #4CAF50;'>Credit Card Fraud Detection App</h1>", unsafe_allow_html=True)
125
+
126
+ # First row: Day of Week, Time, Type of Card
127
+ col1, col2, col3 = st.columns(3)
128
+ day_of_week = col1.selectbox("Day of Week", ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'])
129
+ time = col2.number_input("Time", min_value=0, max_value=24, value=12) # Example for time in HHMM format
130
+ type_of_card = col3.selectbox("Type of Card", ['Visa', 'MasterCard'])
131
+
132
+ # Second row: Entry Mode, Amount, Type of Transaction
133
+ col4, col5, col6 = st.columns(3)
134
+ entry_mode = col4.selectbox("Entry Mode", ['Tap', 'PIN', 'CVC'])
135
+ amount = col5.number_input("Amount", min_value=0.0, format="%.2f")
136
+ type_of_transaction = col6.selectbox("Type of Transaction", ['POS', 'Online', 'ATM'])
137
+
138
+ # Third row: Merchant Group, Country of Transaction
139
+ col7, col8 = st.columns(2)
140
+ merchant_group = col7.selectbox("Merchant Group", ['Entertainment', 'Services', 'Restaurant', 'Electronics',
141
+ 'Children', 'Fashion', 'Food', 'Products',
142
+ 'Subscription', 'Gaming'])
143
+ country_of_transaction = col8.selectbox("Country of Transaction", ['United Kingdom', 'USA', 'India', 'Russia', 'China'])
144
+
145
+ # Fourth row: Shipping Address, Country of Residence, Gender
146
+ col9, col10, col11 = st.columns(3)
147
+ shipping_address = col9.selectbox("Shipping Address", ['United Kingdom', 'USA', 'India', 'Russia', 'China'])
148
+ country_of_residence = col10.selectbox("Country of Residence", ['United Kingdom', 'USA', 'India', 'Russia', 'China'])
149
+ gender = col11.selectbox("Gender", ['M', 'F'])
150
+
151
+ # Fifth row: Age
152
+ col12 = st.columns(1)
153
+ age = col12[0].number_input("Age", min_value=0)
154
+
155
+ if st.button("Predict"):
156
+ prediction = predict_fraud(day_of_week, time, type_of_card, entry_mode, amount, type_of_transaction,
157
+ merchant_group, country_of_transaction, shipping_address,
158
+ country_of_residence, gender, age)
159
+
160
+ # Conditional formatting for prediction output
161
+ if prediction == "Fraud":
162
+ st.markdown(f"<p class='output-text fraud'>Prediction: {prediction}</p>", unsafe_allow_html=True)
163
+ else:
164
+ st.markdown(f"<p class='output-text not-fraud'>Prediction: {prediction}</p>", unsafe_allow_html=True)
random_forest_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4557b088c71bc34018ed1f7e38cf871b7bdbdc844f5c43121c6d24cbfa7de52c
3
+ size 34608825
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ streamlit
3
+ joblib
4
+ pandas
5
+ scikit-learn==1.2.2