Spaces:
Sleeping
Sleeping
Update slapp.py
Browse files
slapp.py
CHANGED
|
@@ -1,96 +1,101 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pickle
|
| 3 |
-
import pandas as pd
|
| 4 |
-
|
| 5 |
-
# Load the saved model
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
'
|
| 59 |
-
'
|
| 60 |
-
'
|
| 61 |
-
'
|
| 62 |
-
'
|
| 63 |
-
'
|
| 64 |
-
'
|
| 65 |
-
'
|
| 66 |
-
'
|
| 67 |
-
'
|
| 68 |
-
'
|
| 69 |
-
'
|
| 70 |
-
'
|
| 71 |
-
'
|
| 72 |
-
'
|
| 73 |
-
'
|
| 74 |
-
'
|
| 75 |
-
'
|
| 76 |
-
'
|
| 77 |
-
'
|
| 78 |
-
'
|
| 79 |
-
'
|
| 80 |
-
'
|
| 81 |
-
'
|
| 82 |
-
'
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pickle
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
# Load the saved model
|
| 6 |
+
@st.cache_resource # Cache the model loading to avoid reloading on each run
|
| 7 |
+
def load_model():
|
| 8 |
+
try:
|
| 9 |
+
model = pickle.load(open('model.pkl', 'rb'))
|
| 10 |
+
return model
|
| 11 |
+
except Exception as e:
|
| 12 |
+
st.error(f"Error loading model: {e}")
|
| 13 |
+
return None
|
| 14 |
+
|
| 15 |
+
model = load_model()
|
| 16 |
+
|
| 17 |
+
# Title of the app
|
| 18 |
+
st.title("Fraud Detection API")
|
| 19 |
+
st.markdown("Welcome to the Fraud Detection API! Please enter the transaction details below:")
|
| 20 |
+
|
| 21 |
+
# Input fields for the transaction features
|
| 22 |
+
time = st.number_input("Time", min_value=0.0, step=0.1)
|
| 23 |
+
v1 = st.number_input("V1", step=0.01)
|
| 24 |
+
v2 = st.number_input("V2", step=0.01)
|
| 25 |
+
v3 = st.number_input("V3", step=0.01)
|
| 26 |
+
v4 = st.number_input("V4", step=0.01)
|
| 27 |
+
v5 = st.number_input("V5", step=0.01)
|
| 28 |
+
v6 = st.number_input("V6", step=0.01)
|
| 29 |
+
v7 = st.number_input("V7", step=0.01)
|
| 30 |
+
v8 = st.number_input("V8", step=0.01)
|
| 31 |
+
v9 = st.number_input("V9", step=0.01)
|
| 32 |
+
v10 = st.number_input("V10", step=0.01)
|
| 33 |
+
v11 = st.number_input("V11", step=0.01)
|
| 34 |
+
v12 = st.number_input("V12", step=0.01)
|
| 35 |
+
v13 = st.number_input("V13", step=0.01)
|
| 36 |
+
v14 = st.number_input("V14", step=0.01)
|
| 37 |
+
v15 = st.number_input("V15", step=0.01)
|
| 38 |
+
v16 = st.number_input("V16", step=0.01)
|
| 39 |
+
v17 = st.number_input("V17", step=0.01)
|
| 40 |
+
v18 = st.number_input("V18", step=0.01)
|
| 41 |
+
v19 = st.number_input("V19", step=0.01)
|
| 42 |
+
v20 = st.number_input("V20", step=0.01)
|
| 43 |
+
v21 = st.number_input("V21", step=0.01)
|
| 44 |
+
v22 = st.number_input("V22", step=0.01)
|
| 45 |
+
v23 = st.number_input("V23", step=0.01)
|
| 46 |
+
v24 = st.number_input("V24", step=0.01)
|
| 47 |
+
v25 = st.number_input("V25", step=0.01)
|
| 48 |
+
v26 = st.number_input("V26", step=0.01)
|
| 49 |
+
v27 = st.number_input("V27", step=0.01)
|
| 50 |
+
v28 = st.number_input("V28", step=0.01)
|
| 51 |
+
amount = st.number_input("Amount", min_value=0.0, step=0.1)
|
| 52 |
+
|
| 53 |
+
# Button to make predictions
|
| 54 |
+
if st.button("Predict"):
|
| 55 |
+
if model:
|
| 56 |
+
# Create a DataFrame from the input data
|
| 57 |
+
transaction_data = pd.DataFrame({
|
| 58 |
+
'Time': [time],
|
| 59 |
+
'V1': [v1],
|
| 60 |
+
'V2': [v2],
|
| 61 |
+
'V3': [v3],
|
| 62 |
+
'V4': [v4],
|
| 63 |
+
'V5': [v5],
|
| 64 |
+
'V6': [v6],
|
| 65 |
+
'V7': [v7],
|
| 66 |
+
'V8': [v8],
|
| 67 |
+
'V9': [v9],
|
| 68 |
+
'V10': [v10],
|
| 69 |
+
'V11': [v11],
|
| 70 |
+
'V12': [v12],
|
| 71 |
+
'V13': [v13],
|
| 72 |
+
'V14': [v14],
|
| 73 |
+
'V15': [v15],
|
| 74 |
+
'V16': [v16],
|
| 75 |
+
'V17': [v17],
|
| 76 |
+
'V18': [v18],
|
| 77 |
+
'V19': [v19],
|
| 78 |
+
'V20': [v20],
|
| 79 |
+
'V21': [v21],
|
| 80 |
+
'V22': [v22],
|
| 81 |
+
'V23': [v23],
|
| 82 |
+
'V24': [v24],
|
| 83 |
+
'V25': [v25],
|
| 84 |
+
'V26': [v26],
|
| 85 |
+
'V27': [v27],
|
| 86 |
+
'V28': [v28],
|
| 87 |
+
'Amount': [amount]
|
| 88 |
+
})
|
| 89 |
+
|
| 90 |
+
# Perform prediction
|
| 91 |
+
prediction = model.predict(transaction_data)
|
| 92 |
+
|
| 93 |
+
# Display results
|
| 94 |
+
if prediction[0] == 0:
|
| 95 |
+
st.success("✅ Acceptable transaction")
|
| 96 |
+
else:
|
| 97 |
+
st.error("🚨 Fraudulent transaction")
|
| 98 |
+
else:
|
| 99 |
+
st.error("Model not loaded.")
|
| 100 |
+
|
| 101 |
+
|