Spaces:
Sleeping
Sleeping
Kwasiasomani
commited on
Commit
•
0a7cda0
1
Parent(s):
dc2c6c1
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ import pickle
|
|
6 |
from sklearn.preprocessing import StandardScaler
|
7 |
from sklearn.ensemble import RandomForestClassifier
|
8 |
import matplotlib.pyplot as plt
|
9 |
-
|
10 |
|
11 |
# Load pre-trained model and scaler
|
12 |
with open('scalers.pkl', 'rb') as scaler_file:
|
@@ -17,21 +17,21 @@ with open('rf_random_model.pkl', 'rb') as model_file:
|
|
17 |
|
18 |
|
19 |
def predict_fraud(user_input):
|
20 |
-
user_input_amount = user_input['Amount']
|
21 |
-
user_input_time = user_input['Time']
|
22 |
-
user_input_features = user_input.drop(columns=['Amount', 'Time'])
|
23 |
|
24 |
# Scale the amount and time columns
|
25 |
user_input_amount_scaled = scaler.transform(np.array(user_input_amount).reshape(-1, 1))
|
26 |
user_input_time_scaled = scaler.transform(np.array(user_input_time).reshape(-1, 1))
|
27 |
-
|
28 |
# Reshape user_input_features if necessary
|
29 |
if len(user_input_features.shape) == 1:
|
30 |
user_input_features = user_input_features.values.reshape(1, -1)
|
31 |
|
32 |
# Combine scaled amount and time with other features
|
33 |
user_input_scaled = np.concatenate((user_input_features, user_input_amount_scaled, user_input_time_scaled), axis=1)
|
34 |
-
|
35 |
# Ensure user_input_scaled is a 2D array
|
36 |
if len(user_input_scaled.shape) == 1:
|
37 |
user_input_scaled = user_input_scaled.reshape(1, -1)
|
@@ -42,7 +42,6 @@ def predict_fraud(user_input):
|
|
42 |
return prediction, probability
|
43 |
|
44 |
|
45 |
-
# Function to generate charts
|
46 |
def generate_charts(prediction, probability, amount):
|
47 |
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
|
48 |
|
@@ -62,7 +61,7 @@ def generate_charts(prediction, probability, amount):
|
|
62 |
# Display charts
|
63 |
st.pyplot(fig)
|
64 |
|
65 |
-
|
66 |
def main():
|
67 |
# Set page layout
|
68 |
st.set_page_config(
|
@@ -71,6 +70,12 @@ def main():
|
|
71 |
initial_sidebar_state="collapsed"
|
72 |
)
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
# Sidebar - User input fields
|
75 |
st.sidebar.title("Input Parameters")
|
76 |
user_input = {}
|
@@ -104,3 +109,7 @@ def main():
|
|
104 |
|
105 |
# Generate, display, and save charts
|
106 |
generate_charts(prediction, probability, amount)
|
|
|
|
|
|
|
|
|
|
6 |
from sklearn.preprocessing import StandardScaler
|
7 |
from sklearn.ensemble import RandomForestClassifier
|
8 |
import matplotlib.pyplot as plt
|
9 |
+
from PIL import Image as PILImage # Renamed PIL to avoid conflict with Streamlit's Image
|
10 |
|
11 |
# Load pre-trained model and scaler
|
12 |
with open('scalers.pkl', 'rb') as scaler_file:
|
|
|
17 |
|
18 |
|
19 |
def predict_fraud(user_input):
|
20 |
+
user_input_amount = user_input['Amount']
|
21 |
+
user_input_time = user_input['Time']
|
22 |
+
user_input_features = user_input.drop(columns=['Amount', 'Time'])
|
23 |
|
24 |
# Scale the amount and time columns
|
25 |
user_input_amount_scaled = scaler.transform(np.array(user_input_amount).reshape(-1, 1))
|
26 |
user_input_time_scaled = scaler.transform(np.array(user_input_time).reshape(-1, 1))
|
27 |
+
|
28 |
# Reshape user_input_features if necessary
|
29 |
if len(user_input_features.shape) == 1:
|
30 |
user_input_features = user_input_features.values.reshape(1, -1)
|
31 |
|
32 |
# Combine scaled amount and time with other features
|
33 |
user_input_scaled = np.concatenate((user_input_features, user_input_amount_scaled, user_input_time_scaled), axis=1)
|
34 |
+
|
35 |
# Ensure user_input_scaled is a 2D array
|
36 |
if len(user_input_scaled.shape) == 1:
|
37 |
user_input_scaled = user_input_scaled.reshape(1, -1)
|
|
|
42 |
return prediction, probability
|
43 |
|
44 |
|
|
|
45 |
def generate_charts(prediction, probability, amount):
|
46 |
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
|
47 |
|
|
|
61 |
# Display charts
|
62 |
st.pyplot(fig)
|
63 |
|
64 |
+
|
65 |
def main():
|
66 |
# Set page layout
|
67 |
st.set_page_config(
|
|
|
70 |
initial_sidebar_state="collapsed"
|
71 |
)
|
72 |
|
73 |
+
# Load the image
|
74 |
+
image = PILImage.open("fraud_detection_image.jpg")
|
75 |
+
|
76 |
+
# Display the image
|
77 |
+
st.image(image, use_column_width=True)
|
78 |
+
|
79 |
# Sidebar - User input fields
|
80 |
st.sidebar.title("Input Parameters")
|
81 |
user_input = {}
|
|
|
109 |
|
110 |
# Generate, display, and save charts
|
111 |
generate_charts(prediction, probability, amount)
|
112 |
+
|
113 |
+
|
114 |
+
if __name__ == "__main__":
|
115 |
+
main()
|