spam / app.py
teohteiktoe's picture
Upload 7 files
4ef2d7c verified
Raw History Blame Contribute Delete
3.34 kB
import gradio as gr
import joblib
model = joblib.load("spam_rf_model.joblib")
def classify_message(message):
if not message or message.strip() == "":
return "Please enter a message.", 0.0
prediction = model.predict([message])[0]
if hasattr(model.named_steps["classifier"], "predict_proba"):
probabilities = model.predict_proba([message])[0]
classes = list(model.classes_)
confidence = probabilities[classes.index(prediction)]
else:
confidence = 0.0
if prediction.lower() == "spam":
label = "SPAM Message"
else:
label = "NOT SPAM Message"
return label, round(float(confidence), 4)
custom_css = """
body {
background: radial-gradient(circle at top, #1e1b4b, #020617 55%, #000000);
}
.gradio-container {
background: transparent !important;
color: #e5e7eb !important;
}
#title-box {
text-align: center;
padding: 28px;
border-radius: 24px;
background: linear-gradient(135deg, rgba(79,70,229,0.35), rgba(6,182,212,0.25));
border: 1px solid rgba(125, 211, 252, 0.35);
box-shadow: 0 0 40px rgba(34, 211, 238, 0.22);
}
#title-box h1 {
font-size: 42px;
margin-bottom: 8px;
background: linear-gradient(90deg, #67e8f9, #a78bfa, #f0abfc);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
#title-box p {
color: #cbd5e1;
font-size: 17px;
}
textarea, input {
background: rgba(15, 23, 42, 0.85) !important;
color: #f8fafc !important;
border: 1px solid rgba(125, 211, 252, 0.35) !important;
border-radius: 18px !important;
}
button {
border-radius: 18px !important;
background: linear-gradient(90deg, #2563eb, #7c3aed, #06b6d4) !important;
color: white !important;
font-weight: 800 !important;
border: none !important;
box-shadow: 0 0 24px rgba(59, 130, 246, 0.35);
}
.output-class {
background: rgba(15, 23, 42, 0.85) !important;
border-radius: 18px !important;
border: 1px solid rgba(167, 139, 250, 0.35) !important;
}
"""
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
gr.HTML(
"""
<div id="title-box">
<h1>AI Spam Message Detector</h1>
<p>CountVectorizer Encoder + Random Forest Classifier</p>
</div>
"""
)
with gr.Row():
with gr.Column():
message = gr.Textbox(
label="Enter SMS / Email Message",
placeholder="Example: Congratulations! You won a free prize. Click now...",
lines=8
)
classify_btn = gr.Button("Analyze Message")
with gr.Column():
result = gr.Textbox(label="Prediction", elem_classes="output-class")
confidence = gr.Number(label="Confidence Score", precision=4)
classify_btn.click(
fn=classify_message,
inputs=message,
outputs=[result, confidence]
)
gr.Examples(
examples=[
["Free entry in 2 a weekly competition to win FA Cup final tickets"],
["Hi, are we still meeting for lunch tomorrow?"],
["Congratulations! You have won a cash prize. Reply now to claim."],
["Can you send me the report by tonight?"]
],
inputs=message
)
demo.launch(server_name="0.0.0.0", server_port=7860)