Mummia-99's picture
Update app.py
2babd39 verified
import streamlit as st
import pandas as pd
import joblib
# heading
html_temp = """
<div style="background-color:black;padding:10px">
<h2 style="color:white;text-align:center;">Fraud Detection APP </h2>
</div>
"""
st.markdown(html_temp, unsafe_allow_html=True)
# image
url="https://tse2.mm.bing.net/th?id=OIP.ROc4vnkJBbKTf8uWRQpldAHaDt&pid=Api&P=0&h=180"
st.image(url, use_container_width=True)
@st.cache_data
def convert_df(df):
return df.to_csv(index=False).encode("utf-8")
# loading model
model=joblib.load('iso_fraude_dection.joblib')
# Dataset preiction
def Dataset_prediction():
# Required column in dataframe
req_col= pd.DataFrame(columns=['step', 'type', 'amount'])
# Download the template
# csv = convert_df(req_col)
# st.download_button(
# label="Download Template",
# data=csv,
# file_name="Template.csv",
# mime="text/csv")
# uploading model
file=st.file_uploader('Please Upload the CSV File', type=["csv"])
col1, col2 = st.columns(2)
if file is not None:
with col1:
df = pd.read_csv(file,encoding='ISO-8859-1')
st.write("Uploaded File Preview:")
st.dataframe(df.head())
if st.button("Predict Outliers"):
try:
# Ensure required columns exist
required_columns = req_col
if not all(col in df.columns for col in required_columns):
st.error("Uploaded file does not match the required template structure.")
else:
predictions = model.predict(df)
with col2:
df['Anomaly'] = ['Anomaly' if pred == -1 else 'Not Anomaly' for pred in predictions]
st.write("Anomaly Detection Results:")
st.dataframe(df.head())
result_csv = convert_df(df)
st.download_button(
label="Download Results",
data=result_csv,
file_name="Anomaly_Detection_Results.csv",
mime="text/csv")
except Exception as e:
st.error(f"An error occurred while processing the file: {e}")
# value prediction
def values_prediction():
step=st.slider("Slide the Step Value:",min_value=1,max_value=743,value=1)
amount=st.slider('Slide the amount Value:',min_value=1,max_value=92445516,value=1)
option_type=['CASH_IN','CASH_OUT','DEBIT','PAYMENT','TRANSFER']
type=st.selectbox("Select the type of Transaction:",options=option_type)
type_value=option_type.index(type)
if st.button('Submit'):
try:
prediction=model.predict([[step,type_value,amount]])[0]
# Define messages and colors
review_status = {
-1: ("✅ Its not a Anomaly", "#32CD32"), # Green
1: ("❌ Its a Anomaly ", "#FF4500") # Red-Orange
}
# Get message and color based on prediction
message, color = review_status.get(prediction, ("❓ Unknown Prediction", "#808080"))
# Display styled result
st.markdown(f"""
<div style="
padding: 15px;
background-color: {color};
border-radius: 10px;
text-align: center;
font-size: 18px;
font-weight: bold;
color: white;">
{message}
</div>
""", unsafe_allow_html=True)
except Exception as e:
st.error(f"⚠️ Error in prediction: {e}")
# main
st.sidebar.title("Select your Choice ")
file_type = st.sidebar.radio("Choose your BOT", ("Dataset Prediction", "Values Prediction"))
# if st.sidebar.button("submit"):
if file_type =="Dataset Prediction":
Dataset_prediction()
elif file_type== "Values Prediction":
values_prediction()