Spaces:
Sleeping
Sleeping
Kwasiasomani
commited on
Commit
•
241dd2e
1
Parent(s):
b180a08
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def predict_fraud(user_input):
|
2 |
user_input_amount = user_input['Amount']
|
3 |
user_input_time = user_input['Time']
|
@@ -23,6 +41,27 @@ def predict_fraud(user_input):
|
|
23 |
probability = model.predict_proba(user_input_scaled)[0][1]
|
24 |
return prediction, probability
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
# Streamlit app
|
27 |
def main():
|
28 |
# Set page layout
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
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:
|
13 |
+
scaler = pickle.load(scaler_file)
|
14 |
+
|
15 |
+
with open('rf_Random_model.pkl', 'rb') as model_file:
|
16 |
+
model = pickle.load(model_file)
|
17 |
+
|
18 |
+
|
19 |
def predict_fraud(user_input):
|
20 |
user_input_amount = user_input['Amount']
|
21 |
user_input_time = user_input['Time']
|
|
|
41 |
probability = model.predict_proba(user_input_scaled)[0][1]
|
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 |
+
|
49 |
+
# Pie chart for prediction distribution
|
50 |
+
prediction_labels = ['Non-Fraudulent', 'Fraudulent']
|
51 |
+
prediction_values = [1 - prediction, prediction]
|
52 |
+
axes[0].pie(prediction_values, labels=prediction_labels, autopct='%1.1f%%', startangle=90, colors=['skyblue', 'lightcoral'])
|
53 |
+
axes[0].set_title('Prediction Distribution')
|
54 |
+
|
55 |
+
# Bar chart for probability distribution
|
56 |
+
axes[1].bar(['Probability'], [probability], color='lightgreen' if prediction == 0 else 'lightcoral')
|
57 |
+
axes[1].set_title('Probability of Fraud')
|
58 |
+
|
59 |
+
# Display amount
|
60 |
+
st.write(f"Transaction Amount: ${amount}")
|
61 |
+
|
62 |
+
# Display charts
|
63 |
+
st.pyplot(fig)
|
64 |
+
|
65 |
# Streamlit app
|
66 |
def main():
|
67 |
# Set page layout
|