-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
79 lines (67 loc) · 2.31 KB
/
app.py
File metadata and controls
79 lines (67 loc) · 2.31 KB
1
2
3
4
5
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
import streamlit as st
import joblib
import re
# ------------------ LOAD MODEL ------------------ #
model = joblib.load("models_v3/mnb.pkl")
vectorizer = joblib.load("models_v3/vectorizer.pkl")
# ------------------ PAGE CONFIG ------------------ #
st.set_page_config(
page_title="Spam SMS Detector",
page_icon="📩",
layout="centered"
)
st.title("📩 Spam / Scam SMS Detector")
st.write("Paste an SMS to check its **risk level**.")
# ------------------ INPUT ------------------ #
message = st.text_area(
"Enter SMS text",
placeholder="Example: Your bank account has been suspended. Verify now..."
)
risk = None
color = None
spam_proba = None
# ------------------ PREDICTION ------------------ #
if st.button("Predict"):
if not message.strip():
st.warning("Please enter a message.")
else:
msg = message.lower()
# -------- Rule 1: OTP override (very low risk) -------- #
if (
re.search(r"\b\d{4,8}\b", msg)
and "otp" in msg
and "click" not in msg
and "link" not in msg
):
spam_proba = 0.05
risk = "Low Risk 🟢"
color = "green"
else:
# ML prediction
vector = vectorizer.transform([message])
spam_proba = model.predict_proba(vector)[0][1]
# -------- Rule 2: Dangerous intent + link boost -------- #
danger_words = [
"verify", "suspended", "restricted",
"urgent", "refund", "account", "blocked"
]
if any(w in msg for w in danger_words) and "http" in msg:
spam_proba = max(spam_proba, 0.85)
# -------- Risk buckets -------- #
if spam_proba < 0.10:
risk = "Likely Legit 🟢"
color = "green"
elif spam_proba < 0.80:
risk = "Promotional / Suspicious 🟡"
color = "orange"
else:
risk = "High Risk Scam 🔴"
color = "red"
# ------------------ OUTPUT ------------------ #
if risk:
st.markdown(
f"<h3 style='color:{color}'>{risk}</h3>",
unsafe_allow_html=True
)
st.write(f"Spam confidence: **{spam_proba * 100:.1f}%**")
st.caption("⚠️ This is a risk estimate. Always verify the sender.")