Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def app():
|
| 7 |
+
st.title('Fraud Prediction')
|
| 8 |
+
|
| 9 |
+
st.header("Transaction Data Input")
|
| 10 |
+
st.write("Choose to upload a CSV file or manually input transaction data.")
|
| 11 |
+
|
| 12 |
+
# Load pre-trained model
|
| 13 |
+
with open('model.pkl', 'rb') as file_1:
|
| 14 |
+
model = joblib.load(file_1)
|
| 15 |
+
|
| 16 |
+
# Option to choose upload or manual input
|
| 17 |
+
option = st.radio("Select input method:", ("Upload CSV", "Manual Input"))
|
| 18 |
+
|
| 19 |
+
if option == "Upload CSV":
|
| 20 |
+
# Option to upload a CSV file
|
| 21 |
+
file_upload = st.file_uploader("Upload CSV", type=["csv"])
|
| 22 |
+
|
| 23 |
+
if file_upload is not None:
|
| 24 |
+
data = pd.read_csv(file_upload)
|
| 25 |
+
st.write("Uploaded Data Preview:")
|
| 26 |
+
st.write(data.head())
|
| 27 |
+
|
| 28 |
+
if st.button("Submit CSV"):
|
| 29 |
+
# Predict using the uploaded CSV data
|
| 30 |
+
predictions = model.predict(data)
|
| 31 |
+
data['prediction'] = predictions
|
| 32 |
+
data['prediction'] = data['prediction'].map({1: 'Fraud Transactions', 0: 'Not Fraud Transactions'})
|
| 33 |
+
|
| 34 |
+
st.write("Predictions:")
|
| 35 |
+
st.write(data[['type','nameOrig', 'nameDest', 'prediction']])
|
| 36 |
+
|
| 37 |
+
elif option == "Manual Input":
|
| 38 |
+
st.write("Manually input data:")
|
| 39 |
+
# Manual input of data
|
| 40 |
+
step = st.number_input("Step", min_value=0)
|
| 41 |
+
type = st.selectbox("Type", ["TRANSFER", "PAYMENT", "DEBIT", "CASH_OUT", "CASH_IN"])
|
| 42 |
+
amount = st.number_input("Amount", min_value=0.0)
|
| 43 |
+
nameOrig = st.text_input("Origin Account Name")
|
| 44 |
+
oldbalanceOrg = st.number_input("Old Balance (Origin)", min_value=0.0)
|
| 45 |
+
newbalanceOrig = st.number_input("New Balance (Origin)", min_value=0.0)
|
| 46 |
+
nameDest = st.text_input("Destination Account Name")
|
| 47 |
+
oldbalanceDest = st.number_input("Old Balance (Destination)", min_value=0.0)
|
| 48 |
+
newbalanceDest = st.number_input("New Balance (Destination)", min_value=0.0)
|
| 49 |
+
isFlaggedFraud = st.selectbox("Is Flagged Fraud?", [0, 1])
|
| 50 |
+
|
| 51 |
+
if st.button("Submit"):
|
| 52 |
+
# Create a DataFrame from manual input
|
| 53 |
+
manual_data = pd.DataFrame({
|
| 54 |
+
"step": [step],
|
| 55 |
+
"type": [type],
|
| 56 |
+
"amount": [amount],
|
| 57 |
+
"nameOrig": [nameOrig],
|
| 58 |
+
"oldbalanceOrg": [oldbalanceOrg],
|
| 59 |
+
"newbalanceOrig": [newbalanceOrig],
|
| 60 |
+
"nameDest": [nameDest],
|
| 61 |
+
"oldbalanceDest": [oldbalanceDest],
|
| 62 |
+
"newbalanceDest": [newbalanceDest],
|
| 63 |
+
"isFlaggedFraud": [isFlaggedFraud]
|
| 64 |
+
})
|
| 65 |
+
|
| 66 |
+
st.write("Manual Input Data:")
|
| 67 |
+
st.write(manual_data)
|
| 68 |
+
|
| 69 |
+
# Predict using the manually input data
|
| 70 |
+
manual_predictions = model.predict(manual_data)
|
| 71 |
+
manual_data['prediction'] = manual_predictions
|
| 72 |
+
manual_data['prediction'] = manual_data['prediction'].map({1: 'Fraud Transactions', 0: 'Not Fraud Transactions'})
|
| 73 |
+
|
| 74 |
+
st.write("Predictions:")
|
| 75 |
+
st.write(manual_data[['type','nameOrig', 'nameDest', 'prediction']])
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
app()
|