Update src/streamlit_app.py
Browse files- src/streamlit_app.py +25 -38
src/streamlit_app.py
CHANGED
|
@@ -5,7 +5,6 @@ import pandas as pd
|
|
| 5 |
import numpy as np
|
| 6 |
import joblib
|
| 7 |
import json
|
| 8 |
-
import uuid
|
| 9 |
from datetime import datetime
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
|
|
@@ -134,7 +133,7 @@ def load_assets():
|
|
| 134 |
model, encoders, scaler, config = load_assets()
|
| 135 |
|
| 136 |
st.title("FinTech Fraud Guard")
|
| 137 |
-
st.markdown("
|
| 138 |
|
| 139 |
with st.form("transaction_form"):
|
| 140 |
c1, c2, c3 = st.columns(3)
|
|
@@ -143,7 +142,7 @@ with st.form("transaction_form"):
|
|
| 143 |
st.subheader("Personal & Card")
|
| 144 |
first = st.text_input("First Name", "Jeff")
|
| 145 |
last = st.text_input("Last Name", "Elliott")
|
| 146 |
-
gender = st.selectbox("Gender", ["M", "F"]
|
| 147 |
dob = st.text_input("Date of Birth (DD-MM-YYYY)", "19-03-1968")
|
| 148 |
cc_num = st.text_input("CC Number", "3725537864060026")
|
| 149 |
job = st.text_input("Job", "Mechanical engineer")
|
|
@@ -167,7 +166,6 @@ with st.form("transaction_form"):
|
|
| 167 |
lon = st.number_input("Customer Long", value=-80.9355)
|
| 168 |
m_lat = st.number_input("Merchant Lat", value=33.986391)
|
| 169 |
m_lon = st.number_input("Merchant Long", value=-81.200714)
|
| 170 |
-
trans_num = str(uuid.uuid4())[:16]
|
| 171 |
|
| 172 |
submit = st.form_submit_button("ANALYZE TRANSACTION", use_container_width=True)
|
| 173 |
|
|
@@ -222,9 +220,9 @@ if submit:
|
|
| 222 |
elif col == 'zip':
|
| 223 |
val = str(int(float(zip_v)))
|
| 224 |
else:
|
| 225 |
-
val =
|
| 226 |
|
| 227 |
-
if hasattr(encoders[col], 'classes_') and val in encoders[col].classes_:
|
| 228 |
cat_encoded.append(encoders[col].transform([val])[0])
|
| 229 |
else:
|
| 230 |
cat_encoded.append(0)
|
|
@@ -238,52 +236,41 @@ if submit:
|
|
| 238 |
prob = torch.sigmoid(logits).item()
|
| 239 |
|
| 240 |
st.divider()
|
| 241 |
-
col1, col2, col3 = st.columns([2, 1, 2])
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
<
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
<
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
''', unsafe_allow_html=True)
|
| 260 |
|
|
|
|
| 261 |
with col1:
|
| 262 |
st.metric("Fraud Probability", f"{prob*100:.2f}%")
|
|
|
|
| 263 |
st.metric("Optimal Threshold", "98.98%")
|
| 264 |
-
|
| 265 |
-
with col3:
|
| 266 |
-
st.json({
|
| 267 |
-
"amount": amt,
|
| 268 |
-
"merchant": merchant,
|
| 269 |
-
"category": category,
|
| 270 |
-
"distance_km": round(distance, 2),
|
| 271 |
-
"hour": dt_obj.hour,
|
| 272 |
-
"is_weekend": dt_obj.weekday() >= 5
|
| 273 |
-
})
|
| 274 |
|
| 275 |
except Exception as e:
|
| 276 |
st.error(f"Processing Error: {str(e)}")
|
| 277 |
-
st.error("Please check input formats and try again")
|
| 278 |
|
| 279 |
st.sidebar.markdown("""
|
| 280 |
-
<div style='text-align: center; padding: 20px;'>
|
| 281 |
<h3>Model Performance</h3>
|
| 282 |
<p><b>F1-Score:</b> 0.8350</p>
|
| 283 |
<p><b>Precision:</b> 0.8706</p>
|
| 284 |
<p><b>Recall:</b> 0.7995</p>
|
| 285 |
<p><b>Threshold:</b> 0.9898</p>
|
| 286 |
</div>
|
| 287 |
-
""")
|
| 288 |
|
| 289 |
-
st.sidebar.info("MoE model with 4 specialized experts for fraud detection.")
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import joblib
|
| 7 |
import json
|
|
|
|
| 8 |
from datetime import datetime
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
|
|
|
|
| 133 |
model, encoders, scaler, config = load_assets()
|
| 134 |
|
| 135 |
st.title("FinTech Fraud Guard")
|
| 136 |
+
st.markdown("MoE Transaction Verifier - Optimized Threshold: 0.9898")
|
| 137 |
|
| 138 |
with st.form("transaction_form"):
|
| 139 |
c1, c2, c3 = st.columns(3)
|
|
|
|
| 142 |
st.subheader("Personal & Card")
|
| 143 |
first = st.text_input("First Name", "Jeff")
|
| 144 |
last = st.text_input("Last Name", "Elliott")
|
| 145 |
+
gender = st.selectbox("Gender", ["M", "F"])
|
| 146 |
dob = st.text_input("Date of Birth (DD-MM-YYYY)", "19-03-1968")
|
| 147 |
cc_num = st.text_input("CC Number", "3725537864060026")
|
| 148 |
job = st.text_input("Job", "Mechanical engineer")
|
|
|
|
| 166 |
lon = st.number_input("Customer Long", value=-80.9355)
|
| 167 |
m_lat = st.number_input("Merchant Lat", value=33.986391)
|
| 168 |
m_lon = st.number_input("Merchant Long", value=-81.200714)
|
|
|
|
| 169 |
|
| 170 |
submit = st.form_submit_button("ANALYZE TRANSACTION", use_container_width=True)
|
| 171 |
|
|
|
|
| 220 |
elif col == 'zip':
|
| 221 |
val = str(int(float(zip_v)))
|
| 222 |
else:
|
| 223 |
+
val = "unknown"
|
| 224 |
|
| 225 |
+
if col in encoders and hasattr(encoders[col], 'classes_') and val in encoders[col].classes_:
|
| 226 |
cat_encoded.append(encoders[col].transform([val])[0])
|
| 227 |
else:
|
| 228 |
cat_encoded.append(0)
|
|
|
|
| 236 |
prob = torch.sigmoid(logits).item()
|
| 237 |
|
| 238 |
st.divider()
|
|
|
|
| 239 |
|
| 240 |
+
if prob > 0.9898:
|
| 241 |
+
st.markdown(f'''
|
| 242 |
+
<div class="fraud-card">
|
| 243 |
+
<h2>FRAUD DETECTED</h2>
|
| 244 |
+
<h3>Confidence: <span style="color: #ff4b4b; font-weight: bold;">{prob*100:.1f}%</span></h3>
|
| 245 |
+
<p>Transaction flagged as high risk</p>
|
| 246 |
+
</div>
|
| 247 |
+
''', unsafe_allow_html=True)
|
| 248 |
+
else:
|
| 249 |
+
st.markdown(f'''
|
| 250 |
+
<div class="legit-card">
|
| 251 |
+
<h2>SECURE</h2>
|
| 252 |
+
<h3>Confidence: <span style="color: #00ffcc; font-weight: bold;">{(1-prob)*100:.1f}%</span></h3>
|
| 253 |
+
<p>Transaction appears legitimate</p>
|
| 254 |
+
</div>
|
| 255 |
+
''', unsafe_allow_html=True)
|
|
|
|
| 256 |
|
| 257 |
+
col1, col2 = st.columns(2)
|
| 258 |
with col1:
|
| 259 |
st.metric("Fraud Probability", f"{prob*100:.2f}%")
|
| 260 |
+
with col2:
|
| 261 |
st.metric("Optimal Threshold", "98.98%")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
except Exception as e:
|
| 264 |
st.error(f"Processing Error: {str(e)}")
|
|
|
|
| 265 |
|
| 266 |
st.sidebar.markdown("""
|
| 267 |
+
<div style='text-align: center; padding: 20px; background-color: #1e2130; border-radius: 10px;'>
|
| 268 |
<h3>Model Performance</h3>
|
| 269 |
<p><b>F1-Score:</b> 0.8350</p>
|
| 270 |
<p><b>Precision:</b> 0.8706</p>
|
| 271 |
<p><b>Recall:</b> 0.7995</p>
|
| 272 |
<p><b>Threshold:</b> 0.9898</p>
|
| 273 |
</div>
|
| 274 |
+
""", unsafe_allow_html=True)
|
| 275 |
|
| 276 |
+
st.sidebar.info("MoE model with 4 specialized experts for fraud detection.")
|